-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.jade
155 lines (148 loc) · 4.89 KB
/
index.jade
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
script(src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js")
script(src="/socket.io/socket.io.js",type='text/javascript')
script(src="/javascripts/jquery.js",type='text/javascript')
script(src="/javascripts/jquery.flot.js",type='text/javascript')
script(src="http://code.highcharts.com/highcharts.js")
script(src="http://code.highcharts.com/modules/exporting.js")
body
h1 Realtime Chart
block content
div#graph(style="width:600px;height:300px;")
script(type='text/javascript').
var socket = io.connect();
var chart = null;
var actualDate = new Date();
function addChartData(chartData) {
if(chart != null && chartData.pc != null) {
var index = serieIndex(chartData.pc);
var series;
if(index >= 0) {
series = chart.series[index];
} else {
series = addNewSerie(chartData.pc);
}
// check if new entry is realy newer than last entry
var isFuture = true;
var length = series.data.length;
if(length == 1) {
// remove testdata
if(new Date(series.data[0].x).getTime()==actualDate.getTime()) {
series.data[0].remove();
}
length = series.data.length;
}
if(length > 0) {
isFuture = new Date(chartData.dat).getTime() > new Date(series.data[length-1].x).getTime();
}
if(isFuture) {
var x = new Date(chartData.dat).getTime();
var y = chartData.cpu * 100;
series.addPoint([x, y], true, false);
}
}
};
chart = new Highcharts.Chart({
chart: {
renderTo: 'graph',
type: 'spline',
marginRight: 10,
events: addChartData(null)
},
title: {
text: 'CPU Usage'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 100
},
yAxis: {
min: 0,
max: 100,
tickInterval: 20,
title: {
text: 'CPU %'
},
plotBands: [{ // highlight area
from: 80,
to: 100,
color: 'rgba(255, 0, 0, 0.2)',
label: {
text: 'Critical',
style: {
color: '#606060'
}
}
}]
},
plotOptions: {
spline: {
lineWidth: 1,
marker: {
enabled: false
},
states: {
hover: {
lineWidth: 3
}
},
}
},
tooltip: {
enabled: false,
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: true
},
exporting: {
enabled: false
},
series: [{
name: 'pc1',
data: (function() {
// generate an array of random data
var data = [];
data.push({
x: actualDate,
y: null
});
return data;
})()
}]
});
function serieIndex(seriesName) {
for (var i = 0; i < chart.series.length; i++) {
if(chart.series[i].name == seriesName) {
return i;
}
}
return -1;
}
function addNewSerie(newName) {
return chart.addSeries({
name: newName,
data: (function() {
var data = [];
data.push({
x: actualDate.getTime(),
y: null
});
return data;
})(),
id: Math.floor(Math.random()*1000)
});
chart.redraw();
}
socket.on('chart', function (data) {
if(!$.isArray(data.chartData)) {
addChartData(data.chartData);
} // TODO handle if it is an array
});