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

refactor(Price tags): matching script: try to match on price only #664

Merged
merged 2 commits into from
Dec 31, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from open_prices.proofs.models import PriceTag, Proof
from open_prices.proofs.utils import (
match_category_price_tag_with_category_price,
match_price_tag_with_price,
match_product_price_tag_with_product_price,
)

Expand Down Expand Up @@ -38,9 +39,10 @@ def handle(self, *args, **options) -> None: # type: ignore
stats()

self.stdout.write("=== Running matching script...")
for proof in Proof.objects.has_type_price_tag().prefetch_related(
proof_qs = Proof.objects.has_type_price_tag().prefetch_related(
"prices", "price_tags", "price_tags__predictions"
):
)
for index, proof in enumerate(proof_qs):
if proof.price_tags.count() == 0:
continue
elif proof.prices.count() == 0:
Expand Down Expand Up @@ -72,6 +74,13 @@ def handle(self, *args, **options) -> None: # type: ignore
price_tag.status = 1
price_tag.save()
break
elif match_price_tag_with_price(price_tag, price):
price_tag.price_id = price.id
price_tag.status = 1
price_tag.save()
break
if index % 500 == 0:
self.stdout.write(f"Processed {index} proofs")

self.stdout.write("=== Stats after...")
stats()
29 changes: 29 additions & 0 deletions open_prices/proofs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ def cleanup_price_tag_prediction_barcode(barcode: str) -> str:
def match_product_price_tag_with_product_price(
price_tag: PriceTag, price: Price
) -> bool:
"""
Match on barcode and price.
"""
price_tag_prediction_data = price_tag.predictions.first().data
price_tag_prediction_barcode = price_tag_prediction_data.get("barcode")
price_tag_prediction_barcode = cleanup_price_tag_prediction_barcode(
Expand All @@ -270,6 +273,9 @@ def match_product_price_tag_with_product_price(
def match_category_price_tag_with_category_price(
price_tag: PriceTag, price: Price
) -> bool:
"""
Match on product (category_tag) and price.
"""
price_tag_prediction_data = price_tag.predictions.first().data
price_tag_prediction_product = price_tag_prediction_data.get("product")
price_tag_prediction_price = price_tag_prediction_data.get("price")
Expand All @@ -278,3 +284,26 @@ def match_category_price_tag_with_category_price(
and (price.product_code == price_tag_prediction_product)
and match_decimal_with_float(price.price, price_tag_prediction_price)
)


def match_price_tag_with_price(price_tag: PriceTag, price: Price) -> bool:
"""
Match only on price.
We make sure this price is unique in the proof to avoid errors.
"""
price_tag_prediction_data = price_tag.predictions.first().data
price_tag_prediction_price = price_tag_prediction_data.get("price")
proof_prices = list(
Price.objects.filter(proof_id=price_tag.proof_id).values_list(
"price", flat=True
)
)
proof_price_tag_prices = [
price_tag.predictions.first().data.get("price")
for price_tag in PriceTag.objects.filter(proof_id=price_tag.proof_id)
]
return (
match_decimal_with_float(price.price, price_tag_prediction_price)
and proof_prices.count(price.price) == 1
and proof_price_tag_prices.count(price_tag_prediction_price) == 1
)
Loading