7 var callLuciRealtimeStats
= rpc
.declare({
9 method
: 'getRealtimeStats',
10 params
: [ 'mode', 'device' ],
11 expect
: { result
: [] }
17 Math
.log2
= Math
.log2
|| function(x
) { return Math
.log(x
) * Math
.LOG2E
; };
22 this.loadSVG(L
.resource('svg/load.svg'))
26 updateGraph: function(svg
, lines
, cb
) {
27 var G
= svg
.firstElementChild
;
29 var view
= document
.querySelector('#view');
31 var width
= view
.offsetWidth
- 2;
35 var data_wanted
= Math
.floor(width
/ step
);
40 for (var i
= 0; i
< lines
.length
; i
++)
50 /* prefill datasets */
51 for (var i
= 0; i
< data_values
.length
; i
++)
52 for (var j
= 0; j
< data_wanted
; j
++)
53 data_values
[i
][j
] = 0;
55 /* plot horizontal time interval lines */
56 for (var i
= width
% (step
* 60); i
< width
; i
+= step
* 60) {
57 var line
= document
.createElementNS('http://www.w3.org/2000/svg', 'line');
58 line
.setAttribute('x1', i
);
59 line
.setAttribute('y1', 0);
60 line
.setAttribute('x2', i
);
61 line
.setAttribute('y2', '100%');
62 line
.setAttribute('style', 'stroke:black;stroke-width:0.1');
64 var text
= document
.createElementNS('http://www.w3.org/2000/svg', 'text');
65 text
.setAttribute('x', i
+ 5);
66 text
.setAttribute('y', 15);
67 text
.setAttribute('style', 'fill:#eee; font-size:9pt; font-family:sans-serif; text-shadow:1px 1px 1px #000');
68 text
.appendChild(document
.createTextNode(Math
.round((width
- i
) / step
/ 60) + 'm'));
74 info
.interval
= pollInterval
;
75 info
.timeframe
= data_wanted
/ 60;
91 pollData: function() {
92 poll
.add(L
.bind(function() {
95 for (var i
= 0; i
< graphPolls
.length
; i
++) {
96 var ctx
= graphPolls
[i
];
97 tasks
.push(L
.resolveDefault(callLuciRealtimeStats('load'), []));
100 return Promise
.all(tasks
).then(L
.bind(function(datasets
) {
101 for (var gi
= 0; gi
< graphPolls
.length
; gi
++) {
102 var ctx
= graphPolls
[gi
],
109 var data_wanted
= Math
.floor(ctx
.width
/ ctx
.step
);
110 var last_timestamp
= NaN
;
112 for (var i
= 0, di
= 0; di
< lines
.length
; di
++) {
113 if (lines
[di
] == null)
116 var multiply
= (lines
[di
].multiply
!= null) ? lines
[di
].multiply
: 1,
117 offset
= (lines
[di
].offset
!= null) ? lines
[di
].offset
: 0;
119 for (var j
= ctx
.timestamp
? 0 : 1; j
< data
.length
; j
++) {
120 /* skip overlapping entries */
121 if (data
[j
][0] <= ctx
.timestamp
)
126 last_timestamp
= data
[j
][0];
129 info
.line_current
[i
] = data
[j
][di
+ 1] * multiply
;
130 info
.line_current
[i
] -= Math
.min(info
.line_current
[i
], offset
);
131 values
[i
].push(info
.line_current
[i
]);
137 /* cut off outdated entries */
138 ctx
.fill
= Math
.min(ctx
.fill
, data_wanted
);
140 for (var i
= 0; i
< values
.length
; i
++) {
141 var len
= values
[i
].length
;
142 values
[i
] = values
[i
].slice(len
- data_wanted
, len
);
144 /* find peaks, averages */
145 info
.line_peak
[i
] = NaN
;
146 info
.line_average
[i
] = 0;
148 for (var j
= 0; j
< values
[i
].length
; j
++) {
149 info
.line_peak
[i
] = isNaN(info
.line_peak
[i
]) ? values
[i
][j
] : Math
.max(info
.line_peak
[i
], values
[i
][j
]);
150 info
.line_average
[i
] += values
[i
][j
];
153 info
.line_average
[i
] = info
.line_average
[i
] / ctx
.fill
;
156 info
.peak
= Math
.max
.apply(Math
, info
.line_peak
);
158 /* remember current timestamp, calculate horizontal scale */
159 if (!isNaN(last_timestamp
))
160 ctx
.timestamp
= last_timestamp
;
162 var size
= Math
.floor(Math
.log2(info
.peak
)),
163 div
= Math
.pow(2, size
- (size
% 10)),
164 mult
= info
.peak
/ div
,
165 mult
= (mult
< 5) ? 2 : ((mult
< 50) ? 10 : ((mult
< 500) ? 100 : 1000));
167 info
.peak
= info
.peak
+ (mult
* div
) - (info
.peak
% (mult
* div
));
169 data_scale
= ctx
.height
/ info
.peak
;
172 for (var i
= 0, di
= 0; di
< lines
.length
; di
++) {
173 if (lines
[di
] == null)
176 var el
= ctx
.svg
.firstElementChild
.getElementById(lines
[di
].line
),
177 pt
= '0,' + ctx
.height
,
183 for (var j
= 0; j
< values
[i
].length
; j
++) {
184 var x
= j
* ctx
.step
;
186 y
= ctx
.height
- Math
.floor(values
[i
][j
] * data_scale
);
187 //y -= Math.floor(y % (1 / data_scale));
189 pt
+= ' ' + x
+ ',' + y
;
192 pt
+= ' ' + ctx
.width
+ ',' + y
+ ' ' + ctx
.width
+ ',' + ctx
.height
;
194 el
.setAttribute('points', pt
);
199 info
.label_25
= 0.25 * info
.peak
;
200 info
.label_50
= 0.50 * info
.peak
;
201 info
.label_75
= 0.75 * info
.peak
;
203 if (typeof(ctx
.cb
) == 'function')
204 ctx
.cb(ctx
.svg
, info
);
207 }, this), pollInterval
);
210 loadSVG: function(src
) {
211 return request
.get(src
).then(function(response
) {
213 throw new Error(response
.statusText
);
216 'style': 'width:100%;height:300px;border:1px solid #000;background:#fff'
217 }, E(response
.text()));
221 render: function(data
) {
226 E('div', { 'class': 'right' }, E('small', { 'id': 'scale' }, '-')),
229 E('table', { 'class': 'table', 'style': 'width:100%;table-layout:fixed' }, [
230 E('tr', { 'class': 'tr' }, [
231 E('td', { 'class': 'td right top' }, E('strong', { 'style': 'border-bottom:2px solid #f00' }, [ _('1 Minute Load:') ])),
232 E('td', { 'class': 'td', 'id': 'lb_load01_cur' }, [ '0.00' ]),
234 E('td', { 'class': 'td right top' }, E('strong', {}, [ _('Average:') ])),
235 E('td', { 'class': 'td', 'id': 'lb_load01_avg' }, [ '0.00' ]),
237 E('td', { 'class': 'td right top' }, E('strong', {}, [ _('Peak:') ])),
238 E('td', { 'class': 'td', 'id': 'lb_load01_peak' }, [ '0.00' ])
240 E('tr', { 'class': 'tr' }, [
241 E('td', { 'class': 'td right top' }, E('strong', { 'style': 'border-bottom:2px solid #f60' }, [ _('5 Minute Load:') ])),
242 E('td', { 'class': 'td', 'id': 'lb_load05_cur' }, [ '0.00' ]),
244 E('td', { 'class': 'td right top' }, E('strong', {}, [ _('Average:') ])),
245 E('td', { 'class': 'td', 'id': 'lb_load05_avg' }, [ '0.00' ]),
247 E('td', { 'class': 'td right top' }, E('strong', {}, [ _('Peak:') ])),
248 E('td', { 'class': 'td', 'id': 'lb_load05_peak' }, [ '0.00' ])
250 E('tr', { 'class': 'tr' }, [
251 E('td', { 'class': 'td right top' }, E('strong', { 'style': 'border-bottom:2px solid #fa0' }, [ _('15 Minute Load:') ])),
252 E('td', { 'class': 'td', 'id': 'lb_load15_cur' }, [ '0.00' ]),
254 E('td', { 'class': 'td right top' }, E('strong', {}, [ _('Average:') ])),
255 E('td', { 'class': 'td', 'id': 'lb_load15_avg' }, [ '0.00' ]),
257 E('td', { 'class': 'td right top' }, E('strong', {}, [ _('Peak:') ])),
258 E('td', { 'class': 'td', 'id': 'lb_load15_peak' }, [ '0.00' ])
263 this.updateGraph(svg
, [ { line
: 'load01' }, { line
: 'load05' }, { line
: 'load15' } ], function(svg
, info
) {
264 var G
= svg
.firstElementChild
, tab
= svg
.parentNode
;
266 G
.getElementById('label_25').firstChild
.data
= '%.2f'.format(info
.label_25
/ 100);
267 G
.getElementById('label_50').firstChild
.data
= '%.2f'.format(info
.label_50
/ 100);
268 G
.getElementById('label_75').firstChild
.data
= '%.2f'.format(info
.label_75
/ 100);
270 tab
.querySelector('#scale').firstChild
.data
= _('(%d minute window, %d second interval)').format(info
.timeframe
, info
.interval
);
272 tab
.querySelector('#lb_load01_cur').firstChild
.data
= '%.2f'.format(info
.line_current
[0] / 100);
273 tab
.querySelector('#lb_load01_avg').firstChild
.data
= '%.2f'.format(info
.line_average
[0] / 100);
274 tab
.querySelector('#lb_load01_peak').firstChild
.data
= '%.2f'.format(info
.line_peak
[0] / 100);
276 tab
.querySelector('#lb_load05_cur').firstChild
.data
= '%.2f'.format(info
.line_current
[1] / 100);
277 tab
.querySelector('#lb_load05_avg').firstChild
.data
= '%.2f'.format(info
.line_average
[1] / 100);
278 tab
.querySelector('#lb_load05_peak').firstChild
.data
= '%.2f'.format(info
.line_peak
[1] / 100);
280 tab
.querySelector('#lb_load15_cur').firstChild
.data
= '%.2f'.format(info
.line_current
[2] / 100);
281 tab
.querySelector('#lb_load15_avg').firstChild
.data
= '%.2f'.format(info
.line_average
[2] / 100);
282 tab
.querySelector('#lb_load15_peak').firstChild
.data
= '%.2f'.format(info
.line_peak
[2] / 100);
290 handleSaveApply
: null,