use Mojolicious::Lite;
use Getopt::Long;
use Storable qw(dclone);
+use Devel::Dwarn;
GetOptions(
'db=s' => \(my $opt_db = '../memnodes.db'),
get '/jit_tree/:id/:depth' => sub {
my $self = shift;
- my $logarea = $self->param('logarea');
my $id = $self->stash('id');
my $depth = $self->stash('depth');
+
+ my $logarea = (defined $self->param('logarea'))
+ ? $self->param('logarea')
+ : Mojo::URL->new($self->req->headers->referrer)->query->param('logarea');
+
my $node_tree = _fetch_node_tree($id, $depth);
my $jit_tree = _transform_node_tree($node_tree, sub {
my ($node) = @_;
$jit_node->{children} = $children if $children;
return $jit_node;
});
-if(1){
- use Devel::Dwarn;
- use Data::Dump qw(pp);
- local $jit_tree->{children};
- pp(dclone($jit_tree)); # dclone to avoid stringification
-}
+
+ if(1){
+ use Devel::Dwarn;
+ use Data::Dump qw(pp);
+ local $jit_tree->{children};
+ pp(dclone($jit_tree)); # dclone to avoid stringification
+ }
+
$self->render_json($jit_tree);
};
if ($node->{child_ids}) {
my @child_ids = split /,/, $node->{child_ids};
my $children;
- if (@child_ids == 1 && $node->{type} == 2) {
+ if (@child_ids == 1
+ && $node->{type} == 2 # only collapse links
+ ) {
my $child = _fetch_node_tree($child_ids[0], $depth); # same depth
# merge node into child
# XXX id, depth, parent_id
}
};
+/**
+ * Convert number of bytes into human readable format
+ *
+ * @param integer bytes Number of bytes to convert
+ * @param integer precision Number of digits after the decimal separator
+ * @return string
+ * via http://codeaid.net/javascript/convert-size-in-bytes-to-human-readable-format-(javascript)
+ */
+function bytesToSize(bytes, precision) {
+ var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
+ var posttxt = 0;
+ while( bytes >= 1024 ) {
+ posttxt++;
+ bytes = bytes / 1024;
+ }
+ var num = (posttxt) ? Number(bytes).toFixed(precision) : bytes;
+ return num + " " + sizes[posttxt];
+}
+
+
// http://stackoverflow.com/questions/5199901/how-to-sort-an-associative-array-by-its-values-in-javascript
function bySortedValue(obj, comparitor, callback, context) {
var tuples = [];
function request_jit_tree(nodeId, level, depth, onComplete){
- var params = { logarea: 0 };
+ var params = { };//logarea: 0 };
jQuery.getJSON('jit_tree/'+nodeId+'/'+depth, params, onComplete);
}
//box offsets
offset: 1,
//use canvas text
- Label: {
+ XXX_Label: {
type: labelType,
size: 10,
family: 'Tahoma, Verdana, Arial'
onShow: function(tip, node, isLeaf, domElement) {
var data = node.data;
var html = "<div class=\"tip-title\">"
- + (data.title ? data.title : "")
+ + (data.title ? "\""+data.title+"\"" : "")
+ " " + data.name
+ "</div><div class=\"tip-text\">";
html += "<br />";
- html += sprintf("Size: %d (%d + %d)<br />", data.self_size+data.kids_size, data.self_size, data.kids_size);
-
+ html += sprintf("Memory use: %s<br />", bytesToSize(data.self_size+data.kids_size,2));
+ if (data.kids_size) {
+ html += sprintf("Child use: %s<br />", bytesToSize(data.kids_size,2));
+ }
if (data.self_size) {
- html += sprintf("Memory usage:<br />");
+ html += sprintf("Own use: %s<br />", bytesToSize(data.self_size,2));
+ html += sprintf("<div style=\"color:grey\">");
bySortedValue(data.leaves,
function(a, b) { return a[1] - b[1] },
- function(k, v) { html += sprintf(" %10s: %5d<br />", k, v);
+ function(k, v) { html += sprintf(" %9s: %s<br />", k, bytesToSize(v,2));
});
- html += "<br />";
+ html += sprintf("</div>");
}
+ html += "<br />";
+ if (0) {
html += sprintf("Attributes:<br />");
bySortedValue(data.attr,
function(a, b) { return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0 },
function(k, v) { html += sprintf(" %10s: %5d<br />", k, v);
});
html += "<br />";
+ }
+ html += sprintf("<div style=\"color:grey\">");
if (data.child_count) {
html += sprintf("Children: %d of %d<br />", data.child_count, data.kids_node_count);
}
html += sprintf("Depth: %d<br />", data.depth);
html += sprintf("Parent: %d<br />", data.parent_id);
- html += JSON.stringify(data.attr, undefined, 4);
+ //html += JSON.stringify(data.attr, undefined, 4);
//html += JSON.stringify(data, undefined, 4);
+ html += sprintf("</div>");
tip.innerHTML = html;
}
style.border = '1px solid transparent';
};
- }
+ },
+ onPlaceLabel: function(domElement, node){ },
});
request_jit_tree(1, 0, levelsToShow, function(data) {