Skip to content

Commit

Permalink
PLANET-7331: Optimize Block report queries (#1157)
Browse files Browse the repository at this point in the history
* PLANET-7331: Optimize Block report queries

Ref: https://jira.greenpeace.org/browse/PLANET-7331

* Reduce queries count

Query template namespace instead of individual templates
  • Loading branch information
lithrel authored Nov 20, 2023
1 parent 268c891 commit 53b72e2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 13 deletions.
10 changes: 10 additions & 0 deletions classes/controller/menu/class-blocks-usage-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ public function plugin_blocks_report_register_rest_route() {
public function plugin_blocks_report_rest_api() {
global $wpdb;

$use_cache = ! current_user_can( 'manage_options' );
$cache_key = 'plugin_blocks/v3/plugin_blocks_report';
if ( $use_cache ) {
$report = wp_cache_get( $cache_key, 'api', false, $found );
if ( $found ) {
return $report;
}
}

$types = \get_post_types(
[
'public' => true,
Expand Down Expand Up @@ -99,6 +108,7 @@ public function plugin_blocks_report_rest_api() {
'block_patterns' => $pattern_api->get_count(),
'post_types' => $post_types,
];
wp_cache_set( $cache_key, $report, 'api', 60 * 5 );

return $report;
}
Expand Down
48 changes: 48 additions & 0 deletions classes/search/block/sql/class-like.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Block search regex
*
* @package P4BKS\Search\Block
*/

namespace P4GBKS\Search\Block\Sql;

use P4GBKS\Search\Block\Query\Parameters;

/**
* Regular expression used in SQL query
*/
class Like {
/**
* @var Parameters
*/
private $params;

/**
* @param Parameters $params Query parameters.
*/
public function __construct( Parameters $params ) {
$this->params = $params;
}

/**
* @return string
* @todo: $params->attributes not implemented.
*/
public function __toString(): string {
$block_ns = $this->params->namespace();
$block_name = $this->params->name();

if ( 'core' === $block_ns ) {
$block = '%';
} elseif ( 0 === strpos( $block_name, 'core/' ) ) {
$block = explode( '/', $block_name )[1];
} else {
$name = $block_name ? $block_name : '%';
$block = $block_ns ? $block_ns . '/%' : $name;
}
$attrs = $this->params->content() ? '%' . $this->params->content() . '%' : '%';

return sprintf( '%%<!-- wp:%s %s', $block, $attrs );
}
}
14 changes: 7 additions & 7 deletions classes/search/block/sql/class-sqlquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ function ( $r ) {
*/
private function get_sql_query( Block\Query\Parameters ...$params_list ): string {
// Prepare query parameters.
$regex = [];
$like = [];
$status = [];
$type = [];
$order = [];
foreach ( $params_list as $params ) {
$regex[] = ( new Regex( $params ) )->__toString();
$status = array_merge( $status, $params->post_status() ?? [] );
$type = array_merge( $type, $params->post_type() ?? [] );
$order = array_merge( $order, $params->order() ?? [] );
$like[] = ( new Like( $params ) )->__toString();
$status = array_merge( $status, $params->post_status() ?? [] );
$type = array_merge( $type, $params->post_type() ?? [] );
$order = array_merge( $order, $params->order() ?? [] );
}
$status = array_unique( array_filter( $status ) );
$type = array_unique( array_filter( $type ) );
Expand All @@ -74,8 +74,8 @@ private function get_sql_query( Block\Query\Parameters ...$params_list ): string
if ( ! empty( $type ) ) {
$sql .= ' AND post_type IN ' . $sql_params->string_list( $type );
}
foreach ( $regex as $r ) {
$sql .= ' AND post_content REGEXP ' . $sql_params->string( $r ) . ' ';
foreach ( $like as $l ) {
$sql .= ' AND post_content LIKE ' . $sql_params->string( $l ) . ' ';
}
if ( ! empty( $order ) ) {
$sql .= ' ORDER BY ' . implode( ',', $order );
Expand Down
14 changes: 8 additions & 6 deletions classes/search/class-patternsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function get_posts( Parameters $params, array $opts = [] ): array {
}

/**
* Search templates by namespace to reduce query count.
*
* @param Parameters $params Search parameters.
* @param PatternData ...$pattern_data Pattern data.
* @return int[] List of posts IDs.
Expand All @@ -66,15 +68,15 @@ private function query_by_pattern_template(
$templates = PatternUsage::patterns_templates_lookup_table();
$block_query = new BlockSqlQuery();

$post_ids = [];
foreach ( $pattern_data as $pattern ) {
if ( empty( $templates[ $pattern->name ] ) ) {
continue;
}
$pattern_names = array_map( fn( $p ) => $p->name, $pattern_data );
$template_names = array_map( fn( $n ) => $templates[ $n ], $pattern_names );
$template_ns = array_filter( array_unique( array_map( fn( $n ) => explode( '/', $n )[0] ?? null, $template_names ) ) );

$post_ids = [];
foreach ( $template_ns as $ns ) {
$block_params = BlockSearchParameters::from_array(
[
'name' => $templates[ $pattern->name ],
'namespace' => $ns,
'post_status' => $params->post_status() ?? self::DEFAULT_POST_STATUS,
'post_type' => $params->post_type() ?? self::DEFAULT_POST_TYPE,
]
Expand Down

0 comments on commit 53b72e2

Please sign in to comment.