processes.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. /**
  3. * Server Status Processes
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. // object to store process list state information
  8. var processList = {
  9. // denotes whether auto refresh is on or off
  10. autoRefresh: false,
  11. // stores the GET request which refresh process list
  12. refreshRequest: null,
  13. // stores the timeout id returned by setTimeout
  14. refreshTimeout: null,
  15. // the refresh interval in seconds
  16. refreshInterval: null,
  17. // the refresh URL (required to save last used option)
  18. // i.e. full or sorting url
  19. refreshUrl: null,
  20. /**
  21. * Handles killing of a process
  22. *
  23. * @return void
  24. */
  25. init: function init() {
  26. processList.setRefreshLabel();
  27. if (processList.refreshUrl === null) {
  28. processList.refreshUrl = 'index.php?route=/server/status/processes/refresh';
  29. }
  30. if (processList.refreshInterval === null) {
  31. processList.refreshInterval = $('#id_refreshRate').val();
  32. } else {
  33. $('#id_refreshRate').val(processList.refreshInterval);
  34. }
  35. },
  36. /**
  37. * Handles killing of a process
  38. *
  39. * @param object the event object
  40. *
  41. * @return void
  42. */
  43. killProcessHandler: function killProcessHandler(event) {
  44. event.preventDefault();
  45. var argSep = CommonParams.get('arg_separator');
  46. var params = $(this).getPostData();
  47. params += argSep + 'ajax_request=1' + argSep + 'server=' + CommonParams.get('server'); // Get row element of the process to be killed.
  48. var $tr = $(this).closest('tr');
  49. $.post($(this).attr('href'), params, function (data) {
  50. // Check if process was killed or not.
  51. if (data.hasOwnProperty('success') && data.success) {
  52. // remove the row of killed process.
  53. $tr.remove(); // As we just removed a row, reapply odd-even classes
  54. // to keep table stripes consistent
  55. var $tableProcessListTr = $('#tableprocesslist').find('> tbody > tr');
  56. $tableProcessListTr.each(function (index) {
  57. if (index >= 0 && index % 2 === 0) {
  58. $(this).removeClass('odd').addClass('even');
  59. } else if (index >= 0 && index % 2 !== 0) {
  60. $(this).removeClass('even').addClass('odd');
  61. }
  62. }); // Show process killed message
  63. Functions.ajaxShowMessage(data.message, false);
  64. } else {
  65. // Show process error message
  66. Functions.ajaxShowMessage(data.error, false);
  67. }
  68. }, 'json');
  69. },
  70. /**
  71. * Handles Auto Refreshing
  72. *
  73. * @return void
  74. */
  75. refresh: function refresh() {
  76. // abort any previous pending requests
  77. // this is necessary, it may go into
  78. // multiple loops causing unnecessary
  79. // requests even after leaving the page.
  80. processList.abortRefresh(); // if auto refresh is enabled
  81. if (processList.autoRefresh) {
  82. // Only fetch the table contents
  83. processList.refreshUrl = 'index.php?route=/server/status/processes/refresh';
  84. var interval = parseInt(processList.refreshInterval, 10) * 1000;
  85. var urlParams = processList.getUrlParams();
  86. processList.refreshRequest = $.post(processList.refreshUrl, urlParams, function (data) {
  87. if (data.hasOwnProperty('success') && data.success) {
  88. var $newTable = $(data.message);
  89. $('#tableprocesslist').html($newTable.html());
  90. Functions.highlightSql($('#tableprocesslist'));
  91. }
  92. processList.refreshTimeout = setTimeout(processList.refresh, interval);
  93. });
  94. }
  95. },
  96. /**
  97. * Stop current request and clears timeout
  98. *
  99. * @return void
  100. */
  101. abortRefresh: function abortRefresh() {
  102. if (processList.refreshRequest !== null) {
  103. processList.refreshRequest.abort();
  104. processList.refreshRequest = null;
  105. }
  106. clearTimeout(processList.refreshTimeout);
  107. },
  108. /**
  109. * Set label of refresh button
  110. * change between play & pause
  111. *
  112. * @return void
  113. */
  114. setRefreshLabel: function setRefreshLabel() {
  115. var img = 'play';
  116. var label = Messages.strStartRefresh;
  117. if (processList.autoRefresh) {
  118. img = 'pause';
  119. label = Messages.strStopRefresh;
  120. processList.refresh();
  121. }
  122. $('a#toggleRefresh').html(Functions.getImage(img) + Functions.escapeHtml(label));
  123. },
  124. /**
  125. * Return the Url Parameters
  126. * for autorefresh request,
  127. * includes showExecuting if the filter is checked
  128. *
  129. * @return urlParams - url parameters with autoRefresh request
  130. */
  131. getUrlParams: function getUrlParams() {
  132. var urlParams = {
  133. 'server': CommonParams.get('server'),
  134. 'ajax_request': true,
  135. 'refresh': true,
  136. 'full': $('input[name="full"]').val(),
  137. 'order_by_field': $('input[name="order_by_field"]').val(),
  138. 'column_name': $('input[name="column_name"]').val(),
  139. 'sort_order': $('input[name="sort_order"]').val()
  140. };
  141. if ($('#showExecuting').is(':checked')) {
  142. urlParams.showExecuting = true;
  143. return urlParams;
  144. }
  145. return urlParams;
  146. }
  147. };
  148. AJAX.registerOnload('server/status/processes.js', function () {
  149. processList.init(); // Bind event handler for kill_process
  150. $('#tableprocesslist').on('click', 'a.kill_process', processList.killProcessHandler); // Bind event handler for toggling refresh of process list
  151. $('a#toggleRefresh').on('click', function (event) {
  152. event.preventDefault();
  153. processList.autoRefresh = !processList.autoRefresh;
  154. processList.setRefreshLabel();
  155. }); // Bind event handler for change in refresh rate
  156. $('#id_refreshRate').on('change', function () {
  157. processList.refreshInterval = $(this).val();
  158. processList.refresh();
  159. }); // Bind event handler for table header links
  160. $('#tableprocesslist').on('click', 'thead a', function () {
  161. processList.refreshUrl = $(this).attr('href');
  162. });
  163. });
  164. /**
  165. * Unbind all event handlers before tearing down a page
  166. */
  167. AJAX.registerTeardown('server/status/processes.js', function () {
  168. $('#tableprocesslist').off('click', 'a.kill_process');
  169. $('a#toggleRefresh').off('click');
  170. $('#id_refreshRate').off('change');
  171. $('#tableprocesslist').off('click', 'thead a'); // stop refreshing further
  172. processList.abortRefresh();
  173. });