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

[WP] Improve sync progress #65

Merged
merged 11 commits into from
Mar 15, 2024
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ services:
profiles: ["wp"]

db:
image: mariadb:10.4
image: mysql:8.0
ports:
- 3306:3306
volumes:
Expand All @@ -84,7 +84,7 @@ services:
profiles: ["wp"]

phpmyadmin:
image: phpmyadmin/phpmyadmin
image: phpmyadmin:latest
ports:
- 8080:80
links:
Expand Down
1 change: 1 addition & 0 deletions wordpress-plugin/src/DB/SyncProgress.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function create_table() {
$sql = 'CREATE TABLE ' . static::$table_name . " (
id INT NOT NULL AUTO_INCREMENT,
model TEXT NOT NULL,
model_id VARCHAR(191),
status MEDIUMTEXT NOT NULL,
message TEXT NOT NULL,
data MEDIUMTEXT NOT NULL,
Expand Down
60 changes: 56 additions & 4 deletions wordpress-plugin/src/Models/SyncProgress.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SyncProgress extends Model {
public static $columns_format = array(
'id' => '%d',
'model' => '%s',
'model_id' => '%s',
'status' => '%s',
'message' => '%s',
'data' => '%s',
Expand All @@ -27,6 +28,7 @@ class SyncProgress extends Model {

public static $validation_rules = array(
'model' => 'required|in:product,product-collection,product-variant,region,thumbnail',
'model_id' => 'nullable|string',
anteprimorac marked this conversation as resolved.
Show resolved Hide resolved
'status' => 'required|in:syncing,success,error',
'message' => 'required|string',
'data' => 'required|string',
Expand Down Expand Up @@ -114,28 +116,78 @@ public static function count_all_synced( int $sync_timestamp ) {
$wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT COUNT(*) FROM $table_name WHERE sync_timestamp = %d",
"SELECT COUNT(DISTINCT model_id) FROM $table_name WHERE sync_timestamp = %d",
$sync_timestamp
)
)
);
}

public static function get_sync_progress_troubleshoot_messages( int $sync_timestamp ) {
public static function get_troubleshoot_messages( int $sync_timestamp, $options = array() ) {
/**
* @var \wpdb $wpdb
*/
global $wpdb;
$table_name = static::$table_name;

if ( empty( $options ) ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $wpdb->get_results(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT * FROM $table_name"
),
ARRAY_A
);
}

$default_options = array(
'page' => 1,
'per_page' => get_option( 'posts_per_page', 10 ),
);

$options = array_merge( $default_options, $options );

$options['page'] = abs( intval( $options['page'] ) );
$options['per_page'] = abs( intval( $options['per_page'] ) );

if ( $options['per_page'] < 1 ) {
$options['per_page'] = $default_options['per_page'];
}

if ( $options['page'] < 1 ) {
$options['page'] = $default_options['page'];
}

$offset = ( $options['page'] - 1 ) * $options['per_page'];

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $wpdb->get_results(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT * FROM $table_name WHERE sync_timestamp = %d AND status = 'error' ORDER BY started_at DESC;",
array( $sync_timestamp )
"SELECT * FROM $table_name WHERE sync_timestamp = %d AND status = 'error' ORDER BY started_at DESC LIMIT %d OFFSET %d;",
array( $sync_timestamp, $options['per_page'], $offset )
),
'ARRAY_A'
);
}

public static function count_troubleshoot_messages( int $sync_timestamp ) {
/**
* @var \wpdb $wpdb
*/
global $wpdb;
$table_name = static::$table_name;

return intval(
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT COUNT(*) FROM $table_name WHERE sync_timestamp = %d AND status = 'error'",
$sync_timestamp
)
)
);
}
}
133 changes: 122 additions & 11 deletions wordpress-plugin/src/Routes/Admin/MedusaBulkSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private function get_sync_message_schema() {
'ended_at',
'medusa_admin_link',
'model',
'model_id',
),
'properties' => array(
'id' => array(
Expand All @@ -54,6 +55,10 @@ private function get_sync_message_schema() {
'type' => 'string',
'description' => __( 'Sync message model.', 'medusawp' ),
),
'model_id' => array(
'type' => array( 'string', 'null' ),
'description' => __( 'Sync message model id.', 'medusawp' ),
),
'status' => array(
'type' => 'string',
'description' => __( 'Sync message status.', 'medusawp' ),
Expand Down Expand Up @@ -121,7 +126,7 @@ public function sync_messages_schema() {
);
}

private function get_sync_response_object_schema( $with_messages = false ) {
private function get_sync_response_object_schema() {
$title = 'MedusaWPSyncResponse';
$required = array(
'started_at',
Expand Down Expand Up @@ -251,15 +256,6 @@ private function get_sync_response_object_schema( $with_messages = false ) {
),
);

if ( $with_messages ) {
$title = 'MedusaWPSyncResponseWithMessages';
$required[] = 'messages';
$properties['messages'] = array(
'type' => 'array',
'items' => $this->get_sync_message_schema(),
);
}

return array(
'title' => $title,
'type' => 'object',
Expand Down Expand Up @@ -300,6 +296,40 @@ public function sync_progress_response_schema() {
);
}

public function sync_progress_messages_response_schema() {
return array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'MedusaWPSyncProgressMessagesResponse',
'type' => 'object',
'additionalProperties' => false,
'required' => array(
'messages',
'total',
'current_page',
'last_page',
),
'properties' => array(
'messages' => array(
'type' => 'array',
'items' => $this->get_sync_message_schema(),
'description' => __( 'Messages.', 'medusawp' ),
),
'total' => array(
'type' => 'number',
'description' => __( 'Total number of messages.', 'medusawp' ),
),
'current_page' => array(
'type' => 'number',
'description' => __( 'Current page.', 'medusawp' ),
),
'last_page' => array(
'type' => 'number',
'description' => __( 'Last page.', 'medusawp' ),
),
),
);
}

public function register_admin_routes() {
register_rest_route(
$this->namespace,
Expand Down Expand Up @@ -369,6 +399,33 @@ public function register_admin_routes() {
),
);

register_rest_route(
$this->namespace,
'/sync-progress/messages',
array(
array(
'methods' => 'GET',
'callback' => array( $this, 'get_sync_progress_messages' ),
'args' => array(
'page' => array(
'type' => 'integer',
'required' => false,
'default' => 1,
),
'per_page' => array(
'type' => 'integer',
'required' => false,
'default' => intval( get_option( 'posts_per_page', 10 ) ),
),
),
'permission_callback' => function () {
return current_user_can( 'manage_options' );
},
),
'schema' => array( $this, 'sync_progress_messages_response_schema' ),
),
);

register_rest_route(
$this->namespace,
'/remove-synced',
Expand Down Expand Up @@ -597,13 +654,67 @@ public function get_sync_progress() {
'totals' => $progress['totals'],
'synced' => $synced,
'import_thumbnails' => $progress['type'] === 'bulk_sync_and_import_thumbnails' || $progress['type'] === 'import_thumbnails',
'messages' => SyncProgress::get_sync_progress_troubleshoot_messages( $sync_timestamp ),
'type' => $progress['type'],
),
)
);
}

/**
* Route callback: Get sync progress messages.
*
* @param \WP_REST_Request $req
* @return WP_REST_Response
*/
public function get_sync_progress_messages( $req ) {
$progress = Settings::get_sync_progress();
$sync_timestamp = $progress['started_at'];

$params = $req->get_params();

/**
* @var int
*/
$page = $params['page'];

/**
* @var int
*/
$per_page = $params['per_page'];

$total_records = SyncProgress::count_troubleshoot_messages( $sync_timestamp );

$total_pages = ceil( $total_records / $per_page );

$options = array(
'page' => $page,
'per_page' => $per_page,
);

$options['page'] = filter_var(
$page,
FILTER_VALIDATE_INT,
array(
'options' => array(
'default' => $page > $total_pages ? $total_pages : 1,
'min_range' => 1,
'max_range' => $total_pages,
),
)
);

$messages = SyncProgress::get_troubleshoot_messages( $sync_timestamp, $options );

return new WP_REST_Response(
array(
'messages' => $messages,
'total' => $total_records,
'current_page' => $options['page'],
'last_page' => $total_pages,
)
);
}

/**
* Route callback: Remove synced data.
*
Expand Down
1 change: 1 addition & 0 deletions wordpress-plugin/src/ScheduledActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static function import_product_thumbnail( string $product_id, ?int $sync_
$processing_item_id = Models\SyncProgress::save(
array(
'model' => 'thumbnail',
'model_id' => $product_id,
'message' => 'Syncing thumbnail of ' . $product_id . ' product...',
'status' => 'syncing',
'data' => $product ? wp_json_encode( $product ) : array( 'id' => $product_id ),
Expand Down
1 change: 1 addition & 0 deletions wordpress-plugin/src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public static function save_sync_progress( string $model, array $data, ?int $syn
return Models\SyncProgress::save(
array(
'model' => $model,
'model_id' => $data['id'],
'message' => 'Syncing ' . $data['id'] . '...',
'status' => 'syncing',
'data' => wp_json_encode( $data ),
Expand Down
Loading