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

Report summaries in JSON (API) output #10466

Merged
merged 4 commits into from
Mar 22, 2023

Conversation

dacook
Copy link
Member

@dacook dacook commented Feb 21, 2023

What? Why?

This change allows you to optionally enable summary rows on JSON report output, via the reports v0 API.

Example output:
http://localhost:3000/api/v0/reports/packing?authenticity_token=****&q%5Border_completed_at_gt%5D=2022-10-01+00%3A00&q%5Border_completed_at_lt%5D=2023-02-22+00%3A00&q%5Border_distributor_id_in%5D%5B%5D=&report_subtype=supplier&report_format=&display_summary_row=true&fields_to_show%5B%5D=hub&fields_to_show%5B%5D=supplier&fields_to_show%5B%5D=customer_code&fields_to_show%5B%5D=first_name&fields_to_show%5B%5D=last_name&fields_to_show%5B%5D=product&fields_to_show%5B%5D=variant&fields_to_show%5B%5D=quantity&fields_to_show%5B%5D=report_row_type

{
  "data": [
    {
      "hub": "Elmore Compost and Organics",
      "supplier": "Elmore Compost and Organics",
      "customer_code": null,
      "first_name": "David",
      "last_name": "Cook",
      "product": "Biochar",
      "variant": "2L",
      "quantity": 1
    },
    {
      "hub": "",
      "supplier": "",
      "customer_code": "",
      "first_name": "",
      "last_name": "",
      "product": "",
      "variant": "Total By Customer",
      "quantity": 1,
      "report_row_type": "summary"
    },
...

What should we test?

Test to ensure no change in Admin Reports:

  • column options in form dropdown
  • displayed columns
  • displayed header rows
  • displayed summary rows

Test to ensure no change in API v0 reports:

  • columns
  • header not displayed by default
  • summary not displayed by default

Test to enable summary rows

  • Add parameter display_header_row=true
  • Add parameter display_summary_row=true

Extra rows should be output, similar to example above

Test to enable summary row markers

Summary rows look just like normal rows, so an additional marker can be used to differentiate them:

  • Add parameter fields_to_show[]=report_row_type

The summary rows should have a new column/field: "report_row_type": "summary"

Release notes

Changelog Category: Technical changes

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

Yep: https://ofn-user-guide.gitbook.io/ofn-api-handbook/ofn-api-v0-unsupported/reports

@dacook dacook marked this pull request as ready for review February 21, 2023 03:34
@@ -10,7 +10,13 @@ def initialize(report)
@report = report
end

# Strip headers and footers for these formats
Copy link
Member

Choose a reason for hiding this comment

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

Does footers refer to the summary rows?

Copy link
Member

Choose a reason for hiding this comment

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

if raw_render? is only used to decide about the header and summary. Maybe we can use a better name?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, you're right it should say "summary rows" rather than "footers".

I agree it would be good to have a better name. I'm not sure what is better though. Maybe render_data_only?

Copy link
Member

Choose a reason for hiding this comment

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

I think this is better :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated the comment, but opted to avoid renaming the method because I hope to remove it anyway.

@abdellani
Copy link
Member

Hi @dacook

Does this solve #6850?

@dacook
Copy link
Member Author

dacook commented Mar 3, 2023

Does this solve #6850?

Actually no, that issue is to allow summary rows in CSV output. But it seems we don't quite have consensus yet.

This PR is to allow summary rows in JSON. I don't think it depends on a decision on the above, and it does unblock our integration requirements in AU so I suggest we continue. I'll create an issue for reference.

@dacook
Copy link
Member Author

dacook commented Mar 3, 2023

@abdellani , I've attached an issue and addressed one of your comments. Would you be able to complete your review?

@dacook dacook requested a review from abdellani March 3, 2023 00:42
Copy link
Member

@abdellani abdellani left a comment

Choose a reason for hiding this comment

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

Everything looks good :)

Thank you @dacook

Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Nice quick solution.

@@ -124,7 +124,7 @@ def check_report
end

it "get correct data" do
allow(subject).to receive(:raw_render?).and_return(true)
allow(subject).to receive(:unformatted_render?).and_return(true)
Copy link
Member

Choose a reason for hiding this comment

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

I think this line should have been in a later commit. Too late now. 🤷

@filipefurtad0 filipefurtad0 self-assigned this Mar 22, 2023
@filipefurtad0 filipefurtad0 added the pr-staged-uk staging.openfoodnetwork.org.uk label Mar 22, 2023
dacook added 4 commits March 22, 2023 11:47
It looks like it was expected that the price and amount columns would be formatted, but this is not currently the case. So I cleaned this up to be less ambiguous.

If any of these columns were to be configured for formatting, this line could be added back to test for unformatted output.
…ndependently

This will allow us to change the options in the following commit.
You wouldn't want it by default, but it can be enabled with a parameter like display_summary_row=true
This can be enabled in the reports API with the hidden parameter `fields_to_show[]=report_row_type` to help with parsing summary row output.
@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Mar 22, 2023

Hey @dacook,

I think there are only three report types, which include the header and summary row options. These are:

  • Bulk Co-Op
  • Orders & Fulfillment
  • Packing

For these three cases, I've tested both the API v0 requests; for all others only UI tests. A summary is found below:

API V0 tests

  • Bulk Co-Op 🟢

Bulk Co-op Supplier Report

https://staging.openfoodnetwork.org.uk/api/v0/reports/bulk_coop?token=****&report_subtype=supplier_report&q[completed_at_gt]=2022-02-22+00:00&q[completed_at_lt]=2023-03-23+00:00&fields_to_show%5B%5D=supplier&fields_to_show%5B%5D=product&fields_to_show%5B%5D=max_quantity_excess&fields_to_show%5B%5D=product&display_header_row=true&display_summary_row=true&fields_to_show[]=report_row_type&fields_to_show[]=bulk_unit_size&fields_to_show[]=variant&fields_to_show[]=sum_total&fields_to_show[]=unallocated&fields_to_show[]=units_required

image

Bulk Co-op Allocation

https://staging.openfoodnetwork.org.uk/api/v0/reports/bulk_coop?token=****&report_subtype=allocation&q[completed_at_gt]=2023-02-22+00:00&q[completed_at_lt]=2023-03-23+00:00&fields_to_show%5B%5D=supplier&fields_to_show%5B%5D=product&fields_to_show%5B%5D=max_quantity_excess&fields_to_show%5B%5D=product&display_header_row=true&display_summary_row=true&fields_to_show[]=report_row_type&fields_to_show[]=bulk_unit_size&fields_to_show[]=variant&fields_to_show[]=sum_total&fields_to_show[]=unallocated&fields_to_show[]=units_required&fields_to_show[]=customer

image

  • Orders & Fulfillment Reports 🟡

Order Cycle Supplier Totals

https://staging.openfoodnetwork.org.uk/api/v0/reports/orders_and_fulfillment?token=****&report_subtype=order_cycle_supplier_totals&q[completed_at_gt]=2023-02-22+00:00&q[completed_at_lt]=2023-03-23+00:00&fields_to_show%5B%5D=producer&fields_to_show%5B%5D=product&fields_to_show%5B%5D=variant&fields_to_show%5B%5D=quantity&fields_to_show%5B%5D=total_units&fields_to_show%5B%5D=total_cost&fields_to_show%5B%5D=sku&fields_to_show%5B%5D=producer_charges_sales_tax?&fields_to_show%5B%5D=product_tax_category&display_header_row=true&display_summary_row=true&fields_to_show[]=report_row_type&fields_to_hide%5B%5D&fields_to_show[]=
image

Order Cycle Supplier Totals by Distributor
https://staging.openfoodnetwork.org.uk/api/v0/reports/orders_and_fulfillment?token=****&report_subtype=order_cycle_supplier_totals_by_distributor&q[completed_at_gt]=2023-02-22+00:00&q[completed_at_lt]=2023-03-23+00:00&fields_to_show%5B%5D=producer&fields_to_show%5B%5D=product&fields_to_show%5B%5D=variant&fields_to_show%5B%5D=quantity&fields_to_show%5B%5D=total_units&fields_to_show%5B%5D=total_cost&fields_to_show%5B%5D=sku&fields_to_show%5B%5D=producer_charges_sales_tax?&fields_to_show%5B%5D=product_tax_category&display_header_row=true&display_summary_row=true&fields_to_show[]=report_row_type&fields_to_hide%5B%5D&fields_to_show[]=shipping_method

image

Order Cycle Distributor Totals by Supplier

I could not make this one work 🔴
I'm not sure why, I cannot spot anything wrong in the params...

I keep getting the error NameError: undefined local variable or method line_item_includes' for`

Are you able to try locally @dacook , and reproduce?

https://staging.openfoodnetwork.org.uk/api/v0/reports/orders_and_fulfillment?token=****&report_subtype=order_cycle_supplier_totals_by_supplier&q[completed_at_gt]=2023-02-22+00:00&q[completed_at_lt]=2023-03-23+00:00&display_header_row=true&display_summary_row=true&fields_to_show[]=report_row_type&fields_to_hide%5B%5D

image

Order Cycle Customer Totals

https://staging.openfoodnetwork.org.uk/api/v0/reports/orders_and_fulfillment?token=****&report_subtype=order_cycle_customer_totals&q[completed_at_gt]=2023-02-22+00:00&q[completed_at_lt]=2023-03-23+00:00&fields_to_show%5B%5D=hub&fields_to_show%5B%5D=product&fields_to_show%5B%5D=variant&fields_to_show%5B%5D=item_price&fields_to_show%5B%5D=billing_street&fields_to_show%5B%5D=date&display_header_row=true&display_summary_row=true&fields_to_show[]=report_row_type&fields_to_hide%5B%5D&fields_to_show[]=shipping

image

  • Packing Reports 🟢

Pack By Customer

https://staging.openfoodnetwork.org.uk/api/v0/reports/packing?token=****&q%5Border_completed_at_gt%5D=2023-02-22+00%3A00&q%5Border_completed_at_lt%5D=2023-03-23+00%3A00&q%5Border_distributor_id_in%5D%5B%5D=&report_subtype=customer&report_format=&display_summary_row=true&fields_to_show%5B%5D=hub&fields_to_show%5B%5D=supplier&fields_to_show%5B%5D=customer_code&fields_to_show%5B%5D=first_name&fields_to_show%5B%5D=last_name&fields_to_show%5B%5D=product&fields_to_show%5B%5D=variant&fields_to_show%5B%5D=quantity&fields_to_show%5B%5D=report_row_type&display_header_row=true&display_summary_row=true&fields_to_show[]=temp_controlled

image

Pack By Supplier
https://staging.openfoodnetwork.org.uk/api/v0/reports/packing?token=****&q%5Border_completed_at_gt%5D=2023-02-22+00%3A00&q%5Border_completed_at_lt%5D=2023-03-23+00%3A00&q%5Border_distributor_id_in%5D%5B%5D=&report_subtype=supplier&report_format=&display_summary_row=true&fields_to_show%5B%5D=hub&fields_to_show%5B%5D=supplier&fields_to_show%5B%5D=customer_code&fields_to_show%5B%5D=first_name&fields_to_show%5B%5D=last_name&fields_to_show%5B%5D=product&fields_to_show%5B%5D=variant&fields_to_show%5B%5D=quantity&fields_to_show%5B%5D=report_row_type&display_header_row=true&display_summary_row=true&fields_to_show[]=temp_controlled

image

Pack By Product

https://staging.openfoodnetwork.org.uk/api/v0/reports/packing?token=****&q%5Border_completed_at_gt%5D=2023-02-22+00%3A00&q%5Border_completed_at_lt%5D=2023-03-23+00%3A00&q%5Border_distributor_id_in%5D%5B%5D=&report_subtype=product&report_format=&display_summary_row=true&fields_to_show%5B%5D=hub&fields_to_show%5B%5D=supplier&fields_to_show%5B%5D=product&fields_to_show%5B%5D=variant&fields_to_show%5B%5D=customer_code&fields_to_show%5B%5D=first_name&fields_to_show%5B%5D=last_name&fields_to_show%5B%5D=quantity&fields_to_show%5B%5D=report_row_type&display_summary_row=true&fields_to_show[]=temp_controlled&fields_to_show%5B%5D=price&fields_to_show%5B%5D=phone

image

  • Manual tests
    I've tested all reports manually and found the same behavior as before.

Adding the feedback label, on the red test-case above (Order Cycle Distributor Totals by Supplier).

@filipefurtad0 filipefurtad0 added feedback-needed and removed pr-staged-uk staging.openfoodnetwork.org.uk labels Mar 22, 2023
@filipefurtad0
Copy link
Contributor

Yaayy,

That's it!

https://staging.openfoodnetwork.org.uk/api/v0/reports/orders_and_fulfillment?token=****&report_subtype=order_cycle_distributor_totals_by_supplier&q[completed_at_gt]=2023-02-22+00:00&q[completed_at_lt]=2023-03-23+00:00&display_header_row=true&display_summary_row=true&fields_to_show[]=report_row_type&fields_to_hide%5B%5D&fields_to_show%5B%5D=shipping_method&fields_to_show%5B%5D=producer_charges_sales_tax?&fields_to_show%5B%5D=product_tax_category&fields_to_show%5B%5D=variant&fields_to_show%5B%5D=product&fields_to_show%5B%5D=producer&fields_to_show%5B%5D=quantity&fields_to_show%5B%5D=curr_cost_per_unit&fields_to_show%5B%5D=total_shipping_cost&fields_to_show%5B%5D=total_cost
image

Thanks @dacook. Merging.

@filipefurtad0 filipefurtad0 merged commit 9b749ee into openfoodfoundation:master Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow report summaries in JSON (API) output
4 participants