From 6ad9cf5d6df8ea2b0c876083f37fe074087eee55 Mon Sep 17 00:00:00 2001 From: TPV Date: Tue, 7 Jul 2026 11:03:21 -0700 Subject: [PATCH] added alerts collection dsql suggested --- src/monitor_utils/database.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/monitor_utils/database.py b/src/monitor_utils/database.py index 4efe87b..c275f24 100644 --- a/src/monitor_utils/database.py +++ b/src/monitor_utils/database.py @@ -78,11 +78,14 @@ async def add_products_to_database( Rewrites current fields on each upsert. Prices are recorded in a pastPrices array of price/date entries; a price already present in the array is never recorded again, so each distinct price keeps only its - first-seen timestamp. New products and price changes upsert an alert - referencing the product, deduplicated so a product holds at most one - unclaimed alert regardless of concurrent category or page tasks - detecting it; alerts write only after the product write succeeds so a - failed upsert cannot produce an orphaned alert. + first-seen timestamp. New products and never-before-recorded prices + upsert an alert referencing the product; a price returning to a value + already in pastPrices emits nothing, so per-source price oscillation + (member vs non-member quotes for the same SKU) alerts once per distinct + price instead of every run. Alerts are deduplicated so a product holds + at most one unclaimed alert regardless of concurrent category or page + tasks detecting it, and write only after the product write succeeds so + a failed upsert cannot produce an orphaned alert. Args: products: Products to upsert. @@ -110,11 +113,11 @@ async def add_products_to_database( document = convert_product_to_mongo(product) document["_id"] = doc_id existing_doc = existing.get(doc_id) - old_price = existing_doc.get("currentPrice") if existing_doc else None new_price = document.get("currentPrice") - price_changed = new_price is not None and new_price != old_price + recorded_prices = extract_recorded_prices(existing_doc) + never_seen_price = new_price is not None and new_price not in recorded_prices set_on_insert = {"discoveredDate": now} - if existing_doc is None or price_changed: + if existing_doc is None or never_seen_price: alert = build_alert(monitor_name, document, now) alert_ops.append( UpdateOne( @@ -125,8 +128,7 @@ async def add_products_to_database( ) update = {"$set": document, "$setOnInsert": set_on_insert} - recorded_prices = extract_recorded_prices(existing_doc) - if new_price is not None and new_price not in recorded_prices: + if never_seen_price: update["$push"] = {"pastPrices": {"price": new_price, "date": now}} ops.append(UpdateOne({"_id": doc_id}, update, upsert=True))