-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascriptrrd.wlibs.js
More file actions
3612 lines (3163 loc) · 253 KB
/
javascriptrrd.wlibs.js
File metadata and controls
3612 lines (3163 loc) · 253 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BinaryFile over XMLHttpRequest
* Part of the javascriptRRD package
* Copyright (c) 2009 Frank Wuerthwein, fkw@ucsd.edu
* MIT License [http://www.opensource.org/licenses/mit-license.php]
*
* Original repository: http://javascriptrrd.sourceforge.net/
*
* Based on:
* Binary Ajax 0.1.5
* Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/
* MIT License [http://www.opensource.org/licenses/mit-license.php]
*/
// ============================================================
// Exception class
function InvalidBinaryFile(msg) {
this.message=msg;
this.name="Invalid BinaryFile";
}
// pretty print
InvalidBinaryFile.prototype.toString = function() {
return this.name + ': "' + this.message + '"';
}
// =====================================================================
// BinaryFile class
// Allows access to element inside a binary stream
function BinaryFile(strData, iDataOffset, iDataLength) {
var data = strData;
var dataOffset = iDataOffset || 0;
var dataLength = 0;
// added
var doubleMantExpHi=Math.pow(2,-28);
var doubleMantExpLo=Math.pow(2,-52);
var doubleMantExpFast=Math.pow(2,-20);
var switch_endian = false;
this.getRawData = function() {
return data;
}
if (typeof strData == "string") {
dataLength = iDataLength || data.length;
this.getByteAt = function(iOffset) {
return data.charCodeAt(iOffset + dataOffset) & 0xFF;
}
} else if (typeof strData == "unknown") {
dataLength = iDataLength || IEBinary_getLength(data);
this.getByteAt = function(iOffset) {
return IEBinary_getByteAt(data, iOffset + dataOffset);
}
} else {
throw new InvalidBinaryFile("Unsupported type " + (typeof strData));
}
this.getEndianByteAt = function(iOffset,width,delta) {
if (this.switch_endian)
return this.getByteAt(iOffset+width-delta-1);
else
return this.getByteAt(iOffset+delta);
}
this.getLength = function() {
return dataLength;
}
this.getSByteAt = function(iOffset) {
var iByte = this.getByteAt(iOffset);
if (iByte > 127)
return iByte - 256;
else
return iByte;
}
this.getShortAt = function(iOffset) {
var iShort = (this.getEndianByteAt(iOffset,2,1) << 8) + this.getEndianByteAt(iOffset,2,0)
if (iShort < 0) iShort += 65536;
return iShort;
}
this.getSShortAt = function(iOffset) {
var iUShort = this.getShortAt(iOffset);
if (iUShort > 32767)
return iUShort - 65536;
else
return iUShort;
}
this.getLongAt = function(iOffset) {
var iByte1 = this.getEndianByteAt(iOffset,4,0),
iByte2 = this.getEndianByteAt(iOffset,4,1),
iByte3 = this.getEndianByteAt(iOffset,4,2),
iByte4 = this.getEndianByteAt(iOffset,4,3);
var iLong = (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1;
if (iLong < 0) iLong += 4294967296;
return iLong;
}
this.getSLongAt = function(iOffset) {
var iULong = this.getLongAt(iOffset);
if (iULong > 2147483647)
return iULong - 4294967296;
else
return iULong;
}
this.getStringAt = function(iOffset, iLength) {
var aStr = [];
for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) {
aStr[j] = String.fromCharCode(this.getByteAt(i));
}
return aStr.join("");
}
// Added
this.getCStringAt = function(iOffset, iMaxLength) {
var aStr = [];
for (var i=iOffset,j=0;(i<iOffset+iMaxLength) && (this.getByteAt(i)>0);i++,j++) {
aStr[j] = String.fromCharCode(this.getByteAt(i));
}
return aStr.join("");
}
// Added
this.getDoubleAt = function(iOffset) {
var iByte1 = this.getEndianByteAt(iOffset,8,0),
iByte2 = this.getEndianByteAt(iOffset,8,1),
iByte3 = this.getEndianByteAt(iOffset,8,2),
iByte4 = this.getEndianByteAt(iOffset,8,3),
iByte5 = this.getEndianByteAt(iOffset,8,4),
iByte6 = this.getEndianByteAt(iOffset,8,5),
iByte7 = this.getEndianByteAt(iOffset,8,6),
iByte8 = this.getEndianByteAt(iOffset,8,7);
var iSign=iByte8 >> 7;
var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4);
var iMantHi=((((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5) << 8) + iByte4;
var iMantLo=((((iByte3) << 8) + iByte2) << 8) + iByte1;
if (iExpRaw==0) return 0.0;
if (iExpRaw==0x7ff) return undefined;
var iExp=(iExpRaw & 0x7FF)-1023;
var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMantLo*doubleMantExpLo + iMantHi*doubleMantExpHi);
return dDouble;
}
// added
// Extracts only 4 bytes out of 8, loosing in precision (20 bit mantissa)
this.getFastDoubleAt = function(iOffset) {
var iByte5 = this.getEndianByteAt(iOffset,8,4),
iByte6 = this.getEndianByteAt(iOffset,8,5),
iByte7 = this.getEndianByteAt(iOffset,8,6),
iByte8 = this.getEndianByteAt(iOffset,8,7);
var iSign=iByte8 >> 7;
var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4);
var iMant=((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5;
if (iExpRaw==0) return 0.0;
if (iExpRaw==0x7ff) return undefined;
var iExp=(iExpRaw & 0x7FF)-1023;
var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMant*doubleMantExpFast);
return dDouble;
}
this.getCharAt = function(iOffset) {
return String.fromCharCode(this.getByteAt(iOffset));
}
}
document.write(
"<script type='text/vbscript'>\r\n"
+ "Function IEBinary_getByteAt(strBinary, iOffset)\r\n"
+ " IEBinary_getByteAt = AscB(MidB(strBinary,iOffset+1,1))\r\n"
+ "End Function\r\n"
+ "Function IEBinary_getLength(strBinary)\r\n"
+ " IEBinary_getLength = LenB(strBinary)\r\n"
+ "End Function\r\n"
+ "</script>\r\n"
);
// ===============================================================
// Load a binary file from the specified URL
// Will return an object of type BinaryFile
function FetchBinaryURL(url) {
var request = new XMLHttpRequest();
request.open("GET", url,false);
try {
request.overrideMimeType('text/plain; charset=x-user-defined');
} catch (err) {
// ignore any error, just to make both FF and IE work
}
request.send(null);
var response=this.responseText;
try {
// for older IE versions, the value in responseText is not usable
if (IEBinary_getLength(this.responseBody)>0) {
// will get here only for older verson of IE
response=this.responseBody;
}
} catch (err) {
// not IE, do nothing
}
var bf=new BinaryFile(response);
return bf;
}
// ===============================================================
// Asyncronously load a binary file from the specified URL
//
// callback must be a function with one or two arguments:
// - bf = an object of type BinaryFile
// - optional argument object (used only if callback_arg not undefined)
function FetchBinaryURLAsync(url, callback, callback_arg) {
var callback_wrapper = function() {
if(this.readyState == 4) {
var response=this.responseText;
try {
// for older IE versions, the value in responseText is not usable
if (IEBinary_getLength(this.responseBody)>0) {
// will get here only for older verson of IE
response=this.responseBody;
}
} catch (err) {
// not IE, do nothing
}
var bf=new BinaryFile(response);
if (callback_arg!=null) {
callback(bf,callback_arg);
} else {
callback(bf);
}
}
}
var request = new XMLHttpRequest();
request.onreadystatechange = callback_wrapper;
request.open("GET", url,true);
try {
request.overrideMimeType('text/plain; charset=x-user-defined');
} catch (err) {
// ignore any error, just to make both FF and IE work
}
request.send(null);
return request
}
/*
* Client library for access to RRD archive files
* Part of the javascriptRRD package
* Copyright (c) 2009-2010 Frank Wuerthwein, fkw@ucsd.edu
* Igor Sfiligoi, isfiligoi@ucsd.edu
*
* Original repository: http://javascriptrrd.sourceforge.net/
*
* MIT License [http://www.opensource.org/licenses/mit-license.php]
*
*/
/*
*
* RRDTool has been developed and is maintained by
* Tobias Oether [http://oss.oetiker.ch/rrdtool/]
*
* This software can be used to read files produced by the RRDTool
* but has been developed independently.
*
* Limitations:
*
* This version of the module assumes RRD files created on linux
* with intel architecture and supports both 32 and 64 bit CPUs.
* All integers in RRD files are suppoes to fit in 32bit values.
*
* Only versions 3 and 4 of the RRD archive are supported.
*
* Only AVERAGE,MAXIMUM,MINIMUM and LAST consolidation functions are
* supported. For all others, the behaviour is at the moment undefined.
*
*/
/*
* Dependencies:
*
* The data provided to this module require an object of a class
* that implements the following methods:
* getByteAt(idx) - Return a 8 bit unsigned integer at offset idx
* getLongAt(idx) - Return a 32 bit unsigned integer at offset idx
* getDoubleAt(idx) - Return a double float at offset idx
* getFastDoubleAt(idx) - Similar to getDoubleAt but with less precision
* getCStringAt(idx,maxsize) - Return a string of at most maxsize characters
* that was 0-terminated in the source
*
* The BinaryFile from binaryXHR.js implements this interface.
*
*/
// ============================================================
// Exception class
function InvalidRRD(msg) {
this.message=msg;
this.name="Invalid RRD";
}
// pretty print
InvalidRRD.prototype.toString = function() {
return this.name + ': "' + this.message + '"';
}
// ============================================================
// RRD DS Info class
function RRDDS(rrd_data,rrd_data_idx,my_idx) {
this.rrd_data=rrd_data;
this.rrd_data_idx=rrd_data_idx;
this.my_idx=my_idx;
}
RRDDS.prototype.getIdx = function() {
return this.my_idx;
}
RRDDS.prototype.getName = function() {
return this.rrd_data.getCStringAt(this.rrd_data_idx,20);
}
RRDDS.prototype.getType = function() {
return this.rrd_data.getCStringAt(this.rrd_data_idx+20,20);
}
RRDDS.prototype.getMin = function() {
return this.rrd_data.getDoubleAt(this.rrd_data_idx+48);
}
RRDDS.prototype.getMax = function() {
return this.rrd_data.getDoubleAt(this.rrd_data_idx+56);
}
// ============================================================
// RRD RRA Info class
function RRDRRAInfo(rrd_data,rra_def_idx,
int_align,row_cnt,pdp_step,my_idx) {
this.rrd_data=rrd_data;
this.rra_def_idx=rra_def_idx;
this.int_align=int_align;
this.row_cnt=row_cnt;
this.pdp_step=pdp_step;
this.my_idx=my_idx;
// char nam[20], uint row_cnt, uint pdp_cnt
this.rra_pdp_cnt_idx=rra_def_idx+Math.ceil(20/int_align)*int_align+int_align;
}
RRDRRAInfo.prototype.getIdx = function() {
return this.my_idx;
}
// Get number of rows
RRDRRAInfo.prototype.getNrRows = function() {
return this.row_cnt;
}
// Get number of slots used for consolidation
// Mostly for internal use
RRDRRAInfo.prototype.getPdpPerRow = function() {
return this.rrd_data.getLongAt(this.rra_pdp_cnt_idx);
}
// Get RRA step (expressed in seconds)
RRDRRAInfo.prototype.getStep = function() {
return this.pdp_step*this.getPdpPerRow();
}
// Get consolidation function name
RRDRRAInfo.prototype.getCFName = function() {
return this.rrd_data.getCStringAt(this.rra_def_idx,20);
}
// ============================================================
// RRD RRA handling class
function RRDRRA(rrd_data,rra_ptr_idx,
rra_info,
header_size,prev_row_cnts,ds_cnt) {
this.rrd_data=rrd_data;
this.rra_info=rra_info;
this.row_cnt=rra_info.row_cnt;
this.ds_cnt=ds_cnt;
var row_size=ds_cnt*8;
this.base_rrd_db_idx=header_size+prev_row_cnts*row_size;
// get imediately, since it will be needed often
this.cur_row=rrd_data.getLongAt(rra_ptr_idx);
// calculate idx relative to base_rrd_db_idx
// mostly used internally
this.calc_idx = function(row_idx,ds_idx) {
if ((row_idx>=0) && (row_idx<this.row_cnt)) {
if ((ds_idx>=0) && (ds_idx<ds_cnt)){
// it is round robin, starting from cur_row+1
var real_row_idx=row_idx+this.cur_row+1;
if (real_row_idx>=this.row_cnt) real_row_idx-=this.row_cnt;
return row_size*real_row_idx+ds_idx*8;
} else {
throw RangeError("DS idx ("+ row_idx +") out of range [0-" + ds_cnt +").");
}
} else {
throw RangeError("Row idx ("+ row_idx +") out of range [0-" + this.row_cnt +").");
}
}
}
RRDRRA.prototype.getIdx = function() {
return this.rra_info.getIdx();
}
// Get number of rows/columns
RRDRRA.prototype.getNrRows = function() {
return this.row_cnt;
}
RRDRRA.prototype.getNrDSs = function() {
return this.ds_cnt;
}
// Get RRA step (expressed in seconds)
RRDRRA.prototype.getStep = function() {
return this.rra_info.getStep();
}
// Get consolidation function name
RRDRRA.prototype.getCFName = function() {
return this.rra_info.getCFName();
}
RRDRRA.prototype.getEl = function(row_idx,ds_idx) {
return this.rrd_data.getDoubleAt(this.base_rrd_db_idx+this.calc_idx(row_idx,ds_idx));
}
// Low precision version of getEl
// Uses getFastDoubleAt
RRDRRA.prototype.getElFast = function(row_idx,ds_idx) {
return this.rrd_data.getFastDoubleAt(this.base_rrd_db_idx+this.calc_idx(row_idx,ds_idx));
}
// ============================================================
// RRD Header handling class
function RRDHeader(rrd_data) {
this.rrd_data=rrd_data;
this.validate_rrd();
this.calc_idxs();
}
// Internal, used for initialization
RRDHeader.prototype.validate_rrd = function() {
if (this.rrd_data.getLength()<1) throw new InvalidRRD("Empty file.");
if (this.rrd_data.getLength()<16) throw new InvalidRRD("File too short.");
if (this.rrd_data.getCStringAt(0,4)!=="RRD") throw new InvalidRRD("Wrong magic id.");
this.rrd_version=this.rrd_data.getCStringAt(4,5);
if ((this.rrd_version!=="0003")&&(this.rrd_version!=="0004")&&(this.rrd_version!=="0001")) {
throw new InvalidRRD("Unsupported RRD version "+this.rrd_version+".");
}
this.float_width=8;
if (this.rrd_data.getLongAt(12)==0) {
// not a double here... likely 64 bit
this.float_align=8;
if (! (this.rrd_data.getDoubleAt(16)==8.642135e+130)) {
// uhm... wrong endian?
this.rrd_data.switch_endian=true;
}
if (this.rrd_data.getDoubleAt(16)==8.642135e+130) {
// now, is it all 64bit or only float 64 bit?
if (this.rrd_data.getLongAt(28)==0) {
// true 64 bit align
this.int_align=8;
this.int_width=8;
} else {
// integers are 32bit aligned
this.int_align=4;
this.int_width=4;
}
} else {
throw new InvalidRRD("Magic float not found at 16.");
}
} else {
/// should be 32 bit alignment
if (! (this.rrd_data.getDoubleAt(12)==8.642135e+130)) {
// uhm... wrong endian?
this.rrd_data.switch_endian=true;
}
if (this.rrd_data.getDoubleAt(12)==8.642135e+130) {
this.float_align=4;
this.int_align=4;
this.int_width=4;
} else {
throw new InvalidRRD("Magic float not found at 12.");
}
}
this.unival_width=this.float_width;
this.unival_align=this.float_align;
// process the header here, since I need it for validation
// char magic[4], char version[5], double magic_float
// long ds_cnt, long rra_cnt, long pdp_step, unival par[10]
this.ds_cnt_idx=Math.ceil((4+5)/this.float_align)*this.float_align+this.float_width;
this.rra_cnt_idx=this.ds_cnt_idx+this.int_width;
this.pdp_step_idx=this.rra_cnt_idx+this.int_width;
//always get only the low 32 bits, the high 32 on 64 bit archs should always be 0
this.ds_cnt=this.rrd_data.getLongAt(this.ds_cnt_idx);
if (this.ds_cnt<1) {
throw new InvalidRRD("ds count less than 1.");
}
this.rra_cnt=this.rrd_data.getLongAt(this.rra_cnt_idx);
if (this.ds_cnt<1) {
throw new InvalidRRD("rra count less than 1.");
}
this.pdp_step=this.rrd_data.getLongAt(this.pdp_step_idx);
if (this.pdp_step<1) {
throw new InvalidRRD("pdp step less than 1.");
}
// best guess, assuming no weird align problems
this.top_header_size=Math.ceil((this.pdp_step_idx+this.int_width)/this.unival_align)*this.unival_align+10*this.unival_width;
var t=this.rrd_data.getLongAt(this.top_header_size);
if (t==0) {
throw new InvalidRRD("Could not find first DS name.");
}
}
// Internal, used for initialization
RRDHeader.prototype.calc_idxs = function() {
this.ds_def_idx=this.top_header_size;
// char ds_nam[20], char dst[20], unival par[10]
this.ds_el_size=Math.ceil((20+20)/this.unival_align)*this.unival_align+10*this.unival_width;
this.rra_def_idx=this.ds_def_idx+this.ds_el_size*this.ds_cnt;
// char cf_nam[20], uint row_cnt, uint pdp_cnt, unival par[10]
this.row_cnt_idx=Math.ceil(20/this.int_align)*this.int_align;
this.rra_def_el_size=Math.ceil((this.row_cnt_idx+2*this.int_width)/this.unival_align)*this.unival_align+10*this.unival_width;
this.live_head_idx=this.rra_def_idx+this.rra_def_el_size*this.rra_cnt;
// time_t last_up, int last_up_usec
this.live_head_size=2*this.int_width;
this.pdp_prep_idx=this.live_head_idx+this.live_head_size;
// char last_ds[30], unival scratch[10]
this.pdp_prep_el_size=Math.ceil(30/this.unival_align)*this.unival_align+10*this.unival_width;
this.cdp_prep_idx=this.pdp_prep_idx+this.pdp_prep_el_size*this.ds_cnt;
// unival scratch[10]
this.cdp_prep_el_size=10*this.unival_width;
this.rra_ptr_idx=this.cdp_prep_idx+this.cdp_prep_el_size*this.ds_cnt*this.rra_cnt;
// uint cur_row
this.rra_ptr_el_size=1*this.int_width;
this.header_size=this.rra_ptr_idx+this.rra_ptr_el_size*this.rra_cnt;
}
// Optional initialization
// Read and calculate row counts
RRDHeader.prototype.load_row_cnts = function() {
this.rra_def_row_cnts=[];
this.rra_def_row_cnt_sums=[]; // how many rows before me
for (var i=0; i<this.rra_cnt; i++) {
this.rra_def_row_cnts[i]=this.rrd_data.getLongAt(this.rra_def_idx+i*this.rra_def_el_size+this.row_cnt_idx,false);
if (i==0) {
this.rra_def_row_cnt_sums[i]=0;
} else {
this.rra_def_row_cnt_sums[i]=this.rra_def_row_cnt_sums[i-1]+this.rra_def_row_cnts[i-1];
}
}
}
// ---------------------------
// Start of user functions
RRDHeader.prototype.getMinStep = function() {
return this.pdp_step;
}
RRDHeader.prototype.getLastUpdate = function() {
return this.rrd_data.getLongAt(this.live_head_idx,false);
}
RRDHeader.prototype.getNrDSs = function() {
return this.ds_cnt;
}
RRDHeader.prototype.getDSNames = function() {
var ds_names=[]
for (var idx=0; idx<this.ds_cnt; idx++) {
var ds=this.getDSbyIdx(idx);
var ds_name=ds.getName()
ds_names.push(ds_name);
}
return ds_names;
}
RRDHeader.prototype.getDSbyIdx = function(idx) {
if ((idx>=0) && (idx<this.ds_cnt)) {
return new RRDDS(this.rrd_data,this.ds_def_idx+this.ds_el_size*idx,idx);
} else {
throw RangeError("DS idx ("+ idx +") out of range [0-" + this.ds_cnt +").");
}
}
RRDHeader.prototype.getDSbyName = function(name) {
for (var idx=0; idx<this.ds_cnt; idx++) {
var ds=this.getDSbyIdx(idx);
var ds_name=ds.getName()
if (ds_name==name)
return ds;
}
throw RangeError("DS name "+ name +" unknown.");
}
RRDHeader.prototype.getNrRRAs = function() {
return this.rra_cnt;
}
RRDHeader.prototype.getRRAInfo = function(idx) {
if ((idx>=0) && (idx<this.rra_cnt)) {
return new RRDRRAInfo(this.rrd_data,
this.rra_def_idx+idx*this.rra_def_el_size,
this.int_align,this.rra_def_row_cnts[idx],this.pdp_step,
idx);
} else {
throw RangeError("RRA idx ("+ idx +") out of range [0-" + this.rra_cnt +").");
}
}
// ============================================================
// RRDFile class
// Given a BinaryFile, gives access to the RRD archive fields
//
// Arguments:
// bf must be an object compatible with the BinaryFile interface
// file_options - currently no semantics... introduced for future expandability
function RRDFile(bf,file_options) {
this.file_options=file_options;
var rrd_data=bf
this.rrd_header=new RRDHeader(rrd_data);
this.rrd_header.load_row_cnts();
// ===================================
// Start of user functions
this.getMinStep = function() {
return this.rrd_header.getMinStep();
}
this.getLastUpdate = function() {
return this.rrd_header.getLastUpdate();
}
this.getNrDSs = function() {
return this.rrd_header.getNrDSs();
}
this.getDSNames = function() {
return this.rrd_header.getDSNames();
}
this.getDS = function(id) {
if (typeof id == "number") {
return this.rrd_header.getDSbyIdx(id);
} else {
return this.rrd_header.getDSbyName(id);
}
}
this.getNrRRAs = function() {
return this.rrd_header.getNrRRAs();
}
this.getRRAInfo = function(idx) {
return this.rrd_header.getRRAInfo(idx);
}
this.getRRA = function(idx) {
rra_info=this.rrd_header.getRRAInfo(idx);
return new RRDRRA(rrd_data,
this.rrd_header.rra_ptr_idx+idx*this.rrd_header.rra_ptr_el_size,
rra_info,
this.rrd_header.header_size,
this.rrd_header.rra_def_row_cnt_sums[idx],
this.rrd_header.ds_cnt);
}
}
/*
* 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://www.flotcharts.org/]
*
*/
// 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};
var ds_list_len = ds_list.length;
for (var ds_list_idx=0; ds_list_idx<ds_list_len; ++ds_list_idx) {
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,
timestamp_shift, 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+timestamp_shift)*1000.0, max:(last_update+timestamp_shift)*1000.0};
// first the stacks stack
var stack_els=[ds_positive_stack_list,ds_negative_stack_list];
var stack_els_len = stack_els.length;
for (var stack_list_id=0; stack_list_id<stack_els_len; ++stack_list_id) {
var stack_list=stack_els[stack_list_id];
var tmp_flot_els=[];
var tmp_ds_ids=[];
var tmp_nr_ids=stack_list.length;
var stack_list_len = stack_list.length;
for (var ds_list_idx=0; ds_list_idx<stack_list_len; ++ds_list_idx) {
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+timestamp_shift)*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
var ds_single_list_len = ds_single_list.length;
for (var ds_list_idx=0; ds_list_idx<ds_single_list_len; ++ds_list_idx) {
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+timestamp_shift)*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;
}
//==============================================================================================//
//This is a copy of rrdRRAStackFlotObj, but this function doesn't stacking the DSs
//edit timezone to +1
function rrdRRAMultiFlotObj(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);
}
//edit of timeshift
var timestamp_shift = 2*3600;
var out_el={data:[], min:(min_ts + timestamp_shift)*1000.0, max:(max_ts+timestamp_shift)*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 + timestamp_shift)*1000.0,rrd_vals[id]]);
}
}