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

feat: packaging stats per parent materials #8594

Merged
merged 12 commits into from
Jun 26, 2023
Merged

Conversation

stephanegigandet
Copy link
Contributor

@stephanegigandet stephanegigandet commented Jun 20, 2023

This PR:

  • computes a new packagings_materials structure for each product, that agregates weights by parent material (platics, glass, paper/cardboard, metal) for each packaging component
  • displays the data in a knowledge panel

image

Part of

@stephanegigandet stephanegigandet requested a review from a team as a code owner June 20, 2023 15:25
@github-actions github-actions bot added 🌱 Green-Score https://world.openfoodfacts.org/eco-score-the-environmental-impact-of-food-products 📖 Knowledge Panels https://wiki.openfoodfacts.org/Knowledge_panels 📦 Packaging https://wiki.openfoodfacts.org/Category:Recycling Template::Toolkit The templating toolkit used by product opener. The starting point for HTML/JS/CSS fixes. 🧪 tests Translations We use a non-standard version of GetText, lack language variants support translate.openfoodfacts.org labels Jun 20, 2023
@github-actions github-actions bot added API Issues related to the Open Food Facts API. More specific labels exist & should be used (API WRITE…) 🧪 integration tests 🧬 Taxonomies https://wiki.openfoodfacts.org/Global_taxonomies labels Jun 20, 2023
@teolemon
Copy link
Member

@stephanegigandet could you add a comparison to category ?

@teolemon
Copy link
Member

@stephanegigandet could you swap column A and column B ?

@stephanegigandet
Copy link
Contributor Author

@stephanegigandet could you add a comparison to category ?

Well it will be the next step, we first need to aggregate by product, then we can aggregate by category, and we can show a comparison to the category on each product. It will be much more work though.

@stephanegigandet
Copy link
Contributor Author

@stephanegigandet could you swap column A and column B ?

Sure, done.

@codecov-commenter
Copy link

codecov-commenter commented Jun 21, 2023

Codecov Report

Merging #8594 (f7f9cdc) into main (5e0a136) will increase coverage by 0.09%.
The diff coverage is 85.48%.

❗ Current head f7f9cdc differs from pull request most recent head f4b5def. Consider uploading reports for the commit f4b5def to get more accurate results

@@            Coverage Diff             @@
##             main    #8594      +/-   ##
==========================================
+ Coverage   48.69%   48.79%   +0.09%     
==========================================
  Files         114      114              
  Lines       21417    21466      +49     
  Branches     4791     4803      +12     
==========================================
+ Hits        10430    10474      +44     
- Misses       9701     9703       +2     
- Partials     1286     1289       +3     
Impacted Files Coverage Δ
lib/ProductOpener/KnowledgePanels.pm 11.44% <0.00%> (-0.07%) ⬇️
tests/unit/packaging.t 75.00% <75.00%> (ø)
lib/ProductOpener/Packaging.pm 77.19% <93.18%> (+2.19%) ⬆️

... and 2 files with indirect coverage changes

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

Copy link
Member

@alexgarel alexgarel 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.

Approving, but I point room for improvement. I don't know if you have the time to pick some.

Comment on lines 873 to 879
foreach my $parent ("en:paper-or-cardboard", "en:plastic", "en:glass", "en:metal") {
if (is_a("packaging_materials", $material, $parent)) {
$parent_material = $parent;
last;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

It's okay, but don't we have a more efficient way to do this ? (like fetching all parents and then computing intersection with our values ?). Maybe with something like Set::Tiny.

Copy link

Choose a reason for hiding this comment

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

I don't know enough of the source code to suggest an entirely different approach to the logic itself, but a common pattern for getting the first item in a list that matches a certain criteria is using List::Util's first:

use List::Util qw(first);

$parent_material = first {
      is_a("packaging_materials", $material, $_)
} ("en:paper-or-cardboard", "en:plastic", "en:glass", "en:metal");

but if this code is hot (i.e. running several times per call) maybe it could be worth turning is_a() into a hash lookup.

Either way, the current implementation is okay and I wouldn't change it unless it was a bottleneck.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestions. I changed to @garu 's suggestion as I find it more readable with first indeed.

It certainly could be done more efficiently, but this code is executed only when writing products, and given the shallowness of the packaging materials taxonomy, it's just a dozen operations, so I'd prefer not to spend time on it. If we want to optimize things, there are targets that are much more likely to make a difference (e.g. for read operations of products, where we compute attributes and knowledge panels for 100 products...)

lib/ProductOpener/Packaging.pm Outdated Show resolved Hide resolved
[% END %]
"html": `
[% FOREACH packaging IN product.packagings %]
[% IF packaging.recycling == recycling_type OR (recycling_type == "en:unknown" AND ((NOT packaging.recycling.defined) OR ((packaging.recycling != "en:discard") AND (packaging.recycling != "en:recycle")))) %]
Copy link
Member

Choose a reason for hiding this comment

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

A better way to do that, would have been to dispatch packagings in arrays in recycling_types instead of putting => 1. So that you just have to iterate over the hash, and elements in array.

Because this condition is not trivial to read !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, but it's likely that we will change how we display packaging components in the future. This code did not change, I just renamed the template for clarity.

'en:plastic' => {}
}

) or diag explain $product_ref->{packagings_materials};
Copy link
Member

Choose a reason for hiding this comment

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

Would be good to add at least one test with an unknown material.

I always like to also test with an empty hash.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@stephanegigandet stephanegigandet merged commit 3ee4411 into main Jun 26, 2023
@stephanegigandet stephanegigandet deleted the packagings-materials branch June 26, 2023 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Issues related to the Open Food Facts API. More specific labels exist & should be used (API WRITE…) 🌱 Green-Score https://world.openfoodfacts.org/eco-score-the-environmental-impact-of-food-products 🧪 integration tests 📖 Knowledge Panels https://wiki.openfoodfacts.org/Knowledge_panels 📦 Packaging https://wiki.openfoodfacts.org/Category:Recycling 🧬 Taxonomies https://wiki.openfoodfacts.org/Global_taxonomies Template::Toolkit The templating toolkit used by product opener. The starting point for HTML/JS/CSS fixes. 🧪 tests Translations We use a non-standard version of GetText, lack language variants support translate.openfoodfacts.org
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

5 participants