1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview functions used in server status pages
8 * @requires jQueryCookie
9 * @requires jQueryTablesorter
10 * @requires Highcharts
12 * @requires js/functions.js
16 // Add a tablesorter parser to properly handle thousands seperated numbers and SI prefixes
18 jQuery.tablesorter.addParser({
21 return /^[0-9]?[0-9,\.]*\s?(k|M|G|T|%)?$/.test(s);
24 var num = jQuery.tablesorter.formatFloat(
25 s.replace(PMA_messages['strThousandsSeperator'],'')
26 .replace(PMA_messages['strDecimalSeperator'],'.')
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;
39 return num * Math.pow(10,factor);
46 $('a[rel="popupLink"]').click( function() {
49 $('.' + $link.attr('href').substr(1))
51 .offset({ top: $link.offset().top + $link.height() + 5, left: $link.offset().left })
52 .addClass('openedPopup');
57 $(document).click( function(event) {
58 $('.openedPopup').each(function() {
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');
70 // Filters for status variables
72 var alertFilter = false;
73 var categoryFilter='';
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') {
103 .hide(300,function() {
104 $(this).data('shown',false);
109 $(document).mousemove(function(e) {
110 if($tableSortHint.data('shown') == true)
118 // Tell highcarts not to use UTC dates (global setting)
119 Highcharts.setOptions({
130 $('#serverStatusTabs').tabs({
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(); }
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';
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,
166 chart_activeTimeouts[chart.options.chart.renderTo] = setTimeout(
167 chart.options.realtime.timeoutCallBack,
168 chart.options.realtime.refreshRate
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');
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();
186 tabStatus[tab.attr('id')]='data';
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') {
203 { name: PMA_messages['strChartKBSent'], data: [] },
204 { name: PMA_messages['strChartKBReceived'], data: [] }
206 title: { text: PMA_messages['strChartServerTraffic'] },
207 realtime: { url:'server_status.php?' + url_query,
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 },
214 numLoadedPoints >= chartObj.options.realtime.numMaxPoints
216 chartObj.series[1].addPoint(
217 { x: curVal.x, y: (curVal.y_received - lastVal.y_received) / 1024 },
219 numLoadedPoints >= chartObj.options.realtime.numMaxPoints
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';
230 $(this).html(PMA_messages['strLiveTrafficChart']);
231 setupLiveChart($tab,this,null);
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') {
245 { name: PMA_messages['strChartConnections'], data: [] },
246 { name: PMA_messages['strChartProcesses'], data: [] }
248 title: { text: PMA_messages['strChartConnectionsTitle'] },
249 realtime: { url:'server_status.php?'+url_query,
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 },
256 numLoadedPoints >= chartObj.options.realtime.numMaxPoints
258 chartObj.series[1].addPoint(
259 { x: curVal.x, y: curVal.y_proc },
261 numLoadedPoints >= chartObj.options.realtime.numMaxPoints
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';
272 $(this).html(PMA_messages['strLiveConnChart']);
273 setupLiveChart($tab,this,null);
279 // Live query statistics
280 $('.buttonlinks a.livequeriesLink').click(function() {
281 var $tab = $(this).parents('div.ui-tabs-panel');
284 if(tabStatus[$tab.attr('id')] == 'static') {
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,
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) },
296 numLoadedPoints >= chartObj.options.realtime.numMaxPoints
302 $(this).html(PMA_messages['strLiveQueryChart']);
305 setupLiveChart($tab,this,settings);
306 tabStatus[$tab.attr('id')] = 'livequeries';
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;
321 if(! settings.chart) settings.chart = {};
322 settings.chart.renderTo = $tab.attr('id') + "_chart_cnt";
324 $tab.find('.tabInnerContent')
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();
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();
344 /* 3 Filtering functions */
345 $('#filterAlert').change(function() {
346 alertFilter = this.checked;
350 $('#filterText').keyup(function(e) {
351 word = $(this).val().replace('_',' ');
353 if(word.length == 0) textFilter = null;
354 else textFilter = new RegExp("(^|_)" + word,'i');
361 $('#filterCategory').change(function() {
362 categoryFilter = $(this).val();
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();
373 case 'statustabs_queries':
375 queryPieChart.destroy();
376 tab.find('.tabInnerContent').html(data);
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)]);
385 queryPieChart = PMA_createChart({
387 renderTo: 'serverstatusquerieschart'
395 name: PMA_messages['strChartQueryPie'],
400 allowPointSelect: true,
404 formatter: function() {
405 return '<b>'+ this.point.name +'</b><br/> ' + Highcharts.numberFormat(this.percentage, 2) + ' %';
411 formatter: function() {
412 return '<b>' + this.point.name + '</b><br/>' + Highcharts.numberFormat(this.y, 2) + '<br/>(' + Highcharts.numberFormat(this.percentage, 2) + ' %)';
418 case 'statustabs_allvars':
420 tab.find('.tabInnerContent').html(data);
426 initTableSorter(tab.attr('id'));
429 function initTableSorter(tabid) {
431 case 'statustabs_queries':
432 $('#serverstatusqueriesdetails').tablesorter({
436 1: { sorter: 'fancyNumber' },
437 2: { sorter: 'fancyNumber' }
441 $('#serverstatusqueriesdetails tr:first th')
442 .append('<img class="icon sortableIcon" src="themes/dot.gif" alt="">');
446 case 'statustabs_allvars':
447 $('#serverstatusvariables').tablesorter({
451 1: { sorter: 'fancyNumber' }
455 $('#serverstatusvariables tr:first th')
456 .append('<img class="icon sortableIcon" src="themes/dot.gif" alt="">');
462 /* Filters the status variables by name/category/alert in the variables tab */
463 function filterVariables() {
464 var useful_links = 0;
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) {
473 $(this).css('display','');
475 $(this).css('display','none');
483 $('#linkSuggestions').css('display','');
484 else $('#linkSuggestions').css('display','none');
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))) {
492 $(this).parent().css('display','');
494 $(this).parent().addClass('odd');
495 $(this).parent().removeClass('even');
497 $(this).parent().addClass('even');
498 $(this).parent().removeClass('odd');
501 $(this).parent().css('display','none');
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();
514 // Separate keys and values, then sort them
515 $.each(queries.pointInfo, function(key,value) {
516 if(value-lastQueries.pointInfo[key] > 0) {
518 queryValues.push(value-lastQueries.pointInfo[key]);
519 sumTotal += value-lastQueries.pointInfo[key];
522 var numQueries = queryKeys.length;
523 var pointInfo = '<b>' + PMA_messages['strTotal'] + ': ' + sumTotal + '</b><br>';
525 while(queryKeys.length > 0) {
527 for(var i=0; i < queryKeys.length; i++) {
528 if(queryValues[i] > max) {
529 max = queryValues[i];
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);
543 pointInfo += PMA_messages['strOther'] + ': ' + sumOther;
551 /**** Monitor charting implementation ****/
552 /* Saves the previous ajax response for differential values */
553 var oldChartData = null;
554 // Holds about to created chart
558 // Runtime parameter of the monitor
560 // Holds all visible charts in the grid
562 // Current max points per chart (needed for auto calculation)
564 // displayed time frame
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
573 // To play/pause the monitor
575 // Object that contains a list of nodes that need to be retrieved from the server for chart updates
579 var monitorSettings = null;
581 var defaultMonitorSettings = {
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 */
590 // Allows drag and drop rearrange and print/edit icons on charts
591 var editMode = false;
595 title: PMA_messages['strSystemCPUUsage'],
596 nodes: [{ dataType: 'cpu', name: 'loadavg', unit: '%'}]
599 title: PMA_messages['strSystemMemory'],
601 { dataType: 'memory', name: 'MemTotal', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
602 { dataType: 'memory', name: 'MemUsed', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
606 title: PMA_messages['strSystemSwap'],
608 { dataType: 'memory', name: 'SwapTotal', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
609 { dataType: 'memory', name: 'SwapUsed', valueDivisor: 1024, unit: PMA_messages['strMiB'] },
613 title: PMA_messages['strSystemCPUUsage'],
616 name: PMA_messages['strAverageLoad'],
618 transformFn: function(cur, prev) {
619 console.log('cpu-linux chart, transformFn()');
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;
631 title: PMA_messages['strSystemMemory'],
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'] },
651 title: PMA_messages['strSystemSwap'],
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'] },
673 'c0': { title: PMA_messages['strQuestions'],
674 nodes: [{ dataType: 'statusvar', name: 'Questions', display: 'differential' }]
677 title: PMA_messages['strChartConnectionsTitle'],
678 nodes: [ { dataType: 'statusvar', name: 'Connections', display: 'differential' },
679 { dataType: 'proc', name: 'Processes'} ]
682 title: PMA_messages['strTraffic'],
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'] }
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];
700 symbol: 'url(' + pmaThemeImage + 's_cog.png)',
702 symbolFill: '#B5C9DF',
703 hoverSymbolFill: '#779ABF',
704 _titleKey: 'settings',
705 menuName: 'gridsettings',
707 textKey: 'editChart',
708 onclick: function() {
712 textKey: 'removeChart',
713 onclick: function() {
720 Highcharts.setOptions({
722 settings: PMA_messages['strSettings'],
723 removeChart: PMA_messages['strRemoveChart'],
724 editChart: PMA_messages['strEditChart']
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);
738 // Close the settings popup
739 $('#statustabs_charting .popupContent').hide().removeClass('openedPopup');
741 $("#chartGrid").sortableTable({
744 left: chartSize().width - 63,
750 // console.log('start.');
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)
765 if(dropRender && value.chart.options.chart.renderTo == dropRender)
769 // Case 1: drag and drop are charts -> Switch keys
772 dragChart = runtime.charts[dragKey];
773 runtime.charts[dragKey] = runtime.charts[dropKey];
774 runtime.charts[dropKey] = dragChart;
776 // Case 2: drop is a empty cell => just completely rebuild the ids
778 var dropKeyNum = parseInt(dropKey.substr(1));
779 var insertBefore = pos.col + pos.row * monitorSettings.columns;
781 var newChartList = {};
784 $.each(runtime.charts, function(key, value) {
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
797 newChartList['c' + (c++)] = runtime.charts[keys[i]];
800 // Not inserted => put at the end
801 if(insertBefore != -1)
802 newChartList['c' + (c++)] = runtime.charts[dropKey];
804 runtime.charts = newChartList;
814 $("#chartGrid").sortableTable('destroy');
815 saveMonitor(); // Save 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 */
832 var $tr = $('table#chartGrid tr:first');
834 while($tr.length != 0) {
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));
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'));
861 /* Apply new chart size to all charts */
862 $.each(runtime.charts, function(key, value) {
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;
877 $("#chartGrid").sortableTable('refresh');
879 saveMonitor(); // Save settings
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);
896 runtime.refreshTimeout = setTimeout(refreshChartGrid, monitorSettings.gridRefresh);
898 saveMonitor(); // Save settings
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];
910 if(! newChart || ! newChart.nodes || newChart.nodes.length == 0) {
911 alert(PMA_messages['strAddOneSeriesWarning']);
916 newChart.title = $('input[name="chartTitle"]').attr('value');
917 // Add a cloned object to the chart grid
918 addChart($.extend(true, {}, newChart));
922 saveMonitor(); // Save settings
924 $(this).dialog("close");
927 dlgButtons[PMA_messages['strClose']] = function() {
929 $('span#clearSeriesLink').hide();
930 $('#seriesPreview').html('');
931 $(this).dialog("close");
934 $('div#addChartDialog').dialog({
940 $('div#addChartDialog #seriesPreview').html('<i>' + PMA_messages['strNone'] + '</i>');
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']);
950 $(this).html('<img src="themes/dot.gif" class="icon ic_pause" alt="" /> ' + PMA_messages['strPauseMonitor']);
951 if(runtime.charts == null) {
953 $('a[href="#settingsPopup"]').show();
959 $('a[href="#monitorInstructionsDialog"]').click(function() {
960 var $dialog = $('div#monitorInstructionsDialog');
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,
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'];
980 msg = PMA_messages['strGenLogOn'];
983 if(msg.length == 0 && logVars['slow_query_log'] == 'ON') {
984 msg = PMA_messages['strSlowLogOn'];
987 if(msg.length == 0) {
989 msg = PMA_messages['strBothLogOff'];
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 />';
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'])
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'])
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)
1026 if(logVars['general_log'] != 'ON')
1027 str += '- <a class="set" href="#general_log-ON">'
1028 + $.sprintf(PMA_messages['strEnableVar'], 'general_log')
1031 str += '- <a class="set" href="#general_log-OFF">'
1032 + $.sprintf(PMA_messages['strDisableVar'], 'general_log')
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')
1040 str += '- <a class="set" href="#slow_query_log-OFF">'
1041 + $.sprintf(PMA_messages['strDisableVar'], 'slow_query_log')
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)
1053 str += PMA_messages['strNoSuperUser'] + '<br/>';
1057 $dialog.find('div.monitorUse').toggle(
1058 logVars['log_output'] == 'TABLE' && (logVars['slow_query_log'] == 'ON' || logVars['general_log'] == 'ON')
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();
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());
1088 $('input[name="useDivisor"]').change(function() {
1089 $('span.divisorInput').toggle(this.checked);
1091 $('input[name="useUnit"]').change(function() {
1092 $('span.unitInput').toggle(this.checked);
1095 $('select[name="varChartList"]').change(function () {
1096 if(this.selectedIndex!=0)
1097 $('#variableInput').attr('value',this.value);
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);
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);
1116 $('a[href="#submitClearSeries"]').click(function() {
1117 $('#seriesPreview').html('<i>' + PMA_messages['strNone'] + '</i>');
1119 $('span#clearSeriesLink').hide();
1122 $('a[href="#submitAddSeries"]').click(function() {
1123 if($('input#variableInput').attr('value').length == 0) return false;
1125 if(newChart == null) {
1126 $('#seriesPreview').html('');
1129 title: $('input[name="chartTitle"]').attr('value'),
1135 dataType:'statusvar',
1136 name: $('input#variableInput').attr('value'),
1137 display: $('input[name="differentialValue"]').attr('checked') ? 'differential' : ''
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();
1170 $("input#variableInput").autocomplete({
1171 source: variableNames
1175 function initGrid() {
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);
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);
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>');
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
1211 $('table#chartGrid').html('');
1213 /* Add all charts - in correct order */
1215 $.each(runtime.charts, function(key, value) {
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>');
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();
1237 function chartSize() {
1238 var wdt = $('div#logTable').innerWidth() / monitorSettings.columns - (monitorSettings.columns - 1) * chartSpacing.width;
1245 function addChart(chartObj, initialize) {
1247 for(var j=0; j<chartObj.nodes.length; j++)
1248 series.push(chartObj.nodes[j]);
1252 renderTo: 'gridchart' + runtime.chartAI,
1253 width: chartSize().width,
1254 height: chartSize().height,
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)));
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;
1280 removeVariables: $('input#removeVariables').prop('checked'),
1281 limitTypes: $('input#limitTypes').prop('checked')
1284 $('#logAnalyseDialog').find('dateStart,dateEnd').datepicker('destroy');
1286 $(this).dialog("close");
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;
1297 removeVariables: $('input#removeVariables').prop('checked'),
1298 limitTypes: $('input#limitTypes').prop('checked')
1301 $('#logAnalyseDialog').find('dateStart,dateEnd').datepicker('destroy');
1303 $(this).dialog("close");
1306 $('#logAnalyseDialog').dialog({
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 || '');
1343 buttons: gridbuttons,
1344 title: { text: chartObj.title }
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>');
1359 chartObj.chart = PMA_createChart(settings);
1360 chartObj.numPoints = 0;
1362 if(initialize != true) {
1363 runtime.charts['c'+runtime.chartAI] = chartObj;
1364 buildRequiredDataList();
1367 // Edit,Print icon only in edit mode
1368 $('table#chartGrid div svg').find('*[zIndex=20], *[zIndex=21], *[zIndex=19]').toggle(editMode)
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];
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() {
1391 $('li#' + htmlnode).remove();
1394 saveMonitor(); // Save settings
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);
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;
1410 for(var j=0; j < elem.nodes.length; j++) {
1411 value = chartData[key][j].y;
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;
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;
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(
1434 (oldChartData == null) ? null : oldChartData[key][j]
1438 if(value != undefined)
1439 elem.chart.series[j].addPoint(
1440 { x: chartData.x, y: value },
1442 elem.numPoints >= runtime.gridMaxPoints
1448 runtime.charts[orderKey].numPoints++;
1449 if(runtime.redrawCharts)
1450 elem.chart.redraw();
1453 oldChartData = chartData;
1455 runtime.refreshTimeout = setTimeout(refreshChartGrid, monitorSettings.gridRefresh);
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
1464 $.each(runtime.charts, function(key, chart) {
1465 runtime.dataList[chartID] = chart.nodes;
1466 runtime.charts[key].chartID = chartID;
1471 function loadLogStatistics(opts) {
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({
1486 'Cancel request': function() {
1487 if(logRequest != null)
1490 $(this).dialog("close");
1496 logRequest = $.get('server_status.php?'+url_query,
1497 { ajax_request: true,
1500 time_start: Math.round(opts.start / 1000),
1501 time_end: Math.round(opts.end / 1000),
1502 removeVariables: opts.removeVariables,
1503 limitTypes: opts.limitTypes
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/>');
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;" />' +
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>' +
1536 $('div#logTable input#noWHEREData').change(function() {
1537 filterQueries(true);
1540 //preg_replace('/\s+([^=]+)=(\d+|((\'|"|)(?U)(.+)(?<!\\\)\4(\s+|$)))/i',' $1={} ',$str);
1542 if(logData.numRows > 250) {
1543 $('div#logTable button#startFilterQueryText').click(filterQueries);
1545 $('div#logTable input#filterQueryText').keyup(filterQueries);
1551 dlgBtns[PMA_messages['strJumpToTable']] = function() {
1552 $(this).dialog("close");
1553 $(document).scrollTop($('div#logTable').offset().top);
1556 $('#loadingLogsDialog').dialog( "option", "buttons", dlgBtns);
1559 $('#loadingLogsDialog').html('<p>' + PMA_messages['strNoDataFound'] + '</p>');
1562 dlgBtns[PMA_messages['strClose']] = function() {
1563 $(this).dialog("close");
1566 $('#loadingLogsDialog').dialog( "option", "buttons", dlgBtns );
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;
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 = {};
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)) {
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());
1603 filteredQueries[q] = parseInt($(this).next().text());;
1604 filteredQueriesLines[q] = i;
1609 $(this).text($(this).parent().data('query')[queryColumnName]);
1610 $(this).next().text($(this).parent().data('query')[sumColumnName]);
1614 if(! hide && (textFilter != null && ! textFilter.exec($(this).text()))) hide = true;
1617 $(this).parent().css('display','none');
1619 totalSum += parseInt($(this).next().text());
1622 odd_row = ! odd_row;
1623 $(this).parent().css('display','');
1625 $(this).parent().addClass('odd');
1626 $(this).parent().removeClass('even');
1628 $(this).parent().addClass('even');
1629 $(this).parent().removeClass('odd');
1637 if(varFilterChange) {
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]);
1649 $('div#logTable table').trigger("update");
1650 setTimeout(function() {
1652 $('div#logTable table').trigger('sorton',[[[runtime.logDataCols.length - 1,1]]]);
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>');
1663 /*loadLogStatistics({
1665 start:1311076210*1000,
1666 end:1311162689*1000,
1667 removeVariables: true,
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) {
1682 return value.replace(/(\[.*?\])+/g,'');
1687 for(var i=0; i < rows.length; i++) {
1689 $.each(rows[0],function(key, value) {
1692 $table.append( '<thead>' +
1693 '<tr><th class="nowrap">' + cols.join('</th><th class="nowrap">') + '</th></tr>' +
1696 $table.append($tBody = $('<tbody></tbody>'));
1699 $tBody.append($tRow = $('<tr class="noclick"></tr>'));
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);
1707 $tRow.append('<td>' + formatValue(cols[j], rows[i][cols[j]]) + '</td>');
1710 $tRow.data('query',rows[i]);
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
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);
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'));
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')
1740 codemirror_editor.setValue(query);
1742 var profilingChart = null;
1744 $('div#queryAnalyzerDialog').dialog({
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, {
1754 query_analyzer: true,
1755 query: codemirror_editor.getValue()
1757 data = $.parseJSON(data);
1761 $('div#queryAnalyzerDialog div.placeHolder').html('<div class="error">' + data.error + '</div>');
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 />';
1775 $('div#queryAnalyzerDialog div.placeHolder td.explain').append(explain);
1777 if(data.profiling) {
1779 var numberTable = '<table class="queryNums"><thead><tr><th>Status</th><th>Time</th></tr></thead><tbody>';
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>';
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();
1801 $('div#queryAnalyzerDialog div.placeHolder a[href="#showChart"]').click(function() {
1802 $('div#queryAnalyzerDialog div#queryProfiling').show();
1803 $('div#queryAnalyzerDialog table.queryNums').hide();
1807 profilingChart = PMA_createProfilingChart(chartData, {
1809 renderTo: 'queryProfiling'
1819 $('div#queryProfiling').resizable();
1824 'Close' : function() {
1825 if(profilingChart != null) {
1826 profilingChart.destroy();
1828 $('div#queryAnalyzerDialog div.placeHolder').html('');
1829 codemirror_editor.setValue('');
1831 $(this).dialog("close");
1837 // Append a tooltip to the count column, if there exist one
1838 if($('#logTable th:last').html() == '#') {
1839 $('#logTable th:last').append(' <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,
1848 target: 'bottomMiddle',
1853 hide: { delay: 1000 }
1857 $('div#logTable table').tablesorter({
1858 sortList: [[cols.length - 1,1]],
1862 $('div#logTable table thead th')
1863 .append('<img class="icon sortableIcon" src="themes/dot.gif" alt="">');
1868 function saveMonitor() {
1871 $.each(runtime.charts, function(key, elem) {
1873 gridCopy[key].nodes = elem.nodes;
1874 gridCopy[key].settings = elem.settings;
1875 gridCopy[key].title = elem.title;
1878 if(window.localStorage) {
1879 window.localStorage['monitorCharts'] = $.toJSON(gridCopy);
1880 window.localStorage['monitorSettings'] = $.toJSON(monitorSettings);
1883 $('a[href="#clearMonitorConfig"]').show();
1886 $('a[href="#clearMonitorConfig"]').click(function() {
1887 window.localStorage.removeItem('monitorCharts');
1888 window.localStorage.removeItem('monitorSettings');