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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/*
* Support library aimed at providing commonly used functions and classes
* that may be used while plotting RRD files with Flot
*
* Part of the javascriptRRD package
* Copyright (c) 2009 Frank Wuerthwein, fkw@ucsd.edu
*
* Original repository: http://javascriptrrd.sourceforge.net/
*
* MIT License [http://www.opensource.org/licenses/mit-license.php]
*
*/
/*
*
* Flot is a javascript plotting library developed and maintained by
* Ole Laursen [http://code.google.com/p/flot/]
*
*/
// Return a Flot-like data structure
// Since Flot does not properly handle empty elements, min and max are returned, too
function rrdDS2FlotSeries(rrd_file,ds_id,rra_idx,want_rounding) {
var ds=rrd_file.getDS(ds_id);
var ds_name=ds.getName();
var ds_idx=ds.getIdx();
var rra=rrd_file.getRRA(rra_idx);
var rra_rows=rra.getNrRows();
var last_update=rrd_file.getLastUpdate();
var step=rra.getStep();
if (want_rounding!=false) {
// round last_update to step
// so that all elements are sync
last_update-=(last_update%step);
}
var first_el=last_update-(rra_rows-1)*step;
var timestamp=first_el;
var flot_series=[];
for (var i=0;i<rra_rows;i++) {
var el=rra.getEl(i,ds_idx);
if (el!=undefined) {
flot_series.push([timestamp*1000.0,el]);
}
timestamp+=step;
} // end for
return {label: ds_name, data: flot_series, min: first_el*1000.0, max:last_update*1000.0};
}
// return an object with an array containing Flot elements, one per DS
// min and max are also returned
function rrdRRA2FlotObj(rrd_file,rra_idx,ds_list,want_ds_labels,want_rounding) {
var rra=rrd_file.getRRA(rra_idx);
var rra_rows=rra.getNrRows();
var last_update=rrd_file.getLastUpdate();
var step=rra.getStep();
if (want_rounding!=false) {
// round last_update to step
// so that all elements are sync
last_update-=(last_update%step);
}
var first_el=last_update-(rra_rows-1)*step;
var out_el={data:[], min:first_el*1000.0, max:last_update*1000.0};
for (ds_list_idx in ds_list) {
var ds_id=ds_list[ds_list_idx];
var ds=rrd_file.getDS(ds_id);
var ds_name=ds.getName();
var ds_idx=ds.getIdx();
var timestamp=first_el;
var flot_series=[];
for (var i=0;i<rra_rows;i++) {
var el=rra.getEl(i,ds_idx);
if (el!=undefined) {
flot_series.push([timestamp*1000.0,el]);
}
timestamp+=step;
} // end for
var flot_el={data:flot_series};
if (want_ds_labels!=false) {
var ds_name=ds.getName();
flot_el.label= ds_name;
}
out_el.data.push(flot_el);
} //end for ds_list_idx
return out_el;
}
// return an object with an array containing Flot elements
// have a positive and a negative stack of DSes, plus DSes with no stacking
// min and max are also returned
// If one_undefined_enough==true, a whole stack is invalidated if a single element
// of the stack is invalid
function rrdRRAStackFlotObj(rrd_file,rra_idx,
ds_positive_stack_list,ds_negative_stack_list,ds_single_list,
want_ds_labels,want_rounding,one_undefined_enough) {
var rra=rrd_file.getRRA(rra_idx);
var rra_rows=rra.getNrRows();
var last_update=rrd_file.getLastUpdate();
var step=rra.getStep();
if (want_rounding!=false) {
// round last_update to step
// so that all elements are sync
last_update-=(last_update%step);
}
if (one_undefined_enough!=true) { // make sure it is a boolean
one_undefined_enough=false;
}
var first_el=last_update-(rra_rows-1)*step;
var out_el={data:[], min:first_el*1000.0, max:last_update*1000.0};
// first the stacks stack
var stack_els=[ds_positive_stack_list,ds_negative_stack_list];
for (stack_list_id in stack_els) {
var stack_list=stack_els[stack_list_id];
var tmp_flot_els=[];
var tmp_ds_ids=[];
var tmp_nr_ids=stack_list.length;
for (ds_list_idx in stack_list) {
var ds_id=stack_list[ds_list_idx];
var ds=rrd_file.getDS(ds_id);
var ds_name=ds.getName();
var ds_idx=ds.getIdx();
tmp_ds_ids.push(ds_idx); // getting this is expensive, call only once
// initialize
var flot_el={data:[]}
if (want_ds_labels!=false) {
var ds_name=ds.getName();
flot_el.label= ds_name;
}
tmp_flot_els.push(flot_el);
}
var timestamp=first_el;
for (var row=0;row<rra_rows;row++) {
var ds_vals=[];
var all_undef=true;
var all_def=true;
for (var id=0; id<tmp_nr_ids; id++) {
var ds_idx=tmp_ds_ids[id];
var el=rra.getEl(row,ds_idx);
if (el!=undefined) {
all_undef=false;
ds_vals.push(el);
} else {
all_def=false;
ds_vals.push(0);
}
} // end for id
if (!all_undef) { // if all undefined, skip
if (all_def || (!one_undefined_enough)) {
// this is a valid column, do the math
for (var id=1; id<tmp_nr_ids; id++) {
ds_vals[id]+=ds_vals[id-1]; // both positive and negative stack use a +, negative stack assumes negative values
}
// fill the flot data
for (var id=0; id<tmp_nr_ids; id++) {
tmp_flot_els[id].data.push([timestamp*1000.0,ds_vals[id]]);
}
}
} // end if
timestamp+=step;
} // end for row
// put flot data in output object
// reverse order so higher numbers are behind
for (var id=0; id<tmp_nr_ids; id++) {
out_el.data.push(tmp_flot_els[tmp_nr_ids-id-1]);
}
} //end for stack_list_id
for (ds_list_idx in ds_single_list) {
var ds_id=ds_single_list[ds_list_idx];
var ds=rrd_file.getDS(ds_id);
var ds_name=ds.getName();
var ds_idx=ds.getIdx();
var timestamp=first_el;
var flot_series=[];
for (var i=0;i<rra_rows;i++) {
var el=rra.getEl(i,ds_idx);
if (el!=undefined) {
flot_series.push([timestamp*1000.0,el]);
}
timestamp+=step;
} // end for
var flot_el={data:flot_series};
if (want_ds_labels!=false) {
var ds_name=ds.getName();
flot_el.label= ds_name;
}
out_el.data.push(flot_el);
} //end for ds_list_idx
return out_el;
}
// return an object with an array containing Flot elements, one per RRD
// min and max are also returned
function rrdRRAMultiStackFlotObj(rrd_files, // a list of [rrd_id,rrd_file] pairs, all rrds must have the same step
rra_idx,ds_id,
want_rrd_labels,want_rounding,
one_undefined_enough) { // If true, a whole stack is invalidated if a single element of the stack is invalid
var reference_rra=rrd_files[0][1].getRRA(rra_idx); // get the first one, all should be the same
var rows=reference_rra.getNrRows();
var step=reference_rra.getStep();
var ds_idx=rrd_files[0][1].getDS(ds_id).getIdx(); // this can be expensive, do once (all the same)
// rrds can be slightly shifted, calculate range
var max_ts=null;
var min_ts=null;
// initialize list of rrd data elements
var tmp_flot_els=[];
var tmp_rras=[];
var tmp_last_updates=[];
var tmp_nr_ids=rrd_files.length;
for (var id=0; id<tmp_nr_ids; id++) {
var rrd_file=rrd_files[id][1];
var rrd_rra=rrd_file.getRRA(rra_idx);
var rrd_last_update=rrd_file.getLastUpdate();
if (want_rounding!=false) {
// round last_update to step
// so that all elements are sync
rrd_last_update-=(rrd_last_update%step);
}
tmp_last_updates.push(rrd_last_update);
var rrd_min_ts=rrd_last_update-(rows-1)*step;
if ((max_ts==null) || (rrd_last_update>max_ts)) {
max_ts=rrd_last_update;
}
if ((min_ts==null) || (rrd_min_ts<min_ts)) {
min_ts=rrd_min_ts;
}
tmp_rras.push(rrd_rra);
// initialize
var flot_el={data:[]}
if (want_rrd_labels!=false) {
var rrd_name=rrd_files[id][0];
flot_el.label= rrd_name;
}
tmp_flot_els.push(flot_el);
}
var out_el={data:[], min:min_ts*1000.0, max:max_ts*1000.0};
for (var ts=min_ts;ts<=max_ts;ts+=step) {
var rrd_vals=[];
var all_undef=true;
var all_def=true;
for (var id=0; id<tmp_nr_ids; id++) {
var rrd_rra=tmp_rras[id];
var rrd_last_update=tmp_last_updates[id];
var row_delta=Math.round((rrd_last_update-ts)/step);
var el=undefined; // if out of range
if ((row_delta>=0) && (row_delta<rows)) {
el=rrd_rra.getEl(rows-row_delta-1,ds_idx);
}
if (el!=undefined) {
all_undef=false;
rrd_vals.push(el);
} else {
all_def=false;
rrd_vals.push(0);
}
} // end for id
if (!all_undef) { // if all undefined, skip
if (all_def || (!one_undefined_enough)) {
// this is a valid column, do the math
for (var id=1; id<tmp_nr_ids; id++) {
rrd_vals[id]+=rrd_vals[id-1];
}
// fill the flot data
for (var id=0; id<tmp_nr_ids; id++) {
tmp_flot_els[id].data.push([ts*1000.0,rrd_vals[id]]);
}
}
} // end if
} // end for ts
// put flot data in output object
// reverse order so higher numbers are behind
for (var id=0; id<tmp_nr_ids; id++) {
out_el.data.push(tmp_flot_els[tmp_nr_ids-id-1]);
}
return out_el;
}
// ======================================
// Helper class for handling selections
// =======================================================
function rrdFlotSelection() {
this.selection_min=null;
this.selection_max=null;
};
// reset to a state where ther is no selection
rrdFlotSelection.prototype.reset = function() {
this.selection_min=null;
this.selection_max=null;
};
// given the selection ranges, set internal variable accordingly
rrdFlotSelection.prototype.setFromFlotRanges = function(ranges) {
this.selection_min=ranges.xaxis.from;
this.selection_max=ranges.xaxis.to;
};
// Return a Flot ranges structure that can be promptly used in setSelection
rrdFlotSelection.prototype.getFlotRanges = function() {
return { xaxis: {from: this.selection_min, to: this.selection_max}};
};
// return true is a selection is in use
rrdFlotSelection.prototype.isSet = function() {
return this.selection_min!=null;
};
// Given an array of flot lines, limit to the selection
rrdFlotSelection.prototype.trim_flot_data = function(flot_data) {
var out_data=[];
for (var i=0; i<flot_data.length; i++) {
var data_el=flot_data[i];
out_data.push({label : data_el.label, data:this.trim_data(data_el.data), color:data_el.color, lines:data_el.lines, yaxis:data_el.yaxis});
}
return out_data;
};
// Limit to selection the flot series data element
rrdFlotSelection.prototype.trim_data = function(data_list) {
if (this.selection_min==null) return data_list; // no selection => no filtering
var out_data=[];
for (var i=0; i<data_list.length; i++) {
if (data_list[i]==null) continue; // protect
//data_list[i][0]+=3550000*5;
var nr=data_list[i][0]; //date in unix time
if ((nr>=this.selection_min) && (nr<=this.selection_max)) {
out_data.push(data_list[i]);
}
}
return out_data;
};
// Given an array of flot lines, limit to the selection
rrdFlotSelection.prototype.trim_flot_timezone_data = function(flot_data,shift) {
var out_data=[];
for (var i=0; i<flot_data.length; i++) {
var data_el=flot_data[i];
out_data.push({label : data_el.label, data:this.trim_timezone_data(data_el.data,shift), color:data_el.color, lines:data_el.lines, yaxis:data_el.yaxis});
}
return out_data;
};
// Limit to selection the flot series data element
rrdFlotSelection.prototype.trim_timezone_data = function(data_list,shift) {
if (this.selection_min==null) return data_list; // no selection => no filtering
var out_data=[];
for (var i=0; i<data_list.length; i++) {
if (data_list[i]==null) continue; // protect
var nr=data_list[i][0]+shift;
if ((nr>=this.selection_min) && (nr<=this.selection_max)) {
out_data.push(data_list[i]);
}
}
return out_data;
};
// ======================================
// Miscelaneous helper functions
// ======================================
function rfs_format_time(s) {
if (s<120) {
return s+"s";
} else {
var s60=s%60;
var m=(s-s60)/60;
if ((m<10) && (s60>9)) {
return m+":"+s60+"min";
} if (m<120) {
return m+"min";
} else {
var m60=m%60;
var h=(m-m60)/60;
if ((h<12) && (m60>9)) {
return h+":"+m60+"h";
} if (h<48) {
return h+"h";
} else {
var h24=h%24;
var d=(h-h24)/24;
if ((d<7) && (h24>0)) {
return d+" days "+h24+"h";
} if (d<60) {
return d+" days";
} else {
var d30=d%30;
var mt=(d-d30)/30;
return mt+" months";
}
}
}
}
}
|