]> arthur.barton.de Git - netdata.git/blob - web/index.html
force update of control chart when clicking play on mainchart
[netdata.git] / web / index.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5     <meta charset="utf-8">
6     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
7     <meta name="viewport" content="width=device-width, initial-scale=1">
8     <meta name="apple-mobile-web-app-capable" content="yes">
9     <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
10     <meta name="description" content="">
11     <meta name="author" content="costa@tsaousis.gr">
12  
13         <title>NetData</title>
14
15         <!-- Google AJAX API -->
16         <script type="text/javascript" src="https://www.google.com/jsapi"></script>
17         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
18
19         <!-- Bootstrap -->
20         <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
21         <!-- <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css"> -->
22         <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
23         <link href="/file/theme.css" rel="stylesheet">
24
25         <!-- NetData -->
26         <script type="text/javascript" src="/file/netdata.js"></script>
27         <script type="text/javascript" src="/file/jquery.visible.js"></script>
28         <script type="text/javascript">
29         
30         // Set a callback to run when the Google Visualization API is loaded.
31         google.setOnLoadCallback(initCharts);
32         
33         var TARGET_THUMB_GRAPH_WIDTH = 500;             // thumb charts width will range from 0.5 to 1.5 of that
34         var MINIMUM_THUMB_GRAPH_WIDTH = 400;    // thumb chart will generally try to be wider than that
35         var TARGET_THUMB_GRAPH_HEIGHT = 220;    // the height of the thumb charts
36         var THUMBS_MAX_TIME_TO_SHOW = 600;              // how much time the thumb charts will present?
37
38         var MAINCHART_MAX_TIME_TO_SHOW = 600;   // how much time the main chart will present by default?
39         var MAINCHART_POINTS_DIVISOR = 10;              // how much detailed will the main chart be by default? 1 = finest, higher is faster
40         var MAINCHART_CONTROL_HEIGHT = 75;              // how tall the control chart will be
41         var MAINCHART_CONTROL_DIVISOR = 2;              // how much detailed will the control chart be? 1 = finest, higher is faster
42
43         var MODE_THUMBS = 1;
44         var MODE_MAIN = 2;
45         var MODE_GROUP_THUMBS = 3;
46         var mode; // one of the MODE_* values
47
48         var mycharts = new Array();
49         var mainchart;
50
51         // html for the main menu
52         var mainmenu = "";
53
54
55         // ------------------------------------------------------------------------
56         // common HTML generation
57
58         function chartIsLoadingHTML(name, width, height) { return "<table><tr><td align=\"center\" width=\"" + width + "\" height=\"" + height + "\" style=\"vertical-align:middle\"><h4><span class=\"glyphicon glyphicon-refresh\"></span><br/><br/>loading " + name + "<br/><br/><span class=\"label label-default\">Please wait...</span></h4></td></tr></table>"; }
59
60         function showChartIsLoading(id, name, width, height) {
61                 //mylog('adding loading chart html in div with id ' + id);
62                 document.getElementById(id).innerHTML = chartIsLoadingHTML(name, width, height);
63         }
64
65         function thumbChartActions(i, c, nogroup) {
66                 var name = c.name;
67                 if(!nogroup) name = c.group_tag;
68
69                 var refinfo = "the chart is drawing ";
70                 if(c.group == 1) refinfo += "every single point collected.";
71                 else refinfo += ((c.group_method == "average")?"the average":"the max") + " value for every " + (c.group * c.update_every) + " seconds of data";
72
73                 var html = "<div class=\"btn-group btn-group\" data-toggle=\"tooltip\" title=\"" + refinfo + "\">"
74                 +               "<button type=\"button\" class=\"btn btn-default\" onclick=\"javascript: return;\"><span class=\"glyphicon glyphicon-info-sign\"></span></button>"
75                 +       "</div>"
76                 +       "<div class=\"btn-group btn-group\"><button type=\"button\" class=\"btn btn-default disabled\"><small>&nbsp;&nbsp; " + name + "</small></button>";
77
78                 if(!nogroup) {
79                         var ingroup = 0;
80                         var ingroup_detail = 0;
81
82                         $.each(mycharts, function(i, d) {
83                                 if(d.group_tag == c.group_tag) {
84                                         ingroup++;
85                                         if(d.isdetail) ingroup_detail++;
86                                 }
87                         });
88
89                         var hidden = "";
90                         if(ingroup_detail) hidden = ", including " + ingroup_detail + " charts not shown now";
91
92                         html += "<button type=\"button\" data-toggle=\"tooltip\" title=\"Show all " + ingroup + " charts in group '" + c.group_tag + "'" + hidden + "\" class=\"btn btn-default\" onclick=\"initGroupGraphs('" + c.group_tag +"');\"><span class=\"glyphicon glyphicon-th-large\"></span></button>";
93                 }
94
95                 html += "<button type=\"button\" data-toggle=\"tooltip\" title=\"show chart '" + c.name + "' in fullscreen\" class=\"btn btn-default\" onclick=\"initMainChartIndex(" + i +");\"><span class=\"glyphicon glyphicon-resize-full\"></span></button>"
96                 +               "<button type=\"button\" data-toggle=\"tooltip\" title=\"set options for chart '" + c.name + "'\" class=\"btn btn-default disabled\" onclick=\"alert('Not implemented yet!');\"><span class=\"glyphicon glyphicon-cog\"></span></button>"
97                 +               "<button type=\"button\" data-toggle=\"tooltip\" title=\"ignore chart '" + c.name + "'\" class=\"btn btn-default\" onclick=\"disableChart(" + i + ");\"><span class=\"glyphicon glyphicon-trash\"></span></button>"
98                 +       "</div>";
99
100                 return html;
101         }
102
103         function mylog(txt) {
104                 console.log(txt);
105         }
106
107         function chartssort(a, b) {
108                 if(a.userpriority < b.userpriority) return -1;
109                 return 1;
110         }
111
112
113         // ------------------------------------------------------------------------
114         // MAINGRAPH = fullscreen view of 1 graph
115
116         function calculateChartGroup(c, divisor, maxtime, group) {
117                 var before = c.before?c.before:new Date().getTime() / 1000;
118                 var after = c.after?c.after:c.first_entry_t;
119
120                 var dt = before - after;
121                 if(dt > c.entries * c.update_every) dt = c.entries * c.update_every;
122
123                 if(maxtime) dt = maxtime;
124
125                 var data_points = Math.round(dt / c.update_every);
126                 var screen_points = Math.round(c.chartOptions.width / divisor);
127                 //mylog('screen = ' + screen_points + ', data = ' + data_points + ', divisor = ' + divisor);
128
129                 if(group <= 0) {
130                         if(screen_points > data_points) {
131                                 c.group = 1;
132                                 c.points_to_show = data_points;
133                                 //mylog("rendering at full detail");
134                         }
135                         else {
136                                 c.group = Math.round(data_points / screen_points);
137
138                                 if(group >= 0) {
139                                              if(c.group > 60) c.group = 60;
140                                         else if(c.group > 45) c.group = 45;
141                                         else if(c.group > 30) c.group = 30;
142                                         else if(c.group > 20) c.group = 20;
143                                         else if(c.group > 15) c.group = 15;
144                                         else if(c.group > 10) c.group = 10;
145                                         else if(c.group > 5) c.group = 5;
146                                         else if(c.group > 2) c.group = 2;
147                                         else c.group = 1;
148                                 }
149
150                                 c.points_to_show = Math.round(data_points / c.group);
151                                 //mylog("rendering adaptive");
152                         }
153                 }
154                 else {
155                         c.group = group;
156                         c.points_to_show = Math.round(data_points / group);
157                         //mylog("rendering with given group");
158                 }
159                 //mylog('group = ' + c.group + ', points = ' + c.points_to_show);
160
161                 if(mainchart.chartType == 'LineChart') {
162                         if(mainchart.group <= 2) mainchart.chartOptions.lineWidth = 1;
163                         else mainchart.chartOptions.lineWidth = 2;
164                 }
165         }
166
167         // copy the chart c to mainchart
168         // switch to main graphs screen
169         function initMainChart(c) {
170                 if(mainchart) cleanThisChart(mainchart);
171
172                 mainchart = $.extend(true, {}, c);
173                 mainchart.refreshCount = 0;
174                 mainchart.last_updated = 0;
175                 mainchart.chartOptions.explorer = null;
176                 mainchart.chart = null;
177
178                 mainchart.chartOptions.width = screenWidth();
179                 mainchart.chartOptions.height = $(window).height() - 150 - MAINCHART_CONTROL_HEIGHT;
180                 if(mainchart.chartOptions.height < 300) mainchart.chartOptions.height = 300;
181                 //mainchart.chartOptions.chartArea = {'width': '80%'};
182
183                 mainchart.div = 'maingraph';
184                 calculateChartGroup(mainchart, MAINCHART_POINTS_DIVISOR, MAINCHART_MAX_TIME_TO_SHOW, 0);
185
186                 // copy it to the hidden chart
187                 mainchart.hiddenchart = $.extend(true, {}, mainchart);
188                 mainchart.hiddenchart.chartOptions.height = MAINCHART_CONTROL_HEIGHT;
189                 mainchart.hiddenchart.div = 'maingraph_control';
190
191                 // initialize the div
192                 showChartIsLoading(mainchart.div, mainchart.name, mainchart.chartOptions.width, mainchart.chartOptions.height);
193
194                 // set the radio buttons
195                 setMainChartGroupMethod(mainchart.group_method, 'no-refresh');
196                 setMainChartMax('normal');
197
198                 $('#group' + mainchart.group).trigger('click');
199                 setMainChartGroup(mainchart.group, 'no-refresh');
200
201                 switchToMainGraph();
202         }
203
204         function refreshHiddenChart(doNext) {
205                 if(refresh_mode == REFRESH_PAUSED && mainchart.hiddenchart.last_updated != 0) {
206                         if(typeof doNext == "function") doNext();
207                         return;
208                 }
209
210                 // is it too soon for a refresh?
211                 var now = new Date().getTime();
212                 if((now - mainchart.hiddenchart.last_updated) < (mainchart.hiddenchart.group * mainchart.hiddenchart.update_every * 1000)) {
213                         if(typeof doNext == "function") doNext();
214                         return;
215                 }
216
217                 if(mainchart.dashboard && mainchart.hiddenchart.refreshCount > 50) {
218                         mainchart.dashboard.clear();
219                         mainchart.control_wrapper.clear();
220                         mainchart.hidden_wrapper.clear();
221
222                         mainchart.dashboard = null;
223                         mainchart.control_wrapper = null;
224                         mainchart.hidden_wrapper = null;
225                 }
226
227                 if(!mainchart.dashboard) {
228                         var controlopts = $.extend(true, {}, mainchart.chartOptions, {
229                                 lineWidth: 1,
230                                 height: mainchart.hiddenchart.chartOptions.height,
231                                 chartArea: {'width': '98%'},
232                                 hAxis: {'baselineColor': 'none'},
233                                 vAxis: {'title': null},
234                         });
235
236                         mainchart.dashboard = new google.visualization.Dashboard(document.getElementById('maingraph_dashboard'));
237                         mainchart.control_wrapper = new google.visualization.ControlWrapper({
238                                 controlType: 'ChartRangeFilter',
239                                 containerId: 'maingraph_control',
240                                 options: {
241                                         filterColumnIndex: 0,
242                                         ui: {
243                                                 chartType: mainchart.chartType,
244                                                 chartOptions: controlopts,
245                                                 minRangeSize: (MAINCHART_MAX_TIME_TO_SHOW * 1000 * mainchart.update_every) / MAINCHART_POINTS_DIVISOR,
246                                         }
247                                 },
248                         });
249                         mainchart.hidden_wrapper = new google.visualization.ChartWrapper({
250                                 chartType: mainchart.chartType,
251                                 containerId: 'maingraph_hidden',
252                                 options: {
253                                         isStacked: mainchart.chartOptions.isStacked,
254                                         width: mainchart.hiddenchart.chartOptions.width,
255                                         height: mainchart.hiddenchart.chartOptions.height,
256                                         //chartArea: {'height': '80%', 'width': '100%'},
257                                         //hAxis: {'slantedText': false},
258                                         //legend: {'position': 'none'}
259                                 },
260                         });
261
262                         mainchart.hiddenchart.refreshCount = 0;
263                 }
264
265                 // load the data for the control and the hidden wrappers
266                 // calculate the group and points to show for the control chart
267                 calculateChartGroup(mainchart.hiddenchart, MAINCHART_CONTROL_DIVISOR, 0, -1);
268
269                 $.ajax({
270                         url: generateChartURL(mainchart.hiddenchart),
271                         dataType:"json",
272                         cache: false
273                 })
274                 .done(function(jsondata) {
275                         if(!jsondata || jsondata.length == 0) return;
276
277                         mainchart.control_data = new google.visualization.DataTable(jsondata);
278
279                         if(mainchart.hiddenchart.last_updated == 0) {
280                                 google.visualization.events.addListener(mainchart.control_wrapper, 'ready', mainchartControlReadyEvent);
281                                 mainchart.dashboard.bind(mainchart.control_wrapper, mainchart.hidden_wrapper);
282                         }
283                         if(refresh_mode != REFRESH_PAUSED)
284                                 mainchart.control_wrapper.setState({range: {
285                                         start: new Date((Math.round(new Date().getTime() / 1000) - MAINCHART_MAX_TIME_TO_SHOW) * 1000),
286                                         end: new Date()
287                                 }});
288
289                         mainchart.dashboard.draw(mainchart.control_data);
290                         mainchart.hiddenchart.last_updated = new Date().getTime();
291                         mainchart.hiddenchart.refreshCount++;
292                 })
293                 .always(function() {
294                         if(typeof doNext == "function") doNext();
295                 });
296         }
297
298         function mainchartControlReadyEvent() {
299                 google.visualization.events.addListener(mainchart.control_wrapper, 'statechange', mainchartControlStateHandler);
300                 //mylog(mainchart);
301         }
302
303         function mainchartControlStateHandler() {
304                 // setMainChartPlay('pause');
305
306                 var state = mainchart.control_wrapper.getState();
307                 mainchart.after = Math.round(state.range.start.getTime() / 1000);
308                 mainchart.before = Math.round(state.range.end.getTime() / 1000);
309
310                 calculateChartGroup(mainchart, MAINCHART_POINTS_DIVISOR, 0, 0);
311                 //mylog('group = ' + mainchart.group + ', points_to_show = ' + mainchart.points_to_show + ', dt = ' + (mainchart.before - mainchart.after));
312
313                 $('#group' + mainchart.group).trigger('click');
314                 mainchart.last_updated = 0;
315
316                 if(refresh_mode != REFRESH_PAUSED) pauseGraphs();
317         }
318
319         function initMainChartIndex(i) {
320                 if(mode == MODE_GROUP_THUMBS) 
321                         initMainChart(group_charts[i]);
322
323                 else if(mode == MODE_THUMBS)
324                         initMainChart(mycharts[i]);
325         }
326
327         var last_main_chart_max='normal';
328         function setMainChartMax(m) {
329                 if(!mainchart) return;
330
331                 if(m == 'toggle') {
332                         if(last_main_chart_max == 'maximized') m = 'normal';
333                         else m = 'maximized';
334                 }
335
336                 if(m == "maximized") {
337                         mainchart.chartOptions.theme = 'maximized';
338                         //mainchart.chartOptions.axisTitlesPosition = 'in';
339                         //mainchart.chartOptions.legend = {position: 'none'};
340                         //mainchart.chartOptions.hAxis.title = null;
341                         mainchart.chartOptions.hAxis.viewWindowMode = 'maximized';
342                         mainchart.chartOptions.vAxis.viewWindowMode = 'maximized';
343                 }
344                 else {
345                         mainchart.chartOptions.theme = null;
346                         mainchart.chartOptions.hAxis.viewWindowMode = null;
347                         mainchart.chartOptions.vAxis.viewWindowMode = null;
348                 }
349                 $('.mainchart_max_button').button(m);
350                 last_main_chart_max = m;
351                 mainchart.last_updated = 0;
352         }
353
354         function setMainChartGroup(g, norefresh) {
355                 if(!mainchart) return;
356
357                 mainchart.group = g;
358
359                 if(!mainchart.before && !mainchart.after)
360                         calculateChartGroup(mainchart, MAINCHART_POINTS_DIVISOR, MAINCHART_MAX_TIME_TO_SHOW, mainchart.group);
361                 else
362                         calculateChartGroup(mainchart, MAINCHART_POINTS_DIVISOR, 0, mainchart.group);
363
364                 if(!norefresh) {
365                         mainchart.last_updated = 0;
366                 }
367         }
368
369         var last_main_chart_avg = null;
370         function setMainChartGroupMethod(g, norefresh) {
371                 if(!mainchart) return;
372
373                 if(g == 'toggle') {
374                         if(last_main_chart_avg == 'max') g = 'average';
375                         else g = 'max';
376                 }
377
378                 mainchart.group_method = g;
379
380                 $('.mainchart_avg_button').button(g);
381
382                 if(!norefresh) {
383                         mainchart.last_updated = 0;
384                 }
385
386                 last_main_chart_avg = g;
387         }
388
389         function setMainChartPlay(p) {
390                 if(!mainchart) return;
391
392                 if(p == 'toggle') {
393                         if(refresh_mode != REFRESH_ALWAYS) p = 'play';
394                         else p = 'pause';
395                 }
396
397                 if(p == 'play') {
398                         //mainchart.chartOptions.explorer = null;
399                         mainchart.after = 0;
400                         mainchart.before = 0;
401                         calculateChartGroup(mainchart, MAINCHART_POINTS_DIVISOR, MAINCHART_MAX_TIME_TO_SHOW, 0);
402                         $('#group' + mainchart.group).trigger('click');
403                         mainchart.last_updated = 0;
404                         mainchart.hiddenchart.last_updated = 0;
405                         playGraphs();
406                 }
407                 else {
408                         //mainchart.chartOptions.explorer = {
409                         //      'axis': 'horizontal',
410                         //      'maxZoomOut': 1,
411                         //};
412                         //mainchart.last_updated = 0;
413                         
414                         //if(!refreshChart(mainchart, pauseGraphs))
415                         pauseGraphs();
416                 }
417         }
418
419
420         // ------------------------------------------------------------------------
421         // Chart resizing
422
423         function screenWidth() {
424                 return (($(window).width() * 0.95) - 50);
425         }
426
427         // calculate the proper width for the thumb charts
428         function thumbWidth() {
429                 var cwidth = screenWidth();
430                 var items = Math.round(cwidth / TARGET_THUMB_GRAPH_WIDTH);
431                 if(items < 1) items = 1;
432
433                 if(items > 1 && (cwidth / items) < MINIMUM_THUMB_GRAPH_WIDTH) items--;
434
435                 return Math.round(cwidth / items) - 1;
436         }
437
438         function groupChartSizes() {
439                 var s = { width: screenWidth() / 2, height: ($(window).height() - 130) / 3 - 10};
440                 if(s.width < MINIMUM_THUMB_GRAPH_WIDTH * 1.5) s.width = screenWidth();
441
442                 var count = 0;
443                 if(group_charts) $.each(group_charts, function(i, c) {
444                         if(c.enabled) count++;
445                 });
446
447                 if(count == 0) {
448                         s.width = TARGET_THUMB_GRAPH_WIDTH;
449                         s.height = TARGET_THUMB_GRAPH_HEIGHT;
450                 }
451                 if(count < 4) {
452                         s.width = screenWidth();
453                         s.height = ($(window).height() - 130) / count - 10;
454                 }
455                 else if(count == 4) {
456                         s.height = ($(window).height() - 130) / 2 - 10;
457                 }
458
459                 if(s.height < TARGET_THUMB_GRAPH_HEIGHT * 1.5)
460                         s.height = TARGET_THUMB_GRAPH_HEIGHT * 1.5;
461
462                 return s;
463         }
464
465         // resize all charts
466         // if the thumb charts need resize in their width, reset them
467         function resizeCharts() {
468                 var width = screenWidth();
469
470                 if(mainchart) {
471                         mainchart.chartOptions.width = width;
472                         mainchart.chartOptions.height = $(window).height() - 150 - MAINCHART_CONTROL_HEIGHT;
473                         mainchart.last_updated = 0;
474
475                         mainchart.hidden_wrapper.setOption('width', width);
476                         mainchart.control_wrapper.setOption('ui.chartOptions.width', width);
477                         mainchart.hiddenchart.chartOptions.width = width;
478                         mainchart.hiddenchart.last_updated = 0;
479                 }
480
481                 width = thumbWidth();
482                 $.each(mycharts, function(i, c) {
483                         if(c.enabled && c.chartOptions.width != width) {
484                                 cleanThisChart(c);
485                                 c.chartOptions.width = width;
486                                 showChartIsLoading(c.div, c.name, c.chartOptions.width, c.chartOptions.height);
487                                 c.last_updated = 0;
488                         }
489                 });
490
491                 if(group_charts) $.each(group_charts, function(i, c) {
492                         var sizes = groupChartSizes();
493
494                         if(c.enabled && (c.chartOptions.width != sizes.width || c.chartOptions.height != sizes.height)) {
495                                 cleanThisChart(c);
496                                 c.chartOptions.width = sizes.width;
497                                 c.chartOptions.height = sizes.height;
498                                 showChartIsLoading(c.div, c.name, c.chartOptions.width, c.chartOptions.height);
499                                 c.last_updated = 0;
500                         }
501                 });
502         }
503
504         var resize_request = false;
505         window.onresize = function(event) {
506                 resize_request = true;
507         };
508
509
510         // ------------------------------------------------------------------------
511         // Core of the thread refreshing the charts
512
513         var REFRESH_PAUSED = 0;
514         var REFRESH_ALWAYS = 1;
515
516         var refresh_mode = REFRESH_PAUSED;
517         var last_refresh = 0;
518         function playGraphs() {
519                 if(refresh_mode == REFRESH_ALWAYS) return;
520
521                 //mylog('PlayGraphs()');
522                 refresh_mode = REFRESH_ALWAYS;
523                 $('.mainchart_play_button').button('play');
524
525                 // check if the thread died due to a javascript error
526                 var now = new Date().getTime();
527                 if((now - last_refresh) > 5000) {
528                         // it died or never started
529                         //mylog('It seems the refresh thread died. Restarting it.');
530                         chartsRefresh();
531                 }
532         }
533
534         function pauseGraphs() {
535                 if(refresh_mode == REFRESH_PAUSED) return;
536
537                 //mylog('PauseGraphs()');
538                 refresh_mode = REFRESH_PAUSED;
539                 $('.mainchart_play_button').button('pause');
540         }
541
542         // refresh the proper chart
543         // this is an internal function.
544         // never call it directly, or new javascript threads will be spawn
545         var timeout;
546         function chartsRefresh() {
547                 if(resize_request) {
548                         resizeCharts();
549                         resize_request = false;
550                         // refresh_mode = REFRESH_ALWAYS;
551                 }
552
553                 last_refresh = new Date().getTime();
554
555                 if(refresh_mode == REFRESH_PAUSED) {
556                         if(mode == MODE_MAIN && mainchart.last_updated == 0)
557                                 mainChartRefresh();
558                         else
559                                 timeout = setTimeout(chartsRefresh, 500);
560
561                         return;
562                 }
563
564                      if(mode == MODE_THUMBS)            thumbChartsRefresh();
565                 else if(mode == MODE_GROUP_THUMBS)  groupChartsRefresh();
566                 else if(mode == MODE_MAIN)              mainChartRefresh();
567                 else                                    timeout = setTimeout(triggerRefresh, 500);
568         }
569
570         // callback for refreshing the charts later
571         // this is an internal function.
572         // never call it directly, or new javascript threads will be spawn
573         function triggerRefresh() {
574                 //mylog('triggerRefresh()');
575
576                 // cleanup has to take place when the charts are not refreshed
577                 // since the refreshing thread is in this function, it means
578                 // nothing is being refreshed.
579                 cleanupCharts();
580
581                      if(mode == MODE_THUMBS)            timeout = setTimeout(chartsRefresh, 200);
582                 else if(mode == MODE_GROUP_THUMBS)      timeout = setTimeout(chartsRefresh, 200);
583                 else if(mode == MODE_MAIN)              timeout = setTimeout(chartsRefresh, 200);
584                 else                                    timeout = setTimeout(triggerRefresh, 500);
585         }
586
587         // refresh the main chart
588         // make sure we don't loose the refreshing thread
589         function mainChartRefresh() {
590                 //mylog('mainChartRefresh()');
591
592                 if(mode != MODE_MAIN || !mainchart) {
593                         triggerRefresh();
594                         return;
595                 }
596
597                 if(!refreshChart(mainchart, hiddenChartRefresh))
598                         hiddenChartRefresh();
599         }
600
601         function hiddenChartRefresh() {
602                 refreshHiddenChart(triggerRefresh);
603         }
604
605         function roundRobinRefresh(charts, startat) {
606                 var refreshed = false;
607
608                 // find a chart to refresh
609                 var all = charts.length;
610                 var cur = startat;
611                 var count = 0;
612
613                 for(count = 0; count < all ; count++, cur++) {
614                         if(cur >= all) cur = 0;
615
616                         if(charts[cur].enabled) {
617                                 //mylog('going to refresh chart ' + charts[cur].name);
618                                 refreshed = refreshChart(charts[cur], chartsRefresh);
619                                 if(refreshed) break;
620                         }
621                 }
622
623                 if(!refreshed) triggerRefresh();
624                 return cur;
625         }
626
627         // refresh the thumb charts
628         // make sure we don't loose the refreshing thread
629         var last_thumb_updated = 0;
630         function thumbChartsRefresh() {
631                 //mylog('thumbChartsRefresh()');
632
633                 if(mycharts.length == 0 || mode != MODE_THUMBS) {
634                         triggerRefresh();
635                         return;
636                 }
637
638                 last_thumb_updated = roundRobinRefresh(mycharts, last_thumb_updated);
639         }
640
641         // refresh the group charts
642         // make sure we don't loose the refreshing thread
643         var last_group_updated = 0;
644         function groupChartsRefresh() {
645                 //mylog('groupChartsRefresh()');
646
647                 if(!group_charts || group_charts.length == 0 || mode != MODE_GROUP_THUMBS) {
648                         //mylog('cannot refresh charts');
649                         triggerRefresh();
650                         return;
651                 }
652
653                 last_group_updated = roundRobinRefresh(group_charts, last_group_updated);
654         }
655
656
657         // ------------------------------------------------------------------------
658         // switch the screen between views
659         // these should be called only from initXXXX()
660
661         function disableChart(i) {
662                 //mylog('disableChart(' + i + ')');
663
664                 var chart = null;
665
666                 var count = 0;
667                 if(mode == MODE_GROUP_THUMBS && group_charts) {
668                         $.each(group_charts, function(i, c) {
669                                 if(c.enabled) count++;
670                         });
671
672                         if(i < group_charts.length) chart = group_charts[i];
673                 }
674                 else if(mode == MODE_THUMBS) {
675                         $.each(mycharts, function(i, c) {
676                                 if(c.enabled) count++;
677                         });
678
679                         if(i < mycharts.length) chart = mycharts[i];
680                 }
681
682                 if(!chart) return;
683
684                 if(count <= 1) {
685                         alert('Cannot close the last chart shown.');
686                         return;
687                 }
688
689                 if(chart) {
690                         //mylog("disabling chart " + chart.name);
691                         chart.disablethisplease = true;
692                 }
693         }
694
695         function cleanThisChart(chart, emptydivs) {
696                 //mylog('cleanThisChart(' + chart.name + ', ' + emptydivs +')');
697
698                 if(chart.dashboard) {
699                         chart.dashboard.clear();
700                         chart.dashboard = null;
701
702                         if(chart.control_wrapper) {
703                                 chart.control_wrapper.clear();
704                                 chart.control_wrapper = null;
705                         }
706
707                         if(chart.hidden_wrapper) {
708                                 chart.hidden_wrapper.clear();
709                                 chart.hidden_wrapper = null;
710                         }
711
712                         chart.control_data = null;
713                 }
714
715                 if(chart.chart) chart.chart.clearChart();
716                 chart.chart = null;
717
718                 if(emptydivs) {
719                         var div = document.getElementById(chart.div);
720                         if(div) {
721                                 div.style.display = 'none';
722                                 div.innerHTML = "";
723                         }
724
725                         div = document.getElementById(chart.div + "_parent");
726                         if(div) {
727                                 div.style.display = 'none';
728                                 div.innerHTML = "";
729                         }
730                 }
731
732                 //mylog("chart " + chart.name + " cleaned with option " + emptydivs);
733         }
734
735         // cleanup the previously shown charts
736         function cleanupCharts() {
737                 //mylog('cleanupCharts()');
738
739                 if(mode != MODE_MAIN && mainchart) {
740                         if(mainchart.chart) cleanThisChart(mainchart);
741                         mainchart = null;
742                 }
743
744                 if(mode != MODE_GROUP_THUMBS && group_charts) {
745                         clearGroupGraphs();
746                 }
747
748                 // cleanup the disabled charts
749                 $.each(mycharts, function(i, c) {
750                         if(c.disablethisplease && c.enabled) {
751                                 cleanThisChart(c, 'emptydivs');
752                                 c.disablethisplease = false;
753                                 c.enabled = false;
754                                 resize_request = true;
755                                 //mylog("disabled chart " + c.name + " removed");
756                         }
757                 });
758
759                 if(group_charts) $.each(group_charts, function(i, c) {
760                         if(c.disablethisplease && c.enabled) {
761                                 cleanThisChart(c, 'emptydivs');
762                                 c.disablethisplease = false;
763                                 c.enabled = false;
764                                 resize_request = true;
765                                 //mylog("disabled chart " + c.name + " removed");
766                         }
767                 });
768
769                 // we never cleanup the thumb charts
770         }
771
772         function updateUI() {
773                 $('[data-toggle="tooltip"]').tooltip({'placement': 'top', 'container': 'body', 'html': true});
774
775                 $('[data-spy="scroll"]').each(function () {
776                         var $spy = $(this).scrollspy('refresh')
777                 })
778         }
779
780         var thumbsScrollPosition = null;
781         function switchToMainGraph() {
782                 //mylog('switchToMainGraph()');
783
784                 if(!mainchart) return;
785
786                 if(!group_charts) thumbsScrollPosition = window.pageYOffset;
787
788                 document.getElementById('maingraph_container').style.display = 'block';
789                 document.getElementById('thumbgraphs_container').style.display = 'none';
790                 document.getElementById('groupgraphs_container').style.display = 'none';
791
792                 document.getElementById("main_menu_div").innerHTML = "<ul class=\"nav navbar-nav\"><li><a href=\"javascript:switchToThumbGraphs();\">Back to Home</a></li><li class=\"active\"><a href=\"#\">" + mainchart.title + "</a></li></ul>";
793
794                 window.scrollTo(0, 0);
795
796                 mode = MODE_MAIN;
797                 playGraphs();
798                 updateUI();
799         }
800
801         function switchToThumbGraphs() {
802                 //mylog('switchToThumbGraphs()');
803
804                 document.getElementById('maingraph_container').style.display = 'none';
805                 document.getElementById('thumbgraphs_container').style.display = 'block';
806                 document.getElementById('groupgraphs_container').style.display = 'none';
807
808                 document.getElementById("main_menu_div").innerHTML = mainmenu;
809
810                 if(thumbsScrollPosition) window.scrollTo(0, thumbsScrollPosition);
811
812                 // switch mode
813                 mode = MODE_THUMBS;
814                 playGraphs();
815                 updateUI();
816         }
817
818         function switchToGroupGraphs() {
819                 //mylog('switchToGroupGraphs()');
820
821                 if(!group_charts) return;
822
823                 if(!mainchart) thumbsScrollPosition = window.pageYOffset;
824
825                 document.getElementById('maingraph_container').style.display = 'none';
826                 document.getElementById('thumbgraphs_container').style.display = 'none';
827                 document.getElementById('groupgraphs_container').style.display = 'block';
828
829                 document.getElementById("main_menu_div").innerHTML = "<ul class=\"nav navbar-nav\"><li><a href=\"javascript:switchToThumbGraphs();\">Back to Home</a></li><li class=\"active\"><a href=\"#\">" + group_charts[0].group_tag + " charts</a></li></ul>";
830
831                 window.scrollTo(0, 0);
832
833                 mode = MODE_GROUP_THUMBS;
834                 playGraphs();
835                 updateUI();
836         }
837
838
839         // ------------------------------------------------------------------------
840         // Group Charts
841
842         var group_charts = null;
843         function initGroupGraphs(group) {
844                 var count = 0;
845                 
846                 if(group_charts) clearGroupGraphs();
847                 group_charts = new Array();
848
849                 var groupbody = "";
850                 $.each(mycharts, function(i, c) {
851                         if(c.group_tag == group) {
852                                 group_charts[count] = [];
853                                 group_charts[count] = $.extend(true, {}, c);
854                                 group_charts[count].div += "_group";
855                                 group_charts[count].enabled = true;
856                                 group_charts[count].chart = null;
857                                 group_charts[count].last_updated = 0;
858                                 count++;
859                         }
860                 });
861                 group_charts.sort(chartssort);
862
863                 var sizes = groupChartSizes();
864
865                 var groupbody = "";
866                 $.each(group_charts, function(i, c) {
867                         c.chartOptions.width = sizes.width;
868                         c.chartOptions.height = sizes.height;
869
870                         groupbody += "<div class=\"thumbgraph\" id=\"" + c.div + "_parent\"><table><tr><td><div class=\"thumbgraph\" id=\"" + c.div + "\">" + chartIsLoadingHTML(c.name, c.chartOptions.width, c.chartOptions.height) + "</div></td></tr><tr><td align=\"center\">" + thumbChartActions(i, c, 'nogroup') + "</td></tr></table></div>";
871                 });
872                 groupbody += "";
873
874                 document.getElementById("groupgraphs").innerHTML = groupbody;
875                 switchToGroupGraphs();
876         }
877
878         function clearGroupGraphs() {
879                 if(group_charts && group_charts.length) {
880                         $.each(group_charts, function(i, c) {
881                                 cleanThisChart(c, 'emptydivs');
882                         });
883
884                         group_charts = null;
885                 }
886
887                 document.getElementById("groupgraphs").innerHTML = "";
888         }
889
890
891         // ------------------------------------------------------------------------
892         // Global entry point
893         // initialize the thumb charts
894
895         // load the charts from the server
896         // generate html for the thumbgraphs to support them
897         function initCharts() {
898                 var width = thumbWidth();
899                 var height = TARGET_THUMB_GRAPH_HEIGHT;
900
901                 loadCharts(null, function(c) {
902                         mycharts = c;
903
904                         if(mycharts == null || mycharts.length == 0) {
905                                 alert("Cannot load data from server.");
906                                 return;
907                         }
908
909                         var thumbsContainer = document.getElementById("thumbgraphs");
910                         if(!thumbsContainer) {
911                                 alert("Cannot find the thumbsContainer");
912                                 return;
913                         }
914
915                         mycharts.sort(chartssort);
916
917                         document.getElementById('hostname_id').innerHTML = mycharts[0].hostname;
918                         document.title = mycharts[0].hostname;
919
920                         // create an array for grouping all same-type graphs together
921                         var categories = new Array();
922                         $.each(mycharts, function(i, c) {
923                                 c.chartOptions.width = width;
924                                 c.chartOptions.height = height;
925
926                                 // calculate how many point to show for each chart
927                                 c.points_to_show = Math.round(c.entries / c.group) - 1;
928
929                                 // show max 10 mins of data
930                                 if(c.points_to_show * c.group > THUMBS_MAX_TIME_TO_SHOW) c.points_to_show = THUMBS_MAX_TIME_TO_SHOW / c.group;
931
932                                 if(c.enabled) {
933                                         var j;
934                                         var h = "<div class=\"thumbgraph\" id=\"" + c.div + "_parent\"><table><tr><td><div class=\"thumbgraph\" id=\"" + c.div + "\">" + chartIsLoadingHTML(c.name, c.chartOptions.width, c.chartOptions.height) + "</div></td></tr><tr><td align=\"center\">"
935                                         + thumbChartActions(i, c)
936                                         +       "</td></tr></table></div>";
937
938                                         // find the categories object for this type
939                                         for(j = 0; j < categories.length ;j++) {
940                                                 if(categories[j].name == c.type) {
941                                                         categories[j].html += h;
942                                                         categories[j].count++;
943                                                         break;
944                                                 }
945                                         }
946
947                                         if(j == categories.length) {
948                                                 categories.push({name: c.type, title: c.category, description: '', priority: 0, count: 1, glyphicon: c.glyphicon, html: h});
949                                         }
950                                 }
951                         });
952
953                         $.each(categories, function(i, a) {
954                                      if(a.name == "system") a.priority = 1;
955                                 else if(a.name == "net") a.priority = 2;
956                                 else if(a.name == "tc") a.priority = 3;
957                                 else if(a.name == "conntrack") a.priority = 4;
958                                 else if(a.name == "ipvs") a.priority = 5;
959                                 else if(a.name == "ipv4") a.priority = 6;
960                                 else if(a.name == "cpu") a.priority = 7;
961                                 else if(a.name == "mem") a.priority = 8;
962                                 else if(a.name == "disk") a.priority = 9;
963                                 else a.priority = 99;
964
965                                 a.html = "<tr><td id=\"" + a.name + "\"><ol class=\"breadcrumb graphs\"><li class=\"active\"><span class=\"glyphicon " + a.glyphicon + "\"></span> &nbsp; <a id=\"" + a.name + "\" href=\"#" + a.name + "\"><b>" + a.title + "</b> " + a.description + " </a></li></ol></td></tr><tr><td><div class=\"thumbgraphs\">" + a.html + "</td></tr>";
966                         });
967
968                         function categoriessort(a, b) {
969                                 if(a.priority < b.priority) return -1;
970                                 return 1;
971                         }
972                         categories.sort(categoriessort);
973                         
974                         // combine all the htmls into one
975                         var allcategories = "<table width=\"100%\">";
976                         mainmenu = "<ul class=\"nav navbar-nav\"><li><a href=\"#\">Home</a></li>";
977                         $.each(categories, function(i, a) {
978                                 allcategories += a.html;
979                                 mainmenu += "<li><a href=\"#" + a.name + "\">" + a.title + "</a></li>";
980                         });
981                         allcategories += "</table>";
982                         mainmenu += "</ul>";
983
984                         thumbsContainer.innerHTML = allcategories;
985                         switchToThumbGraphs();
986                 });
987         }
988
989         </script>
990 </head>
991
992 <body role="document" data-spy="scroll" data-target="#main_menu_div">
993     <nav id="mynav" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
994       <div class="container">
995         <div class="navbar-header">
996           <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main_menu_div">
997             <span class="sr-only">Toggle navigation</span>
998             <span class="icon-bar"></span>
999             <span class="icon-bar"></span>
1000             <span class="icon-bar"></span>
1001           </button>
1002           <a class="navbar-brand" href="javascript:switchToThumbGraphs();" id="hostname_id">NetData</a>
1003         </div>
1004         <div class="collapse navbar-collapse" id="main_menu_div">
1005           <ul class="nav navbar-nav">
1006             <li><a href="#">Home</a></li>
1007           </ul>
1008         </div><!--/.nav-collapse -->
1009       </div>
1010     </nav>
1011
1012     <div class="container graphs" id="maingraph_container" style="display: none">
1013                 <table>
1014                         <tr><td>
1015                         <div class="maingraph">
1016                                         <table>
1017                                                 <tr><td>
1018                                                 <div class="maingraph" id="maingraph"></div>
1019                                                 <div id="maingraph_dashboard">
1020                                                         <div class="maingraph" id="maingraph_hidden" style="display: none"></div>
1021                                                         <div class="maingraph" id="maingraph_control"></div>
1022                                                 </div>
1023                                                 </td></tr>
1024                                                 <tr><td align="center">
1025                                                         <div class="btn-group">
1026                                                                 <form id="mainchartform">
1027                                                                         <div class="btn-group btn-group" data-toggle="tooltip" title=" click <span class='glyphicon glyphicon-play'></span> to have the graph auto-refresh, or click <span class='glyphicon glyphicon-pause'></span> to pause the graph. When paused the graph can be zoomed and panned horizontally." >
1028                                                                                 <button type="button" class="btn btn-primary mainchart_play_button" data-play-text="<span class='glyphicon glyphicon-pause'></span>" data-pause-text="<span class='glyphicon glyphicon-play'></span>" onClick="setMainChartPlay('toggle');">
1029                                                                                         <span class="glyphicon glyphicon-pause"></span>
1030                                                                                 </button>
1031                                                                         </div>
1032                                                                         <div class="btn-group btn-group" data-toggle="tooltip" title="use the maximum ( <span class='glyphicon glyphicon-signal'></span> ) or the average ( <span class='glyphicon glyphicon-align-justify'></span> ) value of grouped points" >
1033                                                                                 <button type="button" class="btn btn-primary mainchart_avg_button" data-max-text="<span class='glyphicon glyphicon-signal'></span>" data-average-text="<span class='glyphicon glyphicon-align-justify'></span>" onClick="setMainChartGroupMethod('toggle');">
1034                                                                                         <span class="glyphicon glyphicon-signal"></span>
1035                                                                                 </button>
1036                                                                         </div>
1037 <!--                                                                    <div class="btn-group btn-group" data-toggle="buttons">
1038                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="show the MAX point among all points grouped">
1039                                                                                         <input type="radio" name="groupmethod" id="groupmax" onChange="setMainChartGroupMethod('max');">
1040                                                                                         <span class="glyphicon glyphicon-signal"></span>
1041                                                                                 </label>
1042                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="show the AVERAGE of all points grouped">
1043                                                                                         <input type="radio" name="groupmethod" id="groupaverage" onChange="setMainChartGroupMethod('average');">
1044                                                                                         <span class="glyphicon glyphicon-align-justify"></span>
1045                                                                                 </label>
1046                                                                         </div>
1047 -->                                                                     <div class="btn-group btn-group" data-toggle="buttons" >
1048                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="do not group points, show the raw data">
1049                                                                                         <input type="radio" name="group" id="group1" onChange="setMainChartGroup(1);">1
1050                                                                                 </label>
1051                                                                                 
1052                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group in half, show 1 every 2 points of data">
1053                                                                                         <input type="radio" name="group" id="group2" onChange="setMainChartGroup(2);">2
1054                                                                                 </label>
1055
1056                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 5 points of data">
1057                                                                                         <input type="radio" name="group" id="group5" onChange="setMainChartGroup(5);">5
1058                                                                                 </label>
1059                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 10 points of data">
1060                                                                                         <input type="radio" name="group" id="group10" onChange="setMainChartGroup(10);">10
1061                                                                                 </label>
1062                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 15 points of data">
1063                                                                                         <input type="radio" name="group" id="group15" onChange="setMainChartGroup(15);">15
1064                                                                                 </label>
1065                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 20 points of data">
1066                                                                                         <input type="radio" name="group" id="group20" onChange="setMainChartGroup(20);">20
1067                                                                                 </label>
1068                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 30 points of data">
1069                                                                                         <input type="radio" name="group" id="group30" onChange="setMainChartGroup(30);">30
1070                                                                                 </label>
1071                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 45 points of data">
1072                                                                                         <input type="radio" name="group" id="group45" onChange="setMainChartGroup(45);">45
1073                                                                                 </label>
1074                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 60 points of data">
1075                                                                                         <input type="radio" name="group" id="group60" onChange="setMainChartGroup(60);">60
1076                                                                                 </label>
1077                                                                         </div>
1078                                                                         <div class="btn-group btn-group" data-toggle="tooltip" title="maximized ( <span class='glyphicon glyphicon-fullscreen'></span> ) or normal ( <span class='glyphicon glyphicon-resize-small'></span> ) view of the graph" >
1079                                                                                 <button type="button" class="btn btn-primary mainchart_max_button" data-maximized-text="<span class='glyphicon glyphicon-resize-small'></span>" data-normal-text="<span class='glyphicon glyphicon-fullscreen'></span>" onClick="setMainChartMax('toggle');">
1080                                                                                         <span class="glyphicon glyphicon-fullscreen"></span>
1081                                                                                 </button>
1082                                                                         </div>
1083 <!--                                                                    <div class="btn-group btn-group" data-toggle="buttons" >
1084                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="NORMAL chart size">
1085                                                                                         <input type="radio" name="chartmax" id="chartnormal" onChange="setMainChartMax('normal');">
1086                                                                                         <span class="glyphicon glyphicon-resize-small"></span>
1087                                                                                 </label>
1088                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="MAXIMIZED chart size">
1089                                                                                         <input type="radio" name="chartmax" id="chartmax" onChange="setMainChartMax('maximized');">
1090                                                                                         <span class="glyphicon glyphicon-fullscreen"></span>
1091                                                                                 </label>
1092                                                                         </div>
1093 -->                                                             </form>
1094                                                         </div>
1095                                                         </td></tr>
1096                                         </table>
1097                            </div><!-- /.maingraph -->
1098                         </td></tr>
1099                 </table>
1100         </div>
1101
1102     <div class="container graphs" id="thumbgraphs_container">
1103         <div class="allgraphs" id="thumbgraphs">
1104                 <table width="100%">
1105                         <tr><td id="splash" align="center" style="vertical-align:middle">
1106                                 <h1>
1107                                 Welcome to <b>NetData</b>!
1108                                 <br/><br/>
1109                                 <span class="glyphicon glyphicon-off"></span>
1110                                 <br/><br/>
1111                                 loading charts
1112                                 <br/><br/>
1113                                 <span class="label label-default">Please wait...</span>
1114                                 </h1>
1115                         </td></tr>
1116                 </table>
1117         </div>
1118         </div>
1119
1120     <div class="container graphs" id="groupgraphs_container">
1121         <div class="allgraphs" id="groupgraphs">
1122         </div>
1123         </div>
1124
1125     <div class="container graphs" id="groupgraphs_container">
1126         &nbsp;<p/>
1127         <hr/>
1128     <h4>NetData</h4>
1129     Realtime System Information over the web for all linux systems.
1130     <p/>
1131     Distributed under GPL.
1132     <p/>
1133     (c) 2014 Costa Tsaousis <a href="mailto:costa@tsaousis.gr">costa@tsaousis.gr</a>
1134         </div>
1135
1136         <script type='text/javascript'>
1137                 google.load('visualization', '1.1', {packages: ['controls']});
1138
1139                 $(document).ready(function(){
1140                         document.getElementById('splash').height = $(window).height();
1141
1142                         $(document).on('click','.navbar-collapse.in',function(e) {
1143                                 if( $(e.target).is('a') ) {
1144                                         $(this).collapse('hide');
1145                                 }
1146                         });
1147                         
1148                         $('body').scrollspy({ target: '#main_menu_div' })
1149                 });
1150
1151         </script>
1152 </html>