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

Calculating disposable diapers from kits #4017

Merged
merged 65 commits into from
Apr 9, 2024

Conversation

jadekstewart3
Copy link
Contributor

@jadekstewart3 jadekstewart3 commented Jan 8, 2024

  • I have performed a self-review of my own code,
  • I have commented my code, particularly in hard-to-understand areas,
  • I have made corresponding changes to the documentation,
  • I have added tests that prove my fix is effective or that my feature works,
  • New and existing unit tests pass locally with my changes ("bundle exec rake")

-->

Resolves #3989

Description

I made modifications to the acquisition report service and the children served report service to take disposable diapers distributed in kits in the annual report. I have added/ modified tests to reflect the changes to the calculations.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Tested in the spec/services/reports/children_served_report_service_spec.rb and spec/services/reports/acquisition_report_service_spec.rb

@jadekstewart3 jadekstewart3 marked this pull request as ready for review January 10, 2024 01:31
@jadekstewart3 jadekstewart3 changed the title Draft method for calculating disposable diapers from kits Calculating disposable diapers from kits Jan 10, 2024
Copy link
Collaborator

@cielf cielf left a comment

Choose a reason for hiding this comment

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

Thank you! This annual report is starting to be a bit of a monster. I think we're eventually going to have to do a major refactor on it, but for now -- let's try to keep the terminology as clear as we can.

@jadekstewart3 jadekstewart3 requested a review from cielf January 16, 2024 18:41
Copy link
Collaborator

@cielf cielf left a comment

Choose a reason for hiding this comment

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

Hey @jadekstewart3 Thank you very much for making the change to the variable names! It made it a lot easier for me to reason about the rest of the code.

Alas, it looks like I wasn't clear enough in describing the relationship between kits and children, so there's going to be a chunk of rework there. (See the inline comments)

I didn't look closely at the tests yet -- I'm holding off until the kids/kits stuff is addressed.

I'll try to keep an eye on the notifications here over the next couple of days if you have questions , or want to check that we really are on the same page - it's usually quicker to poke me on slack, though.

@cielf
Copy link
Collaborator

cielf commented Mar 13, 2024

@jadekstewart3 Hmmm.... doesn't seem to be including the kits in. Here's the sequence I tried. From a fresh seed:
1/ check the disposable diapers in the 2023 report - call this amount A.
2/ Add a kit with 5 Adult Briefs L /XL, and 5 Adult Briefs M/L
3/ Allocate at least 10 of those
4/ Create a distribution in 2023 with 10 of those kits, and 10 Adult Briefs L/XL
5/ Recalculate the report
6/ I expect to see Disposable Diapers distributed as A + 110. I see Disposable Diapers as A + 10 -- so I know it's picking up the Adult Briefs L/XL, but it doesn't seem to be picking up the diapers in the kit.

Thank you for sticking with this through a fairly long haul!

@cielf
Copy link
Collaborator

cielf commented Mar 13, 2024

(Pokes into the code a bit) Looks like you might only be using the magic SQL for the children served service, and not for the aquisition_report_service. That would explain it.

@jadekstewart3
Copy link
Contributor Author

@cielf I am trying to implement the magic sql in the acquisition report and something is funky. Can you take a look? when I test the distributed_disposable_diapers_from_kits it seems to return 100, but in the full report it is returning 1500?

@dorner
Copy link
Collaborator

dorner commented Mar 14, 2024

@jadekstewart3 there seems to be some flakiness here - the order of tests seems to matter. If you run it with --seed 43773, both tests fail with 1500. (In other words, if returns the correct quantity of disposable diapers from kits runs first, it fails, but if it runs after the other test, it passes.)

@dorner
Copy link
Collaborator

dorner commented Mar 14, 2024

I think this has to do with it:

      disposable_item = organization.items.disposable.first
      non_disposable_item = organization.items.where.not(id: organization.items.disposable).first

Rather than creating data from scratch, you're relying on items that were created by the factory/seed. To be sure that only the data you care about is being created, you probably should clear out all the items and only create the ones you want to see in the report.

…o below the creation of the kits, but still getting the same out of range value
AND LOWER(base_items.category) LIKE '%diaper%'
AND NOT (LOWER(base_items.category) LIKE '%cloth%' OR LOWER(base_items.name) LIKE '%cloth%')
SQL

Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to make sure that the itemizable type on kit_line_items is Kit, don't we? Isn't there a kit # 1 and a distribution # 1 and a purchase # 1 and a donation # 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cielf Ill look into this today! I think you may be on to something!

@jadekstewart3
Copy link
Contributor Author

@dorner I am getting a return value of 1500 for the sql query that is calculating distributed disposable diapers from kits. I've gone over my test set up a few times and created the kits and kit items in a similar fashion to the set up in the children_served_report_service_spec, but I cant quite figure out why we are returning 1500 instead of the expected 100.

@cielf
Copy link
Collaborator

cielf commented Mar 27, 2024

Hey @jadekstewart3 -- did you look into adding in a clause to in your sql to make sure that the itemizable type for the kit line items is kit?

@jadekstewart3
Copy link
Contributor Author

@cielf I was messing with it yesterday but still having troubles, but I think I just got it!

@jadekstewart3
Copy link
Contributor Author

@cielf I think this is working! It looks to be failing on a test file that doesnt have anything to do with the reports! 😃

@cielf
Copy link
Collaborator

cielf commented Mar 28, 2024

It passes my functional test (adding a mixed diaper and kit-with-diapers distribution) with flying colours! Hurrah!

@cielf cielf requested a review from dorner March 28, 2024 14:15
@cielf
Copy link
Collaborator

cielf commented Mar 28, 2024

Hey @dorner -- Just checking if you have any last comments on this -- you've chimed in, but more on a problem solving than critiquing the code pov. Thanks!

Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

Code looks good to me - just some comments that should be removed.

.distributions
.for_year(year)
.joins(line_items: :item)
.merge(Item.disposable)
.sum('line_items.quantity')
# returning 220
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this comment shouldn't be here?

result = ActiveRecord::Base.connection.execute(sanitized_sql)

result.first['sum'].to_i
# returning 1500
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto

# .merge(Item.disposable)
# .where.not(items: {kit_id: nil})
# .sum("line_items.quantity")
# end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's not leave commented-out code in the repo. If we need it, we can always use git history to get it back. 😄

spec/services/reports/acquisition_report_service_spec.rb Outdated Show resolved Hide resolved
@jadekstewart3 jadekstewart3 requested a review from dorner April 1, 2024 17:59
@cielf
Copy link
Collaborator

cielf commented Apr 4, 2024

I should probably take this out for one last spin before we merge it. [Edit: Actually -- no -- if it's only comment removal, I don't need to.]

@cielf
Copy link
Collaborator

cielf commented Apr 7, 2024

Holding off on the merge until the release of April 7 is done.

@cielf cielf merged commit 7c5ea3f into rubyforgood:main Apr 9, 2024
19 checks passed
Copy link
Contributor

@jadekstewart3: Your PR Calculating disposable diapers from kits is part of today's Human Essentials production release: 2024.04.14.
Thank you very much for your contribution!

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.

Include diapers in kits in NDBN report values that are based on 'number of diapers'.
3 participants