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

Make split_promotions_with_any_match_policy task faster #4737

Conversation

tvdeyen
Copy link
Member

@tvdeyen tvdeyen commented Nov 23, 2022

Summary

Do not re-apply promotions to incomplete orders if there are no promotions effected. This is not necessary and speeds up the task for shop with lots of incomplete orders (this could easily be in the thousands).

Also make the effected promotions update faster by using find_each and include actions as well.

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

  • I have written a thorough PR description.
  • I have kept my commits small and atomic.
  • I have used clear, explanatory commit messages.

Do not re-apply promotions to incomplete orders if there
are no promotions effected. This is not necessary and speeds
up the task for shop with lots of incomplete orders (this could
easily be in the thousands).

Also make the effected promotions update faster by using find_each
and include actions as well.
@github-actions github-actions bot added the changelog:solidus_core Changes to the solidus_core gem label Nov 23, 2022
@tvdeyen
Copy link
Member Author

tvdeyen commented Nov 23, 2022

@mamhoff would love your feedback on this :)

@waiting-for-dev waiting-for-dev added the type:enhancement Proposed or newly added feature label Nov 23, 2022
Copy link
Contributor

@mamhoff mamhoff left a comment

Choose a reason for hiding this comment

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

Looks good to me, if it helps your performance woes, LGTM!

@codecov
Copy link

codecov bot commented Nov 23, 2022

Codecov Report

Merging #4737 (7ecdc7e) into master (d582f54) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #4737   +/-   ##
=======================================
  Coverage   86.14%   86.14%           
=======================================
  Files         578      578           
  Lines       14654    14656    +2     
=======================================
+ Hits        12623    12625    +2     
  Misses       2031     2031           
Impacted Files Coverage Δ
...olidus/split_promotions_with_any_match_policy.rake 96.00% <100.00%> (+0.34%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

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

Thanks! I left a suggestion to further improve performance here.

@@ -28,6 +29,8 @@ namespace :solidus do
end
end

Spree::Order.where(completed_at: nil).each { |order| Spree::PromotionHandler::Cart.new(order).activate }
if promotions.any?
Spree::Order.where(completed_at: nil).each { |order| Spree::PromotionHandler::Cart.new(order).activate }
Copy link
Contributor

Choose a reason for hiding this comment

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

What about using #find_each also here?

Copy link
Member

Choose a reason for hiding this comment

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

@tvdeyen did you see this comment? would love to merge this before we release 3.4, otherwise we need to backport there as well. 🙏

@@ -3,7 +3,8 @@
namespace :solidus do
desc "Split Promotions with 'any' match policy"
task split_promotions_with_any_match_policy: :environment do
Spree::Promotion.where(match_policy: :any).includes(:promotion_rules).all.each do |promotion|
promotions = Spree::Promotion.where(match_policy: :any)
promotions.includes(:promotion_rules, :promotion_actions).find_each do |promotion|
if promotion.promotion_rules.length <= 1
promotion.update!(match_policy: :all)
elsif promotion.active?
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't link it, but isn't line 15 unnecessarily nested? I don't think we should be iterating over each action to create a duplicate for every rule 🤔

new_promotion.promotion_actions = promotion.actions.map do |action|

If we want to make it faster, we could move the iteration out.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is about creating a setup where promotions with a match_policy of any are transformed into multiple promotions, one per rule of the existing promotions. It might look like a lot of data, but that's really what match_policy: any means...

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh of course! 🤦 Thanks for your reply, forgive my mind going blank on this 😅

@kennyadsl kennyadsl added the backport-v3.3 Backport this pull-request to v3.3 label Apr 14, 2023
@tvdeyen
Copy link
Member Author

tvdeyen commented Nov 3, 2023

closing as stale

@tvdeyen tvdeyen closed this Nov 3, 2023
@tvdeyen tvdeyen deleted the fix-split_promotions_with_any_match_policy-task branch November 3, 2023 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-v3.3 Backport this pull-request to v3.3 changelog:solidus_core Changes to the solidus_core gem type:enhancement Proposed or newly added feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants