1 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 AJAX.registerOnload('server_status_monitor.js', function () {
9 var $js_data_form = $('#js_data');
10 server_time_diff = new Date().getTime() - $js_data_form.find('input[name=server_time]').val();
11 server_os = $js_data_form.find('input[name=server_os]').val();
12 is_superuser = $js_data_form.find('input[name=is_superuser]').val();
13 server_db_isLocal = $js_data_form.find('input[name=server_db_isLocal]').val();
17 * Unbind all event handlers before tearing down a page
19 AJAX.registerTeardown('server_status_monitor.js', function () {
20 $('#emptyDialog').remove();
21 $('#addChartDialog').remove();
22 $('a.popupLink').off('click');
23 $('body').off('click');
28 AJAX.registerOnload('server_status_monitor.js', function () {
30 .attr('id', 'emptyDialog')
31 .appendTo('#page_content');
33 .appendTo('#page_content');
35 $('a.popupLink').click(function () {
37 $('div.' + $link.attr('href').substr(1))
39 .offset({ top: $link.offset().top + $link.height() + 5, left: $link.offset().left })
40 .addClass('openedPopup');
44 $('body').click(function (event) {
45 $('div.openedPopup').each(function () {
47 var pos = $cnt.offset();
48 // Hide if the mouseclick is outside the popupcontent
49 if (event.pageX < pos.left ||
50 event.pageY < pos.top ||
51 event.pageX > pos.left + $cnt.outerWidth() ||
52 event.pageY > pos.top + $cnt.outerHeight()
54 $cnt.hide().removeClass('openedPopup');
60 AJAX.registerTeardown('server_status_monitor.js', function () {
61 $('a[href="#rearrangeCharts"], a[href="#endChartEditMode"]').off('click');
62 $('div.popupContent select[name="chartColumns"]').off('change');
63 $('div.popupContent select[name="gridChartRefresh"]').off('change');
64 $('a[href="#addNewChart"]').off('click');
65 $('a[href="#exportMonitorConfig"]').off('click');
66 $('a[href="#importMonitorConfig"]').off('click');
67 $('a[href="#clearMonitorConfig"]').off('click');
68 $('a[href="#pauseCharts"]').off('click');
69 $('a[href="#monitorInstructionsDialog"]').off('click');
70 $('input[name="chartType"]').off('click');
71 $('input[name="useDivisor"]').off('click');
72 $('input[name="useUnit"]').off('click');
73 $('select[name="varChartList"]').off('click');
74 $('a[href="#kibDivisor"]').off('click');
75 $('a[href="#mibDivisor"]').off('click');
76 $('a[href="#submitClearSeries"]').off('click');
77 $('a[href="#submitAddSeries"]').off('click');
78 // $("input#variableInput").destroy();
79 $('#chartPreset').off('click');
80 $('#chartStatusVar').off('click');
84 AJAX.registerOnload('server_status_monitor.js', function () {
86 $('div.tabLinks').show();
87 $('#loadingMonitorIcon').remove();
88 // Codemirror is loaded on demand so we might need to initialize it
89 if (! codemirror_editor) {
90 var $elm = $('#sqlquery');
91 if ($elm.length > 0 && typeof CodeMirror !== 'undefined') {
92 codemirror_editor = CodeMirror.fromTextArea(
104 // Timepicker is loaded on demand so we need to initialize
105 // datetime fields from the 'load log' dialog
106 $('#logAnalyseDialog').find('.datetimefield').each(function () {
107 PMA_addDatepicker($(this));
110 /** ** Monitor charting implementation ****/
111 /* Saves the previous ajax response for differential values */
112 var oldChartData = null;
113 // Holds about to be created chart
117 // Whenever the monitor object (runtime.charts) or the settings object
118 // (monitorSettings) changes in a way incompatible to the previous version,
119 // increase this number. It will reset the users monitor and settings object
120 // in his localStorage to the default configuration
121 var monitorProtocolVersion = '1.0';
123 // Runtime parameter of the monitor, is being fully set in initGrid()
125 // Holds all visible charts in the grid
127 // Stores the timeout handler so it can be cleared
128 refreshTimeout: null,
129 // Stores the GET request to refresh the charts
130 refreshRequest: null,
131 // Chart auto increment
133 // To play/pause the monitor
135 // Object that contains a list of nodes that need to be retrieved
136 // from the server for chart updates
138 // Current max points per chart (needed for auto calculation)
140 // displayed time frame
144 var monitorSettings = null;
146 var defaultMonitorSettings = {
148 chartSize: { width: 295, height: 250 },
149 // Max points in each chart. Settings it to 'auto' sets
150 // gridMaxPoints to (chartwidth - 40) / 12
151 gridMaxPoints: 'auto',
152 /* Refresh rate of all grid charts in ms */
156 // Allows drag and drop rearrange and print/edit icons on charts
157 var editMode = false;
159 /* List of preconfigured charts that the user may select */
161 // Query cache efficiency
163 title: PMA_messages.strQueryCacheEfficiency,
165 label: PMA_messages.strQueryCacheEfficiency
168 dataPoints: [{ type: 'statusvar', name: 'Qcache_hits' }, { type: 'statusvar', name: 'Com_select' }],
175 title: PMA_messages.strQueryCacheUsage,
177 label: PMA_messages.strQueryCacheUsed
180 dataPoints: [{ type: 'statusvar', name: 'Qcache_free_memory' }, { type: 'servervar', name: 'query_cache_size' }],
187 // time span selection
188 var selectionTimeDiff = [];
193 var drawTimeSpan = false;
198 /* Add OS specific system info charts to the preset chart list */
201 $.extend(presetCharts, {
203 title: PMA_messages.strSystemCPUUsage,
205 label: PMA_messages.strAverageLoad
208 dataPoints: [{ type: 'cpu', name: 'loadavg' }]
214 title: PMA_messages.strSystemMemory,
216 label: PMA_messages.strTotalMemory,
220 label: PMA_messages.strUsedMemory,
223 nodes: [{ dataPoints: [{ type: 'memory', name: 'MemTotal' }], valueDivisor: 1024 },
224 { dataPoints: [{ type: 'memory', name: 'MemUsed' }], valueDivisor: 1024 }
230 title: PMA_messages.strSystemSwap,
232 label: PMA_messages.strTotalSwap,
235 label: PMA_messages.strUsedSwap,
238 nodes: [{ dataPoints: [{ type: 'memory', name: 'SwapTotal' }] },
239 { dataPoints: [{ type: 'memory', name: 'SwapUsed' }] }
247 $.extend(presetCharts, {
249 title: PMA_messages.strSystemCPUUsage,
251 label: PMA_messages.strAverageLoad
253 nodes: [{ dataPoints: [{ type: 'cpu', name: 'irrelevant' }], transformFn: 'cpu-linux' }],
257 title: PMA_messages.strSystemMemory,
259 { label: PMA_messages.strBufferedMemory, fill: true },
260 { label: PMA_messages.strUsedMemory, fill: true },
261 { label: PMA_messages.strCachedMemory, fill: true },
262 { label: PMA_messages.strFreeMemory, fill: true }
265 { dataPoints: [{ type: 'memory', name: 'Buffers' }], valueDivisor: 1024 },
266 { dataPoints: [{ type: 'memory', name: 'MemUsed' }], valueDivisor: 1024 },
267 { dataPoints: [{ type: 'memory', name: 'Cached' }], valueDivisor: 1024 },
268 { dataPoints: [{ type: 'memory', name: 'MemFree' }], valueDivisor: 1024 }
273 title: PMA_messages.strSystemSwap,
275 { label: PMA_messages.strCachedSwap, fill: true },
276 { label: PMA_messages.strUsedSwap, fill: true },
277 { label: PMA_messages.strFreeSwap, fill: true }
280 { dataPoints: [{ type: 'memory', name: 'SwapCached' }], valueDivisor: 1024 },
281 { dataPoints: [{ type: 'memory', name: 'SwapUsed' }], valueDivisor: 1024 },
282 { dataPoints: [{ type: 'memory', name: 'SwapFree' }], valueDivisor: 1024 }
290 $.extend(presetCharts, {
292 title: PMA_messages.strSystemCPUUsage,
294 label: PMA_messages.strAverageLoad
297 dataPoints: [{ type: 'cpu', name: 'loadavg' }]
302 title: PMA_messages.strSystemMemory,
304 { label: PMA_messages.strUsedMemory, fill: true },
305 { label: PMA_messages.strFreeMemory, fill: true }
308 { dataPoints: [{ type: 'memory', name: 'MemUsed' }], valueDivisor: 1024 },
309 { dataPoints: [{ type: 'memory', name: 'MemFree' }], valueDivisor: 1024 }
314 title: PMA_messages.strSystemSwap,
316 { label: PMA_messages.strUsedSwap, fill: true },
317 { label: PMA_messages.strFreeSwap, fill: true }
320 { dataPoints: [{ type: 'memory', name: 'SwapUsed' }], valueDivisor: 1024 },
321 { dataPoints: [{ type: 'memory', name: 'SwapFree' }], valueDivisor: 1024 }
329 // Default setting for the chart grid
330 var defaultChartGrid = {
332 title: PMA_messages.strQuestions,
334 { label: PMA_messages.strQuestions }
337 { dataPoints: [{ type: 'statusvar', name: 'Questions' }], display: 'differential' }
342 title: PMA_messages.strChartConnectionsTitle,
344 { label: PMA_messages.strConnections },
345 { label: PMA_messages.strProcesses }
348 { dataPoints: [{ type: 'statusvar', name: 'Connections' }], display: 'differential' },
349 { dataPoints: [{ type: 'proc', name: 'processes' }] }
354 title: PMA_messages.strTraffic,
356 { label: PMA_messages.strBytesSent },
357 { label: PMA_messages.strBytesReceived }
360 { dataPoints: [{ type: 'statusvar', name: 'Bytes_sent' }], display: 'differential', valueDivisor: 1024 },
361 { dataPoints: [{ type: 'statusvar', name: 'Bytes_received' }], display: 'differential', valueDivisor: 1024 }
367 // Server is localhost => We can add cpu/memory/swap to the default chart
368 if (server_db_isLocal && typeof presetCharts.cpu !== 'undefined') {
369 defaultChartGrid.c3 = presetCharts.cpu;
370 defaultChartGrid.c4 = presetCharts.memory;
371 defaultChartGrid.c5 = presetCharts.swap;
374 $('a[href="#rearrangeCharts"], a[href="#endChartEditMode"]').click(function (event) {
375 event.preventDefault();
376 editMode = !editMode;
377 if ($(this).attr('href') === '#endChartEditMode') {
381 $('a[href="#endChartEditMode"]').toggle(editMode);
384 // Close the settings popup
385 $('div.popupContent').hide().removeClass('openedPopup');
387 $('#chartGrid').sortableTable({
390 left: chartSize.width - 63,
396 $('#chartGrid').sortableTable('destroy');
398 saveMonitor(); // Save settings
403 $('div.popupContent select[name="chartColumns"]').change(function () {
404 monitorSettings.columns = parseInt(this.value, 10);
406 calculateChartSize();
407 // Empty cells should keep their size so you can drop onto them
408 $('#chartGrid').find('tr td').css('width', chartSize.width + 'px');
409 $('#chartGrid').find('.monitorChart').css({
410 width: chartSize.width + 'px',
411 height: chartSize.height + 'px'
414 /* Reorder all charts that it fills all column cells */
416 var $tr = $('#chartGrid').find('tr:first');
419 var tempManageCols = function () {
420 if (numColumns > monitorSettings.columns) {
421 if ($tr.next().length === 0) {
422 $tr.after('<tr></tr>');
424 $tr.next().prepend($(this));
429 var tempAddCol = function () {
430 if ($(this).next().length !== 0) {
431 $(this).append($(this).next().find('td:first'));
435 while ($tr.length !== 0) {
437 // To many cells in one row => put into next row
438 $tr.find('td').each(tempManageCols);
440 // To little cells in one row => for each cell to little,
441 // move all cells backwards by 1
442 if ($tr.next().length > 0) {
443 var cnt = monitorSettings.columns - $tr.find('td').length;
444 for (var i = 0; i < cnt; i++) {
445 $tr.append($tr.next().find('td:first'));
446 $tr.nextAll().each(tempAddCol);
454 if (monitorSettings.gridMaxPoints === 'auto') {
455 runtime.gridMaxPoints = Math.round((chartSize.width - 40) / 12);
458 runtime.xmin = new Date().getTime() - server_time_diff - runtime.gridMaxPoints * monitorSettings.gridRefresh;
459 runtime.xmax = new Date().getTime() - server_time_diff + monitorSettings.gridRefresh;
462 $('#chartGrid').sortableTable('refresh');
466 saveMonitor(); // Save settings
469 $('div.popupContent select[name="gridChartRefresh"]').change(function () {
470 monitorSettings.gridRefresh = parseInt(this.value, 10) * 1000;
471 clearTimeout(runtime.refreshTimeout);
473 if (runtime.refreshRequest) {
474 runtime.refreshRequest.abort();
477 runtime.xmin = new Date().getTime() - server_time_diff - runtime.gridMaxPoints * monitorSettings.gridRefresh;
478 // fixing chart shift towards left on refresh rate change
479 // runtime.xmax = new Date().getTime() - server_time_diff + monitorSettings.gridRefresh;
480 runtime.refreshTimeout = setTimeout(refreshChartGrid, monitorSettings.gridRefresh);
482 saveMonitor(); // Save settings
485 $('a[href="#addNewChart"]').click(function (event) {
486 event.preventDefault();
487 var dlgButtons = { };
489 dlgButtons[PMA_messages.strAddChart] = function () {
490 var type = $('input[name="chartType"]:checked').val();
492 if (type === 'preset') {
493 newChart = presetCharts[$('#addChartDialog').find('select[name="presetCharts"]').prop('value')];
495 // If user builds his own chart, it's being set/updated
496 // each time he adds a series
497 // So here we only warn if he didn't add a series yet
498 if (! newChart || ! newChart.nodes || newChart.nodes.length === 0) {
499 alert(PMA_messages.strAddOneSeriesWarning);
504 newChart.title = $('input[name="chartTitle"]').val();
505 // Add a cloned object to the chart grid
506 addChart($.extend(true, {}, newChart));
510 saveMonitor(); // Save settings
512 $(this).dialog('close');
515 dlgButtons[PMA_messages.strClose] = function () {
517 $('span#clearSeriesLink').hide();
518 $('#seriesPreview').html('');
519 $(this).dialog('close');
522 var $presetList = $('#addChartDialog').find('select[name="presetCharts"]');
523 if ($presetList.html().length === 0) {
524 $.each(presetCharts, function (key, value) {
525 $presetList.append('<option value="' + key + '">' + value.title + '</option>');
527 $presetList.change(function () {
528 $('input[name="chartTitle"]').val(
529 $presetList.find(':selected').text()
531 $('#chartPreset').prop('checked', true);
533 $('#chartPreset').click(function () {
534 $('input[name="chartTitle"]').val(
535 $presetList.find(':selected').text()
538 $('#chartStatusVar').click(function () {
539 $('input[name="chartTitle"]').val(
540 $('#chartSeries').find(':selected').text().replace(/_/g, ' ')
543 $('#chartSeries').change(function () {
544 $('input[name="chartTitle"]').val(
545 $('#chartSeries').find(':selected').text().replace(/_/g, ' ')
550 $('#addChartDialog').dialog({
556 $('#seriesPreview').html('<i>' + PMA_messages.strNone + '</i>');
561 $('a[href="#exportMonitorConfig"]').click(function (event) {
562 event.preventDefault();
564 $.each(runtime.charts, function (key, elem) {
566 gridCopy[key].nodes = elem.nodes;
567 gridCopy[key].settings = elem.settings;
568 gridCopy[key].title = elem.title;
571 monitorCharts: gridCopy,
572 monitorSettings: monitorSettings
575 var blob = new Blob([JSON.stringify(exportData)], { type: 'application/octet-stream' });
576 var url = window.URL.createObjectURL(blob);
577 window.location.href = url;
578 window.URL.revokeObjectURL(url);
581 $('a[href="#importMonitorConfig"]').click(function (event) {
582 event.preventDefault();
583 $('#emptyDialog').dialog({ title: PMA_messages.strImportDialogTitle });
584 $('#emptyDialog').html(PMA_messages.strImportDialogMessage + ':<br/><form>' +
585 '<input type="file" name="file" id="import_file"> </form>');
589 dlgBtns[PMA_messages.strImport] = function () {
590 var input = $('#emptyDialog').find('#import_file')[0];
591 var reader = new FileReader();
593 reader.onerror = function (event) {
594 alert(PMA_messages.strFailedParsingConfig + '\n' + event.target.error.code);
596 reader.onload = function (e) {
597 var data = e.target.result;
599 // Try loading config
601 json = JSON.parse(data);
603 alert(PMA_messages.strFailedParsingConfig);
604 $('#emptyDialog').dialog('close');
608 // Basic check, is this a monitor config json?
609 if (!json || ! json.monitorCharts || ! json.monitorCharts) {
610 alert(PMA_messages.strFailedParsingConfig);
611 $('#emptyDialog').dialog('close');
615 // If json ok, try applying config
617 window.localStorage.monitorCharts = JSON.stringify(json.monitorCharts);
618 window.localStorage.monitorSettings = JSON.stringify(json.monitorSettings);
622 alert(PMA_messages.strFailedBuildingGrid);
623 // If an exception is thrown, load default again
624 if (isStorageSupported('localStorage')) {
625 window.localStorage.removeItem('monitorCharts');
626 window.localStorage.removeItem('monitorSettings');
631 $('#emptyDialog').dialog('close');
633 reader.readAsText(input.files[0]);
636 dlgBtns[PMA_messages.strCancel] = function () {
637 $(this).dialog('close');
640 $('#emptyDialog').dialog({
647 $('a[href="#clearMonitorConfig"]').click(function (event) {
648 event.preventDefault();
649 if (isStorageSupported('localStorage')) {
650 window.localStorage.removeItem('monitorCharts');
651 window.localStorage.removeItem('monitorSettings');
652 window.localStorage.removeItem('monitorVersion');
658 $('a[href="#pauseCharts"]').click(function (event) {
659 event.preventDefault();
660 runtime.redrawCharts = ! runtime.redrawCharts;
661 if (! runtime.redrawCharts) {
662 $(this).html(PMA_getImage('play') + PMA_messages.strResumeMonitor);
664 $(this).html(PMA_getImage('pause') + PMA_messages.strPauseMonitor);
665 if (! runtime.charts) {
667 $('a[href="#settingsPopup"]').show();
673 $('a[href="#monitorInstructionsDialog"]').click(function (event) {
674 event.preventDefault();
676 var $dialog = $('#monitorInstructionsDialog');
681 }).find('img.ajaxIcon').show();
683 var loadLogVars = function (getvars) {
684 var vars = { ajax_request: true, logging_vars: true };
686 $.extend(vars, getvars);
689 $.post('server_status_monitor.php' + PMA_commonParams.get('common_query'), vars,
692 if (typeof data !== 'undefined' && data.success === true) {
693 logVars = data.message;
695 return serverResponseError();
697 var icon = PMA_getImage('s_success');
701 if (logVars.general_log === 'ON') {
702 if (logVars.slow_query_log === 'ON') {
703 msg = PMA_messages.strBothLogOn;
705 msg = PMA_messages.strGenLogOn;
709 if (msg.length === 0 && logVars.slow_query_log === 'ON') {
710 msg = PMA_messages.strSlowLogOn;
713 if (msg.length === 0) {
714 icon = PMA_getImage('s_error');
715 msg = PMA_messages.strBothLogOff;
718 str = '<b>' + PMA_messages.strCurrentSettings + '</b><br/><div class="smallIndent">';
719 str += icon + msg + '<br />';
721 if (logVars.log_output !== 'TABLE') {
722 str += PMA_getImage('s_error') + ' ' + PMA_messages.strLogOutNotTable + '<br />';
724 str += PMA_getImage('s_success') + ' ' + PMA_messages.strLogOutIsTable + '<br />';
727 if (logVars.slow_query_log === 'ON') {
728 if (logVars.long_query_time > 2) {
729 str += PMA_getImage('s_attention') + ' ';
730 str += PMA_sprintf(PMA_messages.strSmallerLongQueryTimeAdvice, logVars.long_query_time);
734 if (logVars.long_query_time < 2) {
735 str += PMA_getImage('s_success') + ' ';
736 str += PMA_sprintf(PMA_messages.strLongQueryTimeSet, logVars.long_query_time);
744 str += '<p></p><b>' + PMA_messages.strChangeSettings + '</b>';
745 str += '<div class="smallIndent">';
746 str += PMA_messages.strSettingsAppliedGlobal + '<br/>';
748 var varValue = 'TABLE';
749 if (logVars.log_output === 'TABLE') {
753 str += '- <a class="set" href="#log_output-' + varValue + '">';
754 str += PMA_sprintf(PMA_messages.strSetLogOutput, varValue);
755 str += ' </a><br />';
757 if (logVars.general_log !== 'ON') {
758 str += '- <a class="set" href="#general_log-ON">';
759 str += PMA_sprintf(PMA_messages.strEnableVar, 'general_log');
760 str += ' </a><br />';
762 str += '- <a class="set" href="#general_log-OFF">';
763 str += PMA_sprintf(PMA_messages.strDisableVar, 'general_log');
764 str += ' </a><br />';
767 if (logVars.slow_query_log !== 'ON') {
768 str += '- <a class="set" href="#slow_query_log-ON">';
769 str += PMA_sprintf(PMA_messages.strEnableVar, 'slow_query_log');
770 str += ' </a><br />';
772 str += '- <a class="set" href="#slow_query_log-OFF">';
773 str += PMA_sprintf(PMA_messages.strDisableVar, 'slow_query_log');
774 str += ' </a><br />';
778 if (logVars.long_query_time > 2) {
782 str += '- <a class="set" href="#long_query_time-' + varValue + '">';
783 str += PMA_sprintf(PMA_messages.setSetLongQueryTime, varValue);
784 str += ' </a><br />';
786 str += PMA_messages.strNoSuperUser + '<br/>';
791 $dialog.find('div.monitorUse').toggle(
792 logVars.log_output === 'TABLE' && (logVars.slow_query_log === 'ON' || logVars.general_log === 'ON')
795 $dialog.find('div.ajaxContent').html(str);
796 $dialog.find('img.ajaxIcon').hide();
797 $dialog.find('a.set').click(function () {
798 var nameValue = $(this).attr('href').split('-');
799 loadLogVars({ varName: nameValue[0].substr(1), varValue: nameValue[1] });
800 $dialog.find('img.ajaxIcon').show();
812 $('input[name="chartType"]').change(function () {
813 $('#chartVariableSettings').toggle(this.checked && this.value === 'variable');
814 var title = $('input[name="chartTitle"]').val();
815 if (title === PMA_messages.strChartTitle ||
816 title === $('label[for="' + $('input[name="chartTitle"]').data('lastRadio') + '"]').text()
818 $('input[name="chartTitle"]')
819 .data('lastRadio', $(this).attr('id'))
820 .val($('label[for="' + $(this).attr('id') + '"]').text());
824 $('input[name="useDivisor"]').change(function () {
825 $('span.divisorInput').toggle(this.checked);
828 $('input[name="useUnit"]').change(function () {
829 $('span.unitInput').toggle(this.checked);
832 $('select[name="varChartList"]').change(function () {
833 if (this.selectedIndex !== 0) {
834 $('#variableInput').val(this.value);
838 $('a[href="#kibDivisor"]').click(function (event) {
839 event.preventDefault();
840 $('input[name="valueDivisor"]').val(1024);
841 $('input[name="valueUnit"]').val(PMA_messages.strKiB);
842 $('span.unitInput').toggle(true);
843 $('input[name="useUnit"]').prop('checked', true);
847 $('a[href="#mibDivisor"]').click(function (event) {
848 event.preventDefault();
849 $('input[name="valueDivisor"]').val(1024 * 1024);
850 $('input[name="valueUnit"]').val(PMA_messages.strMiB);
851 $('span.unitInput').toggle(true);
852 $('input[name="useUnit"]').prop('checked', true);
856 $('a[href="#submitClearSeries"]').click(function (event) {
857 event.preventDefault();
858 $('#seriesPreview').html('<i>' + PMA_messages.strNone + '</i>');
860 $('#clearSeriesLink').hide();
863 $('a[href="#submitAddSeries"]').click(function (event) {
864 event.preventDefault();
865 if ($('#variableInput').val() === '') {
869 if (newChart === null) {
870 $('#seriesPreview').html('');
873 title: $('input[name="chartTitle"]').val(),
881 dataPoints: [{ type: 'statusvar', name: $('#variableInput').val() }],
882 display: $('input[name="differentialValue"]').prop('checked') ? 'differential' : ''
885 if (serie.dataPoints[0].name === 'Processes') {
886 serie.dataPoints[0].type = 'proc';
889 if ($('input[name="useDivisor"]').prop('checked')) {
890 serie.valueDivisor = parseInt($('input[name="valueDivisor"]').val(), 10);
893 if ($('input[name="useUnit"]').prop('checked')) {
894 serie.unit = $('input[name="valueUnit"]').val();
897 var str = serie.display === 'differential' ? ', ' + PMA_messages.strDifferential : '';
898 str += serie.valueDivisor ? (', ' + PMA_sprintf(PMA_messages.strDividedBy, serie.valueDivisor)) : '';
899 str += serie.unit ? (', ' + PMA_messages.strUnit + ': ' + serie.unit) : '';
902 label: $('#variableInput').val().replace(/_/g, ' ')
904 newChart.series.push(newSeries);
905 $('#seriesPreview').append('- ' + escapeHtml(newSeries.label + str) + '<br/>');
906 newChart.nodes.push(serie);
907 $('#variableInput').val('');
908 $('input[name="differentialValue"]').prop('checked', true);
909 $('input[name="useDivisor"]').prop('checked', false);
910 $('input[name="useUnit"]').prop('checked', false);
911 $('input[name="useDivisor"]').trigger('change');
912 $('input[name="useUnit"]').trigger('change');
913 $('select[name="varChartList"]').get(0).selectedIndex = 0;
915 $('#clearSeriesLink').show();
920 $('#variableInput').autocomplete({
921 source: variableNames
924 /* Initializes the monitor, called only once */
925 function initGrid () {
928 /* Apply default values & config */
929 if (isStorageSupported('localStorage')) {
930 if (typeof window.localStorage.monitorCharts !== 'undefined') {
931 runtime.charts = JSON.parse(window.localStorage.monitorCharts);
933 if (typeof window.localStorage.monitorSettings !== 'undefined') {
934 monitorSettings = JSON.parse(window.localStorage.monitorSettings);
937 $('a[href="#clearMonitorConfig"]').toggle(runtime.charts !== null);
939 if (runtime.charts !== null
940 && typeof window.localStorage.monitorVersion !== 'undefined'
941 && monitorProtocolVersion !== window.localStorage.monitorVersion
943 $('#emptyDialog').dialog({ title: PMA_messages.strIncompatibleMonitorConfig });
944 $('#emptyDialog').html(PMA_messages.strIncompatibleMonitorConfigDescription);
947 dlgBtns[PMA_messages.strClose] = function () {
948 $(this).dialog('close');
951 $('#emptyDialog').dialog({
958 if (runtime.charts === null) {
959 runtime.charts = defaultChartGrid;
961 if (monitorSettings === null) {
962 monitorSettings = defaultMonitorSettings;
965 $('select[name="gridChartRefresh"]').val(monitorSettings.gridRefresh / 1000);
966 $('select[name="chartColumns"]').val(monitorSettings.columns);
968 if (monitorSettings.gridMaxPoints === 'auto') {
969 runtime.gridMaxPoints = Math.round((monitorSettings.chartSize.width - 40) / 12);
971 runtime.gridMaxPoints = monitorSettings.gridMaxPoints;
974 runtime.xmin = new Date().getTime() - server_time_diff - runtime.gridMaxPoints * monitorSettings.gridRefresh;
975 runtime.xmax = new Date().getTime() - server_time_diff + monitorSettings.gridRefresh;
977 /* Calculate how much spacing there is between each chart */
978 $('#chartGrid').html('<tr><td></td><td></td></tr><tr><td></td><td></td></tr>');
980 width: $('#chartGrid').find('td:nth-child(2)').offset().left -
981 $('#chartGrid').find('td:nth-child(1)').offset().left,
982 height: $('#chartGrid').find('tr:nth-child(2) td:nth-child(2)').offset().top -
983 $('#chartGrid').find('tr:nth-child(1) td:nth-child(1)').offset().top
985 $('#chartGrid').html('');
987 /* Add all charts - in correct order */
989 $.each(runtime.charts, function (key, value) {
993 for (i = 0; i < keys.length; i++) {
994 addChart(runtime.charts[keys[i]], true);
997 /* Fill in missing cells */
998 var numCharts = $('#chartGrid').find('.monitorChart').length;
999 var numMissingCells = (monitorSettings.columns - numCharts % monitorSettings.columns) % monitorSettings.columns;
1000 for (i = 0; i < numMissingCells; i++) {
1001 $('#chartGrid').find('tr:last').append('<td></td>');
1004 // Empty cells should keep their size so you can drop onto them
1005 calculateChartSize();
1006 $('#chartGrid').find('tr td').css('width', chartSize.width + 'px');
1008 buildRequiredDataList();
1012 /* Calls destroyGrid() and initGrid(), but before doing so it saves the chart
1013 * data from each chart and restores it after the monitor is initialized again */
1014 function rebuildGrid () {
1016 if (runtime.charts) {
1018 $.each(runtime.charts, function (key, chartObj) {
1019 for (var i = 0, l = chartObj.nodes.length; i < l; i++) {
1020 oldData[chartObj.nodes[i].dataPoint] = [];
1021 for (var j = 0, ll = chartObj.chart.series[i].data.length; j < ll; j++) {
1022 oldData[chartObj.nodes[i].dataPoint].push([chartObj.chart.series[i].data[j].x, chartObj.chart.series[i].data[j].y]);
1032 /* Calculactes the dynamic chart size that depends on the column width */
1033 function calculateChartSize () {
1035 if ($('body').height() > $(window).height()) { // has vertical scroll bar
1036 panelWidth = $('#logTable').innerWidth();
1038 panelWidth = $('#logTable').innerWidth() - 10; // leave some space for vertical scroll bar
1041 var wdt = panelWidth;
1042 var windowWidth = $(window).width();
1044 if (windowWidth > 768) {
1045 wdt = (panelWidth - monitorSettings.columns * chartSpacing.width) / monitorSettings.columns;
1049 width: Math.floor(wdt),
1050 height: Math.floor(0.75 * wdt)
1054 /* Adds a chart to the chart grid */
1055 function addChart (chartObj, initialize) {
1058 title: escapeHtml(chartObj.title),
1062 background: 'rgba(0,0,0,0)'
1066 renderer: $.jqplot.DateAxisRenderer,
1068 formatString: '%H:%M:%S',
1095 if (settings.title === PMA_messages.strSystemCPUUsage ||
1096 settings.title === PMA_messages.strQueryCacheEfficiency
1098 settings.axes.yaxis.tickOptions = {
1099 formatString: '%d %%'
1101 } else if (settings.title === PMA_messages.strSystemMemory ||
1102 settings.title === PMA_messages.strSystemSwap
1104 settings.stackSeries = true;
1105 settings.axes.yaxis.tickOptions = {
1106 formatter: $.jqplot.byteFormatter(2) // MiB
1108 } else if (settings.title === PMA_messages.strTraffic) {
1109 settings.axes.yaxis.tickOptions = {
1110 formatter: $.jqplot.byteFormatter(1) // KiB
1112 } else if (settings.title === PMA_messages.strQuestions ||
1113 settings.title === PMA_messages.strConnections
1115 settings.axes.yaxis.tickOptions = {
1116 formatter: function (format, val) {
1117 if (Math.abs(val) >= 1000000) {
1118 return $.jqplot.sprintf('%.3g M', val / 1000000);
1119 } else if (Math.abs(val) >= 1000) {
1120 return $.jqplot.sprintf('%.3g k', val / 1000);
1122 return $.jqplot.sprintf('%d', val);
1128 settings.series = chartObj.series;
1130 if ($('#' + 'gridchart' + runtime.chartAI).length === 0) {
1131 var numCharts = $('#chartGrid').find('.monitorChart').length;
1133 if (numCharts === 0 || (numCharts % monitorSettings.columns === 0)) {
1134 $('#chartGrid').append('<tr></tr>');
1138 calculateChartSize();
1140 $('#chartGrid').find('tr:last').append(
1141 '<td><div id="gridChartContainer' + runtime.chartAI + '" class="">' +
1142 '<div class="ui-state-default monitorChart"' +
1143 ' id="gridchart' + runtime.chartAI + '"' +
1144 ' style="width:' + chartSize.width + 'px; height:' + chartSize.height + 'px;"></div>' +
1149 // Set series' data as [0,0], smooth lines won't plot with data array having null values.
1150 // also chart won't plot initially with no data and data comes on refreshChartGrid()
1152 for (i in chartObj.series) {
1153 series.push([[0, 0]]);
1156 var tempTooltipContentEditor = function (str, seriesIndex, pointIndex, plot) {
1158 // TODO: move style to theme CSS
1159 var tooltipHtml = '<div style="font-size:12px;background-color:#FFFFFF;' +
1160 'opacity:0.95;filter:alpha(opacity=95);padding:5px;">';
1161 // x value i.e. time
1162 var timeValue = str.split(',')[0];
1164 tooltipHtml += 'Time: ' + timeValue;
1165 tooltipHtml += '<span style="font-weight:bold;">';
1166 // Add y values to the tooltip per series
1167 for (j in plot.series) {
1168 // get y value if present
1169 if (plot.series[j].data.length > pointIndex) {
1170 seriesValue = plot.series[j].data[pointIndex][1];
1174 var seriesLabel = plot.series[j].label;
1175 var seriesColor = plot.series[j].color;
1177 if (plot.series[0]._yaxis.tickOptions.formatter) {
1178 // using formatter function
1179 seriesValue = plot.series[0]._yaxis.tickOptions.formatter('%s', seriesValue);
1180 } else if (plot.series[0]._yaxis.tickOptions.formatString) {
1181 // using format string
1182 seriesValue = PMA_sprintf(plot.series[0]._yaxis.tickOptions.formatString, seriesValue);
1184 tooltipHtml += '<br /><span style="color:' + seriesColor + '">' +
1185 seriesLabel + ': ' + seriesValue + '</span>';
1187 tooltipHtml += '</span></div>';
1191 // set Tooltip for each series
1192 for (i in settings.series) {
1193 settings.series[i].highlighter = {
1195 tooltipContentEditor: tempTooltipContentEditor
1199 chartObj.chart = $.jqplot('gridchart' + runtime.chartAI, series, settings);
1200 // remove [0,0] after plotting
1201 for (i in chartObj.chart.series) {
1202 chartObj.chart.series[i].data.shift();
1205 var $legend = $('<div />').css('padding', '0.5em');
1206 for (i in chartObj.chart.series) {
1208 $('<div />').append(
1212 background: chartObj.chart.seriesColors[i]
1213 }).addClass('floatleft')
1216 chartObj.chart.series[i].label
1217 ).addClass('floatleft')
1219 $('<div class="clearfloat">')
1220 ).addClass('floatleft')
1223 $('#gridchart' + runtime.chartAI)
1227 if (initialize !== true) {
1228 runtime.charts['c' + runtime.chartAI] = chartObj;
1229 buildRequiredDataList();
1232 // time span selection
1233 $('#gridchart' + runtime.chartAI).on('jqplotMouseDown', function (ev, gridpos, datapos, neighbor, plot) {
1234 drawTimeSpan = true;
1235 selectionTimeDiff.push(datapos.xaxis);
1236 if ($('#selection_box').length) {
1237 $('#selection_box').remove();
1239 var selectionBox = $('<div id="selection_box" style="z-index:1000;height:205px;position:absolute;background-color:#87CEEB;opacity:0.4;filter:alpha(opacity=40);pointer-events:none;">');
1240 $(document.body).append(selectionBox);
1241 selectionStartX = ev.pageX;
1242 selectionStartY = ev.pageY;
1244 .attr({ id: 'selection_box' })
1246 top: selectionStartY - gridpos.y,
1247 left: selectionStartX
1252 $('#gridchart' + runtime.chartAI).on('jqplotMouseUp', function (ev, gridpos, datapos, neighbor, plot) {
1253 if (! drawTimeSpan || editMode) {
1257 selectionTimeDiff.push(datapos.xaxis);
1259 if (selectionTimeDiff[1] <= selectionTimeDiff[0]) {
1260 selectionTimeDiff = [];
1263 // get date from timestamp
1264 var min = new Date(Math.ceil(selectionTimeDiff[0]));
1265 var max = new Date(Math.ceil(selectionTimeDiff[1]));
1266 PMA_getLogAnalyseDialog(min, max);
1267 selectionTimeDiff = [];
1268 drawTimeSpan = false;
1271 $('#gridchart' + runtime.chartAI).on('jqplotMouseMove', function (ev, gridpos, datapos, neighbor, plot) {
1272 if (! drawTimeSpan || editMode) {
1275 if (selectionStartX !== undefined) {
1278 width: Math.ceil(ev.pageX - selectionStartX)
1284 $('#gridchart' + runtime.chartAI).on('jqplotMouseLeave', function (ev, gridpos, datapos, neighbor, plot) {
1285 drawTimeSpan = false;
1288 $(document.body).mouseup(function () {
1289 if ($('#selection_box').length) {
1290 $('#selection_box').remove();
1294 // Edit, Print icon only in edit mode
1295 $('#chartGrid').find('div svg').find('*[zIndex=20], *[zIndex=21], *[zIndex=19]').toggle(editMode);
1300 function PMA_getLogAnalyseDialog (min, max) {
1301 var $logAnalyseDialog = $('#logAnalyseDialog');
1302 var $dateStart = $logAnalyseDialog.find('input[name="dateStart"]');
1303 var $dateEnd = $logAnalyseDialog.find('input[name="dateEnd"]');
1304 $dateStart.prop('readonly', true);
1305 $dateEnd.prop('readonly', true);
1309 dlgBtns[PMA_messages.strFromSlowLog] = function () {
1310 loadLog('slow', min, max);
1311 $(this).dialog('close');
1314 dlgBtns[PMA_messages.strFromGeneralLog] = function () {
1315 loadLog('general', min, max);
1316 $(this).dialog('close');
1319 $logAnalyseDialog.dialog({
1325 PMA_addDatepicker($dateStart, 'datetime', {
1326 showMillisec: false,
1327 showMicrosec: false,
1328 timeFormat: 'HH:mm:ss'
1330 PMA_addDatepicker($dateEnd, 'datetime', {
1331 showMillisec: false,
1332 showMicrosec: false,
1333 timeFormat: 'HH:mm:ss'
1335 $dateStart.datepicker('setDate', min);
1336 $dateEnd.datepicker('setDate', max);
1339 function loadLog (type, min, max) {
1340 var dateStart = Date.parse($('#logAnalyseDialog').find('input[name="dateStart"]').datepicker('getDate')) || min;
1341 var dateEnd = Date.parse($('#logAnalyseDialog').find('input[name="dateEnd"]').datepicker('getDate')) || max;
1347 removeVariables: $('#removeVariables').prop('checked'),
1348 limitTypes: $('#limitTypes').prop('checked')
1352 /* Called in regular intervals, this function updates the values of each chart in the grid */
1353 function refreshChartGrid () {
1354 /* Send to server */
1355 runtime.refreshRequest = $.post('server_status_monitor.php' + PMA_commonParams.get('common_query'), {
1359 requiredData: JSON.stringify(runtime.dataList),
1360 server: PMA_commonParams.get('server')
1361 }, function (data) {
1363 if (typeof data !== 'undefined' && data.success === true) {
1364 chartData = data.message;
1366 return serverResponseError();
1373 /* Update values in each graph */
1374 $.each(runtime.charts, function (orderKey, elem) {
1375 var key = elem.chartID;
1376 // If newly added chart, we have no data for it yet
1377 if (! chartData[key]) {
1382 for (var j = 0; j < elem.nodes.length; j++) {
1384 if (i === 0 && j === 0) {
1385 if (oldChartData === null) {
1386 diff = chartData.x - runtime.xmax;
1388 diff = parseInt(chartData.x - oldChartData.x, 10);
1391 runtime.xmin += diff;
1392 runtime.xmax += diff;
1395 // elem.chart.xAxis[0].setExtremes(runtime.xmin, runtime.xmax, false);
1396 /* Calculate y value */
1398 // If transform function given, use it
1399 if (elem.nodes[j].transformFn) {
1400 value = chartValueTransform(
1401 elem.nodes[j].transformFn,
1403 // Check if first iteration (oldChartData==null), or if newly added chart oldChartData[key]==null
1405 oldChartData === null ||
1406 oldChartData[key] === null ||
1407 oldChartData[key] === undefined ? null : oldChartData[key][j]
1411 // Otherwise use original value and apply differential and divisor if given,
1412 // in this case we have only one data point per series - located at chartData[key][j][0]
1414 value = parseFloat(chartData[key][j][0].value);
1416 if (elem.nodes[j].display === 'differential') {
1417 if (oldChartData === null ||
1418 oldChartData[key] === null ||
1419 oldChartData[key] === undefined
1423 value -= oldChartData[key][j][0].value;
1426 if (elem.nodes[j].valueDivisor) {
1427 value = value / elem.nodes[j].valueDivisor;
1431 // Set y value, if defined
1432 if (value !== undefined) {
1433 elem.chart.series[j].data.push([chartData.x, value]);
1434 if (value > elem.maxYLabel) {
1435 elem.maxYLabel = value;
1436 } else if (elem.maxYLabel === 0) {
1437 elem.maxYLabel = 0.5;
1439 // free old data point values and update maxYLabel
1440 if (elem.chart.series[j].data.length > runtime.gridMaxPoints &&
1441 elem.chart.series[j].data[0][0] < runtime.xmin
1443 // check if the next freeable point is highest
1444 if (elem.maxYLabel <= elem.chart.series[j].data[0][1]) {
1445 elem.chart.series[j].data.splice(0, elem.chart.series[j].data.length - runtime.gridMaxPoints);
1446 elem.maxYLabel = getMaxYLabel(elem.chart.series[j].data);
1448 elem.chart.series[j].data.splice(0, elem.chart.series[j].data.length - runtime.gridMaxPoints);
1451 if (elem.title === PMA_messages.strSystemMemory ||
1452 elem.title === PMA_messages.strSystemSwap
1459 // update chart options
1460 // keep ticks number/positioning consistent while refreshrate changes
1461 var tickInterval = (runtime.xmax - runtime.xmin) / 5;
1462 elem.chart.axes.xaxis.ticks = [(runtime.xmax - tickInterval * 4),
1463 (runtime.xmax - tickInterval * 3), (runtime.xmax - tickInterval * 2),
1464 (runtime.xmax - tickInterval), runtime.xmax];
1466 if (elem.title !== PMA_messages.strSystemCPUUsage &&
1467 elem.title !== PMA_messages.strQueryCacheEfficiency &&
1468 elem.title !== PMA_messages.strSystemMemory &&
1469 elem.title !== PMA_messages.strSystemSwap
1471 elem.chart.axes.yaxis.max = Math.ceil(elem.maxYLabel * 1.1);
1472 elem.chart.axes.yaxis.tickInterval = Math.ceil(elem.maxYLabel * 1.1 / 5);
1473 } else if (elem.title === PMA_messages.strSystemMemory ||
1474 elem.title === PMA_messages.strSystemSwap
1476 elem.chart.axes.yaxis.max = Math.ceil(total * 1.1 / 100) * 100;
1477 elem.chart.axes.yaxis.tickInterval = Math.ceil(total * 1.1 / 5);
1481 if (runtime.redrawCharts) {
1482 elem.chart.replot();
1486 oldChartData = chartData;
1488 runtime.refreshTimeout = setTimeout(refreshChartGrid, monitorSettings.gridRefresh);
1492 /* Function to get highest plotted point's y label, to scale the chart,
1493 * TODO: make jqplot's autoscale:true work here
1495 function getMaxYLabel (dataValues) {
1496 var maxY = dataValues[0][1];
1497 $.each(dataValues, function (k, v) {
1498 maxY = (v[1] > maxY) ? v[1] : maxY;
1503 /* Function that supplies special value transform functions for chart values */
1504 function chartValueTransform (name, cur, prev) {
1507 if (prev === null) {
1510 // cur and prev are datapoint arrays, but containing
1511 // only 1 element for cpu-linux
1515 var diff_total = cur.busy + cur.idle - (prev.busy + prev.idle);
1516 var diff_idle = cur.idle - prev.idle;
1517 return 100 * (diff_total - diff_idle) / diff_total;
1519 // Query cache efficiency (%)
1521 if (prev === null) {
1524 // cur[0].value is Qcache_hits, cur[1].value is Com_select
1525 var diffQHits = cur[0].value - prev[0].value;
1526 // No NaN please :-)
1527 if (cur[1].value - prev[1].value === 0) {
1531 return diffQHits / (cur[1].value - prev[1].value + diffQHits) * 100;
1533 // Query cache usage (%)
1535 if (cur[1].value === 0) {
1538 // cur[0].value is Qcache_free_memory, cur[1].value is query_cache_size
1539 return 100 - cur[0].value / cur[1].value * 100;
1544 /* Build list of nodes that need to be retrieved from server.
1545 * It creates something like a stripped down version of the runtime.charts object.
1547 function buildRequiredDataList () {
1548 runtime.dataList = {};
1549 // Store an own id, because the property name is subject of reordering,
1550 // thus destroying our mapping with runtime.charts <=> runtime.dataList
1552 $.each(runtime.charts, function (key, chart) {
1553 runtime.dataList[chartID] = [];
1554 for (var i = 0, l = chart.nodes.length; i < l; i++) {
1555 runtime.dataList[chartID][i] = chart.nodes[i].dataPoints;
1557 runtime.charts[key].chartID = chartID;
1562 /* Loads the log table data, generates the table and handles the filters */
1563 function loadLogStatistics (opts) {
1565 var logRequest = null;
1567 if (! opts.removeVariables) {
1568 opts.removeVariables = false;
1570 if (! opts.limitTypes) {
1571 opts.limitTypes = false;
1574 $('#emptyDialog').dialog({ title: PMA_messages.strAnalysingLogsTitle });
1575 $('#emptyDialog').html(PMA_messages.strAnalysingLogs +
1576 ' <img class="ajaxIcon" src="' + pmaThemeImage +
1577 'ajax_clock_small.gif" alt="">');
1580 dlgBtns[PMA_messages.strCancelRequest] = function () {
1581 if (logRequest !== null) {
1585 $(this).dialog('close');
1588 $('#emptyDialog').dialog({
1594 logRequest = $.post(
1595 'server_status_monitor.php' + PMA_commonParams.get('common_query'),
1600 time_start: Math.round(opts.start / 1000),
1601 time_end: Math.round(opts.end / 1000),
1602 removeVariables: opts.removeVariables,
1603 limitTypes: opts.limitTypes
1608 if (typeof data !== 'undefined' && data.success === true) {
1609 logData = data.message;
1611 return serverResponseError();
1614 if (logData.rows.length === 0) {
1615 $('#emptyDialog').dialog({ title: PMA_messages.strNoDataFoundTitle });
1616 $('#emptyDialog').html('<p>' + PMA_messages.strNoDataFound + '</p>');
1618 dlgBtns[PMA_messages.strClose] = function () {
1619 $(this).dialog('close');
1622 $('#emptyDialog').dialog('option', 'buttons', dlgBtns);
1626 runtime.logDataCols = buildLogTable(logData, opts.removeVariables);
1628 /* Show some stats in the dialog */
1629 $('#emptyDialog').dialog({ title: PMA_messages.strLoadingLogs });
1630 $('#emptyDialog').html('<p>' + PMA_messages.strLogDataLoaded + '</p>');
1631 $.each(logData.sum, function (key, value) {
1632 key = key.charAt(0).toUpperCase() + key.slice(1).toLowerCase();
1633 if (key === 'Total') {
1634 key = '<b>' + key + '</b>';
1636 $('#emptyDialog').append(key + ': ' + value + '<br/>');
1639 /* Add filter options if more than a bunch of rows there to filter */
1640 if (logData.numRows > 12) {
1641 $('#logTable').prepend(
1642 '<fieldset id="logDataFilter">' +
1643 ' <legend>' + PMA_messages.strFiltersForLogTable + '</legend>' +
1644 ' <div class="formelement">' +
1645 ' <label for="filterQueryText">' + PMA_messages.strFilterByWordRegexp + '</label>' +
1646 ' <input name="filterQueryText" type="text" id="filterQueryText" style="vertical-align: baseline;" />' +
1648 ((logData.numRows > 250) ? ' <div class="formelement"><button name="startFilterQueryText" id="startFilterQueryText">' + PMA_messages.strFilter + '</button></div>' : '') +
1649 ' <div class="formelement">' +
1650 ' <input type="checkbox" id="noWHEREData" name="noWHEREData" value="1" /> ' +
1651 ' <label for="noWHEREData"> ' + PMA_messages.strIgnoreWhereAndGroup + '</label>' +
1656 $('#noWHEREData').change(function () {
1657 filterQueries(true);
1660 if (logData.numRows > 250) {
1661 $('#startFilterQueryText').click(filterQueries);
1663 $('#filterQueryText').keyup(filterQueries);
1667 dlgBtns[PMA_messages.strJumpToTable] = function () {
1668 $(this).dialog('close');
1669 $(document).scrollTop($('#logTable').offset().top);
1672 $('#emptyDialog').dialog('option', 'buttons', dlgBtns);
1676 /* Handles the actions performed when the user uses any of the
1677 * log table filters which are the filter by name and grouping
1678 * with ignoring data in WHERE clauses
1680 * @param boolean Should be true when the users enabled or disabled
1681 * to group queries ignoring data in WHERE clauses
1683 function filterQueries (varFilterChange) {
1686 var val = $('#filterQueryText').val();
1688 if (val.length === 0) {
1692 textFilter = new RegExp(val, 'i');
1693 $('#filterQueryText').removeClass('error');
1695 if (e instanceof SyntaxError) {
1696 $('#filterQueryText').addClass('error');
1706 var noVars = $('#noWHEREData').prop('checked');
1707 var equalsFilter = /([^=]+)=(\d+|((\'|"|).*?[^\\])\4((\s+)|$))/gi;
1708 var functionFilter = /([a-z0-9_]+)\(.+?\)/gi;
1709 var filteredQueries = {};
1710 var filteredQueriesLines = {};
1713 var queryColumnName = runtime.logDataCols[runtime.logDataCols.length - 2];
1714 var sumColumnName = runtime.logDataCols[runtime.logDataCols.length - 1];
1715 var isSlowLog = opts.src === 'slow';
1716 var columnSums = {};
1718 // For the slow log we have to count many columns (query_time, lock_time, rows_examined, rows_sent, etc.)
1719 var countRow = function (query, row) {
1720 var cells = row.match(/<td>(.*?)<\/td>/gi);
1721 if (!columnSums[query]) {
1722 columnSums[query] = [0, 0, 0, 0];
1725 // lock_time and query_time and displayed in timespan format
1726 columnSums[query][0] += timeToSec(cells[2].replace(/(<td>|<\/td>)/gi, ''));
1727 columnSums[query][1] += timeToSec(cells[3].replace(/(<td>|<\/td>)/gi, ''));
1728 // rows_examind and rows_sent are just numbers
1729 columnSums[query][2] += parseInt(cells[4].replace(/(<td>|<\/td>)/gi, ''), 10);
1730 columnSums[query][3] += parseInt(cells[5].replace(/(<td>|<\/td>)/gi, ''), 10);
1733 // We just assume the sql text is always in the second last column, and that the total count is right of it
1734 $('#logTable').find('table tbody tr td:nth-child(' + (runtime.logDataCols.length - 1) + ')').each(function () {
1736 // If query is a SELECT and user enabled or disabled to group
1737 // queries ignoring data in where statements, we
1738 // need to re-calculate the sums of each row
1739 if (varFilterChange && $t.html().match(/^SELECT/i)) {
1741 // Group on => Sum up identical columns, and hide all but 1
1743 q = $t.text().replace(equalsFilter, '$1=...$6').trim();
1744 q = q.replace(functionFilter, ' $1(...)');
1746 // Js does not specify a limit on property name length,
1747 // so we can abuse it as index :-)
1748 if (filteredQueries[q]) {
1749 filteredQueries[q] += parseInt($t.next().text(), 10);
1750 totalSum += parseInt($t.next().text(), 10);
1753 filteredQueries[q] = parseInt($t.next().text(), 10);
1754 filteredQueriesLines[q] = i;
1758 countRow(q, $t.parent().html());
1761 // Group off: Restore original columns
1763 rowData = $t.parent().data('query');
1765 $t.text(rowData[queryColumnName]);
1766 // Restore total count
1767 $t.next().text(rowData[sumColumnName]);
1768 // Restore slow log columns
1770 $t.parent().children('td:nth-child(3)').text(rowData.query_time);
1771 $t.parent().children('td:nth-child(4)').text(rowData.lock_time);
1772 $t.parent().children('td:nth-child(5)').text(rowData.rows_sent);
1773 $t.parent().children('td:nth-child(6)').text(rowData.rows_examined);
1778 // If not required to be hidden, do we need
1779 // to hide because of a not matching text filter?
1780 if (! hide && (textFilter !== null && ! textFilter.exec($t.text()))) {
1784 // Now display or hide this column
1786 $t.parent().css('display', 'none');
1788 totalSum += parseInt($t.next().text(), 10);
1790 $t.parent().css('display', '');
1797 // We finished summarizing counts => Update count values of all grouped entries
1798 if (varFilterChange) {
1802 var $table = $('#logTable').find('table tbody');
1803 $.each(filteredQueriesLines, function (key, value) {
1804 if (filteredQueries[key] <= 1) {
1808 row = $table.children('tr:nth-child(' + (value + 1) + ')');
1809 numCol = row.children(':nth-child(' + (runtime.logDataCols.length) + ')');
1810 numCol.text(filteredQueries[key]);
1813 row.children('td:nth-child(3)').text(secToTime(columnSums[key][0]));
1814 row.children('td:nth-child(4)').text(secToTime(columnSums[key][1]));
1815 row.children('td:nth-child(5)').text(columnSums[key][2]);
1816 row.children('td:nth-child(6)').text(columnSums[key][3]);
1821 $('#logTable').find('table').trigger('update');
1822 setTimeout(function () {
1823 $('#logTable').find('table').trigger('sorton', [[[runtime.logDataCols.length - 1, 1]]]);
1827 // Display some stats at the bottom of the table
1828 $('#logTable').find('table tfoot tr')
1829 .html('<th colspan="' + (runtime.logDataCols.length - 1) + '">' +
1830 PMA_messages.strSumRows + ' ' + rowSum + '<span class="floatright">' +
1831 PMA_messages.strTotal + '</span></th><th class="right">' + totalSum + '</th>');
1835 /* Turns a timespan (12:12:12) into a number */
1836 function timeToSec (timeStr) {
1837 var time = timeStr.split(':');
1838 return (parseInt(time[0], 10) * 3600) + (parseInt(time[1], 10) * 60) + parseInt(time[2], 10);
1841 /* Turns a number into a timespan (100 into 00:01:40) */
1842 function secToTime (timeInt) {
1843 var hours = Math.floor(timeInt / 3600);
1844 timeInt -= hours * 3600;
1845 var minutes = Math.floor(timeInt / 60);
1846 timeInt -= minutes * 60;
1849 hours = '0' + hours;
1852 minutes = '0' + minutes;
1855 timeInt = '0' + timeInt;
1858 return hours + ':' + minutes + ':' + timeInt;
1861 /* Constructs the log table out of the retrieved server data */
1862 function buildLogTable (data, groupInserts) {
1863 var rows = data.rows;
1865 var $table = $('<table class="sortable"></table>');
1870 $('#logTable').html($table);
1872 var tempPushKey = function (key, value) {
1876 var formatValue = function (name, value) {
1877 if (name === 'user_host') {
1878 return value.replace(/(\[.*?\])+/g, '');
1880 return escapeHtml(value);
1883 for (var i = 0, l = rows.length; i < l; i++) {
1885 $.each(rows[0], tempPushKey);
1886 $table.append('<thead>' +
1887 '<tr><th class="nowrap">' + cols.join('</th><th class="nowrap">') + '</th></tr>' +
1891 $table.append($tBody = $('<tbody></tbody>'));
1894 $tBody.append($tRow = $('<tr class="noclick"></tr>'));
1896 for (var j = 0, ll = cols.length; j < ll; j++) {
1897 // Assuming the query column is the second last
1898 if (j === cols.length - 2 && rows[i][cols[j]].match(/^SELECT/i)) {
1899 $tRow.append($tCell = $('<td class="linkElem">' + formatValue(cols[j], rows[i][cols[j]]) + '</td>'));
1900 $tCell.click(openQueryAnalyzer);
1902 $tRow.append('<td>' + formatValue(cols[j], rows[i][cols[j]]) + '</td>');
1905 $tRow.data('query', rows[i]);
1909 $table.append('<tfoot>' +
1910 '<tr><th colspan="' + (cols.length - 1) + '">' + PMA_messages.strSumRows +
1911 ' ' + data.numRows + '<span class="floatright">' + PMA_messages.strTotal +
1912 '</span></th><th class="right">' + data.sum.TOTAL + '</th></tr></tfoot>');
1914 // Append a tooltip to the count column, if there exist one
1915 if ($('#logTable').find('tr:first th:last').text().indexOf('#') > -1) {
1916 $('#logTable').find('tr:first th:last').append(' ' + PMA_getImage('b_help', '', { 'class': 'qroupedQueryInfoIcon' }));
1918 var tooltipContent = PMA_messages.strCountColumnExplanation;
1920 tooltipContent += '<p>' + PMA_messages.strMoreCountColumnExplanation + '</p>';
1924 $('img.qroupedQueryInfoIcon'),
1930 $('#logTable').find('table').tablesorter({
1931 sortList: [[cols.length - 1, 1]],
1932 widgets: ['fast-zebra']
1935 $('#logTable').find('table thead th')
1936 .append('<div class="sorticon"></div>');
1941 /* Opens the query analyzer dialog */
1942 function openQueryAnalyzer () {
1943 var rowData = $(this).parent().data('query');
1944 var query = rowData.argument || rowData.sql_text;
1946 if (codemirror_editor) {
1947 // TODO: somehow PMA_SQLPrettyPrint messes up the query, needs be fixed
1948 // query = PMA_SQLPrettyPrint(query);
1949 codemirror_editor.setValue(query);
1950 // Codemirror is bugged, it doesn't refresh properly sometimes.
1951 // Following lines seem to fix that
1952 setTimeout(function () {
1953 codemirror_editor.refresh();
1956 $('#sqlquery').val(query);
1959 var profilingChart = null;
1962 dlgBtns[PMA_messages.strAnalyzeQuery] = function () {
1963 loadQueryAnalysis(rowData);
1965 dlgBtns[PMA_messages.strClose] = function () {
1966 $(this).dialog('close');
1969 $('#queryAnalyzerDialog').dialog({
1974 close: function () {
1975 if (profilingChart !== null) {
1976 profilingChart.destroy();
1978 $('#queryAnalyzerDialog').find('div.placeHolder').html('');
1979 if (codemirror_editor) {
1980 codemirror_editor.setValue('');
1982 $('#sqlquery').val('');
1988 /* Loads and displays the analyzed query data */
1989 function loadQueryAnalysis (rowData) {
1990 var db = rowData.db || '';
1992 $('#queryAnalyzerDialog').find('div.placeHolder').html(
1993 PMA_messages.strAnalyzing + ' <img class="ajaxIcon" src="' +
1994 pmaThemeImage + 'ajax_clock_small.gif" alt="">');
1996 $.post('server_status_monitor.php' + PMA_commonParams.get('common_query'), {
1998 query_analyzer: true,
1999 query: codemirror_editor ? codemirror_editor.getValue() : $('#sqlquery').val(),
2001 server: PMA_commonParams.get('server')
2002 }, function (data) {
2005 if (typeof data !== 'undefined' && data.success === true) {
2006 data = data.message;
2009 if (data.error.indexOf('1146') !== -1 || data.error.indexOf('1046') !== -1) {
2010 data.error = PMA_messages.strServerLogError;
2012 $('#queryAnalyzerDialog').find('div.placeHolder').html('<div class="error">' + data.error + '</div>');
2016 // Float sux, I'll use table :(
2017 $('#queryAnalyzerDialog').find('div.placeHolder')
2018 .html('<table width="100%" border="0"><tr><td class="explain"></td><td class="chart"></td></tr></table>');
2020 var explain = '<b>' + PMA_messages.strExplainOutput + '</b> ' + $('#explain_docu').html();
2021 if (data.explain.length > 1) {
2023 for (i = 0; i < data.explain.length; i++) {
2027 explain += '<a href="#showExplain-' + i + '">' + i + '</a>';
2031 explain += '<p></p>';
2033 var tempExplain = function (key, value) {
2034 value = (value === null) ? 'null' : escapeHtml(value);
2036 if (key === 'type' && value.toLowerCase() === 'all') {
2037 value = '<span class="attention">' + value + '</span>';
2039 if (key === 'Extra') {
2040 value = value.replace(/(using (temporary|filesort))/gi, '<span class="attention">$1</span>');
2042 explain += key + ': ' + value + '<br />';
2045 for (i = 0, l = data.explain.length; i < l; i++) {
2046 explain += '<div class="explain-' + i + '"' + (i > 0 ? 'style="display:none;"' : '') + '>';
2047 $.each(data.explain[i], tempExplain);
2048 explain += '</div>';
2051 explain += '<p><b>' + PMA_messages.strAffectedRows + '</b> ' + data.affectedRows;
2053 $('#queryAnalyzerDialog').find('div.placeHolder td.explain').append(explain);
2055 $('#queryAnalyzerDialog').find('div.placeHolder a[href*="#showExplain"]').click(function () {
2056 var id = $(this).attr('href').split('-')[1];
2057 $(this).parent().find('div[class*="explain"]').hide();
2058 $(this).parent().find('div[class*="explain-' + id + '"]').show();
2061 if (data.profiling) {
2063 var numberTable = '<table class="queryNums"><thead><tr><th>' + PMA_messages.strStatus + '</th><th>' + PMA_messages.strTime + '</th></tr></thead><tbody>';
2067 for (i = 0, l = data.profiling.length; i < l; i++) {
2068 duration = parseFloat(data.profiling[i].duration);
2070 totalTime += duration;
2072 numberTable += '<tr><td>' + data.profiling[i].state + ' </td><td> ' + PMA_prettyProfilingNum(duration, 2) + '</td></tr>';
2075 // Only put those values in the pie which are > 2%
2076 for (i = 0, l = data.profiling.length; i < l; i++) {
2077 duration = parseFloat(data.profiling[i].duration);
2079 if (duration / totalTime > 0.02) {
2080 chartData.push([PMA_prettyProfilingNum(duration, 2) + ' ' + data.profiling[i].state, duration]);
2082 otherTime += duration;
2086 if (otherTime > 0) {
2087 chartData.push([PMA_prettyProfilingNum(otherTime, 2) + ' ' + PMA_messages.strOther, otherTime]);
2090 numberTable += '<tr><td><b>' + PMA_messages.strTotalTime + '</b></td><td>' + PMA_prettyProfilingNum(totalTime, 2) + '</td></tr>';
2091 numberTable += '</tbody></table>';
2093 $('#queryAnalyzerDialog').find('div.placeHolder td.chart').append(
2094 '<b>' + PMA_messages.strProfilingResults + ' ' + $('#profiling_docu').html() + '</b> ' +
2095 '(<a href="#showNums">' + PMA_messages.strTable + '</a>, <a href="#showChart">' + PMA_messages.strChart + '</a>)<br/>' +
2096 numberTable + ' <div id="queryProfiling"></div>');
2098 $('#queryAnalyzerDialog').find('div.placeHolder a[href="#showNums"]').click(function () {
2099 $('#queryAnalyzerDialog').find('#queryProfiling').hide();
2100 $('#queryAnalyzerDialog').find('table.queryNums').show();
2104 $('#queryAnalyzerDialog').find('div.placeHolder a[href="#showChart"]').click(function () {
2105 $('#queryAnalyzerDialog').find('#queryProfiling').show();
2106 $('#queryAnalyzerDialog').find('table.queryNums').hide();
2110 profilingChart = PMA_createProfilingChart(
2115 // $('#queryProfiling').resizable();
2120 /* Saves the monitor to localstorage */
2121 function saveMonitor () {
2124 $.each(runtime.charts, function (key, elem) {
2126 gridCopy[key].nodes = elem.nodes;
2127 gridCopy[key].settings = elem.settings;
2128 gridCopy[key].title = elem.title;
2129 gridCopy[key].series = elem.series;
2130 gridCopy[key].maxYLabel = elem.maxYLabel;
2133 if (isStorageSupported('localStorage')) {
2134 window.localStorage.monitorCharts = JSON.stringify(gridCopy);
2135 window.localStorage.monitorSettings = JSON.stringify(monitorSettings);
2136 window.localStorage.monitorVersion = monitorProtocolVersion;
2139 $('a[href="#clearMonitorConfig"]').show();
2143 // Run the monitor once loaded
2144 AJAX.registerOnload('server_status_monitor.js', function () {
2145 $('a[href="#pauseCharts"]').trigger('click');
2148 function serverResponseError () {
2150 btns[PMA_messages.strReloadPage] = function () {
2151 window.location.reload();
2153 $('#emptyDialog').dialog({ title: PMA_messages.strRefreshFailed });
2154 $('#emptyDialog').html(
2155 PMA_getImage('s_attention') +
2156 PMA_messages.strInvalidResponseExplanation
2158 $('#emptyDialog').dialog({ buttons: btns });
2161 /* Destroys all monitor related resources */
2162 function destroyGrid () {
2163 if (runtime.charts) {
2164 $.each(runtime.charts, function (key, value) {
2166 value.chart.destroy();
2172 runtime.refreshRequest.abort();
2175 clearTimeout(runtime.refreshTimeout);
2177 $('#chartGrid').html('');
2178 runtime.charts = null;
2179 runtime.chartAI = 0;
2180 monitorSettings = null; // TODO:this not global variable