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

Adding checksums to Jetpack_JSON_API_Sync_Status_Endpoint #12209

Merged
merged 19 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from 12 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
10 changes: 6 additions & 4 deletions json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ protected function validate_queue( $query ) {
// GET /sites/%s/sync/status
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
protected function result() {
return Jetpack_Sync_Actions::get_sync_status();
$args = $this->query_args();
$fields = isset( $args['fields'] ) ? $args['fields'] : array();
return Jetpack_Sync_Actions::get_sync_status( $fields );
}
}

Expand Down Expand Up @@ -101,7 +103,7 @@ protected function result() {
if ( is_numeric( $value ) ) {
$value = (int) $value;
}

// special case for sending empty arrays - a string with value 'empty'
if ( $value === 'empty' ) {
$value = array();
Expand Down Expand Up @@ -209,12 +211,12 @@ protected function result() {
}

$buffer = $this->get_buffer( $queue, $args[ 'number_of_items' ] );

// Check that the $buffer is not checkout out already
if ( is_wp_error( $buffer ) ) {
return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 );
}

if ( ! is_object( $buffer ) ) {
return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 );
}
Expand Down
65 changes: 37 additions & 28 deletions json-endpoints/jetpack/json-api-jetpack-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,34 +455,43 @@
) );

// GET /sites/%s/sync/status
new Jetpack_JSON_API_Sync_Status_Endpoint( array(
'description' => 'Status of the current full sync or the previous full sync',
'method' => 'GET',
'path' => '/sites/%s/sync/status',
'stat' => 'sync-status',
'path_labels' => array(
'$site' => '(int|string) The site ID, The site domain'
),
'response_format' => array(
'started' => '(int|null) The unix timestamp when the last sync started',
'queue_finished' => '(int|null) The unix timestamp when the enqueuing was done for the last sync',
'send_started' => '(int|null) The unix timestamp when the last sent process started',
'finished' => '(int|null) The unix timestamp when the last sync finished',
'total' => '(array) Count of actions that could be sent',
'queue' => '(array) Count of actions that have been added to the queue',
'sent' => '(array) Count of actions that have been sent',
'config' => '(array) Configuration of the last full sync',
'queue_size' => '(int) Number of items in the sync queue',
'queue_lag' => '(float) Time delay of the oldest item in the sync queue',
'queue_next_sync' => '(float) Time in seconds before trying to sync again',
'full_queue_size' => '(int) Number of items in the full sync queue',
'full_queue_lag' => '(float) Time delay of the oldest item in the full sync queue',
'full_queue_next_sync' => '(float) Time in seconds before trying to sync the full sync queue again',
'cron_size' => '(int) Size of the current cron array',
'next_cron' => '(int) The number of seconds till the next item in cron.',
),
'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/example.wordpress.org/sync/status'
) );
new Jetpack_JSON_API_Sync_Status_Endpoint(
array(
'description' => 'Status of the current full sync or the previous full sync',
'method' => 'GET',
'path' => '/sites/%s/sync/status',
'stat' => 'sync-status',
'path_labels' => array(
'$site' => '(int|string) The site ID, The site domain',
),
lezama marked this conversation as resolved.
Show resolved Hide resolved
'query_parameters' => array(
'fields' => '(string|null) List of comma-separated fields to return (see `response_format`).',
),
'response_format' => array(
'posts_checksum' => '(string|null) Posts checksum. Needs to be requested using the filter parameter.',
'comments_checksum' => '(string|null) Comments checksum. Needs to be requested using the filter parameter.',
'post_meta_checksum' => '(string|null) Post Meta checksum. Needs to be requested using the filter parameter.',
'comment_meta_checksum' => '(string|null) Comment Meta checksum. Needs to be requested using the filter parameter.',
'started' => '(int|null) The unix timestamp when the last sync started',
'queue_finished' => '(int|null) The unix timestamp when the enqueuing was done for the last sync',
'send_started' => '(int|null) The unix timestamp when the last send process started',
'finished' => '(int|null) The unix timestamp when the last sync finished',
'total' => '(array) Count of actions that could be sent',
'queue' => '(array) Count of actions that have been added to the queue',
'sent' => '(array) Count of actions that have been sent',
'config' => '(array) Configuration of the last full sync',
'queue_size' => '(int) Number of items in the sync queue',
'queue_lag' => '(float) Time delay of the oldest item in the sync queue',
'queue_next_sync' => '(float) Time in seconds before trying to sync again',
'full_queue_size' => '(int) Number of items in the full sync queue',
'full_queue_lag' => '(float) Time delay of the oldest item in the full sync queue',
'full_queue_next_sync' => '(float) Time in seconds before trying to sync the full sync queue again',
'cron_size' => '(int) Size of the current cron array',
'next_cron' => '(int) The number of seconds till the next item in cron.',
),
'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/example.wordpress.org/sync/status',
)
);


// GET /sites/%s/data-checksums
Expand Down
26 changes: 24 additions & 2 deletions sync/class.jetpack-sync-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ static function cleanup_on_upgrade( $new_version = null, $old_version = null ) {
}
}

static function get_sync_status() {
static function get_sync_status( $fields = null ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if fields was always an array. instead of sometimes an array and sometimes a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a helper function. Query params need to be strings anyway and I think this is the most straightforward way to handle things.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also support both array and string (like many other WP core functions and methods do), but we should make sure that's properly supported by adding specific tests for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say we don't need that because this is just a helper function used in a single JSON API response.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we never really using anything but a string.

self::initialize_sender();

$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
Expand All @@ -448,9 +448,32 @@ static function get_sync_status() {
$cron_timestamps = array_keys( _get_cron_array() );
$next_cron = $cron_timestamps[0] - time();

$checksums = array();

if ( ! is_null( $fields ) ) {
lezama marked this conversation as resolved.
Show resolved Hide resolved
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php';
$store = new Jetpack_Sync_WP_Replicastore();
$fields_params = array_map( 'trim', explode( ',', $fields ) );

if ( in_array( 'posts_checksum', $fields_params, true ) ) {
$checksums['posts_checksum'] = $store->posts_checksum();
}
if ( in_array( 'comments_checksum', $fields_params, true ) ) {
$checksums['comments_checksum'] = $store->comments_checksum();
}
if ( in_array( 'post_meta_checksum', $fields_params, true ) ) {
$checksums['post_meta_checksum'] = $store->post_meta_checksum();
}
if ( in_array( 'comment_meta_checksum', $fields_params, true ) ) {
$checksums['comment_meta_checksum'] = $store->comment_meta_checksum();
}
}

$full_sync_status = ( $sync_module ) ? $sync_module->get_status() : array();

return array_merge(
$full_sync_status,
$checksums,
array(
'cron_size' => count( $cron_timestamps ),
'next_cron' => $next_cron,
Expand Down Expand Up @@ -482,4 +505,3 @@ static function get_sync_status() {
// We need to define this here so that it's hooked before `updating_jetpack_version` is called
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'cleanup_on_upgrade' ), 10, 2 );
add_action( 'jetpack_user_authorized', array( 'Jetpack_Sync_Actions', 'do_initial_sync' ), 10, 0 );