QueriesController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Displays query statistics for the server
  5. *
  6. * @package PhpMyAdmin\Controllers
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin\Controllers\Server\Status;
  10. /**
  11. * Class QueriesController
  12. * @package PhpMyAdmin\Controllers\Server\Status
  13. */
  14. class QueriesController extends AbstractController
  15. {
  16. /**
  17. * @return string HTML
  18. */
  19. public function index(): string
  20. {
  21. if ($this->data->dataLoaded) {
  22. $hourFactor = 3600 / $this->data->status['Uptime'];
  23. $usedQueries = $this->data->used_queries;
  24. $totalQueries = array_sum($usedQueries);
  25. $stats = [
  26. 'total' => $totalQueries,
  27. 'per_hour' => $totalQueries * $hourFactor,
  28. 'per_minute' => $totalQueries * 60 / $this->data->status['Uptime'],
  29. 'per_second' => $totalQueries / $this->data->status['Uptime'],
  30. ];
  31. // reverse sort by value to show most used statements first
  32. arsort($usedQueries);
  33. $chart = [];
  34. $querySum = array_sum($usedQueries);
  35. $otherSum = 0;
  36. $queries = [];
  37. foreach ($usedQueries as $key => $value) {
  38. // For the percentage column, use Questions - Connections, because
  39. // the number of connections is not an item of the Query types
  40. // but is included in Questions. Then the total of the percentages is 100.
  41. $name = str_replace(['Com_', '_'], ['', ' '], $key);
  42. // Group together values that make out less than 2% into "Other", but only
  43. // if we have more than 6 fractions already
  44. if ($value < $querySum * 0.02 && count($chart) > 6) {
  45. $otherSum += $value;
  46. } else {
  47. $chart[$name] = $value;
  48. }
  49. $queries[$key] = [
  50. 'name' => $name,
  51. 'value' => $value,
  52. 'per_hour' => $value * $hourFactor,
  53. 'percentage' => $value * 100 / $totalQueries,
  54. ];
  55. }
  56. if ($otherSum > 0) {
  57. $chart[__('Other')] = $otherSum;
  58. }
  59. }
  60. return $this->template->render('server/status/queries/index', [
  61. 'is_data_loaded' => $this->data->dataLoaded,
  62. 'stats' => $stats ?? null,
  63. 'queries' => $queries ?? [],
  64. 'chart' => $chart ?? [],
  65. ]);
  66. }
  67. }