-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat(backfill): WP CLI script to backfill reader_registered, donation_new events #51
Conversation
3505a9e
to
bc295f3
Compare
bc295f3
to
188ff65
Compare
00da47a
to
ecf40bf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good, I left a couple of comments, but I wanted to suggest an architectural change so this is more aligned to the rest of the plugin.
Instead of having everything inside the Data_Backfill class, we should keep it just with the CLI command.
Then we can create a new namespace, something like Newspack_Network\Backfillers
, where we can have one class per event, following the same nomenclature define in Accepted_Actions
and which we use in other places.
The Backfiller_Abstract_Class
could implement a method that is common to every event, what is currently the process_event_entity
.
And each class need to implement its own get_events
method, that will return an array with Income_Event
objects (instances of their specific classes, and not the abstract one). The process_event_entity
method would receive such array and process each item.
In the command callback, we would not need the switch case https://github.com/Automattic/newspack-network/pull/51/files#diff-2fbd8473ad6861f6fec58b5890525ac5092f375cbe554e54f604a9871e490ed8R324 as we would simply create an instance of the backfiller for the required action.
And we could fallback to the default class, which would simply return that this action is not backfillable (something like we do here ->
newspack-network/includes/hub/stores/class-event-log.php
Lines 52 to 56 in bcb0615
$class_name = 'Newspack_Network\\Hub\\Stores\\Event_Log_Items\\' . Accepted_Actions::ACTIONS[ $item->action_name ]; | |
if ( ! class_exists( $class_name ) ) { | |
$class_name = 'Newspack_Network\\Hub\\Stores\\Event_Log_Items\\Generic'; | |
} | |
$results[] = new $class_name( |
I hope this makes sense to you.
I think it has a number of advantages, not only to keep consistency with the rest of the plugin, but also to keep small classes and avoid this class to grow indefinitely. It will also be easier to implement new backfillers for new actions, as all you need to know is implement one class, no need to touch several places in the code.
Since all the work is done and this is basically moving code around, I'm happy to do it if you prefer to focus on other tasks. Let me know!
includes/cli/class-data-backfill.php
Outdated
*/ | ||
private static function process_reader_registered( $start, $end, $live, $verbose ) { | ||
// Get all users registered between $start and $end. | ||
$users = get_users( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could filter out users with newspack_remote_id
metadata. No need to backfill users that came from other sites. We want users that were originally registered on this site
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, great suggestion! Done in 111a7b4
*/ | ||
public function __construct( $site, $data, $timestamp ) { | ||
public function __construct( $site, $data, $timestamp, $action_name = false ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we shouldn't need this. See my comment on the suggested architecture
Re: architectural change – sounds good, but for later. There's already another PR - #62 - relying on this PR. |
includes/cli/class-data-backfill.php
Outdated
} | ||
foreach ( $orders as $order ) { | ||
$order_id = $order->get_id(); | ||
$order_data = \Newspack\Data_Events\Utils::get_order_data( $order_id ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to check if it is a donation related order. see my review on the newspack plugin's PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in the newspack-plugin
PR
includes/cli/class-data-backfill.php
Outdated
self::$progress = \WP_CLI\Utils\make_progress_bar( 'Processing subscriptions', count( $subscriptions ) ); | ||
} | ||
foreach ( $subscriptions as $subscription ) { | ||
$subscription_data = \Newspack\Data_Events\Utils::get_subscription_data( $subscription ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again we need to check if it's a donation subscription here. See my comment on the main plugin PR
What happened to me was that I had a bunch of cancellations for non-donation subscriptions. My backfill data was then full of subscription_cancelled events with empy data (because the check was on the Utils function).
So the check should not be there, and be here instead, so we don't include subscription data at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handled in 0153127
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good
I left one comment about the checks in donation_subscription_cancelled and donation_new, but other than that it seems to be working fine!
Also left a minor comment about the command output
Oh, I added the comments to the commit, not to this PR. Let me add them here as well for visibility We also need to check if the data is empty for the order in donation_new, otherwise we get events with empty data for non donation orders. And I think these checks should trigger a |
Fixed in f9cc61e |
Refactor/backfillers
feat(backfill-cli): handle membership events
@adekbadek I have performed a bunch of tests and all seems to be working fine, including Memberships. I'll leave it as pre-approved to unblock you. If you could run a few more tests it would be nice. This will need to be tested with real data anyway. And I hope you like the refactor :) |
# [1.3.0-alpha.1](v1.2.0...v1.3.0-alpha.1) (2024-02-23) ### Bug Fixes * memberships sync ([#63](#63)) ([0a54f1d](0a54f1d)) ### Features * add hub bookmarks to nodes ([#56](#56)) ([54700a0](54700a0)) * add option to manually sync users ([#53](#53)) ([3ec1b19](3ec1b19)) * display membership plans from the network ([#59](#59)) ([6fa01fd](6fa01fd)) * enhance network subscriptions and orders search ([#55](#55)) ([bcb0615](bcb0615)) * Node connection using a link ([#58](#58)) ([721f8b2](721f8b2)) * **wp-cli:** add command to backfill events ([#51](#51)) ([13d2498](13d2498)) * **wp-cli:** add process pending webhook requests command ([#67](#67)) ([7dbd8dc](7dbd8dc)) * **wp-cli:** sync all events from the Hub ([#65](#65)) ([dc595ca](dc595ca))
🎉 This PR is included in version 1.3.0-alpha.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
# [1.3.0](v1.2.0...v1.3.0) (2024-02-28) ### Bug Fixes * backfill duplicate handling; woo links in wp-admin-bar ([#71](#71), [#72](#72)) ([bbce13b](bbce13b)) * cli commands ([#73](#73)) ([ff563ac](ff563ac)) * memberships sync ([#63](#63)) ([0a54f1d](0a54f1d)) * missing woocommerce-memberships handling ([76dbdf7](76dbdf7)) ### Features * add hub bookmarks to nodes ([#56](#56)) ([54700a0](54700a0)) * add option to manually sync users ([#53](#53)) ([3ec1b19](3ec1b19)) * display membership plans from the network ([#59](#59)) ([6fa01fd](6fa01fd)) * enhance network subscriptions and orders search ([#55](#55)) ([bcb0615](bcb0615)) * Node connection using a link ([#58](#58)) ([721f8b2](721f8b2)) * **wp-cli:** add command to backfill events ([#51](#51)) ([13d2498](13d2498)) * **wp-cli:** add process pending webhook requests command ([#67](#67)) ([7dbd8dc](7dbd8dc)) * **wp-cli:** sync all events from the Hub ([#65](#65)) ([dc595ca](dc595ca))
🎉 This PR is included in version 1.3.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
All Submissions:
Changes proposed in this Pull Request:
Adds a WP CLI script to backfill historical data.
How to test the changes in this Pull Request:
newspack-plugin
tofeat/tweaks-for-network-update
branch (Feat/tweaks for network update newspack-plugin#2935)newspack-network
plugin on both siteswp newspack-network data-backfill reader_registered --start=2024-01-01 --end=2024-12-31
on the Hub site, observe the output the dry run and confirm nothing was added to the event log.donation_new
as the argument instead ofreader_registered
.donation_subscription_cancelled
--verbose
flag for more outputwp newspack-network data-backfill reader_registered --start=2024-01-01 --end=2024-12-31 --live
on the Hub site and observe the event was added to the event log.donation_new
as the argument instead ofreader_registered
.donation_subscription_cancelled
all
as action name argument and observe it has the same effect as running the above two scripts one after another* unless the webhook request queue has be cleared already, but the event log should handle deduplication itself anyway
Other information: