-
Notifications
You must be signed in to change notification settings - Fork 76
/
dashboard.js
10133 lines (8586 loc) · 371 KB
/
dashboard.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
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
// SPDX-License-Identifier: GPL-3.0-or-later
// DO NOT EDIT: This file is automatically generated from the source files in src/
// ----------------------------------------------------------------------------
// You can set the following variables before loading this script:
// 'use strict';
/*global netdataNoDygraphs *//* boolean, disable dygraph charts
* (default: false) */
/*global netdataNoSparklines *//* boolean, disable sparkline charts
* (default: false) */
/*global netdataNoPeitys *//* boolean, disable peity charts
* (default: false) */
/*global netdataNoGoogleCharts *//* boolean, disable google charts
* (default: false) */
/*global netdataNoMorris *//* boolean, disable morris charts
* (default: false) */
/*global netdataNoEasyPieChart *//* boolean, disable easypiechart charts
* (default: false) */
/*global netdataNoGauge *//* boolean, disable gauge.js charts
* (default: false) */
/*global netdataNoD3 *//* boolean, disable d3 charts
* (default: false) */
/*global netdataNoC3 *//* boolean, disable c3 charts
* (default: false) */
/*global netdataNoD3pie *//* boolean, disable d3pie charts
* (default: false) */
/*global netdataNoBootstrap *//* boolean, disable bootstrap - disables help too
* (default: false) */
/*global netdataNoFontAwesome *//* boolean, disable fontawesome (do not load it)
* (default: false) */
/*global netdataIcons *//* object, overwrite netdata fontawesome icons
* (default: null) */
/*global netdataDontStart *//* boolean, do not start the thread to process the charts
* (default: false) */
/*global netdataErrorCallback *//* function, callback to be called when the dashboard encounters an error
* (default: null) */
/*global netdataRegistry:true *//* boolean, use the netdata registry
* (default: false) */
/*global netdataNoRegistry *//* boolean, included only for compatibility with existing custom dashboard
* (obsolete - do not use this any more) */
/*global netdataRegistryCallback *//* function, callback that will be invoked with one param: the URLs from the registry
* (default: null) */
/*global netdataShowHelp:true *//* boolean, disable charts help
* (default: true) */
/*global netdataShowAlarms:true *//* boolean, enable alarms checks and notifications
* (default: false) */
/*global netdataRegistryAfterMs:true *//* ms, delay registry use at started
* (default: 1500) */
/*global netdataCallback *//* function, callback to be called when netdata is ready to start
* (default: null)
* netdata will be running while this is called
* (call NETDATA.pause to stop it) */
/*global netdataPrepCallback *//* function, callback to be called before netdata does anything else
* (default: null) */
/*global netdataServer *//* string, the URL of the netdata server to use
* (default: the URL the page is hosted at) */
/*global netdataServerStatic *//* string, the URL of the netdata server to use for static files
* (default: netdataServer) */
/*global netdataSnapshotData *//* object, a netdata snapshot loaded
* (default: null) */
/*global netdataAlarmsRecipients *//* array, an array of alarm recipients to show notifications for
* (default: null) */
/*global netdataAlarmsRemember *//* boolen, keep our position in the alarm log at browser local storage
* (default: true) */
/*global netdataAlarmsActiveCallback *//* function, a hook for the alarm logs
* (default: undefined) */
/*global netdataAlarmsNotifCallback *//* function, a hook for alarm notifications
* (default: undefined) */
/*global netdataIntersectionObserver *//* boolean, enable or disable the use of intersection observer
* (default: true) */
/*global netdataCheckXSS *//* boolean, enable or disable checking for XSS issues
* (default: false) */
// ----------------------------------------------------------------------------
// global namespace
// Should stay var!
var NETDATA = window.NETDATA || {};
(function(window, document, $, undefined) {
// *** src/dashboard.js/utils.js
NETDATA.name2id = function (s) {
return s
.replace(/ /g, '_')
.replace(/:/g, '_')
.replace(/\(/g, '_')
.replace(/\)/g, '_')
.replace(/\./g, '_')
.replace(/\//g, '_');
};
NETDATA.encodeURIComponent = function (s) {
if (typeof(s) === 'string') {
return encodeURIComponent(s);
}
return s;
};
/// A heuristic for detecting slow devices.
let isSlowDeviceResult = undefined;
const isSlowDevice = function () {
if (!isSlowDeviceResult) {
return isSlowDeviceResult;
}
try {
let ua = navigator.userAgent.toLowerCase();
let iOS = /ipad|iphone|ipod/.test(ua) && !window.MSStream;
let android = /android/.test(ua) && !window.MSStream;
isSlowDeviceResult = (iOS || android);
} catch (e) {
isSlowDeviceResult = false;
}
return isSlowDeviceResult;
};
NETDATA.guid = function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
};
NETDATA.zeropad = function (x) {
if (x > -10 && x < 10) {
return '0' + x.toString();
} else {
return x.toString();
}
};
NETDATA.seconds4human = function (seconds, options) {
let defaultOptions = {
now: '现在',
space: ' ',
negative_suffix: '前',
day: '日',
days: '日',
hour: '小时',
hours: '小时',
minute: '分钟',
minutes: '分钟',
second: '秒',
seconds: '秒',
and: '及'
};
if (typeof options !== 'object') {
options = defaultOptions;
} else {
for (var x in defaultOptions) {
if (typeof options[x] !== 'string') {
options[x] = defaultOptions[x];
}
}
}
if (typeof seconds === 'string') {
seconds = parseInt(seconds, 10);
}
if (seconds === 0) {
return options.now;
}
let suffix = '';
if (seconds < 0) {
seconds = -seconds;
if (options.negative_suffix !== '') {
suffix = options.space + options.negative_suffix;
}
}
let days = Math.floor(seconds / 86400);
seconds -= (days * 86400);
let hours = Math.floor(seconds / 3600);
seconds -= (hours * 3600);
let minutes = Math.floor(seconds / 60);
seconds -= (minutes * 60);
let strings = [];
if (days > 1) {
strings.push(days.toString() + options.space + options.days);
} else if (days === 1) {
strings.push(days.toString() + options.space + options.day);
}
if (hours > 1) {
strings.push(hours.toString() + options.space + options.hours);
} else if (hours === 1) {
strings.push(hours.toString() + options.space + options.hour);
}
if (minutes > 1) {
strings.push(minutes.toString() + options.space + options.minutes);
} else if (minutes === 1) {
strings.push(minutes.toString() + options.space + options.minute);
}
if (seconds > 1) {
strings.push(Math.floor(seconds).toString() + options.space + options.seconds);
} else if (seconds === 1) {
strings.push(Math.floor(seconds).toString() + options.space + options.second);
}
if (strings.length === 1) {
return strings.pop() + suffix;
}
let last = strings.pop();
return strings.join(", ") + " " + options.and + " " + last + suffix;
};
// ----------------------------------------------------------------------------------------------------------------
// element data attributes
NETDATA.dataAttribute = function (element, attribute, def) {
let key = 'data-' + attribute.toString();
if (element.hasAttribute(key)) {
let data = element.getAttribute(key);
if (data === 'true') {
return true;
}
if (data === 'false') {
return false;
}
if (data === 'null') {
return null;
}
// Only convert to a number if it doesn't change the string
if (data === +data + '') {
return +data;
}
if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(data)) {
return JSON.parse(data);
}
return data;
} else {
return def;
}
};
NETDATA.dataAttributeBoolean = function (element, attribute, def) {
let value = NETDATA.dataAttribute(element, attribute, def);
if (value === true || value === false) // gmosx: Love this :)
{
return value;
}
if (typeof(value) === 'string') {
if (value === 'yes' || value === 'on') {
return true;
}
if (value === '' || value === 'no' || value === 'off' || value === 'null') {
return false;
}
return def;
}
if (typeof(value) === 'number') {
return value !== 0;
}
return def;
};
// ----------------------------------------------------------------------------------------------------------------
// fast numbers formatting
NETDATA.fastNumberFormat = {
formattersFixed: [],
formattersZeroBased: [],
// this is the fastest and the preferred
getIntlNumberFormat: function (min, max) {
let key = max;
if (min === max) {
if (typeof this.formattersFixed[key] === 'undefined') {
this.formattersFixed[key] = new Intl.NumberFormat(undefined, {
// style: 'decimal',
// minimumIntegerDigits: 1,
// minimumSignificantDigits: 1,
// maximumSignificantDigits: 1,
useGrouping: true,
minimumFractionDigits: min,
maximumFractionDigits: max
});
}
return this.formattersFixed[key];
} else if (min === 0) {
if (typeof this.formattersZeroBased[key] === 'undefined') {
this.formattersZeroBased[key] = new Intl.NumberFormat(undefined, {
// style: 'decimal',
// minimumIntegerDigits: 1,
// minimumSignificantDigits: 1,
// maximumSignificantDigits: 1,
useGrouping: true,
minimumFractionDigits: min,
maximumFractionDigits: max
});
}
return this.formattersZeroBased[key];
} else {
// this is never used
// it is added just for completeness
return new Intl.NumberFormat(undefined, {
// style: 'decimal',
// minimumIntegerDigits: 1,
// minimumSignificantDigits: 1,
// maximumSignificantDigits: 1,
useGrouping: true,
minimumFractionDigits: min,
maximumFractionDigits: max
});
}
},
// this respects locale
getLocaleString: function (min, max) {
let key = max;
if (min === max) {
if (typeof this.formattersFixed[key] === 'undefined') {
this.formattersFixed[key] = {
format: function (value) {
return value.toLocaleString(undefined, {
// style: 'decimal',
// minimumIntegerDigits: 1,
// minimumSignificantDigits: 1,
// maximumSignificantDigits: 1,
useGrouping: true,
minimumFractionDigits: min,
maximumFractionDigits: max
});
}
};
}
return this.formattersFixed[key];
} else if (min === 0) {
if (typeof this.formattersZeroBased[key] === 'undefined') {
this.formattersZeroBased[key] = {
format: function (value) {
return value.toLocaleString(undefined, {
// style: 'decimal',
// minimumIntegerDigits: 1,
// minimumSignificantDigits: 1,
// maximumSignificantDigits: 1,
useGrouping: true,
minimumFractionDigits: min,
maximumFractionDigits: max
});
}
};
}
return this.formattersZeroBased[key];
} else {
return {
format: function (value) {
return value.toLocaleString(undefined, {
// style: 'decimal',
// minimumIntegerDigits: 1,
// minimumSignificantDigits: 1,
// maximumSignificantDigits: 1,
useGrouping: true,
minimumFractionDigits: min,
maximumFractionDigits: max
});
}
};
}
},
// the fallback
getFixed: function (min, max) {
let key = max;
if (min === max) {
if (typeof this.formattersFixed[key] === 'undefined') {
this.formattersFixed[key] = {
format: function (value) {
if (value === 0) {
return "0";
}
return value.toFixed(max);
}
};
}
return this.formattersFixed[key];
} else if (min === 0) {
if (typeof this.formattersZeroBased[key] === 'undefined') {
this.formattersZeroBased[key] = {
format: function (value) {
if (value === 0) {
return "0";
}
return value.toFixed(max);
}
};
}
return this.formattersZeroBased[key];
} else {
return {
format: function (value) {
if (value === 0) {
return "0";
}
return value.toFixed(max);
}
};
}
},
testIntlNumberFormat: function () {
let value = 1.12345;
let e1 = "1.12", e2 = "1,12";
let s = "";
try {
let x = new Intl.NumberFormat(undefined, {
useGrouping: true,
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
s = x.format(value);
} catch (e) {
s = "";
}
// console.log('NumberFormat: ', s);
return (s === e1 || s === e2);
},
testLocaleString: function () {
let value = 1.12345;
let e1 = "1.12", e2 = "1,12";
let s = "";
try {
s = value.toLocaleString(undefined, {
useGrouping: true,
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
} catch (e) {
s = "";
}
// console.log('localeString: ', s);
return (s === e1 || s === e2);
},
// on first run we decide which formatter to use
get: function (min, max) {
if (this.testIntlNumberFormat()) {
// console.log('numberformat');
this.get = this.getIntlNumberFormat;
} else if (this.testLocaleString()) {
// console.log('localestring');
this.get = this.getLocaleString;
} else {
// console.log('fixed');
this.get = this.getFixed;
}
return this.get(min, max);
}
};
// ----------------------------------------------------------------------------------------------------------------
// Detect the netdata server
// http://stackoverflow.com/questions/984510/what-is-my-script-src-url
// http://stackoverflow.com/questions/6941533/get-protocol-domain-and-port-from-url
NETDATA._scriptSource = function () {
let script = null;
if (typeof document.currentScript !== 'undefined') {
script = document.currentScript;
} else {
const all_scripts = document.getElementsByTagName('script');
script = all_scripts[all_scripts.length - 1];
}
if (typeof script.getAttribute.length !== 'undefined') {
script = script.src;
} else {
script = script.getAttribute('src', -1);
}
return script;
};
// *** src/dashboard.js/server-detection.js
if (typeof netdataServer !== 'undefined') {
NETDATA.serverDefault = netdataServer;
} else {
let s = NETDATA._scriptSource();
if (s) {
NETDATA.serverDefault = s.replace(/\/dashboard.js(\?.*)?$/g, "");
} else {
console.log('WARNING: Cannot detect the URL of the netdata server.');
NETDATA.serverDefault = null;
}
}
if (NETDATA.serverDefault === null) {
NETDATA.serverDefault = '';
} else if (NETDATA.serverDefault.slice(-1) !== '/') {
NETDATA.serverDefault += '/';
}
if (typeof netdataServerStatic !== 'undefined' && netdataServerStatic !== null && netdataServerStatic !== '') {
NETDATA.serverStatic = netdataServerStatic;
if (NETDATA.serverStatic.slice(-1) !== '/') {
NETDATA.serverStatic += '/';
}
} else {
NETDATA.serverStatic = NETDATA.serverDefault;
}
// *** src/dashboard.js/dependencies.js
// default URLs for all the external files we need
// make them RELATIVE so that the whole thing can also be
// installed under a web server
NETDATA.jQuery = NETDATA.serverStatic + 'lib/jquery-2.2.4.min.js';
NETDATA.peity_js = NETDATA.serverStatic + 'lib/jquery.peity-3.2.0.min.js';
NETDATA.sparkline_js = NETDATA.serverStatic + 'lib/jquery.sparkline-2.1.2.min.js';
NETDATA.easypiechart_js = NETDATA.serverStatic + 'lib/jquery.easypiechart-97b5824.min.js';
NETDATA.gauge_js = NETDATA.serverStatic + 'lib/gauge-1.3.2.min.js';
NETDATA.dygraph_js = NETDATA.serverStatic + 'lib/dygraph-c91c859.min.js';
NETDATA.dygraph_smooth_js = NETDATA.serverStatic + 'lib/dygraph-smooth-plotter-c91c859.js';
// NETDATA.raphael_js = NETDATA.serverStatic + 'lib/raphael-2.2.4-min.js';
// NETDATA.c3_js = NETDATA.serverStatic + 'lib/c3-0.4.18.min.js';
// NETDATA.c3_css = NETDATA.serverStatic + 'css/c3-0.4.18.min.css';
NETDATA.d3pie_js = NETDATA.serverStatic + 'lib/d3pie-0.2.1-netdata-3.js';
NETDATA.d3_js = NETDATA.serverStatic + 'lib/d3-4.12.2.min.js';
// NETDATA.morris_js = NETDATA.serverStatic + 'lib/morris-0.5.1.min.js';
// NETDATA.morris_css = NETDATA.serverStatic + 'css/morris-0.5.1.css';
NETDATA.google_js = 'https://www.google.com/jsapi';
// Error Handling
NETDATA.errorCodes = {
100: {message: "Cannot load chart library", alert: true},
101: {message: "Cannot load jQuery", alert: true},
402: {message: "Chart library not found", alert: false},
403: {message: "Chart library not enabled/is failed", alert: false},
404: {message: "Chart not found", alert: false},
405: {message: "Cannot download charts index from server", alert: true},
406: {message: "Invalid charts index downloaded from server", alert: true},
407: {message: "Cannot HELLO netdata server", alert: false},
408: {message: "Netdata servers sent invalid response to HELLO", alert: false},
409: {message: "Cannot ACCESS netdata registry", alert: false},
410: {message: "Netdata registry ACCESS failed", alert: false},
411: {message: "Netdata registry server send invalid response to DELETE ", alert: false},
412: {message: "Netdata registry DELETE failed", alert: false},
413: {message: "Netdata registry server send invalid response to SWITCH ", alert: false},
414: {message: "Netdata registry SWITCH failed", alert: false},
415: {message: "Netdata alarms download failed", alert: false},
416: {message: "Netdata alarms log download failed", alert: false},
417: {message: "Netdata registry server send invalid response to SEARCH ", alert: false},
418: {message: "Netdata registry SEARCH failed", alert: false}
};
NETDATA.errorLast = {
code: 0,
message: "",
datetime: 0
};
NETDATA.error = function (code, msg) {
NETDATA.errorLast.code = code;
NETDATA.errorLast.message = msg;
NETDATA.errorLast.datetime = Date.now();
console.log("ERROR " + code + ": " + NETDATA.errorCodes[code].message + ": " + msg);
let ret = true;
if (typeof netdataErrorCallback === 'function') {
ret = netdataErrorCallback('system', code, msg);
}
if (ret && NETDATA.errorCodes[code].alert) {
alert("ERROR " + code + ": " + NETDATA.errorCodes[code].message + ": " + msg);
}
};
NETDATA.errorReset = function () {
NETDATA.errorLast.code = 0;
NETDATA.errorLast.message = "You are doing fine!";
NETDATA.errorLast.datetime = 0;
};
// *** src/dashboard.js/compatibility.js
// Compatibility fixes.
// fix IE issue with console
if (!window.console) {
window.console = {
log: function () {
}
};
}
// if string.endsWith is not defined, define it
if (typeof String.prototype.endsWith !== 'function') {
String.prototype.endsWith = function (s) {
if (s.length > this.length) {
return false;
}
return this.slice(-s.length) === s;
};
}
// if string.startsWith is not defined, define it
if (typeof String.prototype.startsWith !== 'function') {
String.prototype.startsWith = function (s) {
if (s.length > this.length) {
return false;
}
return this.slice(s.length) === s;
};
}
// ----------------------------------------------------------------------------------------------------------------
// XSS checks
NETDATA.xss = {
enabled: (typeof netdataCheckXSS === 'undefined') ? false : netdataCheckXSS,
enabled_for_data: (typeof netdataCheckXSS === 'undefined') ? false : netdataCheckXSS,
string: function (s) {
return s.toString()
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
},
object: function (name, obj, ignore_regex) {
if (typeof ignore_regex !== 'undefined' && ignore_regex.test(name)) {
// console.log('XSS: ignoring "' + name + '"');
return obj;
}
switch (typeof(obj)) {
case 'string':
const ret = this.string(obj);
if (ret !== obj) {
console.log('XSS protection changed string ' + name + ' from "' + obj + '" to "' + ret + '"');
}
return ret;
case 'object':
if (obj === null) {
return obj;
}
if (Array.isArray(obj)) {
// console.log('checking array "' + name + '"');
let len = obj.length;
while (len--) {
obj[len] = this.object(name + '[' + len + ']', obj[len], ignore_regex);
}
} else {
// console.log('checking object "' + name + '"');
for (var i in obj) {
if (obj.hasOwnProperty(i) === false) {
continue;
}
if (this.string(i) !== i) {
console.log('XSS protection removed invalid object member "' + name + '.' + i + '"');
delete obj[i];
} else {
obj[i] = this.object(name + '.' + i, obj[i], ignore_regex);
}
}
}
return obj;
default:
return obj;
}
},
checkOptional: function (name, obj, ignore_regex) {
if (this.enabled) {
//console.log('XSS: checking optional "' + name + '"...');
return this.object(name, obj, ignore_regex);
}
return obj;
},
checkAlways: function (name, obj, ignore_regex) {
//console.log('XSS: checking always "' + name + '"...');
return this.object(name, obj, ignore_regex);
},
checkData: function (name, obj, ignore_regex) {
if (this.enabled_for_data) {
//console.log('XSS: checking data "' + name + '"...');
return this.object(name, obj, ignore_regex);
}
return obj;
}
};
NETDATA.colorHex2Rgb = function (hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
let shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function (m, r, g, b) {
return r + r + g + g + b + b;
});
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
};
NETDATA.colorLuminance = function (hex, lum) {
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
lum = lum || 0;
// convert to decimal and change luminosity
let rgb = "#";
for (let i = 0; i < 3; i++) {
let c = parseInt(hex.substr(i * 2, 2), 16);
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
rgb += ("00" + c).substr(c.length);
}
return rgb;
};
NETDATA.unitsConversion = {
keys: {}, // keys for data-common-units
latest: {}, // latest selected units for data-common-units
globalReset: function () {
this.keys = {};
this.latest = {};
},
scalableUnits: {
'packets/s': {
'pps': 1,
'Kpps': 1000,
'Mpps': 1000000
},
'pps': {
'pps': 1,
'Kpps': 1000,
'Mpps': 1000000
},
'kilobits/s': {
'bits/s': 1 / 1000,
'kilobits/s': 1,
'megabits/s': 1000,
'gigabits/s': 1000000,
'terabits/s': 1000000000
},
'kilobytes/s': {
'bytes/s': 1 / 1024,
'kilobytes/s': 1,
'megabytes/s': 1024,
'gigabytes/s': 1024 * 1024,
'terabytes/s': 1024 * 1024 * 1024
},
'KB/s': {
'B/s': 1 / 1024,
'KB/s': 1,
'MB/s': 1024,
'GB/s': 1024 * 1024,
'TB/s': 1024 * 1024 * 1024
},
'KiB/s': {
'B/s': 1 / 1024,
'KiB/s': 1,
'MiB/s': 1024,
'GiB/s': 1024 * 1024,
'TiB/s': 1024 * 1024 * 1024
},
'B': {
'B': 1,
'KiB': 1024,
'MiB': 1024 * 1024,
'GiB': 1024 * 1024 * 1024,
'TiB': 1024 * 1024 * 1024 * 1024,
'PiB': 1024 * 1024 * 1024 * 1024 * 1024
},
'KB': {
'B': 1 / 1024,
'KB': 1,
'MB': 1024,
'GB': 1024 * 1024,
'TB': 1024 * 1024 * 1024
},
'KiB': {
'B': 1 / 1024,
'KiB': 1,
'MiB': 1024,
'GiB': 1024 * 1024,
'TiB': 1024 * 1024 * 1024
},
'MB': {
'B': 1 / (1024 * 1024),
'KB': 1 / 1024,
'MB': 1,
'GB': 1024,
'TB': 1024 * 1024,
'PB': 1024 * 1024 * 1024
},
'MiB': {
'B': 1 / (1024 * 1024),
'KiB': 1 / 1024,
'MiB': 1,
'GiB': 1024,
'TiB': 1024 * 1024,
'PiB': 1024 * 1024 * 1024
},
'GB': {
'B': 1 / (1024 * 1024 * 1024),
'KB': 1 / (1024 * 1024),
'MB': 1 / 1024,
'GB': 1,
'TB': 1024,
'PB': 1024 * 1024,
'EB': 1024 * 1024 * 1024
},
'GiB': {
'B': 1 / (1024 * 1024 * 1024),
'KiB': 1 / (1024 * 1024),
'MiB': 1 / 1024,
'GiB': 1,
'TiB': 1024,
'PiB': 1024 * 1024,
'EiB': 1024 * 1024 * 1024
}
/*
'milliseconds': {
'seconds': 1000
},
'seconds': {
'milliseconds': 0.001,
'seconds': 1,
'minutes': 60,
'hours': 3600,
'days': 86400
}
*/
},
convertibleUnits: {
'Celsius': {
'Fahrenheit': {
check: function (max) {
void(max);
return NETDATA.options.current.temperature === 'fahrenheit';
},
convert: function (value) {
return value * 9 / 5 + 32;
}
}
},
'celsius': {
'fahrenheit': {
check: function (max) {
void(max);
return NETDATA.options.current.temperature === 'fahrenheit';
},
convert: function (value) {
return value * 9 / 5 + 32;
}
}
},
'seconds': {
'time': {
check: function (max) {
void(max);
return NETDATA.options.current.seconds_as_time;
},
convert: function (seconds) {
return NETDATA.unitsConversion.seconds2time(seconds);
}
}
},
'milliseconds': {
'milliseconds': {
check: function (max) {
return NETDATA.options.current.seconds_as_time && max < 1000;
},
convert: function (milliseconds) {
let tms = Math.round(milliseconds * 10);
milliseconds = Math.floor(tms / 10);
tms -= milliseconds * 10;
return (milliseconds).toString() + '.' + tms.toString();
}
},
'seconds': {
check: function (max) {
return NETDATA.options.current.seconds_as_time && max >= 1000 && max < 60000;
},
convert: function (milliseconds) {
milliseconds = Math.round(milliseconds);
let seconds = Math.floor(milliseconds / 1000);
milliseconds -= seconds * 1000;
milliseconds = Math.round(milliseconds / 10);
return seconds.toString() + '.'
+ NETDATA.zeropad(milliseconds);
}
},
'M:SS.ms': {
check: function (max) {
return NETDATA.options.current.seconds_as_time && max >= 60000;
},
convert: function (milliseconds) {
milliseconds = Math.round(milliseconds);
let minutes = Math.floor(milliseconds / 60000);
milliseconds -= minutes * 60000;
let seconds = Math.floor(milliseconds / 1000);
milliseconds -= seconds * 1000;
milliseconds = Math.round(milliseconds / 10);
return minutes.toString() + ':'
+ NETDATA.zeropad(seconds) + '.'
+ NETDATA.zeropad(milliseconds);
}
}
}
},
seconds2time: function (seconds) {
seconds = Math.abs(seconds);
let days = Math.floor(seconds / 86400);
seconds -= days * 86400;
let hours = Math.floor(seconds / 3600);
seconds -= hours * 3600;
let minutes = Math.floor(seconds / 60);
seconds -= minutes * 60;
seconds = Math.round(seconds);
let ms_txt = '';
/*
let ms = seconds - Math.floor(seconds);
seconds -= ms;
ms = Math.round(ms * 1000);
if (ms > 1) {
if (ms < 10)
ms_txt = '.00' + ms.toString();
else if (ms < 100)
ms_txt = '.0' + ms.toString();
else
ms_txt = '.' + ms.toString();
}