Merge pull request 'added price drops to alerts/fixed race condition where late claimed docs got their tags wiped and sent to the api without tags' (#19) from development into main

Reviewed-on: #19
This commit was merged in pull request #19.
This commit is contained in:
TPV committed 2026-09-09 16:52:30 +00:00
commit 0fceafb364
2 files changed
+37 -6

No files matched your search

+9 -5
View File
@@ -282,16 +282,19 @@ def extract_recorded_prices(existing_doc: dict | None) -> set[float]:
def build_alert(monitor_name: str, document: dict, now: datetime) -> dict:
"""Build an alert referencing a product for consumer delivery.
The alert carries its own copy of the document's newListing flag: the
product doc is rewritten every cycle, so its flag flips False one cycle
after discovery, while a discovery alert may sit in a backed-up queue
longer than that. The alert's copy is written once and cannot rot.
The alert carries its own copy of the document's newListing flag and
priceBeforeDrop: the product doc is rewritten every cycle, so its flag
flips False one cycle after discovery and its priceBeforeDrop is
cleared by the next cycle that sees no drop, while an alert may sit in
a backed-up queue longer than that. The alert's copies are written once
and cannot rot.
Args:
monitor_name: Monitor that produced the product, used by the
consumer to resolve the owning collection and thresholds.
document: Product document supplying the id, pricing for the
priority calculation, and the stamped newListing flag.
priority calculation, and the stamped newListing flag and
priceBeforeDrop.
now: Detection timestamp stored as createdAt.
Returns:
@@ -306,6 +309,7 @@ def build_alert(monitor_name: str, document: dict, now: datetime) -> dict:
"attempts": 0,
"createdAt": now,
"newListing": document.get("newListing", False),
"priceBeforeDrop": document.get("priceBeforeDrop"),
}
+28 -1
View File
@@ -99,6 +99,32 @@ def test_existing_doc_with_new_price_stamps_false():
assert alert_doc(collection)["newListing"] is False
def test_drop_alert_carries_price_before_drop():
"""the doc's priceBeforeDrop is cleared by the next no-drop cycle, so the
alert keeps its own copy from emit time — a drop claimed late still knows
what it dropped from"""
existing = {"_id": "p1", "currentPrice": 100.0, "pastPrices": []}
collection = FakeCollection([existing])
run(collection, [_product(current=95.0)])
assert alert_doc(collection)["priceBeforeDrop"] == 100.0
def test_non_drop_alert_carries_none():
"""a never-seen higher price alerts without a drop; discoveries have no
prior price at all"""
existing = {"_id": "p1", "currentPrice": 100.0, "pastPrices": []}
collection = FakeCollection([existing])
run(collection, [_product(current=120.0)])
assert alert_doc(collection)["priceBeforeDrop"] is None
discovery = FakeCollection()
run(discovery, [_product()])
assert alert_doc(discovery)["priceBeforeDrop"] is None
def test_existing_doc_with_known_price_emits_no_alert():
existing = {
"_id": "p1",
@@ -112,8 +138,9 @@ def test_existing_doc_with_known_price_emits_no_alert():
assert collection.alerts is None
def test_build_alert_defaults_missing_flag_to_false():
def test_build_alert_defaults_missing_fields():
"""Direct callers passing an unstamped document must not KeyError."""
alert = build_alert("bestbuy", {"_id": "p1"}, NOW)
assert alert["newListing"] is False
assert alert["priceBeforeDrop"] is None