Skip to content

Commit

Permalink
Merge pull request #84 from 10up/feature/query-monitor-support
Browse files Browse the repository at this point in the history
Add support to Query Monitor
  • Loading branch information
felipeelia authored Sep 14, 2023
2 parents fdb2abf + ec8018d commit 1105fd0
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 32 deletions.
39 changes: 29 additions & 10 deletions assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
padding: 0.5em 10px;
}

.ep-queries-debug pre {
padding: 10px;
}

.ep-queries-debug .dashicons {
cursor: pointer;
}
Expand Down Expand Up @@ -69,17 +73,14 @@
clear: left;
}

#query-monitor-main .qm-debug-bar {
margin: 20px;
}

/* stylelint-disable selector-id-pattern */

/* The giant list of selectors here is needed to
* override QM's important rules
*/
#debug-menu-target-EP_Debug_Bar_ElasticPress .button,
#debug-menu-target-EP_Debug_Bar_ElasticPress a.button {
#debug-menu-target-EP_Debug_Bar_ElasticPress a.button,
#qm-elasticpress .button {
background: #f6f7f7 !important;
border: 1px solid #2271b1 !important;
border-radius: 3px !important;
Expand All @@ -97,37 +98,55 @@
}

#debug-menu-target-EP_Debug_Bar_ElasticPress .button:hover,
#query-monitor-main #qm-debug_bar_ep_debug_bar_elasticpress .button:hover {
#query-monitor-main #qm-elasticpress .button:hover {
background: #f0f0f1;
border-color: #0a4b78;
color: #0a4b78 !important;
text-decoration: none !important;
}

#debug-menu-target-EP_Debug_Bar_ElasticPress .button:active,
#query-monitor-main #qm-debug_bar_ep_debug_bar_elasticpress .button:hover {
#query-monitor-main #qm-elasticpress .button:hover {
background: #f6f7f7 !important;
border-color: #8c8f94 !important;
box-shadow: none;
}

#debug-menu-target-EP_Debug_Bar_ElasticPress .button:focus {
#debug-menu-target-EP_Debug_Bar_ElasticPress .button:focus,
#query-monitor-main #qm-elasticpress .button:focus {
outline: none;
}

/* Overriding some Debug Bar "important" rules */
#debug-menu-target-EP_Debug_Bar_ElasticPress .button-primary,
#debug-menu-target-EP_Debug_Bar_ElasticPress a.button-primary,
#debug-menu-target-EP_Debug_Bar_ElasticPress button.button-primary {
#debug-menu-target-EP_Debug_Bar_ElasticPress button.button-primary,
#query-monitor-main #qm-elasticpress .button-primary {
background: #007cba !important;
border: 1px solid #007cba !important;
color: #fff !important;
}

#debug-menu-target-EP_Debug_Bar_ElasticPress a.button-primary:hover,
#debug-menu-target-EP_Debug_Bar_ElasticPress a.button-primary:focus {
#debug-menu-target-EP_Debug_Bar_ElasticPress a.button-primary:focus,
#query-monitor-main #qm-elasticpress a.button-primary:hover,
#query-monitor-main #qm-elasticpress a.button-primary:focus {
background: #007cba !important;
color: #fff !important;
text-decoration: none !important;
}

#query-monitor-main #qm-elasticpress h3,
#query-monitor-main #qm-elasticpress p {
margin: 0 !important;
padding: 0 !important;
text-align: center !important;
}

#query-monitor-main #qm-elasticpress li {
border-top: 1px solid var(--qm-panel-separator) !important;
line-height: 20px !important;
padding: 20px 0 !important;
}

/* stylelint-enable selector-id-pattern */
70 changes: 70 additions & 0 deletions classes/CommonPanel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* CommonPanel class file.
*
* @since 3.1.0
* @package DebugBarElasticPress
*/

namespace DebugBarElasticPress;

defined( 'ABSPATH' ) || exit;

/**
* CommonPanel class.
*/
class CommonPanel {
/**
* Panel menu title
*
* @var string
*/
public $title = '';

/**
* Class constructor
*/
public function __construct() {
$this->title = esc_html__( 'ElasticPress', 'debug-bar-elasticpress' );
}

/**
* Return the panel title
*
* @return string
*/
public function get_title() : string {
return $this->title;
}
/**
* Enqueue scripts for front end and admin
*/
public function enqueue_scripts_styles() {
if ( ! is_user_logged_in() ) {
return;
}

wp_enqueue_script( 'debug-bar-elasticpress', EP_DEBUG_URL . 'assets/js/main.js', array( 'wp-dom-ready', 'clipboard' ), EP_DEBUG_VERSION, true );
wp_enqueue_style( 'debug-bar-elasticpress', EP_DEBUG_URL . 'assets/css/main.css', array(), EP_DEBUG_VERSION );
}

/**
* Show the contents of the panel
*/
public function render() {
$queries = \ElasticPress\Elasticsearch::factory()->get_query_log();

if ( function_exists( '\ElasticPress\Utils\is_indexing' ) && \ElasticPress\Utils\is_indexing() ) {
?>
<div class="ep-debug-bar-warning">
<?php esc_html_e( 'ElasticPress is currently indexing.', 'debug-bar-elasticpress' ); ?>
</div>
<?php
}

$debug_bar_output = new \DebugBarElasticPress\QueryOutput( $queries );
$debug_bar_output->render_buttons();
$debug_bar_output->render_additional_buttons();
$debug_bar_output->render_queries();
}
}
29 changes: 11 additions & 18 deletions classes/EP_Debug_Bar_ElasticPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@ class EP_Debug_Bar_ElasticPress extends \Debug_Bar_Panel {
*/
public $title;

/**
* Common panel instance
*
* @var \DebugBarElasticPress\CommonPanel
*/
protected $common_panel;

/**
* Initial debug bar stuff
*/
public function init() {
$this->title( esc_html__( 'ElasticPress', 'debug-bar-elasticpress' ) );

add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_styles' ) );
$this->common_panel = new \DebugBarElasticPress\CommonPanel();
$this->common_panel->enqueue_scripts_styles();
}

/**
* Enqueue scripts for front end and admin
*/
public function enqueue_scripts_styles() {
if ( ! is_user_logged_in() ) {
return;
}

wp_enqueue_script( 'debug-bar-elasticpress', EP_DEBUG_URL . 'assets/js/main.js', array( 'wp-dom-ready', 'clipboard' ), EP_DEBUG_VERSION, true );
wp_enqueue_style( 'debug-bar-elasticpress', EP_DEBUG_URL . 'assets/css/main.css', array(), EP_DEBUG_VERSION );
_deprecated_function( __METHOD__, '3.1.0', 'DebugBarElasticPress\EP_Panel::enqueue_scripts_styles()' );
}

/**
Expand Down Expand Up @@ -85,16 +87,7 @@ public function render() {
?>
</h2>

<?php if ( function_exists( '\ElasticPress\Utils\is_indexing' ) && \ElasticPress\Utils\is_indexing() ) : ?>
<div class="ep-debug-bar-warning">
<?php esc_html_e( 'ElasticPress is currently indexing.', 'debug-bar-elasticpress' ); ?>
</div>
<?php endif; ?>

<?php
$debug_bar_output = new \DebugBarElasticPress\QueryOutput( $queries );
$debug_bar_output->render_buttons();
$debug_bar_output->render_additional_buttons();
$debug_bar_output->render_queries();
$this->common_panel->render();
}
}
27 changes: 25 additions & 2 deletions classes/QueryLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function setup() {
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
add_action( 'admin_init', array( $this, 'maybe_clear_log' ) );
add_action( 'init', array( $this, 'maybe_disable' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );

/**
* Handle query storage as JSON strings.
Expand Down Expand Up @@ -109,11 +110,18 @@ public function maybe_clear_log() {
/**
* Add options page
*
* @since 1.3
* @since 1.3
* @return void
*/
public function action_admin_menu() {
add_submenu_page( 'elasticpress', esc_html__( 'Query Log', 'debug-bar-elasticpress' ), esc_html__( 'Query Log', 'debug-bar-elasticpress' ), 'manage_options', 'ep-query-log', array( $this, 'screen_options' ) );
add_submenu_page(
'elasticpress',
esc_html__( 'Query Log', 'debug-bar-elasticpress' ),
esc_html__( 'Query Log', 'debug-bar-elasticpress' ),
'manage_options',
'ep-query-log',
array( $this, 'screen_options' )
);
}

/**
Expand Down Expand Up @@ -452,6 +460,21 @@ public function maybe_add_request_query_type( array $request_args, string $path,
return $request_args;
}

/**
* Enqueue assets if we are in the correct admin screen
*
* @since 3.1.0
*/
public function admin_enqueue_scripts() {
$current_screen = get_current_screen();

if ( ! isset( $current_screen->id ) || 'elasticpress_page_ep-query-log' !== $current_screen->id ) {
return;
}

( new CommonPanel() )->enqueue_scripts_styles();
}

/**
* Conditionally add the request type to the request args (for query requests)
*
Expand Down
23 changes: 23 additions & 0 deletions classes/QueryMonitorCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* QueryMonitorCollector class file.
*
* @since 3.1.0
* @package DebugBarElasticPress
*/

namespace DebugBarElasticPress;

defined( 'ABSPATH' ) || exit;

/**
* QueryMonitorCollector class.
*/
class QueryMonitorCollector extends \QM_Collector {
/**
* Collector ID
*
* @var string
*/
public $id = 'elasticpress';
}
101 changes: 101 additions & 0 deletions classes/QueryMonitorOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* QueryMonitorOutput class file.
*
* @since 3.1.0
* @package DebugBarElasticPress
*/

namespace DebugBarElasticPress;

defined( 'ABSPATH' ) || exit;

/**
* QueryMonitorOutput class.
*/
class QueryMonitorOutput extends \QM_Output_Html {
/**
* Common panel instance
*
* @var CommonPanel
*/
protected $common_panel;

/**
* Class constructor
*
* @param QueryMonitorCollector $collector Our QM Collector
*/
public function __construct( QueryMonitorCollector $collector ) {
parent::__construct( $collector );

$this->common_panel = new CommonPanel();

add_filter( 'qm/output/menus', [ $this, 'admin_menu' ] );
}

/**
* Panel title
*
* @return string
*/
public function name() : string {
return $this->common_panel->get_title();
}

/**
* Echoes the output
*
* @return void
*/
public function output() {
?>
<div class="qm qm-non-tabular qm-debug-bar qm-panel-show" id="<?php echo esc_attr( $this->collector->id() ); ?>">
<?php $this->render_summary(); ?>
<?php $this->common_panel->render(); ?>
</div>
<?php
}

/**
* Render the summary in Query Monitor format
*
* @return void
*/
protected function render_summary() {
$queries = \ElasticPress\Elasticsearch::factory()->get_query_log();
$total_query_time = 0;

foreach ( $queries as $query ) {
if ( ! empty( $query['time_start'] ) && ! empty( $query['time_finish'] ) ) {
$total_query_time += ( $query['time_finish'] - $query['time_start'] );
}
}
?>
<div class="qm-boxed">
<section>
<h3>
<?php esc_html_e( 'Total ElasticPress Queries:', 'debug-bar-elasticpress' ); ?>
</h3>
<p><?php echo count( $queries ); ?></p>
</section>
<section>
<h3>
<?php esc_html_e( 'Total Blocking ElasticPress Query Time:', 'debug-bar-elasticpress' ); ?>
</h3>
<p>
<?php
echo esc_html(
sprintf(
/* translators: time spent */
__( '%d ms', 'debug-bar-elasticpress' ),
(int) ( $total_query_time * 1000 )
)
);
?>
</p>
</section>
</div>
<?php
}
}
Loading

0 comments on commit 1105fd0

Please sign in to comment.