Queries.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * functions for displaying query statistics for the server
  5. *
  6. * @usedby server_status_queries.php
  7. *
  8. * @package PhpMyAdmin
  9. */
  10. namespace PhpMyAdmin\Server\Status;
  11. use PhpMyAdmin\Server\Status\Data;
  12. use PhpMyAdmin\Util;
  13. /**
  14. * PhpMyAdmin\Server\Status\Queries class
  15. *
  16. * @package PhpMyAdmin
  17. */
  18. class Queries
  19. {
  20. /**
  21. * Returns the html content for the query statistics
  22. *
  23. * @param Data $serverStatusData Server status data
  24. *
  25. * @return string
  26. */
  27. public static function getHtmlForQueryStatistics(Data $serverStatusData)
  28. {
  29. $retval = '';
  30. $hour_factor = 3600 / $serverStatusData->status['Uptime'];
  31. $used_queries = $serverStatusData->used_queries;
  32. $total_queries = array_sum($used_queries);
  33. $retval .= '<h3 id="serverstatusqueries">';
  34. /* l10n: Questions is the name of a MySQL Status variable */
  35. $retval .= sprintf(
  36. __('Questions since startup: %s'),
  37. Util::formatNumber($total_queries, 0)
  38. );
  39. $retval .= ' ';
  40. $retval .= Util::showMySQLDocu(
  41. 'server-status-variables',
  42. false,
  43. 'statvar_Questions'
  44. );
  45. $retval .= '<br />';
  46. $retval .= '<span>';
  47. $retval .= '&oslash; ' . __('per hour:') . ' ';
  48. $retval .= Util::formatNumber($total_queries * $hour_factor, 0);
  49. $retval .= '<br />';
  50. $retval .= '&oslash; ' . __('per minute:') . ' ';
  51. $retval .= Util::formatNumber(
  52. $total_queries * 60 / $serverStatusData->status['Uptime'],
  53. 0
  54. );
  55. $retval .= '<br />';
  56. if ($total_queries / $serverStatusData->status['Uptime'] >= 1) {
  57. $retval .= '&oslash; ' . __('per second:') . ' ';
  58. $retval .= Util::formatNumber(
  59. $total_queries / $serverStatusData->status['Uptime'],
  60. 0
  61. );
  62. }
  63. $retval .= '</span>';
  64. $retval .= '</h3>';
  65. $retval .= self::getHtmlForDetails($serverStatusData);
  66. return $retval;
  67. }
  68. /**
  69. * Returns the html content for the query details
  70. *
  71. * @param Data $serverStatusData Server status data
  72. *
  73. * @return string
  74. */
  75. public static function getHtmlForDetails(Data $serverStatusData)
  76. {
  77. $hour_factor = 3600 / $serverStatusData->status['Uptime'];
  78. $used_queries = $serverStatusData->used_queries;
  79. $total_queries = array_sum($used_queries);
  80. // reverse sort by value to show most used statements first
  81. arsort($used_queries);
  82. //(- $serverStatusData->status['Connections']);
  83. $perc_factor = 100 / $total_queries;
  84. $retval = '<table id="serverstatusqueriesdetails" '
  85. . 'class="width100 data sortable noclick">';
  86. $retval .= '<col class="namecol" />';
  87. $retval .= '<col class="valuecol" span="3" />';
  88. $retval .= '<thead>';
  89. $retval .= '<tr><th>' . __('Statements') . '</th>';
  90. $retval .= '<th>';
  91. /* l10n: # = Amount of queries */
  92. $retval .= __('#');
  93. $retval .= '</th>';
  94. $retval .= '<th>&oslash; ' . __('per hour')
  95. . '</th>';
  96. $retval .= '<th>%</div></th>';
  97. $retval .= '</tr>';
  98. $retval .= '</thead>';
  99. $retval .= '<tbody>';
  100. $chart_json = array();
  101. $query_sum = array_sum($used_queries);
  102. $other_sum = 0;
  103. foreach ($used_queries as $name => $value) {
  104. // For the percentage column, use Questions - Connections, because
  105. // the number of connections is not an item of the Query types
  106. // but is included in Questions. Then the total of the percentages is 100.
  107. $name = str_replace(array('Com_', '_'), array('', ' '), $name);
  108. // Group together values that make out less than 2% into "Other", but only
  109. // if we have more than 6 fractions already
  110. if ($value < $query_sum * 0.02 && count($chart_json)>6) {
  111. $other_sum += $value;
  112. } else {
  113. $chart_json[$name] = $value;
  114. }
  115. $retval .= '<tr>';
  116. $retval .= '<th class="name">' . htmlspecialchars($name) . '</th>';
  117. $retval .= '<td class="value">';
  118. $retval .= htmlspecialchars(
  119. Util::formatNumber($value, 5, 0, true)
  120. );
  121. $retval .= '</td>';
  122. $retval .= '<td class="value">';
  123. $retval .= htmlspecialchars(
  124. Util::formatNumber($value * $hour_factor, 4, 1, true)
  125. );
  126. $retval .= '</td>';
  127. $retval .= '<td class="value">';
  128. $retval .= htmlspecialchars(
  129. Util::formatNumber($value * $perc_factor, 0, 2)
  130. );
  131. $retval .= '</td>';
  132. $retval .= '</tr>';
  133. }
  134. $retval .= '</tbody>';
  135. $retval .= '</table>';
  136. $retval .= '<div id="serverstatusquerieschart" class="width100" data-chart="';
  137. if ($other_sum > 0) {
  138. $chart_json[__('Other')] = $other_sum;
  139. }
  140. $retval .= htmlspecialchars(json_encode($chart_json), ENT_QUOTES);
  141. $retval .= '"></div>';
  142. return $retval;
  143. }
  144. }