/
node-stat.js
117 lines (95 loc) · 2.57 KB
/
node-stat.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import EmberHighChartsComponent from 'ember-highcharts/components/high-charts';
import _ from 'lodash/lodash';
/*globals Highcharts */
export default EmberHighChartsComponent.extend({
defaultOptions: {
chart: {
type: 'spline',
animation: Highcharts.svg
},
title: {
text: 'Node Data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
}
},
node: null,
statToGraph: null,
plotThreshold: 20,
content: null,
chartOptions: null,
getMostRecentStats: function() {
return _.takeRight(this.get('node').get('statsHistory'), this.get('plotThreshold'));
},
setInitialData: function() {
let statName = this.get('statToGraph');
let options = _.assign(_.cloneDeep(this.defaultOptions), { title: { text: statName } });
let stats = this.getMostRecentStats();
this.set('chartOptions', options);
this.set('content', [{
name: statName,
data: stats.map(function(stat) {
return {
x: stat.timestamp,
y: stat.stats[statName]
};
})
}]);
},
init: function() {
this._super(...arguments);
this.setInitialData();
},
streamNewDataIntoChart: function() {
let chart = this.get('chart');
let series = _.head(chart.series);
let mostRecentStats = this.getMostRecentStats();
let statName = this.get('chartOptions.title.text');
let plotThreshold = this.get('plotThreshold');
if (mostRecentStats.length < plotThreshold) {
series.setData(mostRecentStats.map(function(stat) {
return {
x: stat.timestamp,
y: stat.stats[statName]
};
}));
} else {
let latestStat = _.last(mostRecentStats);
series.addPoint([latestStat.timestamp, latestStat.stats[statName]], true, true);
}
}.observes('node.stats'),
switchChart: function() {
let statName = this.get('statToGraph');
let currentDisplayedStat = this.get('chartOptions.title.text');
let chart = this.get('chart');
if (statName !== currentDisplayedStat) {
this.setInitialData();
chart.redraw();
}
}.observes('statToGraph')
});