From 6eb81bebf61d2f576514d6172472db5e0ef778b3 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Thu, 21 Sep 2023 16:36:09 -0300 Subject: [PATCH 1/5] Store additional syncs info --- includes/classes/Command.php | 2 +- includes/classes/IndexHelper.php | 87 +++++++++++++++++++--- includes/classes/REST/Sync.php | 2 +- includes/classes/Screen/Sync.php | 8 +- includes/classes/StatusReport/LastSync.php | 2 +- includes/classes/Upgrades.php | 18 +++++ uninstall.php | 2 +- 7 files changed, 103 insertions(+), 18 deletions(-) diff --git a/includes/classes/Command.php b/includes/classes/Command.php index c7716e36a5..192be7b6a6 100644 --- a/includes/classes/Command.php +++ b/includes/classes/Command.php @@ -1068,7 +1068,7 @@ public function get_ongoing_sync_status( $args, $assoc_args ) { */ public function get_last_sync( $args, $assoc_args ) { $pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' ); - $last_sync = \ElasticPress\IndexHelper::factory()->get_last_index(); + $last_sync = \ElasticPress\IndexHelper::factory()->get_last_sync(); $this->pretty_json_encode( $last_sync, $pretty ); } diff --git a/includes/classes/IndexHelper.php b/includes/classes/IndexHelper.php index 3b7db2950f..4431cedbbc 100644 --- a/includes/classes/IndexHelper.php +++ b/includes/classes/IndexHelper.php @@ -144,6 +144,7 @@ protected function build_index_meta() { 'start_date_time' => $start_date_time ? $start_date_time->format( DATE_ATOM ) : false, 'starting_indices' => $starting_indices, 'messages_queue' => [], + 'trigger' => 'manual', 'totals' => [ 'total' => 0, 'synced' => 0, @@ -870,24 +871,64 @@ protected function index_cleanup() { * @since 4.2.0 */ protected function update_last_index() { + $is_full_sync = $this->index_meta['put_mapping']; + $method = $this->index_meta['method']; $start_time = $this->index_meta['start_time']; $totals = $this->index_meta['totals']; - $method = $this->index_meta['method']; - $is_full_sync = $this->index_meta['put_mapping']; + $trigger = $this->index_meta['trigger']; $this->index_meta = null; $end_date_time = date_create( 'now', wp_timezone() ); $start_time_sec = (int) $start_time; + // Time related info $totals['end_date_time'] = $end_date_time ? $end_date_time->format( DATE_ATOM ) : false; $totals['start_date_time'] = $start_time ? wp_date( DATE_ATOM, $start_time_sec ) : false; $totals['end_time_gmt'] = time(); $totals['total_time'] = microtime( true ) - $start_time; - $totals['method'] = $method; - $totals['is_full_sync'] = $is_full_sync; + + // Additional info + $totals['is_full_sync'] = $is_full_sync; + $totals['method'] = $method; + $totals['trigger'] = $trigger; + Utils\update_option( 'ep_last_cli_index', $totals, false ); - Utils\update_option( 'ep_last_index', $totals, false ); + + $this->add_last_sync( $totals ); + } + + /** + * Add a sync to the list of all past syncs + * + * @since 5.0.0 + * @param array $last_sync_info The latest sync info to be added to the log + * @return void + */ + protected function add_last_sync( array $last_sync_info ) { + // Remove error messages from previous syncs - we only store msgs for the newest one. + $last_syncs = array_map( + function( $sync ) { + unset( $sync['errors'] ); + return $sync; + }, + $this->get_last_syncs() + ); + + /** + * Filter the number of past syncs to keep info + * + * @since 5.0.0 + * @hook ep_syncs_to_keep_info + * @param {int} $number Number of past syncs to keep info + * @return {int} New number + */ + $syncs_to_keep = (int) apply_filters( 'ep_syncs_to_keep_info', 5 - 1 ); + + $last_syncs = array_slice( $last_syncs, 0, $syncs_to_keep ); + array_unshift( $last_syncs, $last_sync_info ); + + Utils\update_option( 'ep_last_syncs', $last_syncs, false ); } /** @@ -980,7 +1021,7 @@ protected function output( $message_text, $type = 'info', $context = '' ) { Utils\update_option( 'ep_index_meta', $this->index_meta ); } else { Utils\delete_option( 'ep_index_meta' ); - $totals = $this->get_last_index(); + $totals = $this->get_last_sync(); } $message = [ @@ -1084,13 +1125,27 @@ public function is_full_reindexing( $indexable_slug, $blog_id = null ) { } /** - * Get the last index/sync meta information. + * Get the last syncs meta information. * - * @since 4.2.0 + * @since 5.0.0 * @return array */ - public function get_last_index() { - return Utils\get_option( 'ep_last_index', [] ); + public function get_last_syncs() : array { + return Utils\get_option( 'ep_last_syncs', [] ); + } + + /** + * Get the last sync meta information. + * + * @since 5.0.0 + * @return array + */ + public function get_last_sync() : array { + $syncs = $this->get_last_syncs(); + if ( empty( $syncs ) ) { + return []; + } + return array_shift( $syncs ); } /** @@ -1427,4 +1482,16 @@ public static function factory() { return $instance; } + + /** + * DEPRECATED. Get the last index/sync meta information. + * + * @since 4.2.0 + * @deprecated 5.0.0 + * @return array + */ + public function get_last_index() { + _deprecated_function( __METHOD__, '5.0.0', '\ElasticPress\IndexHelper::get_last_sync' ); + return $this->get_last_sync(); + } } diff --git a/includes/classes/REST/Sync.php b/includes/classes/REST/Sync.php index e36db136cd..79e6865c56 100644 --- a/includes/classes/REST/Sync.php +++ b/includes/classes/REST/Sync.php @@ -188,7 +188,7 @@ public function get_sync_status( \WP_REST_Request $request ) { wp_send_json_success( [ 'is_finished' => true, - 'totals' => Utils\get_option( 'ep_last_index' ), + 'totals' => IndexHelper::factory()->get_last_sync(), ] ); } diff --git a/includes/classes/Screen/Sync.php b/includes/classes/Screen/Sync.php index bbaa852d40..556be9836a 100644 --- a/includes/classes/Screen/Sync.php +++ b/includes/classes/Screen/Sync.php @@ -83,14 +83,14 @@ public function admin_enqueue_scripts() { $data['index_meta'] = $index_meta; } - $ep_last_index = IndexHelper::factory()->get_last_index(); + $ep_last_sync = IndexHelper::factory()->get_last_sync(); $indices_comparison = Elasticsearch::factory()->get_indices_comparison(); $sync_required = count( $indices_comparison['missing_indices'] ) > 0; - if ( ! empty( $ep_last_index ) && ! $sync_required ) { - $data['ep_last_sync_date'] = ! empty( $ep_last_index['end_date_time'] ) ? $ep_last_index['end_date_time'] : false; - $data['ep_last_sync_failed'] = ! empty( $ep_last_index['failed'] ) || ! empty( $ep_last_index['errors'] ) ? true : false; + if ( ! empty( $ep_last_sync ) && ! $sync_required ) { + $data['ep_last_sync_date'] = ! empty( $ep_last_sync['end_date_time'] ) ? $ep_last_sync['end_date_time'] : false; + $data['ep_last_sync_failed'] = ! empty( $ep_last_sync['failed'] ) || ! empty( $ep_last_sync['errors'] ) ? true : false; } /** diff --git a/includes/classes/StatusReport/LastSync.php b/includes/classes/StatusReport/LastSync.php index 70582093c5..badc3c968c 100644 --- a/includes/classes/StatusReport/LastSync.php +++ b/includes/classes/StatusReport/LastSync.php @@ -34,7 +34,7 @@ public function get_title() : string { public function get_groups() : array { $fields = []; - $sync_info = \ElasticPress\IndexHelper::factory()->get_last_index(); + $sync_info = \ElasticPress\IndexHelper::factory()->get_last_sync(); if ( empty( $sync_info ) ) { return []; diff --git a/includes/classes/Upgrades.php b/includes/classes/Upgrades.php index 4f928180c3..2a775541cc 100644 --- a/includes/classes/Upgrades.php +++ b/includes/classes/Upgrades.php @@ -49,6 +49,12 @@ public function setup() { '4.4.0' => [ 'upgrade_4_4_0', 'init' ], '4.5.0' => [ 'upgrade_4_5_0', 'init' ], '4.7.0' => [ 'upgrade_4_7_0', 'init' ], + /** + * Adding this without changing the number will make it run on every load. + * + * @todo Uncomment this before the reelase + */ + // '5.0.0' => [ 'upgrade_5_0_0', 'init' ], ]; array_walk( $routines, [ $this, 'run_upgrade_routine' ] ); @@ -245,6 +251,18 @@ public function upgrade_4_7_0() { delete_transient( 'ep_autosuggest_query_request_cache' ); } + /** + * Upgrade routine of v5.0.0. + */ + public function upgrade_5_0_0() { + /** + * Remove the 'ep_last_index' option and store it as an entry of 'ep_last_syncs' + */ + $last_sync = Utils\get_option( 'ep_last_index', [] ); + Utils\delete_option( 'ep_last_index' ); + Utils\update_option( 'ep_last_syncs', [ $last_sync ] ); + } + /** * Adjust the upgrade sync notice to warn users about Instant Results. * diff --git a/uninstall.php b/uninstall.php index 88e6c9a891..6f6fc52faf 100644 --- a/uninstall.php +++ b/uninstall.php @@ -41,7 +41,7 @@ class EP_Uninstaller { 'ep_prefix', 'ep_language', 'ep_bulk_setting', - 'ep_last_index', + 'ep_last_syncs', // Admin notices options 'ep_hide_host_error_notice', From 32969030b9c7b324c24b98ee53693efa1176388c Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Sep 2023 09:31:51 -0300 Subject: [PATCH 2/5] Rename option and method --- includes/classes/IndexHelper.php | 12 ++++++------ includes/classes/Upgrades.php | 4 ++-- uninstall.php | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/includes/classes/IndexHelper.php b/includes/classes/IndexHelper.php index 4431cedbbc..9d7341bfb0 100644 --- a/includes/classes/IndexHelper.php +++ b/includes/classes/IndexHelper.php @@ -912,7 +912,7 @@ function( $sync ) { unset( $sync['errors'] ); return $sync; }, - $this->get_last_syncs() + $this->get_sync_history() ); /** @@ -928,7 +928,7 @@ function( $sync ) { $last_syncs = array_slice( $last_syncs, 0, $syncs_to_keep ); array_unshift( $last_syncs, $last_sync_info ); - Utils\update_option( 'ep_last_syncs', $last_syncs, false ); + Utils\update_option( 'ep_sync_history', $last_syncs, false ); } /** @@ -1125,13 +1125,13 @@ public function is_full_reindexing( $indexable_slug, $blog_id = null ) { } /** - * Get the last syncs meta information. + * Get the previous syncs meta information. * * @since 5.0.0 * @return array */ - public function get_last_syncs() : array { - return Utils\get_option( 'ep_last_syncs', [] ); + public function get_sync_history() : array { + return Utils\get_option( 'ep_sync_history', [] ); } /** @@ -1141,7 +1141,7 @@ public function get_last_syncs() : array { * @return array */ public function get_last_sync() : array { - $syncs = $this->get_last_syncs(); + $syncs = $this->get_sync_history(); if ( empty( $syncs ) ) { return []; } diff --git a/includes/classes/Upgrades.php b/includes/classes/Upgrades.php index 2a775541cc..69f634146e 100644 --- a/includes/classes/Upgrades.php +++ b/includes/classes/Upgrades.php @@ -256,11 +256,11 @@ public function upgrade_4_7_0() { */ public function upgrade_5_0_0() { /** - * Remove the 'ep_last_index' option and store it as an entry of 'ep_last_syncs' + * Remove the 'ep_last_index' option and store it as an entry of 'ep_sync_history' */ $last_sync = Utils\get_option( 'ep_last_index', [] ); Utils\delete_option( 'ep_last_index' ); - Utils\update_option( 'ep_last_syncs', [ $last_sync ] ); + Utils\update_option( 'ep_sync_history', [ $last_sync ] ); } /** diff --git a/uninstall.php b/uninstall.php index 6f6fc52faf..9dea34054b 100644 --- a/uninstall.php +++ b/uninstall.php @@ -41,7 +41,7 @@ class EP_Uninstaller { 'ep_prefix', 'ep_language', 'ep_bulk_setting', - 'ep_last_syncs', + 'ep_sync_history', // Admin notices options 'ep_hide_host_error_notice', From eb9ee884fdb8cc1efd6bd6573b75213b927f9f4e Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Sep 2023 09:32:51 -0300 Subject: [PATCH 3/5] small refactor --- includes/classes/IndexHelper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/classes/IndexHelper.php b/includes/classes/IndexHelper.php index 9d7341bfb0..3f7453f9ae 100644 --- a/includes/classes/IndexHelper.php +++ b/includes/classes/IndexHelper.php @@ -923,9 +923,9 @@ function( $sync ) { * @param {int} $number Number of past syncs to keep info * @return {int} New number */ - $syncs_to_keep = (int) apply_filters( 'ep_syncs_to_keep_info', 5 - 1 ); + $syncs_to_keep = (int) apply_filters( 'ep_syncs_to_keep_info', 5 ); - $last_syncs = array_slice( $last_syncs, 0, $syncs_to_keep ); + $last_syncs = array_slice( $last_syncs, 0, $syncs_to_keep - 1 ); array_unshift( $last_syncs, $last_sync_info ); Utils\update_option( 'ep_sync_history', $last_syncs, false ); From 94eeeefccf5179e25cc842627a06fc44c49d1a5c Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Sep 2023 10:01:54 -0300 Subject: [PATCH 4/5] Fix tests --- tests/php/TestStatusReport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/TestStatusReport.php b/tests/php/TestStatusReport.php index ae19884fa3..8a6d611014 100644 --- a/tests/php/TestStatusReport.php +++ b/tests/php/TestStatusReport.php @@ -159,7 +159,7 @@ public function testLastSyncReport() { $last_index['total_time'] = microtime( true ) - $start_time; $last_index['method'] = 'cli'; $last_index['is_full_sync'] = 'Yes'; - Utils\update_option( 'ep_last_index', $last_index ); + Utils\update_option( 'ep_sync_history', [ $last_index ] ); $expected_result = array( array( From b9e22f399a9b0eb338cadc5efeb2fad3a7e33101 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 25 Sep 2023 10:13:13 -0300 Subject: [PATCH 5/5] Move 5.0.0 upgrade routine to main comment --- includes/classes/Upgrades.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/Upgrades.php b/includes/classes/Upgrades.php index 69f634146e..16ce95f481 100644 --- a/includes/classes/Upgrades.php +++ b/includes/classes/Upgrades.php @@ -53,8 +53,8 @@ public function setup() { * Adding this without changing the number will make it run on every load. * * @todo Uncomment this before the reelase + * '5.0.0' => [ 'upgrade_5_0_0', 'init' ], */ - // '5.0.0' => [ 'upgrade_5_0_0', 'init' ], ]; array_walk( $routines, [ $this, 'run_upgrade_routine' ] );