Skip to content

Commit 2b7c55a

Browse files
author
Robert Jackson
committed
Refactor to use graph service, and heimdalljs-graph.
1 parent 5bdb8b9 commit 2b7c55a

File tree

11 files changed

+115
-84
lines changed

11 files changed

+115
-84
lines changed

app/components/basic-tree.js

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,39 @@ import { select, event } from 'd3-selection';
55
import { tree, cluster, hierarchy } from 'd3-hierarchy';
66
import { zoom, zoomIdentity } from 'd3-zoom';
77

8-
const { run, get } = Ember;
8+
const { run, get, inject } = Ember;
99

1010
const DURATION = 500;
1111

1212
// copied these functions temporarily from `broccoli-viz` here:
1313
// https://github.com/ember-cli/broccoli-viz/blob/master/lib/node-by-id.js
14-
function nodesById(nodes) {
15-
var result = new Array(nodes.length);
16-
nodes.forEach(function(node) {
17-
result[node.id] = node;
18-
});
19-
return result;
20-
}
2114

2215
export default Ember.Component.extend({
2316
classNames: ['basic-tree'],
2417

18+
graph: inject.service(),
19+
2520
init() {
2621
this._super(...arguments);
2722

28-
this._lastGraphPath = null;
2923
this._graphData = null;
3024
},
3125

3226
didReceiveAttrs() {
3327
let graphData = get(this, 'graphData');
3428

3529
if (this._lastGraphData !== graphData && graphData) {
36-
this.processRawData(graphData);
30+
run.schedule('render', this, this.drawTree, graphData);
3731

3832
this._lastGraphData = graphData;
3933
}
4034
},
4135

42-
processRawData(response) {
43-
this._graphData = {
44-
nodesById: nodesById(response.nodes),
45-
nodes: response.nodes
46-
};
47-
48-
run.schedule('render', this, this.drawTree, this._graphData);
49-
},
50-
5136
nodeFilter(node) {
5237
return node.label.broccoliNode;
5338
},
5439

55-
drawTree(data) {
40+
drawTree(graphNode) {
5641
let svgContainer = this.element.querySelector('.svg-container');
5742
svgContainer.innerHTML = '';
5843

@@ -64,12 +49,19 @@ export default Ember.Component.extend({
6449

6550
let g = svg.append("g");
6651

67-
let root = hierarchy(data.nodes[0], (node) => {
68-
return node.children
69-
.map((childId) => data.nodesById[childId])
70-
.filter(this.nodeFilter);
52+
let root = hierarchy(graphNode, (node) => {
53+
let children = [];
54+
for (let child of node.adjacentIterator()) {
55+
if (this.nodeFilter && !this.nodeFilter(child)) {
56+
continue;
57+
}
58+
59+
children.push(child);
60+
}
61+
62+
return children;
7163
})
72-
.sum(d => d.stats.time.self);
64+
.sum(d => d._stats.time.self);
7365

7466
// make debugging easier, this is dumb though
7567
self.root = root;
@@ -122,7 +114,7 @@ export default Ember.Component.extend({
122114
.attr("dy", '0.0em')
123115
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
124116
.text(function(d) {
125-
return d.data.label.name;
117+
return `${d.data.label.name} (${d.data._id})`;
126118
});
127119

128120
nodeEnter
@@ -138,7 +130,7 @@ export default Ember.Component.extend({
138130
.attr("dy", '2.1em')
139131
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
140132
.text(function(d) {
141-
return `self: ${(d.data.stats.time.self / 1000000).toFixed(2)}`;
133+
return `self: ${(d.data._stats.time.self / 1000000).toFixed(2)}`;
142134
});
143135

144136
// update exiting node locations

app/components/slow-node-times.js

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import Ember from 'ember';
2-
import heimdallGraph from 'heimdalljs-graph';
32

43
const {
5-
computed
4+
computed,
5+
getOwner,
6+
inject
67
} = Ember;
78

89
function selfTime(node) {
@@ -23,54 +24,57 @@ function nodeTime(node) {
2324
}
2425

2526
export default Ember.Component.extend({
27+
graph: inject.service(),
28+
2629
nodes: computed('data', 'filter', 'pluginNameFilter', 'groupByPluginName', function() {
2730
let data = this.get('data');
2831
if (!data) { return []; }
2932

3033
let nodes = [];
31-
let graph = heimdallGraph.loadFromJSON(data);
3234

33-
for (let node of graph.dfsIterator()) {
35+
for (let node of data.dfsIterator()) {
3436
if (node.label.broccoliNode) {
35-
nodes.push({
36-
label: node.label,
37-
time: nodeTime(node)
38-
})
37+
nodes.push(node);
38+
if (!node._stats.time.plugin) {
39+
node._stats.time.plugin = nodeTime(node);
40+
}
3941
}
4042
}
4143

42-
//let groupByPluginName = this.get('groupByPluginName');
43-
//let pluginName = this.get('pluginNameFilter');
44-
// nodes = data.nodes.filter((node) => {
45-
// if (!node.label.broccoliNode) { return false; }
46-
// if (pluginName && node.label.broccoliPluginName !== pluginName) { return false; }
47-
48-
// return true;
49-
// })
50-
51-
// if (groupByPluginName) {
52-
// let pluginNameMap = nodes.reduce((memo, node) => {
53-
// let pluginName = node.label.broccoliPluginName;
54-
// memo[pluginName] = memo[pluginName] || { count: 0, time: 0 };
55-
// memo[pluginName].time += node.stats.time.self;
56-
// memo[pluginName].count++;
57-
// return memo;
58-
// }, {})
59-
60-
// nodes = [];
61-
// for (let pluginName in pluginNameMap) {
62-
// nodes.push({
63-
// label: { name: pluginName, broccoliPluginName: pluginNameMap[pluginName].count },
64-
// stats: {
65-
// time: { self: pluginNameMap[pluginName].time}
66-
// }
67-
// });
68-
// }
69-
// }
44+
let pluginNameFilter = this.get('pluginNameFilter');
45+
if (pluginNameFilter) {
46+
nodes = nodes.filter((node) => {
47+
if (!node.label.broccoliNode) { return false; }
48+
if (node.label.broccoliPluginName !== pluginNameFilter) { return false; }
49+
50+
return true;
51+
});
52+
}
53+
54+
let groupByPluginName = this.get('groupByPluginName');
55+
if (groupByPluginName) {
56+
let pluginNameMap = nodes.reduce((memo, node) => {
57+
let pluginName = node.label.broccoliPluginName;
58+
memo[pluginName] = memo[pluginName] || { count: 0, time: 0 };
59+
memo[pluginName].time += node._stats.time.plugin;
60+
memo[pluginName].count++;
61+
return memo;
62+
}, {})
63+
64+
nodes = [];
65+
for (let pluginName in pluginNameMap) {
66+
nodes.push({
67+
label: { name: pluginName, broccoliPluginName: pluginNameMap[pluginName].count },
68+
_stats: {
69+
time: { plugin: pluginNameMap[pluginName].time }
70+
}
71+
});
72+
}
73+
}
7074

7175
let sortedNodes = nodes
7276
.sort((a, b) => {
73-
return b.time - a.time;
77+
return b._stats.time.plugin - a._stats.time.plugin;
7478
});
7579

7680
return sortedNodes;
@@ -80,10 +84,13 @@ export default Ember.Component.extend({
8084
let nodes = this.get('nodes');
8185

8286
return nodes.reduce(function(previousValue, node){
83-
return previousValue + node.time;
87+
return previousValue + node._stats.time.plugin;
8488
}, 0);
8589
}),
8690

8791
actions: {
92+
'focus-node'(node) {
93+
this.get('graph').selectNode(node);
94+
}
8895
}
8996
});

app/controllers/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import Ember from 'ember';
22
import fetch from "ember-network/fetch";
33
import config from '../config/environment';
44

5-
export default Ember.Controller.extend({
6-
init() {
7-
this._super(...arguments);
5+
const {
6+
inject
7+
} = Ember;
88

9-
this.graphData = null;
10-
},
9+
export default Ember.Controller.extend({
10+
graph: inject.service(),
1111

1212
actions: {
1313
parseFile(event) {
1414
let reader = new FileReader();
1515
reader.onload = (e) => {
1616
var contents = e.target.result;
17-
this.set('graphData', JSON.parse(contents));
17+
this.get('graph').setGraph(JSON.parse(contents));
1818
};
1919

2020
reader.readAsText(event.target.files[0]);
@@ -26,7 +26,7 @@ export default Ember.Controller.extend({
2626
return response.json();
2727
})
2828
.then((response) => {
29-
this.set('graphData', response);
29+
this.get('graph').setGraph(JSON.parse(contents));
3030
});
3131
}
3232
}

app/controllers/selected-node.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Ember from 'ember';
2+
3+
const {
4+
inject
5+
} = Ember;
6+
7+
export default Ember.Controller.extend({
8+
graph: inject.service()
9+
})

app/controllers/slow-nodes.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import Ember from 'ember';
22

3+
const {
4+
inject
5+
} = Ember;
6+
37
export default Ember.Controller.extend({
4-
init() {
5-
this._super(...arguments);
6-
this.data = null;
7-
},
8+
graph: inject.service(),
89

910
actions: {
1011
parseFile(event) {
1112
let reader = new FileReader();
1213
reader.onload = (e) => {
1314
var contents = e.target.result;
14-
this.set('data', JSON.parse(contents));
15+
this.get('graph').setGraph(JSON.parse(contents));
1516
};
1617

1718
reader.readAsText(event.target.files[0]);

app/router.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Router = Ember.Router.extend({
77
});
88

99
Router.map(function() {
10+
this.route('selected-node');
1011
this.route('slow-nodes');
1112
});
1213

app/services/graph.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Ember from 'ember';
2+
import heimdallGraph from 'heimdalljs-graph';
3+
4+
const {
5+
getOwner
6+
} = Ember;
7+
8+
export default Ember.Service.extend({
9+
setGraph(data) {
10+
let graph = heimdallGraph.loadFromJSON(data);
11+
12+
this.set('data', data);
13+
this.set('graph', graph);
14+
},
15+
16+
selectNode(node) {
17+
this.set('selectedNode', node);
18+
getOwner(this).lookup('router:main').transitionTo('selected-node');
19+
}
20+
});

app/templates/components/slow-node-times.hbs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
<table>
66
<thead>
77
<tr>
8-
<td>Annotation</td>
9-
<td>Additional Details</td>
10-
<td>Time</td>
8+
<td>Description</td>
9+
<td>{{if groupByPluginName "Count" "Plugin Name"}}</td>
10+
<td>Time (ms)</td>
1111
</tr>
1212
</thead>
1313
<tbody>
1414
{{#each nodes as |node|}}
15-
<tr>
15+
<tr onclick={{action 'focus-node' node}}>
1616
<td>{{node.label.name}}</td>
1717
<td>{{node.label.broccoliPluginName}}</td>
18-
<td>{{ns-to-ms node.time}}</td>
18+
<td>{{ns-to-ms node._stats.time.plugin}}</td>
1919
</tr>
2020
{{/each}}
2121
</tbody>

app/templates/index.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
{{! basic-tree graphData=graphData}}
1616

1717
{{! useful for testing... }}
18-
{{ basic-tree graphData=graphData}}
18+
{{basic-tree graphData=graph.graph}}

app/templates/selected-node.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{basic-tree graphData=graph.selectedNode nodeFilter=null}}

0 commit comments

Comments
 (0)