Skip to content

Commit

Permalink
Merge pull request #3647 from 10up/feature/tw-19354085-store-addl-syncs
Browse files Browse the repository at this point in the history
Store additional syncs info
  • Loading branch information
felipeelia authored Sep 25, 2023
2 parents 8e07599 + b9e22f3 commit 315188a
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 19 deletions.
2 changes: 1 addition & 1 deletion includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
87 changes: 77 additions & 10 deletions includes/classes/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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_sync_history()
);

/**
* 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 );

$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 );
}

/**
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -1084,13 +1125,27 @@ public function is_full_reindexing( $indexable_slug, $blog_id = null ) {
}

/**
* Get the last index/sync meta information.
* Get the previous 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_sync_history() : array {
return Utils\get_option( 'ep_sync_history', [] );
}

/**
* Get the last sync meta information.
*
* @since 5.0.0
* @return array
*/
public function get_last_sync() : array {
$syncs = $this->get_sync_history();
if ( empty( $syncs ) ) {
return [];
}
return array_shift( $syncs );
}

/**
Expand Down Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion includes/classes/REST/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]
);
}
Expand Down
8 changes: 4 additions & 4 deletions includes/classes/Screen/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/StatusReport/LastSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down
18 changes: 18 additions & 0 deletions includes/classes/Upgrades.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
Expand Down Expand Up @@ -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_sync_history'
*/
$last_sync = Utils\get_option( 'ep_last_index', [] );
Utils\delete_option( 'ep_last_index' );
Utils\update_option( 'ep_sync_history', [ $last_sync ] );
}

/**
* Adjust the upgrade sync notice to warn users about Instant Results.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/php/TestStatusReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class EP_Uninstaller {
'ep_prefix',
'ep_language',
'ep_bulk_setting',
'ep_last_index',
'ep_sync_history',

// Admin notices options
'ep_hide_host_error_notice',
Expand Down

0 comments on commit 315188a

Please sign in to comment.