Skip to content

Commit

Permalink
wip refactor to add more facets
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarel committed Feb 10, 2023
1 parent 05c02ac commit 9d41d1c
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 83 deletions.
54 changes: 52 additions & 2 deletions lib/ProductOpener/ContributionKnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ BEGIN {
@EXPORT_OK = qw(
&create_contribution_card_panel
&create_data_quality_errors_panel
&create_data_quality_panel
); # symbols to export on request
%EXPORT_TAGS = (all => [@EXPORT_OK]);
}
Expand Down Expand Up @@ -79,6 +80,7 @@ sub create_contribution_card_panel ($product_ref, $target_lc, $target_cc, $optio

$log->debug("create contribution card panel", {code => $product_ref->{code}}) if $log->is_debug();

# we need to create it first because it can condition contribution panel display
create_data_quality_errors_panel($product_ref, $target_lc, $target_cc, $options_ref);

my $panel_data_ref = {};
Expand Down Expand Up @@ -113,17 +115,65 @@ sub create_data_quality_errors_panel ($product_ref, $target_lc, $target_cc, $opt
my $panel_data_ref = {};

my @data_quality_errors_tags = @{$product_ref->{data_quality_errors_tags} // []};
# Only display to login user on the web and if we have errors !
# Only display to login user on the web !
if ( $options_ref->{user_logged_in}
&& ($options_ref->{knowledge_panels_client} eq 'web')
&& (scalar @data_quality_errors_tags))
{
$panel_data_ref->{quality_errors} = tags_by_prop("data_quality", $product_ref->{data_quality_errors_tags} ,"fix_action:en", ["description:en"]);
$panel_data_ref->{quality_errors} = tags_by_prop("data_quality", $product_ref->{data_quality_errors_tags} ,"fix_action:en", ["description:en", "marker_type:en", "show"]);
create_panel_from_json_template("data_quality_errors",
"api/knowledge-panels/contribution/data_quality_errors.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref);
}
return;
}

sub _polished_action_tags($quality_tags_by_action) {
my $result = {};
# remove all properties that do not have a description or that have same description
while (my ($action, $tags) = each %$quality_tags_by_action) {
my $action_result = {};
my %seen_descriptions = ();
while (my ($tagid, $infos) = each %$tags) {
my $desc = $infos->{"description:en"};
if ((defined $desc) && (!defined $seen_descriptions{$desc})) {
$action_result->{$tagid} = $infos;
$seen_descriptions{$desc} = 1;
}
}
# only keep non empty actions (it may happen if all descriptions are empty !)
if (scalar $action_result) {
$result->{$action} = $action_result;
}
}
return $result;
}


sub create_data_quality_panel($tags_type, $product_ref, $target_lc, $target_cc, $options_ref) {

$log->debug("create quality errors panel", {code => $product_ref->{code}}) if $log->is_debug();

my $field_name = $tags_type . "_tags";
my @data_quality_tags = @{$product_ref->{$field_name} // []};
# Only display to login user on the web !
if ( $options_ref->{user_logged_in}
&& ($options_ref->{knowledge_panels_client} eq 'web')
&& (scalar @data_quality_tags))
{
my $panel_data_ref = {};
my $quality_tags_by_action = tags_by_prop("data_quality", $product_ref->{$field_name} ,"fix_action:en", ["description:en", "show_to:en"]);
$quality_tags_by_action = _polished_action_tags($quality_tags_by_action);
if (%$quality_tags_by_action) {
$panel_data_ref->{tags_type} = $tags_type;
$panel_data_ref->{quality_tags} = $quality_tags_by_action;
create_panel_from_json_template("data_quality_errors",
"api/knowledge-panels/contribution/data_quality_tags.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref);
}
}
return;
}


1;
21 changes: 11 additions & 10 deletions taxonomies/data_quality.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ description:en: Error detected on data

<en:Data quality errors
en:Nutrition errors
description:en:Error on nutrition data (nutriments)
description:en:Error on nutrition data (nutrients)
show_on_producers_platform:en:yes
fix_action:en: add_nutrition_facts
marker_type:en: error

<en:Nutrition errors
en:Energy value in kcal does not match value computed from other nutrients
Expand Down Expand Up @@ -303,7 +304,6 @@ description:en:Water hardness value is over 1000g per 100g, which is impossible.
en:Nutrition value over 1000 - Zinc
description:en:Zinc value is over 1000g per 100g, which is impossible.

<en:Nutrition errors
<en:Nutrition errors
en:Nutrition value over 105 - Alcohol
description:en:Alcohol value is over 105g per 100g, which is impossible.
Expand Down Expand Up @@ -767,10 +767,11 @@ description:fr:La somme des valeurs nutritionnelles pour 100 g ou 100 ml est sup

en:Data quality warnings
description:en: Possible error on data detected. Might be a false alert.
marker_type:en: warning

en:Data quality ok tags
description:en: Positive appreciations on data quality

marker_type:en: info

### Eco-Score and packaging
<en:Data quality warnings
Expand Down Expand Up @@ -849,7 +850,7 @@ en:Packaging data complete

<Data quality warnings
en:Food groups warnings
description:en: Food groups can't be infered
description:en: Food groups can't be inferred

<en:Food groups warnings
en:Food groups 3 unknown
Expand Down Expand Up @@ -889,8 +890,8 @@ fix_action:en: add_quantity
### Fibers

<en:Data quality warnings
en:Fiber warnings
description:en: Their might be an issue with fiber nutriment
en:Fibers warnings
description:en: There might be an issue with the fibers nutrient
fix_action:en: add_nutrition_facts

<en:Fiber warnings
Expand Down Expand Up @@ -2803,16 +2804,16 @@ en:Serving size in mg
### Carbon footprint

<en:Data quality warnings
en:Carbon footbrint warnings
en:Carbon footprint warnings

<en:Carbon footbrint warnings
<en:Carbon footprint warnings
en:Carbon footprint from known ingredients but not from meat or fish
#description:en:

<en:Carbon footbrint warnings
<en:Carbon footprint warnings
en:Carbon footprint from known ingredients less than from meat or fish
#description:en:

<en:Carbon footbrint warnings
<en:Carbon footprint warnings
en:Carbon footprint from known ingredients more than from meat or fish
#description:en:
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
"text_element": {
"html": `[% FILTER collapse %]
<ul>
[% FOREACH action_pair IN pair.value -%]
<li id="data-error-[% action_pair.key %]">[% action_pair.value.$description_key %]</li>
[%- END %]
[% FOREACH action_pair IN pair.value %]
[% IF action_pair.value.$description_key.defined %]
<li id="data-error-[% action_pair.key %]">[% action_pair.value.$description_key %]</li>
[% END %]
[% END %]
</ul>
[% END %]`
},
Expand Down
17 changes: 15 additions & 2 deletions tests/unit/create_data_quality_errors_panel.t
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,20 @@ my @tests = (
"en:nutri-score-grade-from-label-does-not-match-calculated-grade",
]},
},
# FIXME add test on show_to ?
{
id => "errors_but_no_actions",
desc => "A panel for a product with errors but no actions",
product => {data_quality_errors_tags => [
"en:energy-value-in-kcal-does-not-match-value-in-kj",
"en:nutrition-saturated-fat-greater-than-fat",
"en:nutrition-sugars-plus-starch-greater-than-carbohydrates",
"en:detected-category-from-name-and-ingredients-may-be-missing-baby-milks",
"en:nutri-score-grade-from-label-does-not-match-calculated-grade",
]},
},

# FIXME: add test on show_to ?
# FIXME: add test where there are error_tags but no action (--> should be null)
);

my %default_options = (user_logged_in => 1, knowledge_panels_client => 'web');
Expand All @@ -61,7 +74,7 @@ foreach my $test_ref (@tests) {
my $test_id = $test_ref->{id};
my %product = (%default_product, %{$test_ref->{product} // {}});
my %options = (%default_options, %{$test_ref->{options} // {}});
create_data_quality_errors_panel(\%product, "en", "world", \%options);
create_data_quality_panel("data_quality_errors", \%product, "en", "world", \%options);
my $panels_ref = $product{"knowledge_panels_en"};
compare_to_expected_results($panels_ref, "$expected_result_dir/$test_id.json", $update_expected_results, $test_ref);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,19 @@
"text_element" : {
"html" : "<ul> </ul>"
}
},
{
"element_type" : "text",
"text_element" : {
"html" : "<ul> <li id=\"data-error-en:detected-category-from-name-and-ingredients-may-be-missing-baby-milks\">potential issues on categories</li> </ul>"
}
},
{
"action_element" : {
"actions" : [
"add_categories"
],
"html" : ""
},
"element_type" : "action"
},
{
"element_type" : "text",
"text_element" : {
"html" : "<ul> <li id=\"data-error-en:energy-value-in-kcal-does-not-match-value-in-kj\">The energy value in kJ must be about 4.2 times greater than the value in kcal</li> <li id=\"data-error-en:nutrition-saturated-fat-greater-than-fat\">Saturated Fat is a sub-section of Fat on the nutrition labels, as a result, it should always be smaller than fat.</li> <li id=\"data-error-en:nutrition-sugars-plus-starch-greater-than-carbohydrates\">Error on nutrition data -nutriments-</li> </ul>"
}
},
{
"action_element" : {
"actions" : [
"add_nutrition_facts"
],
"html" : ""
},
"element_type" : "action"
},
{
"element_type" : "text",
"text_element" : {
"html" : "<ul> <li id=\"data-error-en:nutri-score-grade-from-label-does-not-match-calculated-grade\">Potential issues on Nutri-Score computation</li> </ul>"
}
}
],
"evaluation" : "unknown",
"evaluation" : "bad",
"expand_for" : "large",
"level" : "info",
"title_element" : {
"icon_color_from_evaluation" : true,
"icon_url" : "http://static.openfoodfacts.localhost/images/icons/dist/healing.svg",
"subtitle" : "Help us improve quality of our data by contributing fixes",
"title" : "Detected Errors"
"title" : "Detected Errors - data_quality_errors_panel_title"
},
"topics" : [
"health"
"contribution"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@
"text_element" : {
"html" : "<ul> </ul>"
}
},
{
"element_type" : "text",
"text_element" : {
"html" : "<ul> <li id=\"data-error-en:nutri-score-grade-from-label-does-not-match-calculated-grade\">Potential issues on Nutri-Score computation</li> </ul>"
}
}
],
"evaluation" : "unknown",
"evaluation" : "bad",
"expand_for" : "large",
"level" : "info",
"title_element" : {
"icon_color_from_evaluation" : true,
"icon_url" : "http://static.openfoodfacts.localhost/images/icons/dist/healing.svg",
"subtitle" : "Help us improve quality of our data by contributing fixes",
"title" : "Detected Errors"
"title" : "Detected Errors - data_quality_errors_panel_title"
},
"topics" : [
"health"
"contribution"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,19 @@
"text_element" : {
"html" : "<ul> </ul>"
}
},
{
"element_type" : "text",
"text_element" : {
"html" : "<ul> <li id=\"data-error-en:energy-value-in-kcal-does-not-match-value-in-kj\">The energy value in kJ must be about 4.2 times greater than the value in kcal</li> </ul>"
}
},
{
"action_element" : {
"actions" : [
"add_nutrition_facts"
],
"html" : ""
},
"element_type" : "action"
}
],
"evaluation" : "unknown",
"evaluation" : "bad",
"expand_for" : "large",
"level" : "info",
"title_element" : {
"icon_color_from_evaluation" : true,
"icon_url" : "http://static.openfoodfacts.localhost/images/icons/dist/healing.svg",
"subtitle" : "Help us improve quality of our data by contributing fixes",
"title" : "Detected Errors"
"title" : "Detected Errors - data_quality_errors_panel_title"
},
"topics" : [
"health"
"contribution"
]
}
}

0 comments on commit 9d41d1c

Please sign in to comment.