Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / js / server_status.js
blobd8f3788576beaf9a2947d7dfe98700a7ac259447
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * @fileoverview    functions used in server status pages
4  * @name            Server Status
5  *
6  * @requires    jQuery
7  * @requires    jQueryUI
8  * @requires    jQueryCookie
9  * @requires    jQueryTablesorter
10  * @requires    Highcharts
11  * @requires    canvg
12  * @requires    js/functions.js
13  *
14  */
16 // Add a tablesorter parser to properly handle thousands seperated numbers and SI prefixes
17 $(function() {
18     jQuery.tablesorter.addParser({
19         id: "fancyNumber",
20         is: function(s) {
21             return /^[0-9]?[0-9,\.]*\s?(k|M|G|T|%)?$/.test(s);
22         },
23         format: function(s) {
24             var num = jQuery.tablesorter.formatFloat(
25                 s.replace(PMA_messages['strThousandsSeperator'],'')
26                  .replace(PMA_messages['strDecimalSeperator'],'.')
27             );
29             var factor = 1;
30             switch (s.charAt(s.length - 1)) {
31                 case '%': factor = -2; break;
32                 // Todo: Complete this list (as well as in the regexp a few lines up)
33                 case 'k': factor = 3; break;
34                 case 'M': factor = 6; break;
35                 case 'G': factor = 9; break;
36                 case 'T': factor = 12; break;
37             }
39             return num * Math.pow(10,factor);
40         },
41         type: "numeric"
42     });
45     // Popup behaviour
46     $('a[rel="popupLink"]').click( function() {
47         var $link = $(this);
49         $('.' + $link.attr('href').substr(1))
50             .show()
51             .offset({ top: $link.offset().top + $link.height() + 5, left: $link.offset().left })
52             .addClass('openedPopup');
54         return false;
55     });
57     $(document).click( function(event) {
58         $('.openedPopup').each(function() {
59             var $cnt = $(this);
60             var pos = $(this).offset();
62             // Hide if the mouseclick is outside the popupcontent
63             if(event.pageX < pos.left || event.pageY < pos.top || event.pageX > pos.left + $cnt.outerWidth() || event.pageY > pos.top + $cnt.outerHeight())
64                 $cnt.hide().removeClass('openedPopup');
65         });
66     });
67 });
69 $(function() {
70     // Filters for status variables
71     var textFilter=null;
72     var alertFilter = false;
73     var categoryFilter='';
74     var odd_row=false;
75     var text=''; // Holds filter text
76     var queryPieChart = null;
78     /* Chart configuration */
79     // Defines what the tabs are currently displaying (realtime or data)
80     var tabStatus = new Object();
81     // Holds the current chart instances for each tab
82     var tabChart = new Object();
85     /*** Table sort tooltip ***/
87     var $tableSortHint = $('<div class="dHint" style="display:none;">' + 'Click to sort' + '</div>');
88     $('body').append($tableSortHint);
90     $('table.sortable thead th').live('mouseover mouseout',function(e) {
91         if(e.type == 'mouseover') {
92             $tableSortHint
93                 .stop(true, true)
94                 .css({
95                     top: e.clientY + 15,
96                     left: e.clientX + 15
97                 })
98                 .show('fast')
99                 .data('shown',true);
100         } else {
101             $tableSortHint
102                 .stop(true, true)
103                 .hide(300,function() {
104                     $(this).data('shown',false);
105                 });
106         }
107     });
109     $(document).mousemove(function(e) {
110         if($tableSortHint.data('shown') == true)
111             $tableSortHint.css({
112                 top: e.clientY + 15,
113                 left: e.clientX + 15
114             })
115     });
118     // Tell highcarts not to use UTC dates (global setting)
119     Highcharts.setOptions({
120         global: {
121             useUTC: false
122         }
123     });
125     $.ajaxSetup({
126         cache:false
127     });
129     // Add tabs
130     $('#serverStatusTabs').tabs({
131         // Tab persistence
132         cookie: { name: 'pma_serverStatusTabs', expires: 1 },
133         // Fixes line break in the menu bar when the page overflows and scrollbar appears
134         show: function() { menuResize(); }
135     });
137     // Fixes wrong tab height with floated elements. See also http://bugs.jqueryui.com/ticket/5601
138     $(".ui-widget-content:not(.ui-tabs):not(.ui-helper-clearfix)").addClass("ui-helper-clearfix");
140     // Initialize each tab
141     $('div.ui-tabs-panel').each(function() {
142         initTab($(this),null);
143         tabStatus[$(this).attr('id')] = 'static';
144     });
146     // Display button links
147     $('div.buttonlinks').show();
149     // Handles refresh rate changing
150     $('.buttonlinks select').change(function() {
151         var chart=tabChart[$(this).parents('div.ui-tabs-panel').attr('id')];
153         // Clear current timeout and set timeout with the new refresh rate
154         clearTimeout(chart_activeTimeouts[chart.options.chart.renderTo]);
155         if(chart.options.realtime.postRequest)
156             chart.options.realtime.postRequest.abort();
158         chart.options.realtime.refreshRate = 1000*parseInt(this.value);
160         chart.xAxis[0].setExtremes(
161             new Date().getTime() - server_time_diff - chart.options.realtime.numMaxPoints * chart.options.realtime.refreshRate,
162             new Date().getTime() - server_time_diff,
163             true
164         );
166         chart_activeTimeouts[chart.options.chart.renderTo] = setTimeout(
167             chart.options.realtime.timeoutCallBack,
168             chart.options.realtime.refreshRate
169         );
170     });
172     // Ajax refresh of variables (always the first element in each tab)
173     $('.buttonlinks a.tabRefresh').click(function() {
174         // ui-tabs-panel class is added by the jquery tabs feature
175         var tab=$(this).parents('div.ui-tabs-panel');
176         var that = this;
178         // Show ajax load icon
179         $(this).find('img').show();
181         $.get($(this).attr('href'),{ajax_request:1},function(data) {
182             $(that).find('img').hide();
183             initTab(tab,data);
184         });
186         tabStatus[tab.attr('id')]='data';
188         return false;
189     });
192     /** Realtime charting of variables **/
194     // Live traffic charting
195     $('.buttonlinks a.livetrafficLink').click(function() {
196         // ui-tabs-panel class is added by the jquery tabs feature
197         var $tab=$(this).parents('div.ui-tabs-panel');
198         var tabstat = tabStatus[$tab.attr('id')];
200         if(tabstat=='static' || tabstat=='liveconnections') {
201             var settings = {
202                 series: [
203                     { name: PMA_messages['strChartKBSent'], data: [] },
204                     { name: PMA_messages['strChartKBReceived'], data: [] }
205                 ],
206                 title: { text: PMA_messages['strChartServerTraffic'] },
207                 realtime: { url:'server_status.php?' + url_query,
208                            type: 'traffic',
209                            callback: function(chartObj, curVal, lastVal, numLoadedPoints) {
210                                if(lastVal==null) return;
211                                 chartObj.series[0].addPoint(
212                                     { x: curVal.x, y: (curVal.y_sent - lastVal.y_sent) / 1024 },
213                                     false,
214                                     numLoadedPoints >= chartObj.options.realtime.numMaxPoints
215                                 );
216                                 chartObj.series[1].addPoint(
217                                     { x: curVal.x, y: (curVal.y_received - lastVal.y_received) / 1024 },
218                                     true,
219                                     numLoadedPoints >= chartObj.options.realtime.numMaxPoints
220                                 );
221                             }
222                          }
223             }
225             setupLiveChart($tab,this,settings);
226             if(tabstat == 'liveconnections')
227                 $tab.find('.buttonlinks a.liveconnectionsLink').html(PMA_messages['strLiveConnChart']);
228             tabStatus[$tab.attr('id')]='livetraffic';
229         } else {
230             $(this).html(PMA_messages['strLiveTrafficChart']);
231             setupLiveChart($tab,this,null);
232         }
234         return false;
235     });
237     // Live connection/process charting
238     $('.buttonlinks a.liveconnectionsLink').click(function() {
239         var $tab=$(this).parents('div.ui-tabs-panel');
240         var tabstat = tabStatus[$tab.attr('id')];
242         if(tabstat == 'static' || tabstat == 'livetraffic') {
243             var settings = {
244                 series: [
245                     { name: PMA_messages['strChartConnections'], data: [] },
246                     { name: PMA_messages['strChartProcesses'], data: [] }
247                 ],
248                 title: { text: PMA_messages['strChartConnectionsTitle'] },
249                 realtime: { url:'server_status.php?'+url_query,
250                            type: 'proc',
251                            callback: function(chartObj, curVal, lastVal,numLoadedPoints) {
252                                if(lastVal==null) return;
253                                 chartObj.series[0].addPoint(
254                                     { x: curVal.x, y: curVal.y_conn - lastVal.y_conn },
255                                     false,
256                                     numLoadedPoints >= chartObj.options.realtime.numMaxPoints
257                                 );
258                                 chartObj.series[1].addPoint(
259                                     { x: curVal.x, y: curVal.y_proc },
260                                     true,
261                                     numLoadedPoints >= chartObj.options.realtime.numMaxPoints
262                                 );
263                             }
264                          }
265             };
267             setupLiveChart($tab,this,settings);
268             if(tabstat == 'livetraffic')
269                 $tab.find('.buttonlinks a.livetrafficLink').html(PMA_messages['strLiveTrafficChart']);
270             tabStatus[$tab.attr('id')]='liveconnections';
271         } else {
272             $(this).html(PMA_messages['strLiveConnChart']);
273             setupLiveChart($tab,this,null);
274         }
276         return false;
277     });
279     // Live query statistics
280     $('.buttonlinks a.livequeriesLink').click(function() {
281         var $tab = $(this).parents('div.ui-tabs-panel');
282         var settings = null;
284         if(tabStatus[$tab.attr('id')] == 'static') {
285             settings = {
286                 series: [ { name: PMA_messages['strChartIssuedQueries'], data: [] } ],
287                 title: { text: PMA_messages['strChartIssuedQueriesTitle'] },
288                 tooltip: { formatter:function() { return this.point.name; } },
289                 realtime: { url:'server_status.php?'+url_query,
290                           type: 'queries',
291                           callback: function(chartObj, curVal, lastVal,numLoadedPoints) {
292                                 if(lastVal == null) return;
293                                 chartObj.series[0].addPoint(
294                                     { x: curVal.x,  y: curVal.y - lastVal.y, name: sortedQueriesPointInfo(curVal,lastVal) },
295                                     true,
296                                     numLoadedPoints >= chartObj.options.realtime.numMaxPoints
297                                 );
298                             }
299                          }
300             };
301         } else {
302             $(this).html(PMA_messages['strLiveQueryChart']);
303         }
305         setupLiveChart($tab,this,settings);
306         tabStatus[$tab.attr('id')] = 'livequeries';
307         return false;
308     });
310     function setupLiveChart($tab,link,settings) {
311         if(settings != null) {
312             // Loading a chart with existing chart => remove old chart first
313             if(tabStatus[$tab.attr('id')] != 'static') {
314                 clearTimeout(chart_activeTimeouts[$tab.attr('id') + "_chart_cnt"]);
315                 chart_activeTimeouts[$tab.attr('id')+"_chart_cnt"] = null;
316                 tabChart[$tab.attr('id')].destroy();
317                 // Also reset the select list
318                 $tab.find('.buttonlinks select').get(0).selectedIndex = 2;
319             }
321             if(! settings.chart) settings.chart = {};
322             settings.chart.renderTo = $tab.attr('id') + "_chart_cnt";
324             $tab.find('.tabInnerContent')
325                 .hide()
326                 .after('<div class="liveChart" id="' + $tab.attr('id') + '_chart_cnt"></div>');
327             tabChart[$tab.attr('id')] = PMA_createChart(settings);
328             $(link).html(PMA_messages['strStaticData']);
329             $tab.find('.buttonlinks a.tabRefresh').hide();
330             $tab.find('.buttonlinks .refreshList').show();
331         } else {
332             clearTimeout(chart_activeTimeouts[$tab.attr('id') + "_chart_cnt"]);
333             chart_activeTimeouts[$tab.attr('id') + "_chart_cnt"]=null;
334             $tab.find('.tabInnerContent').show();
335             $tab.find('div#'+$tab.attr('id') + '_chart_cnt').remove();
336             tabStatus[$tab.attr('id')]='static';
337             tabChart[$tab.attr('id')].destroy();
338             $tab.find('.buttonlinks a.tabRefresh').show();
339             $tab.find('.buttonlinks select').get(0).selectedIndex=2;
340             $tab.find('.buttonlinks .refreshList').hide();
341         }
342     }
344     /* 3 Filtering functions */
345     $('#filterAlert').change(function() {
346         alertFilter = this.checked;
347         filterVariables();
348     });
350     $('#filterText').keyup(function(e) {
351         word = $(this).val().replace('_',' ');
353         if(word.length == 0) textFilter = null;
354         else textFilter = new RegExp("(^|_)" + word,'i');
356         text = word;
358         filterVariables();
359     });
361     $('#filterCategory').change(function() {
362         categoryFilter = $(this).val();
363         filterVariables();
364     });
366     /* Adjust DOM / Add handlers to the tabs */
367     function initTab(tab,data) {
368         switch(tab.attr('id')) {
369             case 'statustabs_traffic':
370                 if(data != null) tab.find('.tabInnerContent').html(data);
371                 PMA_convertFootnotesToTooltips();
372                 break;
373             case 'statustabs_queries':
374                 if(data != null) {
375                     queryPieChart.destroy();
376                     tab.find('.tabInnerContent').html(data);
377                 }
379                 // Build query statistics chart
380                 var cdata = new Array();
381                 $.each(jQuery.parseJSON($('#serverstatusquerieschart span').html()),function(key,value) {
382                     cdata.push([key,parseInt(value)]);
383                 });
385                 queryPieChart = PMA_createChart({
386                     chart: {
387                         renderTo: 'serverstatusquerieschart'
388                     },
389                     title: {
390                         text:'',
391                         margin:0
392                     },
393                     series: [{
394                         type:'pie',
395                         name: PMA_messages['strChartQueryPie'],
396                         data: cdata
397                     }],
398                     plotOptions: {
399                         pie: {
400                             allowPointSelect: true,
401                             cursor: 'pointer',
402                             dataLabels: {
403                                 enabled: true,
404                                 formatter: function() {
405                                     return '<b>'+ this.point.name +'</b><br/> ' + Highcharts.numberFormat(this.percentage, 2) + ' %';
406                                }
407                             }
408                         }
409                     },
410                     tooltip: {
411                         formatter: function() {
412                             return '<b>' + this.point.name + '</b><br/>' + Highcharts.numberFormat(this.y, 2) + '<br/>(' + Highcharts.numberFormat(this.percentage, 2) + ' %)';
413                         }
414                     }
415                 });
416                 break;
418             case 'statustabs_allvars':
419                 if(data != null) {
420                     tab.find('.tabInnerContent').html(data);
421                     filterVariables();
422                 }
423                 break;
424         }
426         initTableSorter(tab.attr('id'));
427     }
429     function initTableSorter(tabid) {
430         switch(tabid) {
431             case 'statustabs_queries':
432                 $('#serverstatusqueriesdetails').tablesorter({
433                         sortList: [[3,1]],
434                         widgets: ['zebra'],
435                         headers: {
436                             1: { sorter: 'fancyNumber' },
437                             2: { sorter: 'fancyNumber' }
438                         }
439                     });
441                 $('#serverstatusqueriesdetails tr:first th')
442                     .append('<img class="icon sortableIcon" src="themes/dot.gif" alt="">');
444                 break;
446             case 'statustabs_allvars':
447                 $('#serverstatusvariables').tablesorter({
448                         sortList: [[0,0]],
449                         widgets: ['zebra'],
450                         headers: {
451                             1: { sorter: 'fancyNumber' }
452                         }
453                     });
455                 $('#serverstatusvariables tr:first th')
456                     .append('<img class="icon sortableIcon" src="themes/dot.gif" alt="">');
458                 break;
459         }
460     }
462     /* Filters the status variables by name/category/alert in the variables tab */
463     function filterVariables() {
464         var useful_links = 0;
465         var section = text;
467         if(categoryFilter.length > 0) section = categoryFilter;
469         if(section.length > 1) {
470             $('#linkSuggestions span').each(function() {
471                 if($(this).attr('class').indexOf('status_'+section) != -1) {
472                     useful_links++;
473                     $(this).css('display','');
474                 } else {
475                     $(this).css('display','none');
476                 }
479             });
480         }
482         if(useful_links > 0)
483             $('#linkSuggestions').css('display','');
484         else $('#linkSuggestions').css('display','none');
486         odd_row=false;
487         $('#serverstatusvariables th.name').each(function() {
488             if((textFilter == null || textFilter.exec($(this).text()))
489                 && (! alertFilter || $(this).next().find('span.attention').length>0)
490                 && (categoryFilter.length == 0 || $(this).parent().hasClass('s_'+categoryFilter))) {
491                 odd_row = ! odd_row;
492                 $(this).parent().css('display','');
493                 if(odd_row) {
494                     $(this).parent().addClass('odd');
495                     $(this).parent().removeClass('even');
496                 } else {
497                     $(this).parent().addClass('even');
498                     $(this).parent().removeClass('odd');
499                 }
500             } else {
501                 $(this).parent().css('display','none');
502             }
503         });
504     }
506     // Provides a nicely formatted and sorted tooltip of each datapoint of the query statistics
507     function sortedQueriesPointInfo(queries, lastQueries){
508         var max, maxIdx, num=0;
509         var queryKeys = new Array();
510         var queryValues = new Array();
511         var sumOther=0;
512         var sumTotal=0;
514         // Separate keys and values, then  sort them
515         $.each(queries.pointInfo, function(key,value) {
516             if(value-lastQueries.pointInfo[key] > 0) {
517                 queryKeys.push(key);
518                 queryValues.push(value-lastQueries.pointInfo[key]);
519                 sumTotal += value-lastQueries.pointInfo[key];
520             }
521         });
522         var numQueries = queryKeys.length;
523         var pointInfo = '<b>' + PMA_messages['strTotal'] + ': ' + sumTotal + '</b><br>';
525         while(queryKeys.length > 0) {
526             max = 0;
527             for(var i=0; i < queryKeys.length; i++) {
528                 if(queryValues[i] > max) {
529                     max = queryValues[i];
530                     maxIdx = i;
531                 }
532             }
533             if(numQueries > 8 && num >= 6)
534                 sumOther += queryValues[maxIdx];
535             else pointInfo += queryKeys[maxIdx].substr(4).replace('_',' ') + ': ' + queryValues[maxIdx] + '<br>';
537             queryKeys.splice(maxIdx,1);
538             queryValues.splice(maxIdx,1);
539             num++;
540         }
542         if(sumOther>0)
543             pointInfo += PMA_messages['strOther'] + ': ' + sumOther;
545         return pointInfo;
546     }
551     /**** Monitor charting implementation ****/
552     /* Saves the previous ajax response for differential values */
553     var oldChartData = null;
554     // Holds about to created chart
555     var newChart = null;
556     var chartSpacing;
558     // Runtime parameter of the monitor
559     var runtime = {
560         // Holds all visible charts in the grid
561         charts: null,
562         // Current max points per chart (needed for auto calculation)
563         gridMaxPoints: 20,
564         // displayed time frame
565         xmin: -1,
566         xmax: -1,
567         // Stores the timeout handler so it can be cleared
568         refreshTimeout: null,
569         // Stores the GET request to refresh the charts
570         refreshRequest: null,
571         // Chart auto increment
572         chartAI: 0,
573         // To play/pause the monitor
574         redrawCharts: false,
575         // Object that contains a list of nodes that need to be retrieved from the server for chart updates
576         dataList: []
577     };
579     var monitorSettings = null;
581     var defaultMonitorSettings = {
582         columns: 4,
583         chartSize: { width: 295, height: 250 },
584         // Max points in each chart. Settings it to 'auto' sets gridMaxPoints to (chartwidth - 40) / 12
585         gridMaxPoints: 'auto',
586         /* Refresh rate of all grid charts in ms */
587         gridRefresh: 5000
588     };
590     // Allows drag and drop rearrange and print/edit icons on charts
591     var editMode = false;
593     var presetCharts = {
594         'cpu-WINNT': {
595             title: PMA_messages['strSystemCPUUsage'],
596             nodes: [{ dataType: 'cpu', name: 'loadavg', unit: '%'}]
597         },
598         'memory-WINNT': {
599             title: PMA_messages['strSystemMemory'],
600             nodes: [
601                 { dataType: 'memory', name: 'MemTotal', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
602                 { dataType: 'memory', name: 'MemUsed', valueDivisor: 1024, unit: PMA_messages['strMiB']  },
603             ]
604         },
605         'swap-WINNT': {
606             title: PMA_messages['strSystemSwap'],
607             nodes: [
608                 { dataType: 'memory', name: 'SwapTotal', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
609                 { dataType: 'memory', name: 'SwapUsed', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
610             ]
611         },
612         'cpu-Linux': {
613             title: PMA_messages['strSystemCPUUsage'],
614             nodes: [
615                 { dataType: 'cpu',
616                   name: PMA_messages['strAverageLoad'],
617                   unit: '%',
618                   transformFn: function(cur, prev) {
619                       console.log('cpu-linux chart, transformFn()');
620                       console.log(cur);
621                       console.log(prev);
622                       if(prev == null) return undefined;
623                       var diff_total = cur.busy + cur.idle - (prev.busy + prev.idle);
624                       var diff_idle = cur.idle - prev.idle;
625                       return 100*(diff_total - diff_idle) / diff_total;
626                   }
627                 }
628             ]
629         },
630         'memory-Linux': {
631             title: PMA_messages['strSystemMemory'],
632             nodes: [
633                 { dataType: 'memory', name: 'MemUsed', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
634                 { dataType: 'memory', name: 'Cached',  valueDivisor: 1024, unit: PMA_messages['strMiB'] },
635                 { dataType: 'memory', name: 'Buffers', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
636                 { dataType: 'memory', name: 'MemFree', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
637             ],
638             settings: {
639                 chart: {
640                     type: 'area',
641                     animation: false
642                 },
643                 plotOptions: {
644                     area: {
645                         stacking: 'percent'
646                     }
647                 }
648             }
649         },
650         'swap-Linux': {
651             title: PMA_messages['strSystemSwap'],
652             nodes: [
653                 { dataType: 'memory', name: 'SwapUsed',   valueDivisor: 1024, unit: PMA_messages['strMiB'] },
654                 { dataType: 'memory', name: 'SwapCached', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
655                 { dataType: 'memory', name: 'SwapFree',   valueDivisor: 1024, unit: PMA_messages['strMiB'] },
656             ],
657             settings: {
658                 chart: {
659                     type: 'area',
660                     animation: false
661                 },
662                 plotOptions: {
663                     area: {
664                         stacking: 'percent'
665                     }
666                 }
667             }
668         }
669     };
671     // Default setting
672     defaultChartGrid = {
673         'c0': {  title: PMA_messages['strQuestions'],
674                  nodes: [{ dataType: 'statusvar', name: 'Questions', display: 'differential' }]
675               },
676          'c1': {
677                  title: PMA_messages['strChartConnectionsTitle'],
678                  nodes: [ { dataType: 'statusvar', name: 'Connections', display: 'differential' },
679                           { dataType: 'proc', name: 'Processes'} ]
680                },
681          'c2': {
682                  title: PMA_messages['strTraffic'],
683                  nodes: [
684                     { dataType: 'statusvar', name: 'Bytes_sent', display: 'differential', valueDivisor: 1024, unit: PMA_messages['strKiB'] },
685                     { dataType: 'statusvar', name: 'Bytes_received', display: 'differential', valueDivisor: 1024, unit: PMA_messages['strKiB'] }
686                  ]
687          }
688     };
690     // Server is localhost => We can add cpu/memory/swap
691     if(server_db_isLocal) {
692         defaultChartGrid['c3'] = presetCharts['cpu-' + server_os];
693         defaultChartGrid['c4'] = presetCharts['memory-' + server_os];
694         defaultChartGrid['c5'] = presetCharts['swap-' + server_os];
695     }
697     var gridbuttons = {
698         cogButton: {
699             //enabled: true,
700             symbol: 'url(' + pmaThemeImage  + 's_cog.png)',
701             x: -36,
702             symbolFill: '#B5C9DF',
703             hoverSymbolFill: '#779ABF',
704             _titleKey: 'settings',
705             menuName: 'gridsettings',
706             menuItems: [{
707                 textKey: 'editChart',
708                 onclick: function() {
709                     alert('tbi');
710                 }
711             }, {
712                 textKey: 'removeChart',
713                 onclick: function() {
714                     removeChart(this);
715                 }
716             }]
717         }
718     };
720     Highcharts.setOptions({
721         lang: {
722             settings:    PMA_messages['strSettings'],
723             removeChart: PMA_messages['strRemoveChart'],
724             editChart:   PMA_messages['strEditChart']
725         }
726     });
728     $('a[href="#rearrangeCharts"], a[href="#endChartEditMode"]').click(function() {
729         editMode = !editMode;
730         if($(this).attr('href') == '#endChartEditMode') editMode = false;
732         // Icon graphics have zIndex 19,20 and 21. Let's just hope nothing else has the same zIndex
733         $('table#chartGrid div svg').find('*[zIndex=20], *[zIndex=21], *[zIndex=19]').toggle(editMode)
735         $('a[href="#endChartEditMode"]').toggle(editMode);
737         if(editMode) {
738             // Close the settings popup
739             $('#statustabs_charting .popupContent').hide().removeClass('openedPopup');
741             $("#chartGrid").sortableTable({
742                 ignoreRect: {
743                     top: 8,
744                     left: chartSize().width - 63,
745                     width: 54,
746                     height: 24
747                 },
748                 events: {
749                     start: function() {
750                       //  console.log('start.');
751                     },
752                     // Drop event. The drag child element is moved into the drop element
753                     // and vice versa. So the parameters are switched.
754                     drop: function(drag, drop, pos) {
755                         var dragKey, dropKey, dropRender;
756                         var dragRender = $(drag).children().first().attr('id');
758                         if($(drop).children().length > 0)
759                             dropRender = $(drop).children().first().attr('id');
761                         // Find the charts in the array
762                         $.each(runtime.charts, function(key, value) {
763                             if(value.chart.options.chart.renderTo == dragRender)
764                                 dragKey = key;
765                             if(dropRender && value.chart.options.chart.renderTo == dropRender)
766                                 dropKey = key;
767                         });
769                         // Case 1: drag and drop are charts -> Switch keys
770                         if(dropKey) {
771                             if(dragKey) {
772                                 dragChart = runtime.charts[dragKey];
773                                 runtime.charts[dragKey] = runtime.charts[dropKey];
774                                 runtime.charts[dropKey] = dragChart;
775                             } else {
776                                 // Case 2: drop is a empty cell => just completely rebuild the ids
777                                 var keys = [];
778                                 var dropKeyNum = parseInt(dropKey.substr(1));
779                                 var insertBefore = pos.col + pos.row * monitorSettings.columns;
780                                 var values = [];
781                                 var newChartList = {};
782                                 var c = 0;
784                                 $.each(runtime.charts, function(key, value) {
785                                     if(key != dropKey)
786                                         keys.push(key);
787                                 });
789                                 keys.sort();
791                                 // Rebuilds all ids, with the dragged chart correctly inserted
792                                 for(var i=0; i<keys.length; i++) {
793                                     if(keys[i] == insertBefore) {
794                                         newChartList['c' + (c++)] = runtime.charts[dropKey];
795                                         insertBefore = -1; // Insert ok
796                                     }
797                                     newChartList['c' + (c++)] = runtime.charts[keys[i]];
798                                 }
800                                 // Not inserted => put at the end
801                                 if(insertBefore != -1)
802                                     newChartList['c' + (c++)] = runtime.charts[dropKey];
804                                 runtime.charts = newChartList;
805                             }
807                             saveMonitor();
808                         }
809                     }
810                 }
811             });
813         } else {
814             $("#chartGrid").sortableTable('destroy');
815             saveMonitor(); // Save settings
816         }
818         return false;
819     });
821     // global settings
822     $('div#statustabs_charting div.popupContent select[name="chartColumns"]').change(function() {
823         monitorSettings.columns = parseInt(this.value);
825         var newSize = chartSize();
827         // Empty cells should keep their size so you can drop onto them
828         $('table#chartGrid tr td').css('width',newSize.width + 'px');
830         /* Reorder all charts that it fills all column cells */
831         var numColumns;
832         var $tr = $('table#chartGrid tr:first');
833         var row=0;
834         while($tr.length != 0) {
835             numColumns = 1;
836             // To many cells in one row => put into next row
837             $tr.find('td').each(function() {
838                 if(numColumns > monitorSettings.columns) {
839                     if($tr.next().length == 0) $tr.after('<tr></tr>');
840                     $tr.next().prepend($(this));
841                 }
842                 numColumns++;
843             });
845             // To little cells in one row => for each cell to little, move all cells backwards by 1
846             if($tr.next().length > 0) {
847                 var cnt = monitorSettings.columns - $tr.find('td').length;
848                 for(var i=0; i < cnt; i++) {
849                     $tr.append($tr.next().find('td:first'));
850                     $tr.nextAll().each(function() {
851                         if($(this).next().length != 0)
852                             $(this).append($(this).next().find('td:first'));
853                     });
854                 }
855             }
857             $tr = $tr.next();
858             row++;
859         }
861         /* Apply new chart size to all charts */
862         $.each(runtime.charts, function(key, value) {
863             value.chart.setSize(
864                 newSize.width,
865                 newSize.height,
866                 false
867             );
868         });
870         if(monitorSettings.gridMaxPoints == 'auto')
871             runtime.gridMaxPoints = Math.round((newSize.width - 40) / 12);
873         runtime.xmin = new Date().getTime() - server_time_diff - runtime.gridMaxPoints * monitorSettings.gridRefresh;
874         runtime.xmax = new Date().getTime() - server_time_diff + monitorSettings.gridRefresh;
876         if(editMode)
877             $("#chartGrid").sortableTable('refresh');
879         saveMonitor(); // Save settings
880     });
882     $('div#statustabs_charting div.popupContent select[name="gridChartRefresh"]').change(function() {
883         monitorSettings.gridRefresh = parseInt(this.value) * 1000;
884         clearTimeout(runtime.refreshTimeout);
886         if(runtime.refreshRequest)
887             runtime.refreshRequest.abort();
889         runtime.xmin = new Date().getTime() - server_time_diff - runtime.gridMaxPoints * monitorSettings.gridRefresh;
890         runtime.xmax = new Date().getTime() - server_time_diff + monitorSettings.gridRefresh;
892         $.each(runtime.charts, function(key, value) {
893             value.chart.xAxis[0].setExtremes(runtime.xmin, runtime.xmax, false);
894         });
896         runtime.refreshTimeout = setTimeout(refreshChartGrid, monitorSettings.gridRefresh);
898         saveMonitor(); // Save settings
899     });
901     $('a[href="#addNewChart"]').click(function() {
902         var dlgButtons = { };
904         dlgButtons[PMA_messages['strAddChart']] = function() {
905             var type = $('input[name="chartType"]:checked').val();
907             if(type == 'cpu' || type == 'memory' || type=='swap')
908                 newChart = presetCharts[type + '-' + server_os];
909             else {
910                 if(! newChart || ! newChart.nodes || newChart.nodes.length == 0) {
911                     alert(PMA_messages['strAddOneSeriesWarning']);
912                     return;
913                 }
914             }
916             newChart.title = $('input[name="chartTitle"]').attr('value');
917             // Add a cloned object to the chart grid
918             addChart($.extend(true, {}, newChart));
920             newChart = null;
922             saveMonitor(); // Save settings
924             $(this).dialog("close");
925         }
927         dlgButtons[PMA_messages['strClose']] = function() {
928             newChart = null;
929             $('span#clearSeriesLink').hide();
930             $('#seriesPreview').html('');
931             $(this).dialog("close");
932         }
934         $('div#addChartDialog').dialog({
935             width:'auto',
936             height:'auto',
937             buttons: dlgButtons
938         });
940         $('div#addChartDialog #seriesPreview').html('<i>' + PMA_messages['strNone'] + '</i>');
942         return false;
943     });
945     $('a[href="#pauseCharts"]').click(function() {
946         runtime.redrawCharts = ! runtime.redrawCharts;
947         if(! runtime.redrawCharts)
948             $(this).html('<img src="themes/dot.gif" class="icon ic_play" alt="" /> ' + PMA_messages['strResumeMonitor']);
949         else {
950             $(this).html('<img src="themes/dot.gif" class="icon ic_pause" alt="" /> ' + PMA_messages['strPauseMonitor']);
951             if(runtime.charts == null) {
952                 initGrid();
953                 $('a[href="#settingsPopup"]').show();
954             }
955         }
956         return false;
957     });
959     $('a[href="#monitorInstructionsDialog"]').click(function() {
960         var $dialog = $('div#monitorInstructionsDialog');
962         $dialog.dialog({
963             width: 595,
964             height: 'auto'
965         }).find('img.ajaxIcon').show();
967         var loadLogVars = function(getvars) {
968             vars = { ajax_request: true, logging_vars: true };
969             if(getvars) $.extend(vars,getvars);
971             $.get('server_status.php?' + url_query, vars,
972                 function(data) {
973                     var logVars = $.parseJSON(data),
974                         icon = 'ic_s_success', msg='', str='';
976                     if(logVars['general_log'] == 'ON') {
977                         if(logVars['slow_query_log'] == 'ON')
978                             msg = PMA_messages['strBothLogOn'];
979                         else
980                             msg = PMA_messages['strGenLogOn'];
981                     }
983                     if(msg.length == 0 && logVars['slow_query_log'] == 'ON') {
984                         msg = PMA_messages['strSlowLogOn'];
985                     }
987                     if(msg.length == 0) {
988                         icon = 'ic_s_error';
989                         msg = PMA_messages['strBothLogOff'];
990                     }
992                     str = '<b>' + PMA_messages['strCurrentSettings'] + '</b><br><div class="smallIndent">';
993                     str += '<img src="themes/dot.gif" class="icon ' + icon + '" alt=""/> ' + msg + '<br />';
995                     if(logVars['log_output'] != 'TABLE')
996                         str += '<img src="themes/dot.gif" class="icon ic_s_error" alt=""/> ' + PMA_messages['strLogOutNotTable'] + '<br />';
997                     else
998                         str += '<img src="themes/dot.gif" class="icon ic_s_success" alt=""/> ' + PMA_messages['strLogOutIsTable'] + '<br />';
1000                     if(logVars['slow_query_log'] == 'ON') {
1001                         if(logVars['long_query_time'] > 2)
1002                             str += '<img src="themes/dot.gif" class="icon ic_s_attention" alt=""/> '
1003                                 + $.sprintf(PMA_messages['strSmallerLongQueryTimeAdvice'], logVars['long_query_time'])
1004                                 + '<br />';
1006                         if(logVars['long_query_time'] < 2)
1007                             str += '<img src="themes/dot.gif" class="icon ic_s_success" alt=""/> '
1008                                 + $.sprintf(PMA_messages['strLongQueryTimeSet'], logVars['long_query_time'])
1009                                 + '<br />';
1010                     }
1012                     str += '</div>';
1014                     if(is_superuser) {
1015                         str += '<p></p><b>Change settings</b>';
1016                         str += '<div class="smallIndent">';
1017                         str += PMA_messages['strSettingsAppliedGlobal'] + '<br/>';
1019                         var varValue = 'TABLE';
1020                         if(logVars['log_output'] == 'TABLE') varValue = 'FILE';
1022                         str += '- <a class="set" href="#log_output-' + varValue + '">'
1023                             + $.sprintf(PMA_messages['strSetLogOutput'], varValue)
1024                             + ' </a><br />';
1026                         if(logVars['general_log'] != 'ON')
1027                             str += '- <a class="set" href="#general_log-ON">'
1028                                 + $.sprintf(PMA_messages['strEnableVar'], 'general_log')
1029                                 + ' </a><br />';
1030                         else
1031                             str += '- <a class="set" href="#general_log-OFF">'
1032                                 + $.sprintf(PMA_messages['strDisableVar'], 'general_log')
1033                                 + ' </a><br />';
1035                         if(logVars['slow_query_log'] != 'ON')
1036                             str += '- <a class="set" href="#slow_query_log-ON">'
1037                                 +  $.sprintf(PMA_messages['strEnableVar'], 'slow_query_log')
1038                                 + ' </a><br />';
1039                         else
1040                             str += '- <a class="set" href="#slow_query_log-OFF">'
1041                                 +  $.sprintf(PMA_messages['strDisableVar'], 'slow_query_log')
1042                                 + ' </a><br />';
1045                         varValue = 5;
1046                         if(logVars['long_query_time'] > 2) varValue = 1;
1048                         str += '- <a class="set" href="#long_query_time-' + varValue + '">'
1049                             + $.sprintf(PMA_messages['setSetLongQueryTime'], varValue)
1050                             + ' </a><br />';
1052                     } else
1053                         str += PMA_messages['strNoSuperUser'] + '<br/>';
1055                     str += '</div>';
1057                     $dialog.find('div.monitorUse').toggle(
1058                         logVars['log_output'] == 'TABLE' && (logVars['slow_query_log'] == 'ON' || logVars['general_log'] == 'ON')
1059                     );
1061                     $dialog.find('div.ajaxContent').html(str);
1062                     $dialog.find('img.ajaxIcon').hide();
1063                     $dialog.find('a.set').click(function() {
1064                         var nameValue = $(this).attr('href').split('-');
1065                         loadLogVars({ varName: nameValue[0].substr(1), varValue: nameValue[1]});
1066                         $dialog.find('img.ajaxIcon').show();
1067                     });
1068                 }
1069             );
1070         }
1073         loadLogVars();
1075         return false;
1076     });
1078     $('input[name="chartType"]').change(function() {
1079         $('#chartVariableSettings').toggle(this.checked && this.value == 'variable');
1080         var title = $('input[name="chartTitle"]').attr('value');
1081         if(title == PMA_messages['strChartTitle'] || title == $('label[for="'+$('input[name="chartTitle"]').data('lastRadio')+'"]').text()) {
1082             $('input[name="chartTitle"]').data('lastRadio',$(this).attr('id'));
1083             $('input[name="chartTitle"]').attr('value',$('label[for="'+$(this).attr('id')+'"]').text());
1084         }
1086     });
1088     $('input[name="useDivisor"]').change(function() {
1089         $('span.divisorInput').toggle(this.checked);
1090     });
1091     $('input[name="useUnit"]').change(function() {
1092         $('span.unitInput').toggle(this.checked);
1093     });
1095     $('select[name="varChartList"]').change(function () {
1096         if(this.selectedIndex!=0)
1097             $('#variableInput').attr('value',this.value);
1098     });
1100     $('a[href="#kibDivisor"]').click(function() {
1101         $('input[name="valueDivisor"]').attr('value',1024);
1102         $('input[name="valueUnit"]').attr('value',PMA_messages['strKiB']);
1103         $('span.unitInput').toggle(true);
1104         $('input[name="useUnit"]').prop('checked',true);
1105         return false;
1106     });
1108     $('a[href="#mibDivisor"]').click(function() {
1109         $('input[name="valueDivisor"]').attr('value',1024*1024);
1110         $('input[name="valueUnit"]').attr('value',PMA_messages['strMiB']);
1111         $('span.unitInput').toggle(true);
1112         $('input[name="useUnit"]').prop('checked',true);
1113         return false;
1114     });
1116     $('a[href="#submitClearSeries"]').click(function() {
1117         $('#seriesPreview').html('<i>' + PMA_messages['strNone'] + '</i>');
1118         newChart = null;
1119         $('span#clearSeriesLink').hide();
1120     });
1122     $('a[href="#submitAddSeries"]').click(function() {
1123         if($('input#variableInput').attr('value').length == 0) return false;
1125         if(newChart == null) {
1126             $('#seriesPreview').html('');
1128             newChart = {
1129                 title: $('input[name="chartTitle"]').attr('value'),
1130                 nodes: []
1131             }
1132         }
1134         var serie = {
1135             dataType:'statusvar',
1136             name: $('input#variableInput').attr('value'),
1137             display: $('input[name="differentialValue"]').attr('checked') ? 'differential' : ''
1138         };
1140         if(serie.name == 'Processes') serie.dataType='proc';
1142         if($('input[name="useDivisor"]').attr('checked'))
1143             serie.valueDivisor = parseInt($('input[name="valueDivisor"]').attr('value'));
1145         if($('input[name="useUnit"]').attr('checked'))
1146             serie.unit = $('input[name="valueUnit"]').attr('value');
1150         var str = serie.display == 'differential' ? ', ' + PMA_messages['strDifferential'] : '';
1151         str += serie.valueDivisor ? (', ' + $.sprintf(PMA_messages['strDividedBy'], serie.valueDivisor)) : '';
1153         $('#seriesPreview').append('- ' + serie.name + str + '<br>');
1155         newChart.nodes.push(serie);
1157         $('input#variableInput').attr('value','');
1158         $('input[name="differentialValue"]').attr('checked',true);
1159         $('input[name="useDivisor"]').attr('checked',false);
1160         $('input[name="useUnit"]').attr('checked',false);
1161         $('input[name="useDivisor"]').trigger('change');
1162         $('input[name="useUnit"]').trigger('change');
1163         $('select[name="varChartList"]').get(0).selectedIndex=0;
1165         $('span#clearSeriesLink').show();
1167         return false;
1168     });
1170     $("input#variableInput").autocomplete({
1171             source: variableNames
1172     });
1175     function initGrid() {
1176         var settings;
1177         var series;
1179         /* Apply default values & config */
1180         if(window.localStorage) {
1181             if(window.localStorage['monitorCharts'])
1182                 runtime.charts = $.parseJSON(window.localStorage['monitorCharts']);
1183             if(window.localStorage['monitorSettings'])
1184                 monitorSettings = $.parseJSON(window.localStorage['monitorSettings']);
1186             $('a[href="#clearMonitorConfig"]').toggle(runtime.charts != null);
1187         }
1189         if(runtime.charts == null)
1190             runtime.charts = defaultChartGrid;
1191         if(monitorSettings == null)
1192             monitorSettings = defaultMonitorSettings;
1194         $('select[name="gridChartRefresh"]').attr('value',monitorSettings.gridRefresh / 1000);
1195         $('select[name="chartColumns"]').attr('value',monitorSettings.columns);
1197         if(monitorSettings.gridMaxPoints == 'auto')
1198             runtime.gridMaxPoints = Math.round((monitorSettings.chartSize.width - 40) / 12);
1199         else
1200             runtime.gridMaxPoints = monitorSettings.gridMaxPoints;
1202         runtime.xmin = new Date().getTime() - server_time_diff - runtime.gridMaxPoints * monitorSettings.gridRefresh;
1203         runtime.xmax = new Date().getTime() - server_time_diff + monitorSettings.gridRefresh;
1205         /* Calculate how much spacing there is between each chart */
1206         $('table#chartGrid').html('<tr><td></td><td></td></tr><tr><td></td><td></td></tr>');
1207         chartSpacing = {
1208             width: $('table#chartGrid td:nth-child(2)').offset().left - $('table#chartGrid td:nth-child(1)').offset().left,
1209             height: $('table#chartGrid tr:nth-child(2) td:nth-child(2)').offset().top - $('table#chartGrid tr:nth-child(1) td:nth-child(1)').offset().top
1210         }
1211         $('table#chartGrid').html('');
1213         /* Add all charts - in correct order */
1214         var keys = [];
1215         $.each(runtime.charts, function(key, value) {
1216             keys.push(key);
1217         });
1218         keys.sort();
1219         for(var i=0; i<keys.length; i++)
1220             addChart(runtime.charts[keys[i]],true);
1222         /* Fill in missing cells */
1223         var numCharts = $('table#chartGrid .monitorChart').length;
1224         var numMissingCells = (monitorSettings.columns - numCharts % monitorSettings.columns) % monitorSettings.columns;
1225         for(var i=0; i < numMissingCells; i++) {
1226             $('table#chartGrid tr:last').append('<td></td>');
1227         }
1229         // Empty cells should keep their size so you can drop onto them
1230         $('table#chartGrid tr td').css('width',chartSize().width + 'px');
1233         buildRequiredDataList();
1234         refreshChartGrid();
1235     }
1237     function chartSize() {
1238         var wdt = $('div#logTable').innerWidth() / monitorSettings.columns - (monitorSettings.columns - 1) * chartSpacing.width;
1239         return {
1240             width: wdt,
1241             height: 0.75 * wdt
1242         }
1243     }
1245     function addChart(chartObj, initialize) {
1246         series = [];
1247         for(var j=0; j<chartObj.nodes.length; j++)
1248             series.push(chartObj.nodes[j]);
1250         settings = {
1251             chart: {
1252                 renderTo: 'gridchart' + runtime.chartAI,
1253                 width: chartSize().width,
1254                 height: chartSize().height,
1255                 marginRight: 5,
1256                 zoomType: 'x',
1257                 events: {
1258                     selection: function(event) {
1259                         if(editMode) return false;
1261                         var extremesObject = event.xAxis[0],
1262                             min = extremesObject.min,
1263                             max = extremesObject.max;
1265                         $('#logAnalyseDialog input[name="dateStart"]')
1266                             .attr('value', Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', new Date(min)));
1267                         $('#logAnalyseDialog input[name="dateEnd"]')
1268                             .attr('value', Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', new Date(max)));
1270                         var dlgBtns = { };
1272                         dlgBtns[PMA_messages['strFromSlowLog']] = function() {
1273                             var dateStart = Date.parse($('#logAnalyseDialog input[name="dateStart"]').attr('value')) || min;
1274                             var dateEnd = Date.parse($('#logAnalyseDialog input[name="dateEnd"]').attr('value')) || max;
1276                             loadLogStatistics({
1277                                 src: 'slow',
1278                                 start: dateStart,
1279                                 end: dateEnd,
1280                                 removeVariables: $('input#removeVariables').prop('checked'),
1281                                 limitTypes: $('input#limitTypes').prop('checked')
1282                             });
1284                             $('#logAnalyseDialog').find('dateStart,dateEnd').datepicker('destroy');
1286                             $(this).dialog("close");
1287                         }
1289                         dlgBtns[PMA_messages['strFromGeneralLog']] = function() {
1290                             var dateStart = Date.parse($('#logAnalyseDialog input[name="dateStart"]').attr('value')) || min;
1291                             var dateEnd = Date.parse($('#logAnalyseDialog input[name="dateEnd"]').attr('value')) || max;
1293                             loadLogStatistics({
1294                                 src: 'general',
1295                                 start: dateStart,
1296                                 end: dateEnd,
1297                                 removeVariables: $('input#removeVariables').prop('checked'),
1298                                 limitTypes: $('input#limitTypes').prop('checked')
1299                             });
1301                             $('#logAnalyseDialog').find('dateStart,dateEnd').datepicker('destroy');
1303                             $(this).dialog("close");
1304                         }
1306                         $('#logAnalyseDialog').dialog({
1307                             width: 'auto',
1308                             height: 'auto',
1309                             buttons: dlgBtns
1310                         });
1312                         return false;
1313                     }
1314                 }
1315             },
1316             xAxis: {
1317                 min: runtime.xmin,
1318                 max: runtime.xmax
1319             },
1321             yAxis: {
1322                 title: {
1323                     text: ''
1324                 }
1325             },
1326             tooltip: {
1327                 formatter: function() {
1328                         var s = '<b>'+Highcharts.dateFormat('%H:%M:%S', this.x)+'</b>';
1330                         $.each(this.points, function(i, point) {
1331                             s += '<br/><span style="color:'+point.series.color+'">'+ point.series.name +':</span> '+
1332                                 ((parseInt(point.y) == point.y) ? point.y : Highcharts.numberFormat(this.y, 2)) + ' ' + (point.series.options.unit || '');
1333                         });
1335                         return s;
1336                 },
1337                 shared: true
1338             },
1339             legend: {
1340                 enabled: false
1341             },
1342             series: series,
1343             buttons: gridbuttons,
1344             title: { text: chartObj.title }
1345         };
1347         if(chartObj.settings)
1348             $.extend(true,settings,chartObj.settings);
1350         if($('#'+settings.chart.renderTo).length==0) {
1351             var numCharts = $('table#chartGrid .monitorChart').length;
1353             if(numCharts == 0 || !( numCharts % monitorSettings.columns))
1354                 $('table#chartGrid').append('<tr></tr>');
1356             $('table#chartGrid tr:last').append('<td><div class="ui-state-default monitorChart" id="'+settings.chart.renderTo+'"></div></td>');
1357         }
1359         chartObj.chart = PMA_createChart(settings);
1360         chartObj.numPoints = 0;
1362         if(initialize != true) {
1363             runtime.charts['c'+runtime.chartAI] = chartObj;
1364             buildRequiredDataList();
1365         }
1367         // Edit,Print icon only in edit mode
1368         $('table#chartGrid div svg').find('*[zIndex=20], *[zIndex=21], *[zIndex=19]').toggle(editMode)
1370         runtime.chartAI++;
1371     }
1373     function removeChart(chartObj) {
1374         var htmlnode = chartObj.options.chart.renderTo;
1375         if(! htmlnode ) return;
1378         $.each(runtime.charts, function(key, value) {
1379             if(value.chart.options.chart.renderTo == htmlnode) {
1380                 delete runtime.charts[key];
1381                 return false;
1382             }
1383         });
1385         buildRequiredDataList();
1387         // Using settimeout() because clicking the remove link fires an onclick event
1388         // which throws an error when the chart is destroyed
1389         setTimeout(function() {
1390             chartObj.destroy();
1391             $('li#' + htmlnode).remove();
1392         },10);
1394         saveMonitor(); // Save settings
1395     }
1397     function refreshChartGrid() {
1398         /* Send to server */
1399         runtime.refreshRequest = $.post('server_status.php?'+url_query, { ajax_request: true, chart_data: 1, type: 'chartgrid', requiredData: $.toJSON(runtime.dataList) },function(data) {
1400             var chartData = $.parseJSON(data);
1401             var value, i=0;
1402             var diff;
1404             /* Update values in each graph */
1405             $.each(runtime.charts, function(orderKey, elem) {
1406                 var key = elem.chartID;
1407                 // If newly added chart, we have no data for it yet
1408                 if(! chartData[key]) return;
1409                 // Draw all points
1410                 for(var j=0; j < elem.nodes.length; j++) {
1411                     value = chartData[key][j].y;
1413                     if(i==0 && j==0) {
1414                         if(oldChartData==null) diff = chartData.x - runtime.xmax;
1415                         else diff = parseInt(chartData.x - oldChartData.x);
1417                         runtime.xmin+= diff;
1418                         runtime.xmax+= diff;
1419                     }
1421                     elem.chart.xAxis[0].setExtremes(runtime.xmin, runtime.xmax, false);
1423                     if(elem.nodes[j].display == 'differential') {
1424                         if(oldChartData == null || oldChartData[key] == null) continue;
1425                         value -= oldChartData[key][j].y;
1426                     }
1428                     if(elem.nodes[j].valueDivisor)
1429                         value = value / elem.nodes[j].valueDivisor;
1431                     if(elem.nodes[j].transformFn) {
1432                         value = elem.nodes[j].transformFn(
1433                             chartData[key][j],
1434                             (oldChartData == null) ? null : oldChartData[key][j]
1435                         );
1436                     }
1438                     if(value != undefined)
1439                         elem.chart.series[j].addPoint(
1440                             {  x: chartData.x, y: value },
1441                             false,
1442                             elem.numPoints >= runtime.gridMaxPoints
1443                         );
1444                 }
1446                 i++;
1448                 runtime.charts[orderKey].numPoints++;
1449                 if(runtime.redrawCharts)
1450                     elem.chart.redraw();
1451             });
1453             oldChartData = chartData;
1455             runtime.refreshTimeout = setTimeout(refreshChartGrid, monitorSettings.gridRefresh);
1456         });
1457     }
1459     /* Build list of nodes that need to be retrieved */
1460     function buildRequiredDataList() {
1461         runtime.dataList = {};
1462         // Store an own id, because the property name is subject of reordering, thus destroying our mapping with runtime.charts <=> runtime.dataList
1463         var chartID = 0;
1464         $.each(runtime.charts, function(key, chart) {
1465             runtime.dataList[chartID] = chart.nodes;
1466             runtime.charts[key].chartID = chartID;
1467             chartID++;
1468         });
1469     }
1471     function loadLogStatistics(opts) {
1472         var tableStr = '';
1473         var logRequest = null;
1475         if(! opts.removeVariables)
1476             opts.removeVariables = false;
1477         if(! opts.limitTypes)
1478             opts.limitTypes = false;
1480         $('#loadingLogsDialog').html(PMA_messages['strAnalysingLogs'] + ' <img class="ajaxIcon" src="' + pmaThemeImage + 'ajax_clock_small.gif" alt="">');
1482         $('#loadingLogsDialog').dialog({
1483             width: 'auto',
1484             height: 'auto',
1485             buttons: {
1486                 'Cancel request': function() {
1487                     if(logRequest != null)
1488                         logRequest.abort();
1490                     $(this).dialog("close");
1491                 }
1492             }
1493         });
1496         logRequest = $.get('server_status.php?'+url_query,
1497             {   ajax_request: true,
1498                 log_data: 1,
1499                 type: opts.src,
1500                 time_start: Math.round(opts.start / 1000),
1501                 time_end: Math.round(opts.end / 1000),
1502                 removeVariables: opts.removeVariables,
1503                 limitTypes: opts.limitTypes
1504             },
1505             function(data) {
1506                 var logData = $.parseJSON(data);
1508                 if(logData.rows.length != 0) {
1509                     runtime.logDataCols = buildLogTable(logData);
1511                     /* Show some stats in the dialog */
1512                     $('#loadingLogsDialog').html('<p>' + PMA_messages['strLogDataLoaded'] + '</p>');
1513                     $.each(logData.sum, function(key, value) {
1514                         key = key.charAt(0).toUpperCase() + key.slice(1).toLowerCase();
1515                         if(key == 'Total') key = '<b>' + key + '</b>';
1516                         $('#loadingLogsDialog').append(key + ': ' + value + '<br/>');
1517                     });
1519                     /* Add filter options if more than a bunch of rows there to filter */
1520                     if(logData.numRows > 12) {
1521                         $('div#logTable').prepend(
1522                             '<fieldset id="logDataFilter">' +
1523                             '   <legend>' + PMA_messages['strFilters'] + '</legend>' +
1524                             '   <div class="formelement">' +
1525                             '           <label for="filterQueryText">' + PMA_messages['strFilterByWordRegexp'] + '</label>' +
1526                             '           <input name="filterQueryText" type="text" id="filterQueryText" style="vertical-align: baseline;" />' +
1527                             '   </div>' +
1528                             ((logData.numRows > 250) ? ' <div class="formelement"><button name="startFilterQueryText" id="startFilterQueryText">' + PMA_messages['strFilter'] + '</button></div>' : '') +
1529                             '   <div class="formelement">' +
1530                             '       <input type="checkbox" id="noWHEREData" name="noWHEREData" value="1" /> ' +
1531                             '       <label for="noWHEREData"> ' + PMA_messages['strIgnoreWhereAndGroup'] + '</label>' +
1532                             '   </div' +
1533                             '</fieldset>'
1534                         );
1536                         $('div#logTable input#noWHEREData').change(function() {
1537                             filterQueries(true);
1538                         });
1540                         //preg_replace('/\s+([^=]+)=(\d+|((\'|"|)(?U)(.+)(?<!\\\)\4(\s+|$)))/i',' $1={} ',$str);
1542                         if(logData.numRows > 250) {
1543                             $('div#logTable button#startFilterQueryText').click(filterQueries);
1544                         } else {
1545                             $('div#logTable input#filterQueryText').keyup(filterQueries);
1546                         }
1548                     }
1550                     var dlgBtns = {};
1551                     dlgBtns[PMA_messages['strJumpToTable']] = function() {
1552                         $(this).dialog("close");
1553                         $(document).scrollTop($('div#logTable').offset().top);
1554                     }
1556                     $('#loadingLogsDialog').dialog( "option", "buttons", dlgBtns);
1558                 } else {
1559                     $('#loadingLogsDialog').html('<p>' + PMA_messages['strNoDataFound'] + '</p>');
1561                     var dlgBtns = {};
1562                     dlgBtns[PMA_messages['strClose']] = function() {
1563                         $(this).dialog("close");
1564                     }
1566                     $('#loadingLogsDialog').dialog( "option", "buttons", dlgBtns );
1567                 }
1568             }
1569         );
1571         function filterQueries(varFilterChange) {
1572             var odd_row=false, cell, textFilter;
1573             var val = $('div#logTable input#filterQueryText').val();
1575             if(val.length == 0) textFilter = null;
1576             else textFilter = new RegExp(val, 'i');
1578             var rowSum = 0, totalSum = 0;
1580             var i=0, q;
1581             var noVars = $('div#logTable input#noWHEREData').attr('checked');
1582             var equalsFilter = /([^=]+)=(\d+|((\'|"|).*?[^\\])\4((\s+)|$))/gi;
1583             var functionFilter = /([a-z0-9_]+)\(.+?\)/gi;
1584             var filteredQueries = {};
1585             var filteredQueriesLines = {};
1586             var hide = false;
1587             var queryColumnName = runtime.logDataCols[runtime.logDataCols.length - 2];
1588             var sumColumnName = runtime.logDataCols[runtime.logDataCols.length - 1];
1590             // We just assume the sql text is always in the second last column, and that the total count is right of it
1591             $('div#logTable table tbody tr td:nth-child(' + (runtime.logDataCols.length - 1) + ')').each(function() {
1592                 if(varFilterChange && $(this).html().match(/^SELECT/i)) {
1593                     if(noVars) {
1594                         q = $(this).text().replace(equalsFilter, '$1=...$6').trim();
1595                         q = q.replace(functionFilter, ' $1(...)');
1597                         // Js does not specify a limit on property name length, so we can abuse it as index :-)
1598                         if(filteredQueries[q]) {
1599                             filteredQueries[q] += parseInt($(this).next().text());
1600                             totalSum += parseInt($(this).next().text());
1601                             hide = true;
1602                         } else {
1603                             filteredQueries[q] = parseInt($(this).next().text());;
1604                             filteredQueriesLines[q] = i;
1605                             $(this).text(q);
1606                         }
1608                     } else {
1609                         $(this).text($(this).parent().data('query')[queryColumnName]);
1610                         $(this).next().text($(this).parent().data('query')[sumColumnName]);
1611                     }
1612                 }
1614                 if(! hide && (textFilter != null && ! textFilter.exec($(this).text()))) hide = true;
1616                 if(hide) {
1617                     $(this).parent().css('display','none');
1618                 } else {
1619                     totalSum += parseInt($(this).next().text());
1620                     rowSum ++;
1622                     odd_row = ! odd_row;
1623                     $(this).parent().css('display','');
1624                     if(odd_row) {
1625                         $(this).parent().addClass('odd');
1626                         $(this).parent().removeClass('even');
1627                     } else {
1628                         $(this).parent().addClass('even');
1629                         $(this).parent().removeClass('odd');
1630                     }
1631                 }
1633                 hide = false;
1634                 i++;
1635             });
1637             if(varFilterChange) {
1638                 if(noVars) {
1639                     $.each(filteredQueriesLines, function(key,value) {
1640                         if(filteredQueries[value] <= 1) return;
1642                         var numCol = $('div#logTable table tbody tr:nth-child(' + (value+1) + ')')
1643                                         .children(':nth-child(' + (runtime.logDataCols.length) + ')');
1645                         numCol.text(filteredQueries[key]);
1646                     });
1647                 }
1649                 $('div#logTable table').trigger("update");
1650                 setTimeout(function() {
1652                     $('div#logTable table').trigger('sorton',[[[runtime.logDataCols.length - 1,1]]]);
1653                 }, 0);
1654             }
1656             $('div#logTable table tfoot tr')
1657                 .html('<th colspan="' + (runtime.logDataCols.length - 1) + '">' +
1658                       PMA_messages['strSumRows'] + ' '+ rowSum +'<span style="float:right">' +
1659                       PMA_messages['strTotal'] + '</span></th><th align="right">' + totalSum + '</th>');
1660         }
1661     }
1663     /*loadLogStatistics({
1664         src: 'general',
1665         start:1311076210*1000,
1666         end:1311162689*1000,
1667         removeVariables: true,
1668         limitTypes: true
1669     });*/
1671     function buildLogTable(data) {
1672         var rows = data.rows;
1673         var cols = new Array();
1674         var $table = $('<table border="0" class="sortable"></table>');
1675         var $tBody, $tRow, $tCell;
1677         $('#logTable').html($table);
1679         var formatValue = function(name, value) {
1680             switch(name) {
1681                 case 'user_host':
1682                     return value.replace(/(\[.*?\])+/g,'');
1683             }
1684             return value;
1685         }
1687         for(var i=0; i < rows.length; i++) {
1688             if(i == 0) {
1689                 $.each(rows[0],function(key, value) {
1690                     cols.push(key);
1691                 });
1692                 $table.append( '<thead>' +
1693                                '<tr><th class="nowrap">' + cols.join('</th><th class="nowrap">') + '</th></tr>' +
1694                                '</thead>');
1696                 $table.append($tBody = $('<tbody></tbody>'));
1697             }
1699             $tBody.append($tRow = $('<tr class="noclick"></tr>'));
1700             var cl=''
1701             for(var j=0; j < cols.length; j++) {
1702                 // Assuming the query column is the second last
1703                 if(j == cols.length - 2 && rows[i][cols[j]].match(/^SELECT/i)) {
1704                     $tRow.append($tCell=$('<td class="analyzableQuery">' + formatValue(cols[j], rows[i][cols[j]]) + '</td>'));
1705                     $tCell.click(queryAnalyzer);
1706                 } else
1707                     $tRow.append('<td>' + formatValue(cols[j], rows[i][cols[j]]) + '</td>');
1710                 $tRow.data('query',rows[i]);
1711             }
1712         }
1714         $table.append('<tfoot>' +
1715                     '<tr><th colspan="' + (cols.length - 1) + '">' + PMA_messages['strSumRows'] +
1716                     ' '+ data.numRows +'<span style="float:right">' + PMA_messages['strTotal'] +
1717                     '</span></th><th align="right">' + data.sum.TOTAL + '</th></tr></tfoot>');
1720         function queryAnalyzer() {
1721             var query = $(this).parent().data('query')[cols[cols.length-2]];
1723             /* A very basic SQL Formatter. Totally fails in the cases of
1724                - Any string appearance containing a MySQL Keyword, surrounded by whitespaces
1725                - Subqueries too probably
1726             */
1727             // .* selector doesn't includde whitespace, [^] doesn't work in IE8, thus we use [^\0] since the zero-byte char (hopefully) doesn't appear in table names ;)
1728             var sLists = query.match(/SELECT\s+[^\0]+\s+FROM\s+/gi);
1729             if(sLists) {
1730                 for(var i=0; i < sLists.length; i++) {
1731                     query = query.replace(sLists[i],sLists[i].replace(/\s*((`|'|"|).*?\1,)\s*/gi,'$1\n\t'));
1732                 }
1733                 query = query
1734                   .replace(/(\s+|^)(SELECT|FROM|WHERE|GROUP BY|HAVING|ORDER BY|LIMIT)(\s+|$)/gi,'\n$2\n\t')
1735                   .replace(/\s+UNION\s+/gi,'\n\nUNION\n\n')
1736                   .replace(/\s+(AND)\s+/gi,' $1\n\t')
1737                   .trim();
1738             }
1740             codemirror_editor.setValue(query);
1742             var profilingChart = null;
1744             $('div#queryAnalyzerDialog').dialog({
1745                 width: 'auto',
1746                 height: 'auto',
1747                 resizable: false,
1748                 buttons: {
1749                     'Analyse Query' : function() {
1750                         $('div#queryAnalyzerDialog div.placeHolder').html('Analyzing... ' + '<img class="ajaxIcon" src="' + pmaThemeImage + 'ajax_clock_small.gif" alt="">');
1752                         $.post('server_status.php?'+url_query, {
1753                             ajax_request: true,
1754                             query_analyzer: true,
1755                             query: codemirror_editor.getValue()
1756                         }, function(data) {
1757                             data = $.parseJSON(data);
1758                             var totalTime = 0;
1760                             if(data.error) {
1761                                 $('div#queryAnalyzerDialog div.placeHolder').html('<div class="error">' + data.error + '</div>');
1762                                 return;
1763                             }
1765                             // Float sux, I'll use table :(
1766                             $('div#queryAnalyzerDialog div.placeHolder')
1767                                 .html('<table width="100%" border="0"><tr><td class="explain"></td><td class="chart"></td></tr></table>');
1769                             var explain = '<b>Explain output</b><p></p>';
1770                             $.each(data.explain, function(key,value) {
1771                                 value = (value==null)?'null':value;
1773                                 explain += key+': ' + value + '<br />';
1774                             });
1775                             $('div#queryAnalyzerDialog div.placeHolder td.explain').append(explain);
1777                             if(data.profiling) {
1778                                 var chartData = [];
1779                                 var numberTable = '<table class="queryNums"><thead><tr><th>Status</th><th>Time</th></tr></thead><tbody>';
1780                                 var duration;
1782                                 for(var i=0; i < data.profiling.length; i++) {
1783                                     duration = parseFloat(data.profiling[i].duration);
1785                                     chartData.push([data.profiling[i].state, duration]);
1786                                     totalTime+=duration;
1788                                     numberTable += '<tr><td>' + data.profiling[i].state + ' </td><td> ' + PMA_prettyProfilingNum(duration,2) + '</td></tr>';
1789                                 }
1790                                 numberTable += '<tr><td><b>Total time:</b></td><td>' + PMA_prettyProfilingNum(totalTime,2) + '</td></tr>';
1791                                 numberTable += '</tbody></table>';
1793                                 $('div#queryAnalyzerDialog div.placeHolder td.chart').append('<b>Profiling results</b> (<a href="#showNums">Table</a> | <a href="#showChart">Chart</a>)<br/>' + numberTable + ' <div id="queryProfiling"></div>');
1795                                 $('div#queryAnalyzerDialog div.placeHolder a[href="#showNums"]').click(function() {
1796                                     $('div#queryAnalyzerDialog div#queryProfiling').hide();
1797                                     $('div#queryAnalyzerDialog table.queryNums').show();
1798                                     return false;
1799                                 });
1801                                 $('div#queryAnalyzerDialog div.placeHolder a[href="#showChart"]').click(function() {
1802                                     $('div#queryAnalyzerDialog div#queryProfiling').show();
1803                                     $('div#queryAnalyzerDialog table.queryNums').hide();
1804                                     return false;
1805                                 });
1807                                 profilingChart = PMA_createProfilingChart(chartData, {
1808                                     chart: {
1809                                         renderTo: 'queryProfiling'
1810                                     },
1811                                     plotOptions: {
1812                                         pie: {
1813                                             size: '50%'
1814                                         }
1815                                     }
1816                                 });
1819                                 $('div#queryProfiling').resizable();
1820                             }
1822                         });
1823                     },
1824                     'Close' : function() {
1825                         if(profilingChart != null) {
1826                             profilingChart.destroy();
1827                         }
1828                         $('div#queryAnalyzerDialog div.placeHolder').html('');
1829                         codemirror_editor.setValue('');
1831                         $(this).dialog("close");
1832                     }
1833                 }
1834             });
1835         }
1837         // Append a tooltip to the count column, if there exist one
1838         if($('#logTable th:last').html() == '#') {
1839             $('#logTable th:last').append('&nbsp;<img class="qroupedQueryInfoIcon icon ic_b_docs" src="themes/dot.gif" alt="" />');
1841             var qtipContent = PMA_messages['strCountColumnExplanation'];
1842             if(groupInserts) qtipContent += '<p>' + PMA_messages['strMoreCountColumnExplanation'] + '</p>';
1844             $('img.qroupedQueryInfoIcon').qtip({
1845                 content: qtipContent,
1846                 position: {
1847                     corner: {
1848                         target: 'bottomMiddle',
1849                         tooltip: 'topRight'
1850                     }
1852                 },
1853                 hide: { delay: 1000 }
1854             })
1855         }
1857         $('div#logTable table').tablesorter({
1858             sortList: [[cols.length - 1,1]],
1859             widgets: ['zebra']
1860         });
1862         $('div#logTable table thead th')
1863             .append('<img class="icon sortableIcon" src="themes/dot.gif" alt="">');
1865         return cols;
1866     }
1868     function saveMonitor() {
1869         var gridCopy = {};
1871         $.each(runtime.charts, function(key, elem) {
1872             gridCopy[key] = {};
1873             gridCopy[key].nodes = elem.nodes;
1874             gridCopy[key].settings = elem.settings;
1875             gridCopy[key].title = elem.title;
1876         });
1878         if(window.localStorage) {
1879             window.localStorage['monitorCharts'] = $.toJSON(gridCopy);
1880             window.localStorage['monitorSettings'] = $.toJSON(monitorSettings);
1881         }
1883         $('a[href="#clearMonitorConfig"]').show();
1884     }
1886     $('a[href="#clearMonitorConfig"]').click(function() {
1887         window.localStorage.removeItem('monitorCharts');
1888         window.localStorage.removeItem('monitorSettings');
1889         $(this).hide();
1890     });