Code coverage report for tracevis/lib/trace/d3Treeify.js

Statements: 100% (21 / 21)      Branches: 100% (6 / 6)      Functions: 100% (6 / 6)      Lines: 100% (20 / 20)     

All files » tracevis/lib/trace/ » d3Treeify.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 661           1 9   15     9 1     1 15     15 6         15   15                   15 1     15 4     15     8                         1 23    
module.exports = d3Treeify;
 
/*
 * Creates a representation of the graph that can be used for hierarchical
 * layouts with d3. There should be no direct dependency on D3 in this module.
 */
function d3Treeify(g) {
  var minStart = Math.min.apply(Math,
                                g.nodes().map(function(u) {
                                  return g.node(u).startNanos;
                                }));
 
  if (g.children(null).length === 0) {
    throw new Error('Input graph must have at least one node!');
  }
 
  function dfs(u) {
    var uValue = g.node(u),
        children = [];
 
    g.children(u).forEach(function(v) {
      children.push(dfs(v));
    });
 
    // Sort children by start time. This is used implicitly by
    // d3.layout.hierarchy layer.
    sortByOrder(children);
 
    var result = {
      id: u,
      name: uValue.name,
      startMillis: (uValue.startNanos - minStart) / (1000 * 1000),
      totalMillis: (uValue.endNanos - uValue.startNanos) / (1000 * 1000),
      value: uValue.value,
      resultType: uValue.resultType,
      order: uValue.order
    };
 
    if ('pendingNanos' in uValue) {
      result.runMillis = (uValue.pendingNanos - uValue.startNanos) / (1000 * 1000);
    }
 
    if (children.length) {
      result.children = children;
    }
 
    return result;
  }
 
  return {
    id: null,
    name: '',
    startMillis: 0,
    runMillis: 0,
    totalMillis: 0,
    value: '',
    resultType: '',
    order: -1,
    children: sortByOrder(g.children(null).map(dfs))
  };
}
 
function sortByOrder(tasks) {
  return tasks.sort(function(a, b) { return a.order - b.order; });
}