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

Automatically match new prices with their PriceTag prediction counterpart #637

Open
1 of 2 tasks
raphodn opened this issue Dec 19, 2024 · 3 comments · Fixed by #650
Open
1 of 2 tasks

Automatically match new prices with their PriceTag prediction counterpart #637

raphodn opened this issue Dec 19, 2024 · 3 comments · Fixed by #650

Comments

@raphodn
Copy link
Member

raphodn commented Dec 19, 2024

Story

When a proof is uploaded, we now automatically extract and create related PriceTags.
But the user might be adding prices with the regular workflows (aka not using the PriceTag predictions).

To avoid the risk of having duplicate prices, we need to hide them, by changing their status + linking them to the created price.

How

@raphael0202
Copy link
Contributor

To avoid duplicating the work in the new contribution assistant, I would suggest adding a new unlinked_price_tags_count column to the proof table, that represents for PRICE_TAG proofs the number of prices that are not linked to a price tag.

By doing this, we can filter in the price validator assistant only price tags associated to proofs for which we added prices through the price tag assistant only.

Detecting price duplicates on the backend seems like a waste of time for users to me, as they already checked and validated the price: we just need to link the price tag to an existing price, which may be done in another game.

@raphodn
Copy link
Member Author

raphodn commented Dec 20, 2024

I was thinking of an automated way, not a user-game way :)

@raphodn
Copy link
Member Author

raphodn commented Dec 20, 2024

Matching script

# for each proof, try to match generated price_tags with existing prices
# - skip proofs without price_tags or without prices
# - skip price_tags that already have a price_id or that have no predictions
# - finally loop on each price and try to match with the price_tag prediction data
for proof in Proof.objects.filter(type="PRICE_TAG").prefetch_related("prices", "price_tags", "price_tags__predictions"):
    if proof.price_tags.count() == 0:
        continue
    elif proof.prices.count() == 0:
        continue
    else:
        for price_tag in proof.price_tags.all():
            if price_tag.price_id is not None:
                continue
            elif price_tag.predictions.count() == 0:
                continue
            else:
                price_tag_prediction_data = price_tag.predictions.first().data
                for price in proof.prices.all():
                    if price.price_tags.count() > 0:
                        continue
                    elif price.type == "PRODUCT" and (price.product_code == price_tag_prediction_data["barcode"]) and (str(price.price) == str(price_tag_prediction_data["price"])):
                        price_tag.price_id = price.id
                        price_tag.status = 1
                        price_tag.save()
                        # print("match! product", proof.id, price_tag.id)
                        break
                    elif price.type == "CATEGORY" and (price.category_tag == price_tag_prediction_data["category"]) and (str(price.price) == str(price_tag_prediction_data["price"])):
                        price_tag.price_id = price.id
                        price_tag.status = 1
                        price_tag.save()
                        # print("match! category", proof.id, price_tag.id)
                        break
                    else:
                        continue

Before running the script

PriceTag.objects.count() 
# 34021
Proof.objects.filter(type="PRICE_TAG").count() 
# 11847

from collections import Counter
Counter(PriceTag.objects.all().values_list("status", flat=True))
# Counter({None: 33108, 2: 515, 1: 235, 3: 164})

PriceTag.objects.filter(price_id__isnull=False).count() 
# 233

After running the script

PriceTag.objects.filter(price_id__isnull=False).count() 
# 4592

PR

added the script as a management command: #650

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In progress
Development

Successfully merging a pull request may close this issue.

2 participants