]> arthur.barton.de Git - netdata.git/blob - web/index.html
finalized new main chart with adaptive explorer on the chart
[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                         playGraphs();
405                 }
406                 else {
407                         //mainchart.chartOptions.explorer = {
408                         //      'axis': 'horizontal',
409                         //      'maxZoomOut': 1,
410                         //};
411                         //mainchart.last_updated = 0;
412                         
413                         //if(!refreshChart(mainchart, pauseGraphs))
414                         pauseGraphs();
415                 }
416         }
417
418
419         // ------------------------------------------------------------------------
420         // Chart resizing
421
422         function screenWidth() {
423                 return (($(window).width() * 0.95) - 50);
424         }
425
426         // calculate the proper width for the thumb charts
427         function thumbWidth() {
428                 var cwidth = screenWidth();
429                 var items = Math.round(cwidth / TARGET_THUMB_GRAPH_WIDTH);
430                 if(items < 1) items = 1;
431
432                 if(items > 1 && (cwidth / items) < MINIMUM_THUMB_GRAPH_WIDTH) items--;
433
434                 return Math.round(cwidth / items) - 1;
435         }
436
437         function groupChartSizes() {
438                 var s = { width: screenWidth() / 2, height: ($(window).height() - 130) / 3 - 10};
439                 if(s.width < MINIMUM_THUMB_GRAPH_WIDTH * 1.5) s.width = screenWidth();
440
441                 var count = 0;
442                 if(group_charts) $.each(group_charts, function(i, c) {
443                         if(c.enabled) count++;
444                 });
445
446                 if(count == 0) {
447                         s.width = TARGET_THUMB_GRAPH_WIDTH;
448                         s.height = TARGET_THUMB_GRAPH_HEIGHT;
449                 }
450                 if(count < 4) {
451                         s.width = screenWidth();
452                         s.height = ($(window).height() - 130) / count - 10;
453                 }
454                 else if(count == 4) {
455                         s.height = ($(window).height() - 130) / 2 - 10;
456                 }
457
458                 if(s.height < TARGET_THUMB_GRAPH_HEIGHT * 1.5)
459                         s.height = TARGET_THUMB_GRAPH_HEIGHT * 1.5;
460
461                 return s;
462         }
463
464         // resize all charts
465         // if the thumb charts need resize in their width, reset them
466         function resizeCharts() {
467                 var width = screenWidth();
468
469                 if(mainchart) {
470                         mainchart.chartOptions.width = width;
471                         mainchart.chartOptions.height = $(window).height() - 150 - MAINCHART_CONTROL_HEIGHT;
472                         mainchart.last_updated = 0;
473
474                         mainchart.hidden_wrapper.setOption('width', width);
475                         mainchart.control_wrapper.setOption('ui.chartOptions.width', width);
476                         mainchart.hiddenchart.chartOptions.width = width;
477                         mainchart.hiddenchart.last_updated = 0;
478                 }
479
480                 width = thumbWidth();
481                 $.each(mycharts, function(i, c) {
482                         if(c.enabled && c.chartOptions.width != width) {
483                                 cleanThisChart(c);
484                                 c.chartOptions.width = width;
485                                 showChartIsLoading(c.div, c.name, c.chartOptions.width, c.chartOptions.height);
486                                 c.last_updated = 0;
487                         }
488                 });
489
490                 if(group_charts) $.each(group_charts, function(i, c) {
491                         var sizes = groupChartSizes();
492
493                         if(c.enabled && (c.chartOptions.width != sizes.width || c.chartOptions.height != sizes.height)) {
494                                 cleanThisChart(c);
495                                 c.chartOptions.width = sizes.width;
496                                 c.chartOptions.height = sizes.height;
497                                 showChartIsLoading(c.div, c.name, c.chartOptions.width, c.chartOptions.height);
498                                 c.last_updated = 0;
499                         }
500                 });
501         }
502
503         var resize_request = false;
504         window.onresize = function(event) {
505                 resize_request = true;
506         };
507
508
509         // ------------------------------------------------------------------------
510         // Core of the thread refreshing the charts
511
512         var REFRESH_PAUSED = 0;
513         var REFRESH_ALWAYS = 1;
514
515         var refresh_mode = REFRESH_PAUSED;
516         var last_refresh = 0;
517         function playGraphs() {
518                 if(refresh_mode == REFRESH_ALWAYS) return;
519
520                 //mylog('PlayGraphs()');
521                 refresh_mode = REFRESH_ALWAYS;
522                 $('.mainchart_play_button').button('play');
523
524                 // check if the thread died due to a javascript error
525                 var now = new Date().getTime();
526                 if((now - last_refresh) > 5000) {
527                         // it died or never started
528                         //mylog('It seems the refresh thread died. Restarting it.');
529                         chartsRefresh();
530                 }
531         }
532
533         function pauseGraphs() {
534                 if(refresh_mode == REFRESH_PAUSED) return;
535
536                 //mylog('PauseGraphs()');
537                 refresh_mode = REFRESH_PAUSED;
538                 $('.mainchart_play_button').button('pause');
539         }
540
541         // refresh the proper chart
542         // this is an internal function.
543         // never call it directly, or new javascript threads will be spawn
544         var timeout;
545         function chartsRefresh() {
546                 if(resize_request) {
547                         resizeCharts();
548                         resize_request = false;
549                         // refresh_mode = REFRESH_ALWAYS;
550                 }
551
552                 last_refresh = new Date().getTime();
553
554                 if(refresh_mode == REFRESH_PAUSED) {
555                         if(mode == MODE_MAIN && mainchart.last_updated == 0)
556                                 mainChartRefresh();
557                         else
558                                 timeout = setTimeout(chartsRefresh, 500);
559
560                         return;
561                 }
562
563                      if(mode == MODE_THUMBS)            thumbChartsRefresh();
564                 else if(mode == MODE_GROUP_THUMBS)  groupChartsRefresh();
565                 else if(mode == MODE_MAIN)              mainChartRefresh();
566                 else                                    timeout = setTimeout(triggerRefresh, 500);
567         }
568
569         // callback for refreshing the charts later
570         // this is an internal function.
571         // never call it directly, or new javascript threads will be spawn
572         function triggerRefresh() {
573                 //mylog('triggerRefresh()');
574
575                 // cleanup has to take place when the charts are not refreshed
576                 // since the refreshing thread is in this function, it means
577                 // nothing is being refreshed.
578                 cleanupCharts();
579
580                      if(mode == MODE_THUMBS)            timeout = setTimeout(chartsRefresh, 200);
581                 else if(mode == MODE_GROUP_THUMBS)      timeout = setTimeout(chartsRefresh, 200);
582                 else if(mode == MODE_MAIN)              timeout = setTimeout(chartsRefresh, 200);
583                 else                                    timeout = setTimeout(triggerRefresh, 500);
584         }
585
586         // refresh the main chart
587         // make sure we don't loose the refreshing thread
588         function mainChartRefresh() {
589                 //mylog('mainChartRefresh()');
590
591                 if(mode != MODE_MAIN || !mainchart) {
592                         triggerRefresh();
593                         return;
594                 }
595
596                 if(!refreshChart(mainchart, hiddenChartRefresh))
597                         hiddenChartRefresh();
598         }
599
600         function hiddenChartRefresh() {
601                 refreshHiddenChart(triggerRefresh);
602         }
603
604         function roundRobinRefresh(charts, startat) {
605                 var refreshed = false;
606
607                 // find a chart to refresh
608                 var all = charts.length;
609                 var cur = startat;
610                 var count = 0;
611
612                 for(count = 0; count < all ; count++, cur++) {
613                         if(cur >= all) cur = 0;
614
615                         if(charts[cur].enabled) {
616                                 //mylog('going to refresh chart ' + charts[cur].name);
617                                 refreshed = refreshChart(charts[cur], chartsRefresh);
618                                 if(refreshed) break;
619                         }
620                 }
621
622                 if(!refreshed) triggerRefresh();
623                 return cur;
624         }
625
626         // refresh the thumb charts
627         // make sure we don't loose the refreshing thread
628         var last_thumb_updated = 0;
629         function thumbChartsRefresh() {
630                 //mylog('thumbChartsRefresh()');
631
632                 if(mycharts.length == 0 || mode != MODE_THUMBS) {
633                         triggerRefresh();
634                         return;
635                 }
636
637                 last_thumb_updated = roundRobinRefresh(mycharts, last_thumb_updated);
638         }
639
640         // refresh the group charts
641         // make sure we don't loose the refreshing thread
642         var last_group_updated = 0;
643         function groupChartsRefresh() {
644                 //mylog('groupChartsRefresh()');
645
646                 if(!group_charts || group_charts.length == 0 || mode != MODE_GROUP_THUMBS) {
647                         //mylog('cannot refresh charts');
648                         triggerRefresh();
649                         return;
650                 }
651
652                 last_group_updated = roundRobinRefresh(group_charts, last_group_updated);
653         }
654
655
656         // ------------------------------------------------------------------------
657         // switch the screen between views
658         // these should be called only from initXXXX()
659
660         function disableChart(i) {
661                 //mylog('disableChart(' + i + ')');
662
663                 var chart = null;
664
665                 var count = 0;
666                 if(mode == MODE_GROUP_THUMBS && group_charts) {
667                         $.each(group_charts, function(i, c) {
668                                 if(c.enabled) count++;
669                         });
670
671                         if(i < group_charts.length) chart = group_charts[i];
672                 }
673                 else if(mode == MODE_THUMBS) {
674                         $.each(mycharts, function(i, c) {
675                                 if(c.enabled) count++;
676                         });
677
678                         if(i < mycharts.length) chart = mycharts[i];
679                 }
680
681                 if(!chart) return;
682
683                 if(count <= 1) {
684                         alert('Cannot close the last chart shown.');
685                         return;
686                 }
687
688                 if(chart) {
689                         //mylog("disabling chart " + chart.name);
690                         chart.disablethisplease = true;
691                 }
692         }
693
694         function cleanThisChart(chart, emptydivs) {
695                 //mylog('cleanThisChart(' + chart.name + ', ' + emptydivs +')');
696
697                 if(chart.dashboard) {
698                         chart.dashboard.clear();
699                         chart.dashboard = null;
700
701                         if(chart.control_wrapper) {
702                                 chart.control_wrapper.clear();
703                                 chart.control_wrapper = null;
704                         }
705
706                         if(chart.hidden_wrapper) {
707                                 chart.hidden_wrapper.clear();
708                                 chart.hidden_wrapper = null;
709                         }
710
711                         chart.control_data = null;
712                 }
713
714                 if(chart.chart) chart.chart.clearChart();
715                 chart.chart = null;
716
717                 if(emptydivs) {
718                         var div = document.getElementById(chart.div);
719                         if(div) {
720                                 div.style.display = 'none';
721                                 div.innerHTML = "";
722                         }
723
724                         div = document.getElementById(chart.div + "_parent");
725                         if(div) {
726                                 div.style.display = 'none';
727                                 div.innerHTML = "";
728                         }
729                 }
730
731                 //mylog("chart " + chart.name + " cleaned with option " + emptydivs);
732         }
733
734         // cleanup the previously shown charts
735         function cleanupCharts() {
736                 //mylog('cleanupCharts()');
737
738                 if(mode != MODE_MAIN && mainchart) {
739                         if(mainchart.chart) cleanThisChart(mainchart);
740                         mainchart = null;
741                 }
742
743                 if(mode != MODE_GROUP_THUMBS && group_charts) {
744                         clearGroupGraphs();
745                 }
746
747                 // cleanup the disabled charts
748                 $.each(mycharts, function(i, c) {
749                         if(c.disablethisplease && c.enabled) {
750                                 cleanThisChart(c, 'emptydivs');
751                                 c.disablethisplease = false;
752                                 c.enabled = false;
753                                 resize_request = true;
754                                 //mylog("disabled chart " + c.name + " removed");
755                         }
756                 });
757
758                 if(group_charts) $.each(group_charts, function(i, c) {
759                         if(c.disablethisplease && c.enabled) {
760                                 cleanThisChart(c, 'emptydivs');
761                                 c.disablethisplease = false;
762                                 c.enabled = false;
763                                 resize_request = true;
764                                 //mylog("disabled chart " + c.name + " removed");
765                         }
766                 });
767
768                 // we never cleanup the thumb charts
769         }
770
771         function updateUI() {
772                 $('[data-toggle="tooltip"]').tooltip({'placement': 'top', 'container': 'body', 'html': true});
773
774                 $('[data-spy="scroll"]').each(function () {
775                         var $spy = $(this).scrollspy('refresh')
776                 })
777         }
778
779         var thumbsScrollPosition = null;
780         function switchToMainGraph() {
781                 //mylog('switchToMainGraph()');
782
783                 if(!mainchart) return;
784
785                 if(!group_charts) thumbsScrollPosition = window.pageYOffset;
786
787                 document.getElementById('maingraph_container').style.display = 'block';
788                 document.getElementById('thumbgraphs_container').style.display = 'none';
789                 document.getElementById('groupgraphs_container').style.display = 'none';
790
791                 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>";
792
793                 window.scrollTo(0, 0);
794
795                 mode = MODE_MAIN;
796                 playGraphs();
797                 updateUI();
798         }
799
800         function switchToThumbGraphs() {
801                 //mylog('switchToThumbGraphs()');
802
803                 document.getElementById('maingraph_container').style.display = 'none';
804                 document.getElementById('thumbgraphs_container').style.display = 'block';
805                 document.getElementById('groupgraphs_container').style.display = 'none';
806
807                 document.getElementById("main_menu_div").innerHTML = mainmenu;
808
809                 if(thumbsScrollPosition) window.scrollTo(0, thumbsScrollPosition);
810
811                 // switch mode
812                 mode = MODE_THUMBS;
813                 playGraphs();
814                 updateUI();
815         }
816
817         function switchToGroupGraphs() {
818                 //mylog('switchToGroupGraphs()');
819
820                 if(!group_charts) return;
821
822                 if(!mainchart) thumbsScrollPosition = window.pageYOffset;
823
824                 document.getElementById('maingraph_container').style.display = 'none';
825                 document.getElementById('thumbgraphs_container').style.display = 'none';
826                 document.getElementById('groupgraphs_container').style.display = 'block';
827
828                 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>";
829
830                 window.scrollTo(0, 0);
831
832                 mode = MODE_GROUP_THUMBS;
833                 playGraphs();
834                 updateUI();
835         }
836
837
838         // ------------------------------------------------------------------------
839         // Group Charts
840
841         var group_charts = null;
842         function initGroupGraphs(group) {
843                 var count = 0;
844                 
845                 if(group_charts) clearGroupGraphs();
846                 group_charts = new Array();
847
848                 var groupbody = "";
849                 $.each(mycharts, function(i, c) {
850                         if(c.group_tag == group) {
851                                 group_charts[count] = [];
852                                 group_charts[count] = $.extend(true, {}, c);
853                                 group_charts[count].div += "_group";
854                                 group_charts[count].enabled = true;
855                                 group_charts[count].chart = null;
856                                 group_charts[count].last_updated = 0;
857                                 count++;
858                         }
859                 });
860                 group_charts.sort(chartssort);
861
862                 var sizes = groupChartSizes();
863
864                 var groupbody = "";
865                 $.each(group_charts, function(i, c) {
866                         c.chartOptions.width = sizes.width;
867                         c.chartOptions.height = sizes.height;
868
869                         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>";
870                 });
871                 groupbody += "";
872
873                 document.getElementById("groupgraphs").innerHTML = groupbody;
874                 switchToGroupGraphs();
875         }
876
877         function clearGroupGraphs() {
878                 if(group_charts && group_charts.length) {
879                         $.each(group_charts, function(i, c) {
880                                 cleanThisChart(c, 'emptydivs');
881                         });
882
883                         group_charts = null;
884                 }
885
886                 document.getElementById("groupgraphs").innerHTML = "";
887         }
888
889
890         // ------------------------------------------------------------------------
891         // Global entry point
892         // initialize the thumb charts
893
894         // load the charts from the server
895         // generate html for the thumbgraphs to support them
896         function initCharts() {
897                 var width = thumbWidth();
898                 var height = TARGET_THUMB_GRAPH_HEIGHT;
899
900                 loadCharts(null, function(c) {
901                         mycharts = c;
902
903                         if(mycharts == null || mycharts.length == 0) {
904                                 alert("Cannot load data from server.");
905                                 return;
906                         }
907
908                         var thumbsContainer = document.getElementById("thumbgraphs");
909                         if(!thumbsContainer) {
910                                 alert("Cannot find the thumbsContainer");
911                                 return;
912                         }
913
914                         mycharts.sort(chartssort);
915
916                         document.getElementById('hostname_id').innerHTML = mycharts[0].hostname;
917                         document.title = mycharts[0].hostname;
918
919                         // create an array for grouping all same-type graphs together
920                         var categories = new Array();
921                         $.each(mycharts, function(i, c) {
922                                 c.chartOptions.width = width;
923                                 c.chartOptions.height = height;
924
925                                 // calculate how many point to show for each chart
926                                 c.points_to_show = Math.round(c.entries / c.group) - 1;
927
928                                 // show max 10 mins of data
929                                 if(c.points_to_show * c.group > THUMBS_MAX_TIME_TO_SHOW) c.points_to_show = THUMBS_MAX_TIME_TO_SHOW / c.group;
930
931                                 if(c.enabled) {
932                                         var j;
933                                         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\">"
934                                         + thumbChartActions(i, c)
935                                         +       "</td></tr></table></div>";
936
937                                         // find the categories object for this type
938                                         for(j = 0; j < categories.length ;j++) {
939                                                 if(categories[j].name == c.type) {
940                                                         categories[j].html += h;
941                                                         categories[j].count++;
942                                                         break;
943                                                 }
944                                         }
945
946                                         if(j == categories.length) {
947                                                 categories.push({name: c.type, title: c.category, description: '', priority: 0, count: 1, glyphicon: c.glyphicon, html: h});
948                                         }
949                                 }
950                         });
951
952                         $.each(categories, function(i, a) {
953                                      if(a.name == "system") a.priority = 1;
954                                 else if(a.name == "net") a.priority = 2;
955                                 else if(a.name == "tc") a.priority = 3;
956                                 else if(a.name == "conntrack") a.priority = 4;
957                                 else if(a.name == "ipvs") a.priority = 5;
958                                 else if(a.name == "ipv4") a.priority = 6;
959                                 else if(a.name == "cpu") a.priority = 7;
960                                 else if(a.name == "mem") a.priority = 8;
961                                 else if(a.name == "disk") a.priority = 9;
962                                 else a.priority = 99;
963
964                                 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>";
965                         });
966
967                         function categoriessort(a, b) {
968                                 if(a.priority < b.priority) return -1;
969                                 return 1;
970                         }
971                         categories.sort(categoriessort);
972                         
973                         // combine all the htmls into one
974                         var allcategories = "<table width=\"100%\">";
975                         mainmenu = "<ul class=\"nav navbar-nav\"><li><a href=\"#\">Home</a></li>";
976                         $.each(categories, function(i, a) {
977                                 allcategories += a.html;
978                                 mainmenu += "<li><a href=\"#" + a.name + "\">" + a.title + "</a></li>";
979                         });
980                         allcategories += "</table>";
981                         mainmenu += "</ul>";
982
983                         thumbsContainer.innerHTML = allcategories;
984                         switchToThumbGraphs();
985                 });
986         }
987
988         </script>
989 </head>
990
991 <body role="document" data-spy="scroll" data-target="#main_menu_div">
992     <nav id="mynav" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
993       <div class="container">
994         <div class="navbar-header">
995           <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main_menu_div">
996             <span class="sr-only">Toggle navigation</span>
997             <span class="icon-bar"></span>
998             <span class="icon-bar"></span>
999             <span class="icon-bar"></span>
1000           </button>
1001           <a class="navbar-brand" href="javascript:switchToThumbGraphs();" id="hostname_id">NetData</a>
1002         </div>
1003         <div class="collapse navbar-collapse" id="main_menu_div">
1004           <ul class="nav navbar-nav">
1005             <li><a href="#">Home</a></li>
1006           </ul>
1007         </div><!--/.nav-collapse -->
1008       </div>
1009     </nav>
1010
1011     <div class="container graphs" id="maingraph_container" style="display: none">
1012                 <table>
1013                         <tr><td>
1014                         <div class="maingraph">
1015                                         <table>
1016                                                 <tr><td>
1017                                                 <div class="maingraph" id="maingraph"></div>
1018                                                 <div id="maingraph_dashboard">
1019                                                         <div class="maingraph" id="maingraph_hidden" style="display: none"></div>
1020                                                         <div class="maingraph" id="maingraph_control"></div>
1021                                                 </div>
1022                                                 </td></tr>
1023                                                 <tr><td align="center">
1024                                                         <div class="btn-group">
1025                                                                 <form id="mainchartform">
1026                                                                         <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." >
1027                                                                                 <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');">
1028                                                                                         <span class="glyphicon glyphicon-pause"></span>
1029                                                                                 </button>
1030                                                                         </div>
1031                                                                         <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" >
1032                                                                                 <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');">
1033                                                                                         <span class="glyphicon glyphicon-signal"></span>
1034                                                                                 </button>
1035                                                                         </div>
1036 <!--                                                                    <div class="btn-group btn-group" data-toggle="buttons">
1037                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="show the MAX point among all points grouped">
1038                                                                                         <input type="radio" name="groupmethod" id="groupmax" onChange="setMainChartGroupMethod('max');">
1039                                                                                         <span class="glyphicon glyphicon-signal"></span>
1040                                                                                 </label>
1041                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="show the AVERAGE of all points grouped">
1042                                                                                         <input type="radio" name="groupmethod" id="groupaverage" onChange="setMainChartGroupMethod('average');">
1043                                                                                         <span class="glyphicon glyphicon-align-justify"></span>
1044                                                                                 </label>
1045                                                                         </div>
1046 -->                                                                     <div class="btn-group btn-group" data-toggle="buttons" >
1047                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="do not group points, show the raw data">
1048                                                                                         <input type="radio" name="group" id="group1" onChange="setMainChartGroup(1);">1
1049                                                                                 </label>
1050                                                                                 
1051                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group in half, show 1 every 2 points of data">
1052                                                                                         <input type="radio" name="group" id="group2" onChange="setMainChartGroup(2);">2
1053                                                                                 </label>
1054
1055                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 5 points of data">
1056                                                                                         <input type="radio" name="group" id="group5" onChange="setMainChartGroup(5);">5
1057                                                                                 </label>
1058                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 10 points of data">
1059                                                                                         <input type="radio" name="group" id="group10" onChange="setMainChartGroup(10);">10
1060                                                                                 </label>
1061                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 15 points of data">
1062                                                                                         <input type="radio" name="group" id="group15" onChange="setMainChartGroup(15);">15
1063                                                                                 </label>
1064                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 20 points of data">
1065                                                                                         <input type="radio" name="group" id="group20" onChange="setMainChartGroup(20);">20
1066                                                                                 </label>
1067                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 30 points of data">
1068                                                                                         <input type="radio" name="group" id="group30" onChange="setMainChartGroup(30);">30
1069                                                                                 </label>
1070                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 45 points of data">
1071                                                                                         <input type="radio" name="group" id="group45" onChange="setMainChartGroup(45);">45
1072                                                                                 </label>
1073                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="group every 60 points of data">
1074                                                                                         <input type="radio" name="group" id="group60" onChange="setMainChartGroup(60);">60
1075                                                                                 </label>
1076                                                                         </div>
1077                                                                         <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" >
1078                                                                                 <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');">
1079                                                                                         <span class="glyphicon glyphicon-fullscreen"></span>
1080                                                                                 </button>
1081                                                                         </div>
1082 <!--                                                                    <div class="btn-group btn-group" data-toggle="buttons" >
1083                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="NORMAL chart size">
1084                                                                                         <input type="radio" name="chartmax" id="chartnormal" onChange="setMainChartMax('normal');">
1085                                                                                         <span class="glyphicon glyphicon-resize-small"></span>
1086                                                                                 </label>
1087                                                                                 <label class="btn btn-primary" data-toggle="tooltip" title="MAXIMIZED chart size">
1088                                                                                         <input type="radio" name="chartmax" id="chartmax" onChange="setMainChartMax('maximized');">
1089                                                                                         <span class="glyphicon glyphicon-fullscreen"></span>
1090                                                                                 </label>
1091                                                                         </div>
1092 -->                                                             </form>
1093                                                         </div>
1094                                                         </td></tr>
1095                                         </table>
1096                            </div><!-- /.maingraph -->
1097                         </td></tr>
1098                 </table>
1099         </div>
1100
1101     <div class="container graphs" id="thumbgraphs_container">
1102         <div class="allgraphs" id="thumbgraphs">
1103                 <table width="100%">
1104                         <tr><td id="splash" align="center" style="vertical-align:middle">
1105                                 <h1>
1106                                 Welcome to <b>NetData</b>!
1107                                 <br/><br/>
1108                                 <span class="glyphicon glyphicon-off"></span>
1109                                 <br/><br/>
1110                                 loading charts
1111                                 <br/><br/>
1112                                 <span class="label label-default">Please wait...</span>
1113                                 </h1>
1114                         </td></tr>
1115                 </table>
1116         </div>
1117         </div>
1118
1119     <div class="container graphs" id="groupgraphs_container">
1120         <div class="allgraphs" id="groupgraphs">
1121         </div>
1122         </div>
1123
1124     <div class="container graphs" id="groupgraphs_container">
1125         &nbsp;<p/>
1126         <hr/>
1127     <h4>NetData</h4>
1128     Realtime System Information over the web for all linux systems.
1129     <p/>
1130     Distributed under GPL.
1131     <p/>
1132     (c) 2014 Costa Tsaousis <a href="mailto:costa@tsaousis.gr">costa@tsaousis.gr</a>
1133         </div>
1134
1135         <script type='text/javascript'>
1136                 google.load('visualization', '1.1', {packages: ['controls']});
1137
1138                 $(document).ready(function(){
1139                         document.getElementById('splash').height = $(window).height();
1140
1141                         $(document).on('click','.navbar-collapse.in',function(e) {
1142                                 if( $(e.target).is('a') ) {
1143                                         $(this).collapse('hide');
1144                                 }
1145                         });
1146                         
1147                         $('body').scrollspy({ target: '#main_menu_div' })
1148                 });
1149
1150         </script>
1151 </html>