Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show WP-CLI progress on sync page #2564

Merged
merged 7 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions assets/js/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,21 @@ updateLastSyncDateTime(epDash?.ep_last_sync_date);
if (epDash.index_meta) {
if (epDash.index_meta.method === 'cli') {
syncStatus = 'wpcli';
processed = epDash.index_meta.items_indexed;
toProcess = epDash.index_meta.total_items;
processed = epDash?.index_meta?.items_indexed;
toProcess = epDash?.index_meta?.total_items;

activeBox = epDash.index_meta.put_mapping ? deleteAndSyncBox : syncBox;

const progressInfoElement = activeBox.querySelector('.ep-sync-box__progress-info');

progressInfoElement.innerText = __('WP-CLI sync in progress', 'elasticpress');

updateStartDateTime(epDash?.index_meta?.start_date_time);

updateDisabledAttribute(syncButton, true);
updateDisabledAttribute(deleteAndSyncButton, true);

showProgress();

updateSyncDash();
cliSync();
Expand Down Expand Up @@ -269,8 +282,15 @@ function updateDisabledAttribute(element, value) {
function updateSyncDash() {
const progressBar = activeBox.querySelector('.ep-sync-box__progressbar_animated');

const progressBarWidth =
toProcess === 0 ? 0 : (parseInt(processed, 10) / parseInt(toProcess, 10)) * 100;
const isSyncing = ['initialsync', 'sync', 'pause', 'wpcli'].includes(syncStatus);

let progressBarWidth;
if (isSyncing) {
progressBarWidth =
toProcess === 0 ? 0 : (parseInt(processed, 10) / parseInt(toProcess, 10)) * 100;
} else {
progressBarWidth = 100;
}

if (
typeof progressBarWidth === 'number' &&
Expand All @@ -282,8 +302,6 @@ function updateSyncDash() {
progressBar.innerText = `${Math.trunc(width)}%`;
}

const isSyncing = ['initialsync', 'sync', 'pause', 'wpcli'].includes(syncStatus);

if (isSyncing) {
progressBar.classList.remove('ep-sync-box__progressbar_complete');
} else if (syncStatus === 'interrupt') {
Expand Down Expand Up @@ -361,18 +379,44 @@ function cliSync() {
}

if (syncStatus === 'wpcli') {
toProcess = response.data?.total_items;
processed = response.data?.items_indexed;
toProcess = response.data?.index_meta?.total_items;
processed = response.data?.index_meta?.items_indexed;

if (response.data.index_meta?.current_sync_item?.failed) {
const message = response.data?.message;
if (Array.isArray(message)) {
message.forEach((item) => {
addErrorToOutput(item);
addLineToOutput(item);
});
const errorTab = activeBox.querySelector('.ep-sync-box__output-tab-error');

totalErrors += response.data.index_meta.current_sync_item.failed;

errorTab.innerText = sprintf(
// translators: Number of errors
__('Errors (%d)', 'elasticpress'),
totalErrors,
);
} else if (typeof message === 'string') {
addErrorToOutput(message);
addLineToOutput(message);
}
} else {
addLineToOutput(response.data.message);
}

updateSyncDash();

if (response.data?.indexing) {
if (response.data?.index_meta?.indexing) {
cliSync();
return;
}
}

syncStatus = 'finished';
addLineToOutput('===============================');
addLineToOutput(__('WP-CLI sync is finished', 'elasticpress'));
updateSyncDash();
});
}
Expand All @@ -383,7 +427,7 @@ function cliSync() {
* @param {string} text Message to show on output
*/
function addLineToOutput(text) {
if (activeBox) {
if (activeBox && text) {
const wrapperElement = activeBox.querySelector('.ep-sync-box__output-wrapper');

const lastLineNumberElement = activeBox.querySelector(
Expand Down
19 changes: 17 additions & 2 deletions includes/classes/Screen/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,25 @@ public function action_wp_ajax_ep_cli_index() {
$index_meta = Utils\get_indexing_status();

if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) {
wp_send_json_success( $index_meta );
wp_send_json_success(
[
'message' => sprintf(
/* translators: 1. Number of objects indexed, 2. Total number of objects, 3. Last object ID */
esc_html__( 'Processed %1$d/%2$d. Last Object ID: %3$d', 'elasticpress' ),
$index_meta['offset'],
$index_meta['found_items'],
$index_meta['current_sync_item']['last_processed_object_id']
),
'index_meta' => $index_meta,
]
);
}

wp_send_json_success( array( 'is_finished' => true ) );
wp_send_json_success(
[
'is_finished' => true,
]
);
}

/**
Expand Down