-
-
Notifications
You must be signed in to change notification settings - Fork 708
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
Take sku overrides into account in customer totals report #5186
Take sku overrides into account in customer totals report #5186
Conversation
I might be seeing ghosts. @oeoeaio is that you?? https://giphy.com/gifs/reaction-2wSaulb0fsDydh0IoB |
I had to reload the page to make sure Github wasn't broken! 😅 |
|
||
def scoper_for(distributor_id) | ||
@scopers_by_distributor_id[distributor_id] ||= | ||
OpenFoodNetwork::ScopeVariantToHub.new(distributor_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.
I'm slightly worried about the potential impact of this in the context of large hubs with a lot of overrides (France has a few) that are already struggling to load reports, given that ScopeVariantToHub
fetches all of the hub's overrides in it's initializer...
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.
Fair enough. I'm pretty rusty and lots has changed (nice work!), any suggestions or examples of a better approach to this kind of problem?
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.
It might be OK. I guess we can just test it in a staging server with lots of data?
I don't think there's an easy alternative for displaying the alternate SKUs.
I'm thinking this change might also need some product team input...?
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.
Righto, who do I ping? Some acceptance criteria that I can work towards would be really helpful.
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.
Maybe @RachL and @lin-d-hop could weigh in on it...
We're changing the SKUs displayed in the customer totals report to show the SKUs from variant overrides instead of the original variant SKU. Could be a surprise for some users? Maybe it would just need an announcement note in the release?
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.
I do agree that it is the technically correct behaviour. But also concerned of the consequences.
SKU is rarely used in the UK so my preference would be to merge this fix.
Would like to hear @RachL's thoughts.
The biggest concern would be performance implications. @Matt-Yorkley can you help us to be as certain as possible this won't increase any server explosion rates?
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.
hmmmm I have no idea if people use SKU on my instance... but if they do I think the change is logical. So I guess we are good to go 👍
Hello @oeoeaio 👋 |
Hey @luisramos0 👋, and thanks, I've marked it as ready for review. |
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.
Hey Rob, it's great to see you around 👍
This will make the report correct.
This will make the report slower. Can be considerably slower, we will load all VariantOverrides of all hubs involved.
The alternative is to do the scoping in the report SQL, it's very difficult. I think @Matt-Yorkley tried to do it at some point for the shopfront endpoints, I cant remember if that code was merged at some point.
I think we should test how the report behaves with lots of data before and after this PR.
I added some "only fetch the objects within the daterange" gymnastics to the line items returned in permissions checks (for reports) a while back in #5030. Is that the one? Maybe we could try to do something similar for the various variant-scoping processes? I'm not sure how easy it would be. |
ah, no, I meant something completely different (older), anyway, you suggestion is awesome! |
So the theory is that the shear number of overrides being loaded into memory will be the problem, rather than the time taken to query for relevant overrides? I am happy to have a crack and building a list of overrides (based on the report parameters) to inject into the scoper if pulling up a smaller number of overrides is desirable. |
fb29d02
to
f9ab3a1
Compare
f9ab3a1
to
14cf168
Compare
@report_variant_overrides ||= | ||
Reports::VariantOverrides.new( | ||
line_items: order_permissions.visible_line_items, | ||
distributor_ids: report_line_items.orders.result.pluck('DISTINCT distributor_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.
Not super happy with this approach, but I couldn't think of a better one. Any ideas?
I had an alternative implementation where the Reports::VariantOverrides
class was only responsible for loading variant overrides for a single distributor at a time, which made it easier to just inject the relevant distributor each time a new one was encountered, but it obviously resulted in more queries. See 2a843bf. Let me know which you think is preferable.
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.
Lets go with this version 👍
Hey @luisramos0, @Matt-Yorkley, I've had another go at this. The new implementation should only load variant overrides that are relevant to the line items in the report. |
Reports::VariantOverrides.new(order_permissions.visible_line_items).indexed | ||
Reports::VariantOverrides.new( | ||
line_items: order_permissions.visible_line_items, | ||
distributor_ids: report_line_items.orders.result.pluck('DISTINCT distributor_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.
report_line_items.orders.result
is an ActiveRecord::Relation
here, so I think in this case #select
will be more efficient than #pluck
for building the distributor_ids
part of the subquery, e.g:
distributor_ids: report_line_items.orders.result.select(:distributor_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.
Done :)
403d7f4
to
9a7e782
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.
Fantastic! It's great to see you working with this code as if it's easy!
This is adding some complexity to an already complex issue in OFN (variant_overrides) but it's a good implementation.
Can you please move the service to app/services? I dont think it needs the reports namespace there, I can see it being re-used in other parts of the app!
Stop using keyword args and accept variant_ids instead of line_items
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.
awesome! thanks @oeoeaio
Hi @kirstenalarsen! Thanks for testing this out.
That is the intended behaviour from my perspective. It is far easier to implement this way (I'm actually not even sure I can think of a way to make it not apply retrospectively, as far as I can recall SKU is a property of variants / overrides, not of line items). I have a use-case implementing it this way too: at BBFH, new variants are often added to the shop on an ad-hoc basis while an order cycle is open (as we get new information about products that are available). Often this is done in a rush to get them onto the shop before we send our weekly newsletter. We may not have time to fill in all of the details required to link products to external systems (i.e. SKU for our POS). We would usually reconcile our orders with other systems after an order cycle has closed, and it is at this point that we would probably go back and look at any new products and add them to our POS or match them to an existing POS product. It is only at this point that an SKU would be set most of the time, so it would be useful for us to have any SKUs added to the inventory apply retrospectively. |
Hi @oeoeaio, Verified all the findings. Also checked whether this is reversible: Checking the "Inherit?" works, and sets back the SKU settings from the shop. The general functionality of reports does not seem affected. All good. In terms of testing I'd say this is good to go. |
What? Why?
Closes #5185
What should we test?
Release notes
Changelog Category: Fixed