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: Statistics for packagings materials by categories #8613

Merged
merged 37 commits into from
Jun 29, 2023

Conversation

stephanegigandet
Copy link
Contributor

@stephanegigandet stephanegigandet commented Jun 27, 2023

This PR:

  • computes statistics of parent packaging materials (glass, metal, paper/cardboard, plastics) by country and categories
  • adds minimal server-side knowledge panels support for tags
  • adds a knowledge panel to display packaging materials stats on the page of a category
  • adds a column for the parent material in the CSV export of all packaging components
  • some refactoring:
  • moved code from gen_packaging_stats.pl script to PackagingStats.pm module so that we can have unit tests for some functions (I needed it for debugging)
  • new get_parent_material() function, with a cache (we have very few different inputs)

e.g. for yoghurts:

image

@codecov-commenter
Copy link

codecov-commenter commented Jun 27, 2023

Codecov Report

Merging #8613 (3d766fd) into main (671ec3c) will decrease coverage by 0.19%.
The diff coverage is 35.37%.

@@            Coverage Diff             @@
##             main    #8613      +/-   ##
==========================================
- Coverage   48.79%   48.60%   -0.19%     
==========================================
  Files         114      117       +3     
  Lines       21475    21747     +272     
  Branches     4804     4852      +48     
==========================================
+ Hits        10478    10571      +93     
- Misses       9705     9880     +175     
- Partials     1292     1296       +4     
Impacted Files Coverage Δ
lib/ProductOpener/Display.pm 10.00% <16.66%> (+<0.01%) ⬆️
lib/ProductOpener/PackagingStats.pm 28.71% <28.71%> (ø)
lib/ProductOpener/KnowledgePanelsTags.pm 34.28% <34.28%> (ø)
lib/ProductOpener/Packaging.pm 74.69% <45.45%> (-2.51%) ⬇️
tests/unit/packaging_stats.t 100.00% <100.00%> (ø)

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

@github-actions github-actions bot added Display 📖 Knowledge Panels https://wiki.openfoodfacts.org/Knowledge_panels 📦 Packaging https://wiki.openfoodfacts.org/Category:Recycling product history We have kept 10 years of product revisions. This is useful to monitor edits & product improvements 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 28, 2023
@stephanegigandet stephanegigandet changed the title feat: Statistics for packagings materials by categories (in progress) feat: Statistics for packagings materials by categories Jun 28, 2023
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.

LGTM

Some minor changes proposed.

lib/ProductOpener/KnowledgePanelsTags.pm Outdated Show resolved Hide resolved
lib/ProductOpener/KnowledgePanelsTags.pm Outdated Show resolved Hide resolved
lib/ProductOpener/KnowledgePanelsTags.pm Outdated Show resolved Hide resolved
lib/ProductOpener/KnowledgePanelsTags.pm Outdated Show resolved Hide resolved
lib/ProductOpener/Packaging.pm Outdated Show resolved Hide resolved
Comment on lines 575 to 597
# Export packaging components of all products to a CSV file
my $filehandle;
my $filename = "$www_root/data/$name.csv";
open($filehandle, ">:encoding(UTF-8)", $filename)
or die("Could not write " . $filename . " : $!\n");
my $csv = Text::CSV->new(
{
eol => "\n",
sep => "\t",
quote_space => 0,
binary => 1
}
) or die "Cannot use CSV: " . Text::CSV->error_diag();

# Print the header line with fields names
$csv->print(
$filehandle,
[
"code", "countries_tags", "categories_tags", "number_of_units",
"shape", "material", "parent_material", "recycling",
"weight", "weight_measured", "weight_specified", "quantity_per_unit"
]
);
Copy link
Member

Choose a reason for hiding this comment

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

style: typically the kind of thing I would like to have in a separate method, to avoid hindering code read. (something like: my ($filehandle, $csv) = init_products_packaging_components_csv())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, will do.

lib/ProductOpener/PackagingStats.pm Show resolved Hide resolved
lib/ProductOpener/PackagingStats.pm Show resolved Hide resolved
lib/ProductOpener/PackagingStats.pm Show resolved Hide resolved
lib/ProductOpener/PackagingStats.pm Show resolved Hide resolved
@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 166b4c9 into main Jun 29, 2023
@stephanegigandet stephanegigandet deleted the packagings-materials-categories branch June 29, 2023 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Display 📖 Knowledge Panels https://wiki.openfoodfacts.org/Knowledge_panels 📦 Packaging https://wiki.openfoodfacts.org/Category:Recycling product history We have kept 10 years of product revisions. This is useful to monitor edits & product improvements 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
None yet
Development

Successfully merging this pull request may close these issues.

3 participants