forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.js
5747 lines (4814 loc) · 186 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
// You can set the following variables before loading this script:
//
// var netdataNoDygraphs = true; // do not use dygraph
// var netdataNoSparklines = true; // do not use sparkline
// var netdataNoPeitys = true; // do not use peity
// var netdataNoGoogleCharts = true; // do not use google
// var netdataNoMorris = true; // do not use morris
// var netdataNoEasyPieChart = true; // do not use easy pie chart
// var netdataNoGauge = true; // do not use gauge.js
// var netdataNoD3 = true; // do not use D3
// var netdataNoC3 = true; // do not use C3
// var netdataNoBootstrap = true; // do not load bootstrap
// var netdataDontStart = true; // do not start the thread to process the charts
// var netdataErrorCallback = null; // Callback function that will be invoked upon error
// var netdataNoRegistry = true; // Don't update the registry for this access
// var netdataRegistryCallback = null; // Callback function that will be invoked with one param,
// the URLs from the registry
//
// You can also set the default netdata server, using the following.
// When this variable is not set, we assume the page is hosted on your
// netdata server already.
// var netdataServer = "http://yourhost:19999"; // set your NetData server
//(function(window, document, undefined) {
// ------------------------------------------------------------------------
// 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;
};
}
// global namespace
var NETDATA = window.NETDATA || {};
// ----------------------------------------------------------------------------------------------------------------
// 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() {
var script = null;
if(typeof document.currentScript !== 'undefined') {
script = document.currentScript;
}
else {
var 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;
};
if(typeof netdataServer !== 'undefined')
NETDATA.serverDefault = netdataServer;
else {
var 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 += '/';
// 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.serverDefault + 'lib/jquery-1.12.0.min.js';
NETDATA.peity_js = NETDATA.serverDefault + 'lib/jquery.peity.min.js';
NETDATA.sparkline_js = NETDATA.serverDefault + 'lib/jquery.sparkline.min.js';
NETDATA.easypiechart_js = NETDATA.serverDefault + 'lib/jquery.easypiechart.min.js';
NETDATA.gauge_js = NETDATA.serverDefault + 'lib/gauge.min.js';
NETDATA.dygraph_js = NETDATA.serverDefault + 'lib/dygraph-combined.js';
NETDATA.dygraph_smooth_js = NETDATA.serverDefault + 'lib/dygraph-smooth-plotter.js';
NETDATA.raphael_js = NETDATA.serverDefault + 'lib/raphael-min.js';
NETDATA.morris_js = NETDATA.serverDefault + 'lib/morris.min.js';
NETDATA.d3_js = NETDATA.serverDefault + 'lib/d3.min.js';
NETDATA.c3_js = NETDATA.serverDefault + 'lib/c3.min.js';
NETDATA.c3_css = NETDATA.serverDefault + 'css/c3.min.css';
NETDATA.morris_css = NETDATA.serverDefault + 'css/morris.css';
NETDATA.google_js = 'https://www.google.com/jsapi';
NETDATA.themes = {
white: {
bootstrap_css: NETDATA.serverDefault + 'css/bootstrap.min.css',
dashboard_css: NETDATA.serverDefault + 'dashboard.css',
background: '#FFFFFF',
foreground: '#000000',
grid: '#DDDDDD',
axis: '#CCCCCC',
colors: [ '#3366CC', '#DC3912', '#109618', '#FF9900', '#990099', '#DD4477',
'#3B3EAC', '#66AA00', '#0099C6', '#B82E2E', '#AAAA11', '#5574A6',
'#994499', '#22AA99', '#6633CC', '#E67300', '#316395', '#8B0707',
'#329262', '#3B3EAC' ],
easypiechart_track: '#f0f0f0',
easypiechart_scale: '#dfe0e0',
gauge_pointer: '#C0C0C0',
gauge_stroke: '#F0F0F0',
gauge_gradient: false
},
slate: {
bootstrap_css: NETDATA.serverDefault + 'css/bootstrap.slate.min.css',
dashboard_css: NETDATA.serverDefault + 'dashboard.slate.css',
background: '#272b30',
foreground: '#C8C8C8',
grid: '#373b40',
axis: '#373b40',
/* colors: [ '#55bb33', '#ff2222', '#0099C6', '#faa11b', '#adbce0', '#DDDD00',
'#4178ba', '#f58122', '#a5cc39', '#f58667', '#f5ef89', '#cf93c0',
'#a5d18a', '#b8539d', '#3954a3', '#c8a9cf', '#c7de8a', '#fad20a',
'#a6a479', '#a66da8' ],
*/
colors: [ '#66AA00', '#FE3912', '#3366CC', '#D66300', '#0099C6', '#DDDD00',
'#5054e6', '#EE9911', '#BB44CC', '#e45757', '#ef0aef', '#CC7700',
'#22AA99', '#109618', '#905bfd', '#f54882', '#4381bf', '#ff3737',
'#329262', '#3B3EFF' ],
easypiechart_track: '#373b40',
easypiechart_scale: '#373b40',
gauge_pointer: '#474b50',
gauge_stroke: '#373b40',
gauge_gradient: false
}
};
if(typeof netdataTheme !== 'undefined' && typeof NETDATA.themes[netdataTheme] !== 'undefined')
NETDATA.themes.current = NETDATA.themes[netdataTheme];
else
NETDATA.themes.current = NETDATA.themes.white;
NETDATA.colors = NETDATA.themes.current.colors;
// these are the colors Google Charts are using
// we have them here to attempt emulate their look and feel on the other chart libraries
// http://there4.io/2012/05/02/google-chart-color-list/
//NETDATA.colors = [ '#3366CC', '#DC3912', '#FF9900', '#109618', '#990099', '#3B3EAC', '#0099C6',
// '#DD4477', '#66AA00', '#B82E2E', '#316395', '#994499', '#22AA99', '#AAAA11',
// '#6633CC', '#E67300', '#8B0707', '#329262', '#5574A6', '#3B3EAC' ];
// an alternative set
// http://www.mulinblog.com/a-color-palette-optimized-for-data-visualization/
// (blue) (red) (orange) (green) (pink) (brown) (purple) (yellow) (gray)
//NETDATA.colors = [ '#5DA5DA', '#F15854', '#FAA43A', '#60BD68', '#F17CB0', '#B2912F', '#B276B2', '#DECF3F', '#4D4D4D' ];
// ----------------------------------------------------------------------------------------------------------------
// the defaults for all charts
// if the user does not specify any of these, the following will be used
NETDATA.chartDefaults = {
host: NETDATA.serverDefault, // the server to get data from
width: '100%', // the chart width - can be null
height: '100%', // the chart height - can be null
min_width: null, // the chart minimum width - can be null
library: 'dygraph', // the graphing library to use
method: 'average', // the grouping method
before: 0, // panning
after: -600, // panning
pixels_per_point: 1, // the detail of the chart
fill_luminance: 0.8 // luminance of colors in solit areas
};
// ----------------------------------------------------------------------------------------------------------------
// global options
NETDATA.options = {
pauseCallback: null, // a callback when we are really paused
pause: false, // when enabled we don't auto-refresh the charts
targets: null, // an array of all the state objects that are
// currently active (independently of their
// viewport visibility)
updated_dom: true, // when true, the DOM has been updated with
// new elements we have to check.
auto_refresher_fast_weight: 0, // this is the current time in ms, spent
// rendering charts continiously.
// used with .current.fast_render_timeframe
page_is_visible: true, // when true, this page is visible
auto_refresher_stop_until: 0, // timestamp in ms - used internaly, to stop the
// auto-refresher for some time (when a chart is
// performing pan or zoom, we need to stop refreshing
// all other charts, to have the maximum speed for
// rendering the chart that is panned or zoomed).
// Used with .current.global_pan_sync_time
last_resized: new Date().getTime(), // the timestamp of the last resize request
last_page_scroll: 0, // the timestamp the last time the page was scrolled
// the current profile
// we may have many...
current: {
pixels_per_point: 1, // the minimum pixels per point for all charts
// increase this to speed javascript up
// each chart library has its own limit too
// the max of this and the chart library is used
// the final is calculated every time, so a change
// here will have immediate effect on the next chart
// update
idle_between_charts: 100, // ms - how much time to wait between chart updates
fast_render_timeframe: 200, // ms - render continously until this time of continious
// rendering has been reached
// this setting is used to make it render e.g. 10
// charts at once, sleep idle_between_charts time
// and continue for another 10 charts.
idle_between_loops: 500, // ms - if all charts have been updated, wait this
// time before starting again.
idle_parallel_loops: 100, // ms - the time between parallel refresher updates
idle_lost_focus: 500, // ms - when the window does not have focus, check
// if focus has been regained, every this time
global_pan_sync_time: 1000, // ms - when you pan or zoon a chart, the background
// autorefreshing of charts is paused for this amount
// of time
sync_selection_delay: 1500, // ms - when you pan or zoom a chart, wait this amount
// of time before setting up synchronized selections
// on hover.
sync_selection: true, // enable or disable selection sync
pan_and_zoom_delay: 50, // when panning or zooming, how ofter to update the chart
sync_pan_and_zoom: true, // enable or disable pan and zoom sync
pan_and_zoom_data_padding: true, // fetch more data for the master chart when panning or zooming
update_only_visible: true, // enable or disable visibility management
parallel_refresher: true, // enable parallel refresh of charts
concurrent_refreshes: true, // when parallel_refresher is enabled, sync also the charts
destroy_on_hide: false, // destroy charts when they are not visible
show_help: true, // when enabled the charts will show some help
show_help_delay_show_ms: 500,
show_help_delay_hide_ms: 0,
eliminate_zero_dimensions: true, // do not show dimensions with just zeros
stop_updates_when_focus_is_lost: true, // boolean - shall we stop auto-refreshes when document does not have user focus
stop_updates_while_resizing: 1000, // ms - time to stop auto-refreshes while resizing the charts
double_click_speed: 500, // ms - time between clicks / taps to detect double click/tap
smooth_plot: true, // enable smooth plot, where possible
charts_selection_animation_delay: 50, // delay to animate charts when syncing selection
color_fill_opacity_line: 1.0,
color_fill_opacity_area: 0.2,
color_fill_opacity_stacked: 0.8,
pan_and_zoom_factor: 0.25, // the increment when panning and zooming with the toolbox
pan_and_zoom_factor_multiplier_control: 2.0,
pan_and_zoom_factor_multiplier_shift: 3.0,
pan_and_zoom_factor_multiplier_alt: 4.0,
setOptionCallback: function() { ; }
},
debug: {
show_boxes: false,
main_loop: false,
focus: false,
visibility: false,
chart_data_url: false,
chart_errors: false, // FIXME
chart_timing: false,
chart_calls: false,
libraries: false,
dygraph: false
}
};
NETDATA.statistics = {
refreshes_total: 0,
refreshes_active: 0,
refreshes_active_max: 0
};
// ----------------------------------------------------------------------------------------------------------------
// local storage options
NETDATA.localStorage = {
default: {},
current: {},
callback: {} // only used for resetting back to defaults
};
NETDATA.localStorageGet = function(key, def, callback) {
var ret = def;
if(typeof NETDATA.localStorage.default[key.toString()] === 'undefined') {
NETDATA.localStorage.default[key.toString()] = def;
NETDATA.localStorage.callback[key.toString()] = callback;
}
if(typeof Storage !== "undefined" && typeof localStorage === 'object') {
try {
// console.log('localStorage: loading "' + key.toString() + '"');
ret = localStorage.getItem(key.toString());
if(ret === null || ret === 'undefined') {
// console.log('localStorage: cannot load it, saving "' + key.toString() + '" with value "' + JSON.stringify(def) + '"');
localStorage.setItem(key.toString(), JSON.stringify(def));
ret = def;
}
else {
// console.log('localStorage: got "' + key.toString() + '" with value "' + ret + '"');
ret = JSON.parse(ret);
// console.log('localStorage: loaded "' + key.toString() + '" as value ' + ret + ' of type ' + typeof(ret));
}
}
catch(error) {
console.log('localStorage: failed to read "' + key.toString() + '", using default: "' + def.toString() + '"');
ret = def;
}
}
if(typeof ret === 'undefined' || ret === 'undefined') {
console.log('localStorage: LOADED UNDEFINED "' + key.toString() + '" as value ' + ret + ' of type ' + typeof(ret));
ret = def;
}
NETDATA.localStorage.current[key.toString()] = ret;
return ret;
};
NETDATA.localStorageSet = function(key, value, callback) {
if(typeof value === 'undefined' || value === 'undefined') {
console.log('localStorage: ATTEMPT TO SET UNDEFINED "' + key.toString() + '" as value ' + value + ' of type ' + typeof(value));
}
if(typeof NETDATA.localStorage.default[key.toString()] === 'undefined') {
NETDATA.localStorage.default[key.toString()] = value;
NETDATA.localStorage.current[key.toString()] = value;
NETDATA.localStorage.callback[key.toString()] = callback;
}
if(typeof Storage !== "undefined" && typeof localStorage === 'object') {
// console.log('localStorage: saving "' + key.toString() + '" with value "' + JSON.stringify(value) + '"');
try {
localStorage.setItem(key.toString(), JSON.stringify(value));
}
catch(e) {
console.log('localStorage: failed to save "' + key.toString() + '" with value: "' + value.toString() + '"');
}
}
NETDATA.localStorage.current[key.toString()] = value;
return value;
};
NETDATA.localStorageGetRecursive = function(obj, prefix, callback) {
for(var i in obj) {
if(typeof obj[i] === 'object') {
//console.log('object ' + prefix + '.' + i.toString());
NETDATA.localStorageGetRecursive(obj[i], prefix + '.' + i.toString(), callback);
continue;
}
obj[i] = NETDATA.localStorageGet(prefix + '.' + i.toString(), obj[i], callback);
}
};
NETDATA.setOption = function(key, value) {
if(key.toString() === 'setOptionCallback') {
if(typeof NETDATA.options.current.setOptionCallback === 'function') {
NETDATA.options.current[key.toString()] = value;
NETDATA.options.current.setOptionCallback();
}
}
else if(NETDATA.options.current[key.toString()] !== value) {
var name = 'options.' + key.toString();
if(typeof NETDATA.localStorage.default[name.toString()] === 'undefined')
console.log('localStorage: setOption() on unsaved option: "' + name.toString() + '", value: ' + value);
//console.log(NETDATA.localStorage);
//console.log('setOption: setting "' + key.toString() + '" to "' + value + '" of type ' + typeof(value) + ' original type ' + typeof(NETDATA.options.current[key.toString()]));
//console.log(NETDATA.options);
NETDATA.options.current[key.toString()] = NETDATA.localStorageSet(name.toString(), value, null);
if(typeof NETDATA.options.current.setOptionCallback === 'function')
NETDATA.options.current.setOptionCallback();
}
return true;
};
NETDATA.getOption = function(key) {
return NETDATA.options.current[key.toString()];
};
// read settings from local storage
NETDATA.localStorageGetRecursive(NETDATA.options.current, 'options', null);
// always start with this option enabled.
NETDATA.setOption('stop_updates_when_focus_is_lost', true);
NETDATA.resetOptions = function() {
for(var i in NETDATA.localStorage.default) {
var a = i.split('.');
if(a[0] === 'options') {
if(a[1] === 'setOptionCallback') continue;
if(typeof NETDATA.localStorage.default[i] === 'undefined') continue;
if(NETDATA.options.current[i] === NETDATA.localStorage.default[i]) continue;
NETDATA.setOption(a[1], NETDATA.localStorage.default[i]);
}
else if(a[0] === 'chart_heights') {
if(typeof NETDATA.localStorage.callback[i] === 'function' && typeof NETDATA.localStorage.default[i] !== 'undefined') {
NETDATA.localStorage.callback[i](NETDATA.localStorage.default[i]);
}
}
}
}
// ----------------------------------------------------------------------------------------------------------------
if(NETDATA.options.debug.main_loop === true)
console.log('welcome to NETDATA');
NETDATA.onresize = function() {
NETDATA.options.last_resized = new Date().getTime();
NETDATA.onscroll();
};
NETDATA.onscroll = function() {
// console.log('onscroll');
NETDATA.options.last_page_scroll = new Date().getTime();
if(NETDATA.options.targets === null) return;
// when the user scrolls he sees that we have
// hidden all the not-visible charts
// using this little function we try to switch
// the charts back to visible quickly
var targets = NETDATA.options.targets;
var len = targets.length;
while(len--) targets[len].isVisible();
};
window.onresize = NETDATA.onresize;
window.onscroll = NETDATA.onscroll;
// ----------------------------------------------------------------------------------------------------------------
// 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 }
};
NETDATA.errorLast = {
code: 0,
message: "",
datetime: 0
};
NETDATA.error = function(code, msg) {
NETDATA.errorLast.code = code;
NETDATA.errorLast.message = msg;
NETDATA.errorLast.datetime = new Date().getTime();
console.log("ERROR " + code + ": " + NETDATA.errorCodes[code].message + ": " + msg);
var 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;
};
// ----------------------------------------------------------------------------------------------------------------
// Chart Registry
// When multiple charts need the same chart, we avoid downloading it
// multiple times (and having it in browser memory multiple time)
// by using this registry.
// Every time we download a chart definition, we save it here with .add()
// Then we try to get it back with .get(). If that fails, we download it.
NETDATA.chartRegistry = {
charts: {},
fixid: function(id) {
return id.replace(/:/g, "_").replace(/\//g, "_");
},
add: function(host, id, data) {
host = this.fixid(host);
id = this.fixid(id);
if(typeof this.charts[host] === 'undefined')
this.charts[host] = {};
//console.log('added ' + host + '/' + id);
this.charts[host][id] = data;
},
get: function(host, id) {
host = this.fixid(host);
id = this.fixid(id);
if(typeof this.charts[host] === 'undefined')
return null;
if(typeof this.charts[host][id] === 'undefined')
return null;
//console.log('cached ' + host + '/' + id);
return this.charts[host][id];
},
downloadAll: function(host, callback) {
while(host.slice(-1) === '/')
host = host.substring(0, host.length - 1);
var self = this;
$.ajax({
url: host + '/api/v1/charts',
async: true,
cache: false,
xhrFields: { withCredentials: true } // required for the cookie
})
.done(function(data) {
if(data !== null) {
var h = NETDATA.chartRegistry.fixid(host);
self.charts[h] = data.charts;
}
else NETDATA.error(406, host + '/api/v1/charts');
if(typeof callback === 'function')
callback(data);
})
.fail(function() {
NETDATA.error(405, host + '/api/v1/charts');
if(typeof callback === 'function')
callback(null);
});
}
};
// ----------------------------------------------------------------------------------------------------------------
// Global Pan and Zoom on charts
// Using this structure are synchronize all the charts, so that
// when you pan or zoom one, all others are automatically refreshed
// to the same timespan.
NETDATA.globalPanAndZoom = {
seq: 0, // timestamp ms
// every time a chart is panned or zoomed
// we set the timestamp here
// then we use it as a sequence number
// to find if other charts are syncronized
// to this timerange
master: null, // the master chart (state), to which all others
// are synchronized
force_before_ms: null, // the timespan to sync all other charts
force_after_ms: null,
// set a new master
setMaster: function(state, after, before) {
if(NETDATA.options.current.sync_pan_and_zoom === false)
return;
if(this.master !== null && this.master !== state)
this.master.resetChart(true, true);
var now = new Date().getTime();
this.master = state;
this.seq = now;
this.force_after_ms = after;
this.force_before_ms = before;
NETDATA.options.auto_refresher_stop_until = now + NETDATA.options.current.global_pan_sync_time;
},
// clear the master
clearMaster: function() {
if(this.master !== null) {
var st = this.master;
this.master = null;
st.resetChart();
}
this.master = null;
this.seq = 0;
this.force_after_ms = null;
this.force_before_ms = null;
NETDATA.options.auto_refresher_stop_until = 0;
},
// is the given state the master of the global
// pan and zoom sync?
isMaster: function(state) {
if(this.master === state) return true;
return false;
},
// are we currently have a global pan and zoom sync?
isActive: function() {
if(this.master !== null && this.force_before_ms !== null && this.force_after_ms !== null && this.seq !== 0) return true;
return false;
},
// check if a chart, other than the master
// needs to be refreshed, due to the global pan and zoom
shouldBeAutoRefreshed: function(state) {
if(this.master === null || this.seq === 0)
return false;
//if(state.needsRecreation())
// return true;
if(state.tm.pan_and_zoom_seq === this.seq)
return false;
return true;
}
};
// ----------------------------------------------------------------------------------------------------------------
// dimensions selection
// FIXME
// move color assignment to dimensions, here
dimensionStatus = function(parent, label, name_div, value_div, color) {
this.enabled = false;
this.parent = parent;
this.label = label;
this.name_div = null;
this.value_div = null;
this.color = NETDATA.themes.current.foreground;
if(parent.selected_count > parent.unselected_count)
this.selected = true;
else
this.selected = false;
this.setOptions(name_div, value_div, color);
};
dimensionStatus.prototype.invalidate = function() {
this.name_div = null;
this.value_div = null;
this.enabled = false;
};
dimensionStatus.prototype.setOptions = function(name_div, value_div, color) {
this.color = color;
if(this.name_div != name_div) {
this.name_div = name_div;
this.name_div.title = this.label;
this.name_div.style.color = this.color;
if(this.selected === false)
this.name_div.className = 'netdata-legend-name not-selected';
else
this.name_div.className = 'netdata-legend-name selected';
}
if(this.value_div != value_div) {
this.value_div = value_div;
this.value_div.title = this.label;
this.value_div.style.color = this.color;
if(this.selected === false)
this.value_div.className = 'netdata-legend-value not-selected';
else
this.value_div.className = 'netdata-legend-value selected';
}
this.enabled = true;
this.setHandler();
};
dimensionStatus.prototype.setHandler = function() {
if(this.enabled === false) return;
var ds = this;
// this.name_div.onmousedown = this.value_div.onmousedown = function(e) {
this.name_div.onclick = this.value_div.onclick = function(e) {
e.preventDefault();
if(ds.isSelected()) {
// this is selected
if(e.shiftKey === true || e.ctrlKey === true) {
// control or shift key is pressed -> unselect this (except is none will remain selected, in which case select all)
ds.unselect();
if(ds.parent.countSelected() === 0)
ds.parent.selectAll();
}
else {
// no key is pressed -> select only this (except if it is the only selected already, in which case select all)
if(ds.parent.countSelected() === 1) {
ds.parent.selectAll();
}
else {
ds.parent.selectNone();
ds.select();
}
}
}
else {
// this is not selected
if(e.shiftKey === true || e.ctrlKey === true) {
// control or shift key is pressed -> select this too
ds.select();
}
else {
// no key is pressed -> select only this
ds.parent.selectNone();
ds.select();
}
}
ds.parent.state.redrawChart();
}
};
dimensionStatus.prototype.select = function() {
if(this.enabled === false) return;
this.name_div.className = 'netdata-legend-name selected';
this.value_div.className = 'netdata-legend-value selected';
this.selected = true;
};
dimensionStatus.prototype.unselect = function() {
if(this.enabled === false) return;
this.name_div.className = 'netdata-legend-name not-selected';
this.value_div.className = 'netdata-legend-value hidden';
this.selected = false;
};
dimensionStatus.prototype.isSelected = function() {
return(this.enabled === true && this.selected === true);
};
// ----------------------------------------------------------------------------------------------------------------
dimensionsVisibility = function(state) {
this.state = state;
this.len = 0;
this.dimensions = {};
this.selected_count = 0;
this.unselected_count = 0;
};
dimensionsVisibility.prototype.dimensionAdd = function(label, name_div, value_div, color) {
if(typeof this.dimensions[label] === 'undefined') {
this.len++;
this.dimensions[label] = new dimensionStatus(this, label, name_div, value_div, color);
}
else
this.dimensions[label].setOptions(name_div, value_div, color);
return this.dimensions[label];
};
dimensionsVisibility.prototype.dimensionGet = function(label) {
return this.dimensions[label];
};
dimensionsVisibility.prototype.invalidateAll = function() {
for(var d in this.dimensions)
this.dimensions[d].invalidate();
};
dimensionsVisibility.prototype.selectAll = function() {
for(var d in this.dimensions)
this.dimensions[d].select();
};
dimensionsVisibility.prototype.countSelected = function() {
var i = 0;
for(var d in this.dimensions)
if(this.dimensions[d].isSelected()) i++;
return i;
};
dimensionsVisibility.prototype.selectNone = function() {
for(var d in this.dimensions)
this.dimensions[d].unselect();
};
dimensionsVisibility.prototype.selected2BooleanArray = function(array) {
var ret = new Array();
this.selected_count = 0;
this.unselected_count = 0;
for(var i = 0, len = array.length; i < len ; i++) {
var ds = this.dimensions[array[i]];
if(typeof ds === 'undefined') {
// console.log(array[i] + ' is not found');
ret.push(false);
continue;
}
if(ds.isSelected()) {
ret.push(true);
this.selected_count++;
}
else {
ret.push(false);
this.unselected_count++;
}
}
if(this.selected_count === 0 && this.unselected_count !== 0) {
this.selectAll();
return this.selected2BooleanArray(array);
}
return ret;
};
// ----------------------------------------------------------------------------------------------------------------
// global selection sync
NETDATA.globalSelectionSync = {
state: null,
dont_sync_before: 0,
last_t: 0,
slaves: [],
stop: function() {
if(this.state !== null)
this.state.globalSelectionSyncStop();
},
delay: function() {
if(this.state !== null) {
this.state.globalSelectionSyncDelay();
}
}
};
// ----------------------------------------------------------------------------------------------------------------
// Our state object, where all per-chart values are stored
chartState = function(element) {
var self = $(element);
this.element = element;
// IMPORTANT:
// all private functions should use 'that', instead of 'this'
var that = this;
/* error() - private
* show an error instead of the chart
*/
var error = function(msg) {
var ret = true;
if(typeof netdataErrorCallback === 'function') {
ret = netdataErrorCallback('chart', that.id, msg);
}
if(ret) {
that.element.innerHTML = that.id + ': ' + msg;
that.enabled = false;
that.current = that.pan;
}
};
// GUID - a unique identifier for the chart
this.uuid = NETDATA.guid();
// string - the name of chart
this.id = self.data('netdata');
// string - the key for localStorage settings
this.settings_id = self.data('id') || null;
// the user given dimensions of the element
this.width = self.data('width') || NETDATA.chartDefaults.width;
this.height = self.data('height') || NETDATA.chartDefaults.height;
if(this.settings_id !== null) {
this.height = NETDATA.localStorageGet('chart_heights.' + this.settings_id, this.height, function(height) {
// this is the callback that will be called
// if and when the user resets all localStorage variables
// to their defaults
resizeChartToHeight(height);
});
}
// string - the netdata server URL, without any path
this.host = self.data('host') || NETDATA.chartDefaults.host;
// make sure the host does not end with /
// all netdata API requests use absolute paths
while(this.host.slice(-1) === '/')
this.host = this.host.substring(0, this.host.length - 1);
// string - the grouping method requested by the user
this.method = self.data('method') || NETDATA.chartDefaults.method;
// the time-range requested by the user
this.after = self.data('after') || NETDATA.chartDefaults.after;
this.before = self.data('before') || NETDATA.chartDefaults.before;
// the pixels per point requested by the user
this.pixels_per_point = self.data('pixels-per-point') || 1;
this.points = self.data('points') || null;
// the dimensions requested by the user
this.dimensions = self.data('dimensions') || null;
// the chart library requested by the user
this.library_name = self.data('chart-library') || NETDATA.chartDefaults.library;
// object - the chart library used
this.library = null;
// color management
this.colors = null;
this.colors_assigned = {};
this.colors_available = null;
// the element already created by the user
this.element_message = null;
// the element with the chart
this.element_chart = null;
// the element with the legend of the chart (if created by us)
this.element_legend = null;
this.element_legend_childs = {