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: manufacturing place + origins of ingredients knowledge panels + Normalize all panels #6069

Merged
merged 5 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions html/js/display-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,26 @@ function displayPointers(pointers) {
var markers = [];
for (var i = 0; i < pointers.length; ++i) {
var pointer = pointers[i];
var marker = new L.marker(pointer);
var coordinates;

// If pointer is an array, it just contains (lat, lng) geo coordinates
if (Array.isArray(pointer)) {
coordinates = pointer;
}
// Otherwise we have a structured object
// e.g. from a map element of a knowledge panel
else {
coordinates = [ pointer.geo.lat, pointer.geo.lng ];
}

var marker = new L.marker(coordinates);
markers.push(marker);
}

if (markers.length > 0) {
L.featureGroup(markers).addTo(actualMap);
fitBoundsToAllLayers(actualMap);
actualMap.setZoom(10);
actualMap.setZoom(8);
}
});
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,11 @@ sub process_template($$$) {
};
$template_data_ref->{sprintf} = sub($$) {
return sprintf ($_[0], $_[1]);
};
};

$template_data_ref->{encode_json} = sub($) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$template_data_ref->{encode_json} = sub($) {
# add json encoding function to templates
$template_data_ref->{encode_json} = sub($) {

Copy link
Contributor Author

@stephanegigandet stephanegigandet Nov 12, 2021

Choose a reason for hiding this comment

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

There's a comment above that code:
# Add functions and values that are passed to all templates
So I'm not sure if it makes sense to have 1 comment per function too.

return JSON::PP->new->utf8->canonical->encode($_[0]);
};

return($tt->process($template_filename, $template_data_ref, $result_content_ref));
}
Expand Down
79 changes: 76 additions & 3 deletions lib/ProductOpener/KnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ sub create_knowledge_panels($$$$) {
$product_ref->{"knowledge_panels_" . $target_lc} = {};

# Test panel to test the start of the API

if ($product_ref->{code} eq "3017620422003") {
# Disabled, kept as reference when we create a "Do you know" panel
if ($product_ref->{code} eq "3017620422003--disabled") {

my $test_panel_ref = {
parent_panel_id => "root",
Expand Down Expand Up @@ -165,6 +165,10 @@ sub create_knowledge_panels($$$$) {
create_ecoscore_panel($product_ref, $target_lc, $target_cc);

create_environment_card_panel($product_ref, $target_lc, $target_cc);

# Create the root panel that contains the panels we want to show directly on the product page
create_panel_from_json_template("root", "api/knowledge-panels/root.tt.json",
{}, $product_ref, $target_lc, $target_cc);
}


Expand Down Expand Up @@ -525,11 +529,80 @@ sub create_environment_card_panel($$$) {

# Tell the environment card template to include packaging recycling panel
$panel_data_ref->{packaging_recycling} = 1;


# Create panel for manufacturing place
$panel_data_ref->{manufacturing_place} = create_manufacturing_place_panel($product_ref, $target_lc, $target_cc);

# Origins of ingredients for the environment card
create_panel_from_json_template("origins_of_ingredients", "api/knowledge-panels/environment/origins_of_ingredients.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);

# Create the environment_card panel
create_panel_from_json_template("environment_card", "api/knowledge-panels/environment/environment_card.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);
}


=head2 create_manufacturing_place_panel ( $product_ref, $target_lc, $target_cc )

Creates a knowledge panel when we know the location of the manufacturing place,
usually through a packaging code.

=head3 Arguments

=head4 product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

=head4 language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users.
This parameter sets the desired language for the user facing strings.

=head4 country code $target_cc

The Eco-Score depends on the country of the consumer (as the transport bonus/malus depends on it)

=head3 Return value

1 to indicate that the panel has been created
0 to indicate that the panel was not created (if we don't have enough data for the product)

=cut

sub create_manufacturing_place_panel($$$) {

my $product_ref = shift;
my $target_lc = shift;
my $target_cc = shift;

$log->debug("create_manufacturing_place_panel", { code => $product_ref->{code} }) if $log->is_debug();

# Go through the product packaging codes, keep the first one with associated geo coordinates
if (defined $product_ref->{emb_codes_tags}) {
Copy link
Member

Choose a reason for hiding this comment

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

you changed indent width ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mmmh, I probably copy/pasted the $log line, and it put a tab, which in my VSCode is 4 spaces.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I'm guessing github only use indent_size 2, but does not switch to 4 for Perl files as defined in .editorconfig

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

Perl files: tab indentation (4 spaces)

[*.{pm,pl,t}]
indent_style = tab
indent_size = 4

Python files: 4 spaces indentation

[*.py]
indent_size = 4

foreach my $packager_code_tagid (@{$product_ref->{emb_codes_tags}}) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
foreach my $packager_code_tagid (@{$product_ref->{emb_codes_tags}}) {
# we will create panel for the first known localtion
foreach my $packager_code_tagid (@{$product_ref->{emb_codes_tags}}) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I'm adding it

# we will create a panel for the first known location
if (exists $packager_codes{$packager_code_tagid}) {
$log->debug("packager code found for the canon_tagid", { cc => $packager_codes{$packager_code_tagid}{cc} }) if $log->is_debug();
my ($lat, $lng) = get_packager_code_coordinates($packager_code_tagid);
if ((defined $lat) and (defined $lng)) {

my $panel_data_ref = {
packager_code_data => $packager_codes{$packager_code_tagid},
lat => $lat + 0.0,
lng => $lng + 0.0,
};
Copy link
Member

Choose a reason for hiding this comment

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

Not necessarily for action, but as we talked about de-intricating modules:

This is typically a case, where I think we could separate concerns :

  1. you want to find a valid packager location from a list of packages codes --> this is a good function to have in the packager code package (and an iterator would even be better)
  2. having this, you use it here to get the data and generate your template.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, filed #6072 to do it later.


create_panel_from_json_template("manufacturing_place", "api/knowledge-panels/environment/manufacturing_place.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);

return 1;
}
}
}
}

return 0;
}

1;
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
[% SET co2_100g = product.ecoscore_data.agribalyse.co2_total / 10 %]
[% SET driving_100g = co2_100g * 100 / 19.3 %]
{
"parent_panel_id": "root",
"type" : "score",
"level" :"info",
"topics": [
"environment"
],
"expanded": false,
[% IF driving_100g >= 3 %]
"evaluation": "bad",
[% ELSIF driving_100g >= 1.5 %]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"parent_panel_id": "ecoscore",
"type" : "score",
"level" :"info",
"grade": "[% panel.agribalyse_grade %]",
"topics": [
"environment"
],
"title_element": {
"title": "[% lang('average_impact_of_the_category') %][% sep %]: [% panel.agribalyse_grade FILTER upper %] (Score: [% panel.agribalyse_score %]/100)",
"subtitle": "[% lang('categories_s') FILTER ucfirst %][% sep %]: [% panel.agribalyse_category_name %]"
"subtitle": "[% lang('categories_s') FILTER ucfirst %][% sep %]: [% panel.agribalyse_category_name %]",
"type": "grade",
"grade": "[% panel.agribalyse_grade %]",
},
"elements": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"parent_panel_id": "root",
"type": "score",
"level": "info",
"grade": "[% panel.grade %]",
"topics": [
"environment"
],
"title_element": {
"icon_url": "[% static_subdomain %]/images/attributes/ecoscore-[% panel.grade %].svg",
"title": "[% panel.title %]",
"type": "grade",
"grade": "[% panel.grade %]",
},
"elements": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
{
"parent_panel_id": "root",
"type": "score",
"level": "info",
"grade": "unknown",
"topics": [
"environment"
],
"title_element": {
"icon_url": "[% static_subdomain %]/images/attributes/ecoscore-unknown.svg",
"title": "[% lang("attribute_ecoscore_unknown_title") %] - [% lang("attribute_ecoscore_unknown_description_short") %]",
"type": "grade",
"grade": "unknown",
},
"elements": [
{
"element_type": "text",
"text_element": {
"text_type": "summary",
"type": "summary",
"html": "We could not compute the Eco-Score of this product as it is missing some data, could you help complete it?"
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"parent_panel_id": "ecoscore",
"type" : "score",
"level" :"info",
"topics": [
"environment"
Expand All @@ -18,7 +16,7 @@
{
"element_type": "text",
"text_element": {
"text_type": "warning",
"type": "warning",
"html": `
[% lang('ecoscore_ingredients_not_indicated') %]<br><br>
[% lang('ecoscore_please_add_the_ingredients') %]<br><br>
Expand Down Expand Up @@ -64,7 +62,6 @@
"element_type": "table",
"table_element": {
"id": "ecoscore_origins_of_ingredients_table",
"table_type": "percents",
"title": "[% lang('ecoscore_origins_of_ingredients') %]",
"columns": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"parent_panel_id": "ecoscore",
"type" : "score",
"level" :"info",
"topics": [
"environment"
Expand Down Expand Up @@ -48,7 +46,6 @@
"element_type": "table",
"table_element": {
"id": "ecoscore_packaging_components",
"type": "table",
"title": "[% lang('packaging_parts') %]",
"columns": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"parent_panel_id": "ecoscore",
"type" : "score",
"level" :"info",
"topics": [
"environment"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"parent_panel_id": "ecoscore",
"type" : "score",
"level" :"info",
"topics": [
"environment"
Expand All @@ -17,7 +15,7 @@
{
"element_type": "text",
"text_element": {
"text_type": "summary",
"type": "summary",
"html": `
[% lang('ecoscore_ingredients_unknown') %]
[% lang('ecoscore_edit_for_more_precise_ecoscore') %]<br><br>
Expand All @@ -39,7 +37,7 @@
{
"element_type": "text",
"text_element": {
"text_type": "summary",
"type": "summary",
"html": `
<p><strong>[% lang('contains_palm_oil') %]</strong></p>
<p>[% lang('contains_palm_oil_description') %]</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
[% SET sum_of_bonuses_and_maluses = product.ecoscore_data.adjustments.production_system.value + product.ecoscore_data.adjustments.packaging.value + product.ecoscore_data.adjustments.threatened_species.value + product.ecoscore_data.adjustments.origins_of_ingredients.value %]
{
"parent_panel_id": "root",
"type": "score",
"level": "info",
"grade": "[% panel.grade %]",
"topics": [
"environment"
],
"title_element": {
"title": "Impact for this product: [% panel.grade FILTER upper %] (Score: [% panel.score %]/100)",
"subtitle": "[% lang("front_alt") %][% sep %]: [% product_name_brand_quantity(product) %]",
"type": "grade",
"grade": "[% panel.grade %]",
},
"elements": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"parent_panel_id": "root",
"level": "info",
"type": "card",
"expanded": true,
"topics": [
"environment"
],
Expand Down Expand Up @@ -34,7 +33,17 @@
],
},
},
[% END %]
[% END %]
{
"element_type": "panel_group",
"panel_group_element": {
"title": "[% lang('ecoscore_transportation') %]",
"panel_ids": [
[% IF panel.manufacturing_place %]"manufacturing_place",[% END %]
"origins_of_ingredients",
],
},
},
[% IF panel.palm_oil %]
{
"element_type": "panel_group",
Expand Down
1 change: 0 additions & 1 deletion templates/api/knowledge-panels/environment/label.tt.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"parent_panel_id": "ecoscore",
"level" :"info",
"topics": [
"environment"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"level" :"info",
"topics": [
"environment"
],
"evaluation": "neutral",
"expanded": true,
"title_element": {
"title": "[% lang('manufacturing_places_s') FILTER ucfirst %]",
[% IF panel.packager_code_data.cc == 'fr' %]
"subtitle": "[% panel.packager_code_data.commune %] - [% display_taxonomy_tag('countries', 'en:france') %]",
[% ELSIF panel.packager_code_data.cc == 'ch' %]
"subtitle": "[% panel.packager_code_data.full_address %]",
[% ELSIF panel.packager_code_data.cc == 'es' %]
"subtitle": "[% panel.packager_code_data.provincia_localidad %]",
[% ELSIF panel.packager_code_data.cc == 'uk' %]
"subtitle": "[% panel.packager_code_data.district %]",
[% END %]
Copy link
Member

Choose a reason for hiding this comment

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

Wow that seems not very scalable ! Why do we have different fields name based on the country ?

We should at least hide it in a perl function…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well it's complicated: each country has a registry of packaging codes to companies, but each of them has completely different fields for them. So far we chose to include all the fields they have, but we have not attempted to map fields to common names.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So here in fact I think it's more of a display choice: for each country, which field do we want to use as title, and which field do we want to use as subtitle. We can't always use the same field (e.g. the company name, or the city + the country) because those fields may not be available for that country.

"icon_url": "[% static_subdomain %]/images/icons/dist/transportation.svg",
"icon_color_from_evaluation": true,
},
"elements": [
{
"element_type": "map",
"map_element": {
"pointers": [
{
"geo": {
"lat": [% panel.lat %],
"lng": [% panel.lng %],
}
}
]
}
},
]
}
Loading