/wp-content/plugins/query-monitor/output/html/overview.php

https://gitlab.com/knowthecode/ktc-sandbox · PHP · 175 lines · 126 code · 28 blank · 21 comment · 15 complexity · 259876ff3aaaa10eac4bdd9d5e5dc28a MD5 · raw file

  1. <?php
  2. /*
  3. Copyright 2009-2016 John Blackbourn
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. */
  13. class QM_Output_Html_Overview extends QM_Output_Html {
  14. public function __construct( QM_Collector $collector ) {
  15. parent::__construct( $collector );
  16. add_filter( 'qm/output/title', array( $this, 'admin_title' ), 10 );
  17. }
  18. public function output() {
  19. $data = $this->collector->get_data();
  20. $db_query_num = null;
  21. $db_query_types = array();
  22. $db_queries = QM_Collectors::get( 'db_queries' );
  23. if ( $db_queries ) {
  24. # @TODO: make this less derpy:
  25. $db_queries_data = $db_queries->get_data();
  26. if ( isset( $db_queries_data['types'] ) && isset( $db_queries_data['total_time'] ) ) {
  27. $db_query_num = $db_queries_data['types'];
  28. }
  29. }
  30. $cache = QM_Collectors::get( 'cache' );
  31. if ( $cache ) {
  32. $cache_data = $cache->get_data();
  33. if ( isset( $cache_data['stats'] ) && isset( $cache_data['cache_hit_percentage'] ) ) {
  34. $cache_hit_percentage = $cache_data['cache_hit_percentage'];
  35. }
  36. }
  37. echo '<div class="qm" id="' . esc_attr( $this->collector->id() ) . '">';
  38. echo '<table cellspacing="0">';
  39. echo '<caption class="screen-reader-text">' . esc_html( $this->collector->name() ). '</caption>';
  40. echo '<thead>';
  41. echo '<tr>';
  42. echo '<th scope="col">' . esc_html__( 'Page generation time', 'query-monitor' ) . '</th>';
  43. echo '<th scope="col">' . esc_html__( 'Peak memory usage', 'query-monitor' ) . '</th>';
  44. if ( isset( $db_query_num ) ) {
  45. echo '<th scope="col">' . esc_html__( 'Database query time', 'query-monitor' ) . '</th>';
  46. echo '<th scope="col">' . esc_html__( 'Database queries', 'query-monitor' ) . '</th>';
  47. }
  48. if ( isset( $cache_hit_percentage ) ) {
  49. echo '<th scope="col">' . esc_html__( 'Object cache', 'query-monitor' ) . '</th>';
  50. }
  51. echo '</tr>';
  52. echo '</thead>';
  53. echo '<tbody>';
  54. echo '<tr>';
  55. echo '<td>';
  56. echo esc_html( number_format_i18n( $data['time_taken'], 4 ) );
  57. echo '<br><span class="qm-info">';
  58. echo esc_html( sprintf(
  59. /* translators: 1: Percentage of time limit used, 2: Time limit in seconds*/
  60. __( '%1$s%% of %2$ss limit', 'query-monitor' ),
  61. number_format_i18n( $data['time_usage'], 1 ),
  62. number_format_i18n( $data['time_limit'] )
  63. ) );
  64. echo '</span>';
  65. echo '</td>';
  66. if ( empty( $data['memory'] ) ) {
  67. echo '<td><em>' . esc_html__( 'Unknown', 'query-monitor' ) . '</em></td>';
  68. } else {
  69. echo '<td>';
  70. echo esc_html( sprintf(
  71. /* translators: %s: Memory used in kilobytes */
  72. __( '%s kB', 'query-monitor' ),
  73. number_format_i18n( $data['memory'] / 1024 )
  74. ) );
  75. echo '<br><span class="qm-info">';
  76. echo esc_html( sprintf(
  77. /* translators: 1: Percentage of memory limit used, 2: Memory limit in kilobytes*/
  78. __( '%1$s%% of %2$s kB limit', 'query-monitor' ),
  79. number_format_i18n( $data['memory_usage'], 1 ),
  80. number_format_i18n( $data['memory_limit'] / 1024 )
  81. ) );
  82. echo '</span>';
  83. echo '</td>';
  84. }
  85. if ( isset( $db_query_num ) ) {
  86. echo '<td>';
  87. echo esc_html( number_format_i18n( $db_queries_data['total_time'], 4 ) );
  88. echo '</td>';
  89. echo '<td>';
  90. foreach ( $db_query_num as $type_name => $type_count ) {
  91. $db_query_types[] = sprintf( '%1$s: %2$s', $type_name, number_format_i18n( $type_count ) );
  92. }
  93. echo implode( '<br>', array_map( 'esc_html', $db_query_types ) );
  94. echo '</td>';
  95. }
  96. if ( isset( $cache_hit_percentage ) ) {
  97. echo '<td>';
  98. echo esc_html( sprintf(
  99. /* translators: %s: Cache hit rate percentage */
  100. __( '%s%% hit rate', 'query-monitor' ),
  101. number_format_i18n( $cache_hit_percentage, 1 )
  102. ) );
  103. echo '<br><span class="qm-info">';
  104. echo ( $cache_data['ext_object_cache'] )
  105. ? esc_html__( 'External object cache in use', 'query-monitor' )
  106. : esc_html__( 'External object cache not in use', 'query-monitor' );
  107. echo '</span>';
  108. echo '</td>';
  109. }
  110. echo '</tr>';
  111. echo '</tbody>';
  112. echo '</table>';
  113. echo '</div>';
  114. }
  115. public function admin_title( array $title ) {
  116. $data = $this->collector->get_data();
  117. if ( empty( $data['memory'] ) ) {
  118. $memory = '??';
  119. } else {
  120. $memory = number_format_i18n( ( $data['memory'] / 1024 ), 0 );
  121. }
  122. $title[] = sprintf(
  123. /* translators: %s: Page load time in seconds */
  124. esc_html_x( '%s S', 'Page load time', 'query-monitor' ),
  125. number_format_i18n( $data['time_taken'], 2 )
  126. );
  127. $title[] = sprintf(
  128. /* translators: %s: Memory usage in kilobytes */
  129. esc_html_x( '%s kB', 'Memory usage', 'query-monitor' ),
  130. $memory
  131. );
  132. foreach ( $title as &$t ) {
  133. $t = preg_replace( '#\s?([^0-9,\.]+)#', '<small>$1</small>', $t );
  134. }
  135. return $title;
  136. }
  137. }
  138. function register_qm_output_html_overview( array $output, QM_Collectors $collectors ) {
  139. if ( $collector = QM_Collectors::get( 'overview' ) ) {
  140. $output['overview'] = new QM_Output_Html_Overview( $collector );
  141. }
  142. return $output;
  143. }
  144. add_filter( 'qm/outputter/html', 'register_qm_output_html_overview', 10, 2 );