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
|
||||
from dataclasses import asdict
|
||||
from datetime import datetime, timezone
|
||||
@@ -78,20 +85,16 @@ async def add_products_to_database(
|
||||
if not products:
|
||||
return
|
||||
|
||||
doc_ids = {
|
||||
id(p): build_doc_id(p.product_id, category_identifier, use_composite)
|
||||
for p in products
|
||||
}
|
||||
cursor = collection.find(
|
||||
{"_id": {"$in": list(doc_ids.values())}}, {"currentPrice": 1}
|
||||
)
|
||||
doc_ids = [
|
||||
build_doc_id(p.product_id, category_identifier, use_composite) for p in products
|
||||
]
|
||||
cursor = collection.find({"_id": {"$in": doc_ids}}, {"currentPrice": 1})
|
||||
existing_prices = {str(doc["_id"]): doc.get("currentPrice") async for doc in cursor}
|
||||
|
||||
ops = []
|
||||
now = datetime.now(timezone.utc)
|
||||
price_key = str(int(now.timestamp() * 1000))
|
||||
for product in products:
|
||||
doc_id = doc_ids[id(product)]
|
||||
for product, doc_id in zip(products, doc_ids):
|
||||
document = convert_product_to_mongo(product, category_identifier)
|
||||
document["_id"] = 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.
|
||||
|
||||
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:
|
||||
product: Product to convert.
|
||||
category_identifier: Category suffix appended to the composite _id.
|
||||
category_identifier: Category suffix retained for signature parity.
|
||||
|
||||
Returns:
|
||||
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(",", ""))
|
||||
|
||||
return {
|
||||
"_id": f"{data['product_id']}_{category_identifier}",
|
||||
"title": data["title"],
|
||||
"url": data["url"],
|
||||
"currentPrice": current_price,
|
||||
|
||||
Reference in new issue
Block a user