fixed price drops/moved logging outside of init_monitor
This commit is contained in:
1 parent
7d070dec40
commit
00d3c12ee0
3 files changed
+98
-27
No files matched your search
@@ -94,10 +94,12 @@ async def add_products_to_database(
|
||||
):
|
||||
"""Upsert products, tracking deduplicated price history and emitting alerts.
|
||||
|
||||
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 never-before-recorded prices
|
||||
Rewrites current fields on each upsert. priceBeforeDrop is derived here,
|
||||
not by monitors: it is set to the previous stored currentPrice when the new
|
||||
price is lower (a drop) and cleared otherwise, so any price_before_drop on
|
||||
the incoming product is ignored. 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 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
|
||||
@@ -133,6 +135,11 @@ async def add_products_to_database(
|
||||
document["_id"] = doc_id
|
||||
existing_doc = existing.get(doc_id)
|
||||
new_price = document.get("currentPrice")
|
||||
previous_price = existing_doc.get("currentPrice") if existing_doc else None
|
||||
document["priceBeforeDrop"] = resolve_price_before_drop(
|
||||
previous_price, new_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}
|
||||
@@ -193,6 +200,29 @@ async def write_alert_ops(
|
||||
raise
|
||||
|
||||
|
||||
def resolve_price_before_drop(
|
||||
previous_price: float | None, new_price: float | None
|
||||
) -> float | None:
|
||||
"""Return the pre-drop price when new_price is a drop from previous_price.
|
||||
|
||||
Owns price-drop detection so monitors do not: a drop is a new price strictly
|
||||
below the previously stored one, and its priceBeforeDrop is that prior price.
|
||||
Any other case (no prior price, unchanged, or a rise) is not a drop.
|
||||
|
||||
Args:
|
||||
previous_price: The product's last stored currentPrice, or None when new.
|
||||
new_price: The incoming currentPrice.
|
||||
|
||||
Returns:
|
||||
previous_price on a drop, otherwise None.
|
||||
"""
|
||||
|
||||
if previous_price is not None and new_price is not None and new_price < previous_price:
|
||||
return previous_price
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def extract_recorded_prices(existing_doc: dict | None) -> set[float]:
|
||||
"""Return the set of prices already recorded in a document's pastPrices.
|
||||
|
||||
|
||||
Reference in new issue
Block a user