-
-
Notifications
You must be signed in to change notification settings - Fork 408
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
taxonomy: adding a new feature in the API to compute the Ecobalyse score #11196
Merged
Merged
Changes from all commits
Commits
Show all changes
138 commits
Select commit
Hold shift + click to select a range
44cf545
taxonomy: a new function assign_property_to_ingredients has been adde…
very-smartin 41f3fd1
Merge branch 'main' into ecobalyse
very-smartin b5ab63e
updated tests
stephanegigandet 49c07ef
lint
stephanegigandet e4d6e54
Merge branch 'main' into ecobalyse
very-smartin 9b29b75
Merge branch 'main' into ecobalyse
very-smartin a606d32
Merge branch 'main' into ecobalyse
very-smartin 98560d5
taxonomy: a new key has been added in country.txt in order to know if…
very-smartin ebb3add
Erratum in MakeFile
very-smartin 9b2741b
ecobalyse: a boolean has been added in the country taxonomy for europ…
very-smartin 2458c0c
ecobalyse: a boolean has been added in the country taxonomy for europ…
very-smartin 8d449e7
Merge branch 'main' into ecobalyse
very-smartin fed86f1
test: Update tests results
150348f
chore: Linting changes
34ad737
Merge branch 'main' into ecobalyse
very-smartin f1803f6
taxonomy: changing the function assign_property_to_ingredient
very-smartin 1273fd3
taxonomy: resolving conflicts
very-smartin 43448e0
taxonomy: changing the function assign_property_to_ingredient
very-smartin 7302c37
Merge branch 'main' into ecobalyse
very-smartin 2206fea
taxonomy; new version of the get_missing_ecobalyse_ids function
very-smartin 678a2a7
Merge branch 'main' into ecobalyse
very-smartin 28d0a36
Merge branch 'main' into ecobalyse
very-smartin 7034337
taxonomy: adressing bugsin Ingredients.pm
very-smartin ca5d35f
Merge branch 'main' into ecobalyse
very-smartin 96b0cfd
tackling (again) Perl errors
very-smartin d5ae6fa
Merge branch 'main' into ecobalyse
very-smartin 995ade1
tackling (again) Perl errors
very-smartin 97695e3
Merge branch 'ecobalyse' of https://github.com/openfoodfacts/openfood…
very-smartin 0828916
tackling (again) Perl errors
very-smartin 5e5a8e2
chore: Linting changes
be2e361
Merge branch 'main' into ecobalyse
very-smartin 1b69d54
Correcting tests/unit for ecobalyse
very-smartin bea6622
Merge branch 'main' into ecobalyse
very-smartin d9e75a1
Merge branch 'main' into ecobalyse
very-smartin ab65eff
Changing Ingredients.pm
very-smartin 00dd1db
Squashing modifications : ecobalyse codes extraction + tacking issues on
very-smartin cf501fc
Updating ecobalyse branch
very-smartin 1733845
taxonomy: taking into account the ingredient origin for ecobalyse codes
very-smartin 2b4257b
taxonomy : new function created to retrieve the geographical area
very-smartin fa5865b
Merge branch 'main' into ecobalyse
very-smartin ced253b
taxonomy: corrections on retrieving the corresponding ecobalyse code …
very-smartin d9fc132
taxonomy: minor correction
very-smartin 0a52271
Merge branch 'main' into ecobalyse
very-smartin 1559266
Merge branch 'main' into ecobalyse
very-smartin 04a3586
Tackling linting issue
very-smartin 56f9f76
Merge branch 'main' into ecobalyse
very-smartin 8167a74
Tackling linting issue
very-smartin 7b6417c
Merge branch 'main' into ecobalyse
very-smartin 6c9a322
chore: Linting changes
755451e
Merge branch 'main' into ecobalyse
very-smartin eb085fa
Tackling unit test issues
very-smartin 46e5369
Merge branch 'ecobalyse' of https://github.com/openfoodfacts/openfood…
very-smartin 28fd034
Tackling unit test issues : suite
very-smartin 1c5d7af
Adding a new function in the API to compute the Ecobalyse score
very-smartin 303549f
Tackling merging issues
very-smartin c3de8cd
Adding new steps in the new function
very-smartin fa20ff0
Changing the structure for the futur code
very-smartin c0af057
Merge branch 'main' into ecobalyse
very-smartin c35d5df
Adding new tests
very-smartin cecc8da
Tackling test issues
very-smartin fabb72f
Merge branch 'main' into ecobalyse
very-smartin 0ccc2f3
Tackling test issues again
very-smartin 1644f1e
Merge branch 'main' into ecobalyse
very-smartin 45e93ef
Tackling minor errors
very-smartin 6f677c6
New tests added + addressing linting issues
very-smartin c6c6217
Making API requests !
very-smartin c79b5d5
Merge branch 'main' into ecobalyse
very-smartin 9adece1
Merge branch 'main' into ecobalyse
very-smartin b6eed1a
Tackling test issues
very-smartin fcaffe9
Tackling small issues
very-smartin b5f7a81
Merge branch 'main' into ecobalyse
very-smartin a5f4f1c
Tackling comments issues + tests
very-smartin fba5f73
Tackling tests issues
very-smartin e415a5a
Tackling tests issues
very-smartin e49d3c9
Minor changes
very-smartin 761395f
Merge branch 'main' into ecobalyse
very-smartin e117282
Minor changes
very-smartin 571adee
Tackling int tests issues
very-smartin 21e017b
Merge branch 'main' into ecobalyse
very-smartin 416712d
Merge branch 'main' into ecobalyse
very-smartin 01de6a5
Merge branch 'main' into ecobalyse
very-smartin 4ecc600
Merge branch 'main' into ecobalyse
very-smartin c273be2
Merge branch 'main' into ecobalyse
very-smartin 81f4a9f
Tackling linting issues
very-smartin 387931e
Merge branch 'ecobalyse' of https://github.com/openfoodfacts/openfood…
very-smartin 6ffc01c
Tackling linting issues
very-smartin 77a27e9
chore: Linting changes
df27823
Tackling PerlCritic issues
very-smartin 2a87d9e
Merge branch 'ecobalyse' of https://github.com/openfoodfacts/openfood…
very-smartin 3d46e5c
Tackling PerlCritic issues
very-smartin d1552fe
Tackling lint issues
very-smartin cdefe8d
Merge branch 'main' into ecobalyse
very-smartin 3681cee
chore: Linting changes
85ee330
Tackling perl issues
very-smartin 2205876
Tackling perl issues
very-smartin fc0294f
chore: Linting changes
dd8125e
Merge branch 'main' into ecobalyse
very-smartin cc189aa
lint
very-smartin a81f3fe
New test
very-smartin cda1c8f
chore: Linting changes
47ce36b
Check perl issues
very-smartin 7757f1b
Merge branch 'ecobalyse' of https://github.com/openfoodfacts/openfood…
very-smartin 4bd73f7
Perl issues
very-smartin e6b1ded
chore: Linting changes
3926495
Merge branch 'main' into ecobalyse
very-smartin e7bc079
linting
very-smartin 072f867
Setting up correctly environmental_score_ecobalyse.t
very-smartin 5b7318f
Merge branch 'main' into ecobalyse
very-smartin 995a78e
chore: Linting changes
43033a8
Merge branch 'main' into ecobalyse
very-smartin 839a67e
Tackling import issues
very-smartin d69aa2e
call ecobalyse
stephanegigandet 913dec6
update tests
stephanegigandet 6e902c3
Merge branch 'main' into ecobalyse
very-smartin d8baf96
Merge branch 'main' into ecobalyse
very-smartin d7d816e
Tackling test issues
very-smartin e0baa9b
Changing test
very-smartin 515586c
Merge branch 'main' into ecobalyse
very-smartin 3e86626
Trying new tests
very-smartin 9a0b091
Merge branch 'main' into ecobalyse
very-smartin 66b0a29
update tests
stephanegigandet 6075f81
fix: ecobalyse service and tests (#11352)
stephanegigandet 6e98ac5
fix: ecobalyse call (#11353)
stephanegigandet 577135c
chore: Linting changes
a2ea5d9
fix: ecobalyse call and tests (#11359)
stephanegigandet ee36db3
Merge branch 'main' into ecobalyse
stephanegigandet 0fbe341
remove old tests
stephanegigandet 78615a6
Merge branch 'main' into ecobalyse
very-smartin 3260a77
taxonomy: fix taxonomy build error: unexisting whole-bananas parent
stephanegigandet 3defb59
Merge branch 'bananas-fix2' into ecobalyse
stephanegigandet 471c052
fix ingredients
stephanegigandet c206d6a
taxonomy: updating ecobalyse codes
very-smartin 902b5a9
More updates
very-smartin 60c4941
call ecobalyse in test_ingredients_analysis.pl
stephanegigandet 4257071
Merge branch 'ecobalyse_update' into ecobalyse
stephanegigandet c39df46
update tests
stephanegigandet 6065dcc
merged main
stephanegigandet 423e320
test: Update tests results
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
# This file is part of Product Opener. | ||
# | ||
# Product Opener | ||
# Copyright (C) 2011-2023 Association Open Food Facts | ||
# Contact: [email protected] | ||
# Address: 21 rue des Iles, 94100 Saint-Maur des Fossés, France | ||
# | ||
# Product Opener is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
=encoding UTF-8 | ||
|
||
=head1 NAME | ||
|
||
ProductOpener::EnvironmentalImpact - process and analyze products | ||
|
||
=head1 SYNOPSIS | ||
|
||
C<ProductOpener::EnvironmentalImpact> processes products to compute | ||
their environmental impact (see french environmental labeling Ecobalyse). | ||
|
||
use ProductOpener::EnvironmentalImpact qw/:all/; | ||
|
||
[..] | ||
|
||
estimate_environmental_impact($product_ref); | ||
|
||
=head1 DESCRIPTION | ||
|
||
[..] | ||
|
||
=cut | ||
|
||
package ProductOpener::EnvironmentalImpact; | ||
|
||
use ProductOpener::PerlStandards; | ||
use Exporter qw< import >; | ||
use LWP::UserAgent; | ||
use HTTP::Request::Common; | ||
use JSON; | ||
use Encode qw(decode_utf8 encode_utf8); | ||
|
||
BEGIN { | ||
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS); | ||
@EXPORT_OK = qw( | ||
|
||
&estimate_environmental_impact_service | ||
|
||
); # symbols to export on request | ||
%EXPORT_TAGS = (all => [@EXPORT_OK]); | ||
} | ||
|
||
use vars @EXPORT_OK; | ||
|
||
use Log::Any '$log', default_adapter => 'Stderr'; | ||
|
||
=head1 FUNCTIONS | ||
|
||
=head2 estimate_environmental_impact_service ( $product_ref, $updated_product_fields_ref, $errors_ref ) | ||
|
||
Compute the environmental impact of a given product (see the french environmental environmental labeling Ecobalyse). | ||
|
||
This function is a product service that can be run through ProductOpener::ApiProductServices | ||
|
||
=head3 Arguments | ||
|
||
=head4 $product_ref | ||
|
||
product object reference | ||
|
||
=head4 $updated_product_fields_ref | ||
|
||
reference to a hash of product fields that have been created or updated | ||
|
||
=head4 $errors_ref | ||
|
||
reference to an array of error messages | ||
|
||
=cut | ||
|
||
sub estimate_environmental_impact_service ($product_ref, $updated_product_fields_ref, $errors_ref) { | ||
|
||
# $updated_product_fields_ref, $errors_ref sont des outputs : chaque service | ||
# dit quels champs sont modifiés | ||
# Ici on en ajoute un : "environmental_impact" | ||
|
||
# If undefined ingredients, do nothing | ||
return if not defined $product_ref->{ingredients}; | ||
|
||
# indicate that the service is modifying the "ingredients" structure | ||
$updated_product_fields_ref->{environmental_impact} = 1; | ||
$product_ref->{environmental_impact} = 0; | ||
|
||
# Initialisation of the payload structure | ||
my $payload = { | ||
ingredients => [], | ||
transform => { | ||
"id" => "7541cf94-1d4d-4d1c-99e3-a9d5be0e7569", | ||
"mass" => 545 | ||
}, | ||
packaging => [], | ||
distribution => "ambient", | ||
preparation => ["refrigeration"] | ||
}; | ||
|
||
# Estimating the environmental impact | ||
foreach my $ingredient_ref (@{$product_ref->{ingredients}}) { | ||
# TODO: when we don't have an ecobalyse_code or ecobalyse_proxy_code, | ||
# we can ignore the ingredient, but we need to record the quantity of unrecognized ingredients | ||
next unless defined $ingredient_ref->{id} && defined $ingredient_ref->{percent_estimate}; | ||
push @{$payload->{ingredients}}, | ||
{ | ||
id => $ingredient_ref->{ecobalyse_code} || $ingredient_ref->{ecobalyse_proxy_code}, | ||
mass => $ingredient_ref->{percent_estimate} | ||
}; | ||
} | ||
|
||
# Adding a transformation | ||
if (defined $product_ref->{transform}) { | ||
$payload->{transform} = { | ||
id => $product_ref->{transform}->{id}, | ||
mass => $product_ref->{transform}->{mass} | ||
} | ||
if defined $product_ref->{transform}->{id} && defined $product_ref->{transform}->{mass}; | ||
} | ||
|
||
# Adding a packaging | ||
if (defined $product_ref->{packaging}) { | ||
foreach my $packaging_ref (@{$product_ref->{packaging}}) { | ||
next unless defined $packaging_ref->{id} && defined $packaging_ref->{mass}; | ||
push @{$payload->{packaging}}, | ||
{ | ||
id => $packaging_ref->{id}, | ||
mass => $packaging_ref->{mass} | ||
}; | ||
} | ||
} | ||
|
||
# API URL | ||
my $url_recipe = "https://staging-ecobalyse.incubateur.net/api/food"; | ||
|
||
# Create a UserAgent object to make the API request | ||
my $ua = LWP::UserAgent->new(); | ||
$ua->timeout(5); | ||
|
||
# Prepare the POST request with the payload | ||
my $request = POST $url_recipe, $payload; | ||
$request->header('content-type' => 'application/json'); | ||
$request->content(decode_utf8(encode_json($payload))); | ||
|
||
# Debug information for the request | ||
$log->debug("send_event request", {endpoint => $url_recipe, payload => $payload}) if $log->is_debug(); | ||
|
||
$product_ref->{environmental_impact} = {ecobalyse_request => {url => $url_recipe, data => $payload}}; | ||
|
||
# Send the request and get the response | ||
my $response = $ua->request($request); | ||
|
||
# Parse the JSON response | ||
my $response_content = $response->decoded_content; | ||
my $response_data = $response_content; | ||
# if the response is JSON, decode it | ||
eval {$response_data = decode_json($response_content);}; | ||
|
||
$product_ref->{environmental_impact}{ecobalyse_response} = $response_data; | ||
|
||
# Handle the response based on success or failure | ||
if ($response->is_success) { | ||
|
||
# Access the specific "ecs" value | ||
if (exists $response_data->{results}{total}{ecs}) { | ||
my $ecs_value = $response_data->{results}{total}{ecs}; | ||
# If 'ecs' is defined, store it in the product reference | ||
if (defined $ecs_value) { | ||
$product_ref->{environmental_impact}{ecs} = $ecs_value; | ||
} | ||
} | ||
} | ||
else { | ||
# If the request failed, log the error | ||
$log->error("send_event request failed", | ||
{endpoint => $url_recipe, payload => $payload, response => $response_content}) | ||
if $log->is_error(); | ||
# Add an error message to the errors array | ||
$product_ref->{environmental_impact}{ecobalyse_response} = $response_data; | ||
|
||
push @{$errors_ref}, | ||
{ | ||
message => {id => "error_response_from_ecobalyse"}, | ||
field => { | ||
id => "ecobalyse_response", | ||
value => $response_content, | ||
}, | ||
impact => {id => "failure"}, | ||
service => {id => "estimate_environmental_impact_service"}, | ||
}; | ||
} | ||
|
||
# If necessary, return error as well | ||
# (number of unattributed ingredients, | ||
# percentage of unattributed mass, etc...) | ||
|
||
# add_error | ||
# add_warning | ||
|
||
# $product_ref->{environmental_impact} = 5; | ||
|
||
return; | ||
} | ||
|
||
1; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think the service should be estimate_environmental_impact, without speciffying ingredients: at first we will look only at ingredients, but maybe in the next iteration, we will also add the packaging data, and maybe some processing implied by the category (e.g. frozen vegetables: freezing).
cost -> I think it may be more generic to use "impact" instead of "cost"
So we could create a separate EnvironmentalImpact.pm module.