QueriesController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Displays query statistics for the server
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Controllers\Server\Status;
  7. use PhpMyAdmin\DatabaseInterface;
  8. use PhpMyAdmin\Response;
  9. use PhpMyAdmin\Server\Status\Data;
  10. use PhpMyAdmin\Template;
  11. use PhpMyAdmin\Url;
  12. use function array_sum;
  13. use function arsort;
  14. use function count;
  15. use function str_replace;
  16. class QueriesController extends AbstractController
  17. {
  18. /** @var DatabaseInterface */
  19. private $dbi;
  20. /**
  21. * @param Response $response
  22. * @param Data $data
  23. * @param DatabaseInterface $dbi
  24. */
  25. public function __construct($response, Template $template, $data, $dbi)
  26. {
  27. parent::__construct($response, $template, $data);
  28. $this->dbi = $dbi;
  29. }
  30. public function index(): void
  31. {
  32. global $err_url;
  33. $err_url = Url::getFromRoute('/');
  34. if ($this->dbi->isSuperUser()) {
  35. $this->dbi->selectDb('mysql');
  36. }
  37. $this->addScriptFiles([
  38. 'chart.js',
  39. 'vendor/jqplot/jquery.jqplot.js',
  40. 'vendor/jqplot/plugins/jqplot.pieRenderer.js',
  41. 'vendor/jqplot/plugins/jqplot.highlighter.js',
  42. 'vendor/jqplot/plugins/jqplot.enhancedPieLegendRenderer.js',
  43. 'vendor/jquery/jquery.tablesorter.js',
  44. 'server/status/sorter.js',
  45. 'server/status/queries.js',
  46. ]);
  47. if ($this->data->dataLoaded) {
  48. $hourFactor = 3600 / $this->data->status['Uptime'];
  49. $usedQueries = $this->data->usedQueries;
  50. $totalQueries = array_sum($usedQueries);
  51. $stats = [
  52. 'total' => $totalQueries,
  53. 'per_hour' => $totalQueries * $hourFactor,
  54. 'per_minute' => $totalQueries * 60 / $this->data->status['Uptime'],
  55. 'per_second' => $totalQueries / $this->data->status['Uptime'],
  56. ];
  57. // reverse sort by value to show most used statements first
  58. arsort($usedQueries);
  59. $chart = [];
  60. $querySum = array_sum($usedQueries);
  61. $otherSum = 0;
  62. $queries = [];
  63. foreach ($usedQueries as $key => $value) {
  64. // For the percentage column, use Questions - Connections, because
  65. // the number of connections is not an item of the Query types
  66. // but is included in Questions. Then the total of the percentages is 100.
  67. $name = str_replace(['Com_', '_'], ['', ' '], $key);
  68. // Group together values that make out less than 2% into "Other", but only
  69. // if we have more than 6 fractions already
  70. if ($value < $querySum * 0.02 && count($chart) > 6) {
  71. $otherSum += $value;
  72. } else {
  73. $chart[$name] = $value;
  74. }
  75. $queries[$key] = [
  76. 'name' => $name,
  77. 'value' => $value,
  78. 'per_hour' => $value * $hourFactor,
  79. 'percentage' => $value * 100 / $totalQueries,
  80. ];
  81. }
  82. if ($otherSum > 0) {
  83. $chart[__('Other')] = $otherSum;
  84. }
  85. }
  86. $this->render('server/status/queries/index', [
  87. 'is_data_loaded' => $this->data->dataLoaded,
  88. 'stats' => $stats ?? null,
  89. 'queries' => $queries ?? [],
  90. 'chart' => $chart ?? [],
  91. ]);
  92. }
  93. }