Skip to content

Commit

Permalink
add optional broccoli-viz —root-id=…
Browse files Browse the repository at this point in the history
Allows rendering a subgraph
  • Loading branch information
stefanpenner committed Dec 9, 2016
1 parent 6649f13 commit 4f3a31b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 8 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,18 @@ broccoli-viz --stats='time.*' --stats='fs.lstatSync.count' --stats='fs.mkdirSync
# Show all stats
broccoli-viz --stats='*' broccoli-viz.0.json > broccoli-viz.0.dot
```


### Render subtree

If you want to render only a subtree, `--root-id=:id` where `:id` is the id of the
root of the subgraph we wish to render

If no `--root-id` option is passed, the full graph is rendered

Examples:

```sh
# only renders 255 and its descendents
broccoli-viz --root-id=255 broccoli-viz.0.json > broccoli-viz.0.dot
```
8 changes: 4 additions & 4 deletions bin/broccoli-viz
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ var viz = require('..');
var fs = require('fs');

var argv = require('minimist')(process.argv.slice(2));
var USAGE = 'Usage: broccoli-viz [--root-id=id] [--stats=pattern] FILE ';

function usage() {
console.error('Usage: broccoli-viz [--stats=pattern] FILE');
console.error(USAGE);
process.exit(1);
}

function help() {
console.log('Usage: broccoli-viz [--stats=pattern] FILE');
console.log(uSAGE);
process.exit(0);
}

Expand All @@ -31,6 +32,5 @@ if (patterns && !Array.isArray(patterns)) {
patterns = [patterns];
}

console.log(viz.dot(viz.process(json.nodes), { stats: patterns }));

console.log(viz.dot(viz.process(json.nodes, argv)));

38 changes: 36 additions & 2 deletions lib/build-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,47 @@ function annotateNodes(graph) {
return graph;
}

module.exports = function(nodes) {
function fromRoot(graph, root) {
if (root === undefined) { return graph; }

var rootNode = graph.nodesById[root];

if (!rootNode) {
throw new TypeError("Can't find root: " + root);
}

var reachable = _reachable(graph, rootNode);
return {
nodesById: nodesById(reachable),
nodes: reachable
}
}

function _reachable(graph, root, acc) {
if (!acc) {
acc = [];
}

if (acc.indexOf(root) > -1) {
return acc;
}

acc.push(root);
root.children.forEach(function (childId) {
_reachable(graph, graph.nodesById[childId], acc);
});

return acc;
}

module.exports = function(nodes, _options) {
var options = _options || {};
var byId = nodesById(nodes);

var graph = {
nodesById: byId,
nodes: nodes,
};

return annotateNodes(graph);
return annotateNodes(fromRoot(graph, options['root-id']));
};
4 changes: 2 additions & 2 deletions lib/process.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var rank = require('./rank');
var buildGraph = require('./build-graph');

module.exports = function processNodes(nodes) {
return rank(buildGraph(nodes));
module.exports = function processNodes(nodes, options) {
return rank(buildGraph(nodes, options));
};
52 changes: 52 additions & 0 deletions test/build-graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,56 @@ describe('buildGraph', function() {
assert.deepEqual(graph.nodesById[3], node3);
assert.deepEqual(graph.nodesById[4], node4);
});

it('handles subgraphs starting at root-id', function() {
var root = {
_id: 1,
stats: {
time: {
self: 1,
}
},
children: [2, 4],
};

var node2 = {
_id: 2,
stats: {
time: {
self: 2,
}
},
children: [3],
};

var node3 = {
_id: 3,
stats: {
time: {
self: 4,
}
},
children: [],
};

var node4 = {
_id: 4,
stats: {
time: {
self: 8,
}
},
children: [],
};

var graph = buildGraph([root, node2, node3, node4], { 'root-id': 2 });

assert.equal(graph.nodes.length, 2, 'graph has correct number of nodes');
assert.deepEqual(graph.nodes.map(byTotalTime), [
6, 4
], 'nodes are annotated with total time');

assert.deepEqual(graph.nodesById[2], node2);
assert.deepEqual(graph.nodesById[3], node3);
});
});

0 comments on commit 4f3a31b

Please sign in to comment.