fixed some small db bugs
This commit is contained in:
1 parent
136a67c16f
commit
54881acaee
1 file changed
+14
-12
@@ -1,3 +1,10 @@
|
|||||||
|
"""Database helpers for upserting monitored products and tracking price history.
|
||||||
|
|
||||||
|
Provides the shared Motor client, collection access, and the upsert path that
|
||||||
|
rewrites current product fields while preserving prior prices in a pastPrices
|
||||||
|
map and stamping lastUpdated on every write.
|
||||||
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from dataclasses import asdict
|
from dataclasses import asdict
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
@@ -78,20 +85,16 @@ async def add_products_to_database(
|
|||||||
if not products:
|
if not products:
|
||||||
return
|
return
|
||||||
|
|
||||||
doc_ids = {
|
doc_ids = [
|
||||||
id(p): build_doc_id(p.product_id, category_identifier, use_composite)
|
build_doc_id(p.product_id, category_identifier, use_composite) for p in products
|
||||||
for p in products
|
]
|
||||||
}
|
cursor = collection.find({"_id": {"$in": doc_ids}}, {"currentPrice": 1})
|
||||||
cursor = collection.find(
|
|
||||||
{"_id": {"$in": list(doc_ids.values())}}, {"currentPrice": 1}
|
|
||||||
)
|
|
||||||
existing_prices = {str(doc["_id"]): doc.get("currentPrice") async for doc in cursor}
|
existing_prices = {str(doc["_id"]): doc.get("currentPrice") async for doc in cursor}
|
||||||
|
|
||||||
ops = []
|
ops = []
|
||||||
now = datetime.now(timezone.utc)
|
now = datetime.now(timezone.utc)
|
||||||
price_key = str(int(now.timestamp() * 1000))
|
price_key = str(int(now.timestamp() * 1000))
|
||||||
for product in products:
|
for product, doc_id in zip(products, doc_ids):
|
||||||
doc_id = doc_ids[id(product)]
|
|
||||||
document = convert_product_to_mongo(product, category_identifier)
|
document = convert_product_to_mongo(product, category_identifier)
|
||||||
document["_id"] = doc_id
|
document["_id"] = doc_id
|
||||||
old_price = existing_prices.get(doc_id)
|
old_price = existing_prices.get(doc_id)
|
||||||
@@ -144,11 +147,11 @@ def convert_product_to_mongo(product: Product, category_identifier: str) -> dict
|
|||||||
"""Flatten a Product into its MongoDB document representation.
|
"""Flatten a Product into its MongoDB document representation.
|
||||||
|
|
||||||
Coerces string prices to floats and stamps lastUpdated with the current
|
Coerces string prices to floats and stamps lastUpdated with the current
|
||||||
UTC time.
|
UTC time. The _id is assigned by the caller via build_doc_id.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
product: Product to convert.
|
product: Product to convert.
|
||||||
category_identifier: Category suffix appended to the composite _id.
|
category_identifier: Category suffix retained for signature parity.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The MongoDB document dict for the product.
|
The MongoDB document dict for the product.
|
||||||
@@ -166,7 +169,6 @@ def convert_product_to_mongo(product: Product, category_identifier: str) -> dict
|
|||||||
original_price = float(original_price.replace(",", ""))
|
original_price = float(original_price.replace(",", ""))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"_id": f"{data['product_id']}_{category_identifier}",
|
|
||||||
"title": data["title"],
|
"title": data["title"],
|
||||||
"url": data["url"],
|
"url": data["url"],
|
||||||
"currentPrice": current_price,
|
"currentPrice": current_price,
|
||||||
|
|||||||
Reference in new issue
Block a user