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: update product_type #11173

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

feat: update product_type #11173

wants to merge 7 commits into from

Conversation

benbenben2
Copy link
Collaborator

What

Automatically change the product type of non-food categorized products

Copy pasted all non-something on top of each categories taxonomies.
Also copy pasted categories from food to other flavors (for example, en: Dry Dog Food is moved from taxonomies/food/categories.txt to taxonomies/petfood/categories.txt

Remark

The following, I could not add, but would be a nice to have:

  • rm tag from previous flavor/project after migration. For example, after changing product_type from food to beauty we would like to remove
  1. 'en:non-food-products' and 'en:open-beauty-facts'
    but also categories from food
  2. "en:desserts", "en:pies", "en:sweet-pies", "en:apple-pies" for example

I tried to do something with remove_tag function from Tags.pm. It removes element of the categories_tags array, but categories (string) and categories_hierarchy (array) remain. And tags are still visible on the website. As I did not want to do some update of the categories string, I decided not to include that in the present PR.

Related issue(s) and discussion

@benbenben2 benbenben2 self-assigned this Dec 27, 2024
@benbenben2 benbenben2 requested a review from a team as a code owner December 27, 2024 16:33
@github-actions github-actions bot added 🧬 Taxonomies https://wiki.openfoodfacts.org/Global_taxonomies categories 🧴 Open Beauty Facts Our cosmetic analysis project https://world.openbeautyfacts.org 🐾 Open Pet Food Facts Our pet food analysis project https://world.openpetfoodfacts.org 🧪 tests 📸 Open Products Facts Our project to increase the lifespan of objects. https://world.openproductsfacts.org Products 🧪 unit tests labels Dec 27, 2024
@codecov-commenter
Copy link

codecov-commenter commented Dec 27, 2024

Codecov Report

Attention: Patch coverage is 64.28571% with 5 lines in your changes missing coverage. Please review.

Project coverage is 49.21%. Comparing base (765d796) to head (8b23765).

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
lib/ProductOpener/Products.pm 64.28% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11173      +/-   ##
==========================================
+ Coverage   49.18%   49.21%   +0.03%     
==========================================
  Files          78       78              
  Lines       22389    22403      +14     
  Branches     5371     5375       +4     
==========================================
+ Hits        11011    11025      +14     
+ Misses      10020    10016       -4     
- Partials     1358     1362       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

$error = change_product_type($product_ref, "petfood");
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Thanks @benbenben2 that's great.

That said, I would have first computed "expected_product_type" using "has_tag" and if it's defined then, I would have compared it to $product_ref->{product_type}.

It would allow less repetitions in the code.

Are you willing to refactor ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure. I applied some changes. Is it what you expected?

Copy link
Member

Choose a reason for hiding this comment

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

it was more:

	my $expected_type;
	if (has_tag($product_ref, "categories", "en:open-food-facts")) {
		$expected_type = "food";
	}
	elsif (has_tag($product_ref, "categories", "en:open-pet-food-facts")) {
		$expected_type = "petfood";
	}
	elsif (has_tag($product_ref, "categories", "en:open-products-facts")) {
		$expected_type = "product";
	}
	elsif (has_tag($product_ref, "categories", "en:open-beauty-facts")) {
		$expected_type = "beauty";
	}
	if ($expected_type and ($product_ref->{product_type} ne expected_type)) {
		$error = change_product_type($product_ref, expected_type);
	}

taxonomies/food/categories.txt Show resolved Hide resolved
Copy link

sonarqubecloud bot commented Jan 8, 2025

@@ -3678,6 +3721,11 @@ sub analyze_and_enrich_product_data ($product_ref, $response_ref) {

compute_languages($product_ref); # need languages for allergens detection and cleaning ingredients

# change the product type of non-food categorized products (issue #11094)
if (has_tag($product_ref, "categories", "en:incorrect-product-type")) {
review_product_type($product_ref);
Copy link
Contributor

Choose a reason for hiding this comment

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

There's something tricky: change_product_type() updates the product_type field and adds a old_product_type, so that store_product() can then remove the product from the old MongoDB collection.

But that means that store_product() must absolutely be called when the product_type is changed.

What you did works for changes done through the website or API, because we call store_product(). But we don't always call store_product(), there is an exception in scripts/update_all_products.pl : when we reprocess ingredients, scores etc.

So we need to change update_all_products.pl so that we do call store_product() if we have a $product_ref->{old_product_type} value. We can use an user like "update-product-type-bot".

See https://github.com/openfoodfacts/openfoodfacts-server/blob/main/scripts/update_all_products.pl#L1405

@github-actions github-actions bot added the 💥 Merge Conflicts 💥 Merge Conflicts label Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
categories config 💥 Merge Conflicts 💥 Merge Conflicts 🧴 Open Beauty Facts Our cosmetic analysis project https://world.openbeautyfacts.org 🐾 Open Pet Food Facts Our pet food analysis project https://world.openpetfoodfacts.org 📸 Open Products Facts Our project to increase the lifespan of objects. https://world.openproductsfacts.org Products ⭐ top pull request Top pull request. 🧬 Taxonomies https://wiki.openfoodfacts.org/Global_taxonomies 🧪 tests 🧪 unit tests
Projects
Status: In progress
Status: In progress
Status: In progress
Development

Successfully merging this pull request may close these issues.

Automatically change the product type of non-food categorized products
4 participants