rename monitor_name parameters to monitor
Applies to setup_monitor_logging, init_monitor, setup_config, get_config, add_products_to_database and build_alert. Breaking for callers passing monitor_name by keyword; every monitor repo is updated alongside.
This commit is contained in:
1 parent
401258f52a
commit
ae637a134f
3 files changed
+21
-21
No files matched your search
@@ -29,7 +29,7 @@ def is_production(development_mode: DevelopmentMode) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
async def get_config(
|
async def get_config(
|
||||||
monitor_name: str,
|
monitor: str,
|
||||||
use_production: bool = False,
|
use_production: bool = False,
|
||||||
mongo_client: AsyncIOMotorClient = None,
|
mongo_client: AsyncIOMotorClient = None,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
@@ -39,7 +39,7 @@ async def get_config(
|
|||||||
development, so a dev run never reads production routing.
|
development, so a dev run never reads production routing.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
monitor_name: Monitor identifier (the config document _id).
|
monitor: Monitor identifier (the config document _id).
|
||||||
use_production: Read the production configs collection instead of
|
use_production: Read the production configs collection instead of
|
||||||
configs_testing.
|
configs_testing.
|
||||||
mongo_client: Client to read through; the shared cluster client when
|
mongo_client: Client to read through; the shared cluster client when
|
||||||
@@ -58,10 +58,10 @@ async def get_config(
|
|||||||
else f"{CONFIGS_COLLECTION_NAME}_testing"
|
else f"{CONFIGS_COLLECTION_NAME}_testing"
|
||||||
)
|
)
|
||||||
config = await get_collection(collection_name, mongo_client=mongo_client).find_one(
|
config = await get_collection(collection_name, mongo_client=mongo_client).find_one(
|
||||||
{"_id": monitor_name}
|
{"_id": monitor}
|
||||||
)
|
)
|
||||||
if config is None:
|
if config is None:
|
||||||
raise ConfigNotFoundError(monitor_name)
|
raise ConfigNotFoundError(monitor)
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ async def add_products_to_database(
|
|||||||
products: list[Product],
|
products: list[Product],
|
||||||
collection: AsyncIOMotorCollection,
|
collection: AsyncIOMotorCollection,
|
||||||
category_identifier: str,
|
category_identifier: str,
|
||||||
monitor_name: str,
|
monitor: str,
|
||||||
use_composite: bool = False,
|
use_composite: bool = False,
|
||||||
):
|
):
|
||||||
"""Upsert products, tracking deduplicated price history and emitting alerts.
|
"""Upsert products, tracking deduplicated price history and emitting alerts.
|
||||||
@@ -138,7 +138,7 @@ async def add_products_to_database(
|
|||||||
products: Products to upsert.
|
products: Products to upsert.
|
||||||
collection: Target MongoDB collection.
|
collection: Target MongoDB collection.
|
||||||
category_identifier: Category suffix appended to composite ids.
|
category_identifier: Category suffix appended to composite ids.
|
||||||
monitor_name: Monitor name stamped onto emitted alerts for consumer
|
monitor: Monitor name stamped onto emitted alerts for consumer
|
||||||
routing.
|
routing.
|
||||||
use_composite: Build composite ids so multi-variant monitors (Woot)
|
use_composite: Build composite ids so multi-variant monitors (Woot)
|
||||||
keep one doc per variant; other monitors collapse to one doc per
|
keep one doc per variant; other monitors collapse to one doc per
|
||||||
@@ -180,7 +180,7 @@ async def add_products_to_database(
|
|||||||
never_seen_price = new_price is not None and new_price not in recorded_prices
|
never_seen_price = new_price is not None and new_price not in recorded_prices
|
||||||
set_on_insert = {"discoveredDate": now}
|
set_on_insert = {"discoveredDate": now}
|
||||||
if existing_doc is None or never_seen_price:
|
if existing_doc is None or never_seen_price:
|
||||||
alert = build_alert(monitor_name, document, now)
|
alert = build_alert(monitor, document, now)
|
||||||
alert_ops.append(
|
alert_ops.append(
|
||||||
UpdateOne(
|
UpdateOne(
|
||||||
{"product_id": doc_id, "claimed": False},
|
{"product_id": doc_id, "claimed": False},
|
||||||
@@ -279,7 +279,7 @@ def extract_recorded_prices(existing_doc: dict | None) -> set[float]:
|
|||||||
return {entry["price"] for entry in existing_doc.get("pastPrices", [])}
|
return {entry["price"] for entry in existing_doc.get("pastPrices", [])}
|
||||||
|
|
||||||
|
|
||||||
def build_alert(monitor_name: str, document: dict, now: datetime) -> dict:
|
def build_alert(monitor: str, document: dict, now: datetime) -> dict:
|
||||||
"""Build an alert referencing a product for consumer delivery.
|
"""Build an alert referencing a product for consumer delivery.
|
||||||
|
|
||||||
The alert carries its own copy of the document's newListing flag and
|
The alert carries its own copy of the document's newListing flag and
|
||||||
@@ -290,7 +290,7 @@ def build_alert(monitor_name: str, document: dict, now: datetime) -> dict:
|
|||||||
and cannot rot.
|
and cannot rot.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
monitor_name: Monitor that produced the product, used by the
|
monitor: Monitor that produced the product, used by the
|
||||||
consumer to resolve the owning collection and thresholds.
|
consumer to resolve the owning collection and thresholds.
|
||||||
document: Product document supplying the id, pricing for the
|
document: Product document supplying the id, pricing for the
|
||||||
priority calculation, and the stamped newListing flag and
|
priority calculation, and the stamped newListing flag and
|
||||||
@@ -302,7 +302,7 @@ def build_alert(monitor_name: str, document: dict, now: datetime) -> dict:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"monitor": monitor_name,
|
"monitor": monitor,
|
||||||
"product_id": document["_id"],
|
"product_id": document["_id"],
|
||||||
"priority": calculate_priority(document),
|
"priority": calculate_priority(document),
|
||||||
"claimed": False,
|
"claimed": False,
|
||||||
|
|||||||
+11
-11
@@ -37,22 +37,22 @@ from monitor_utils.models import (
|
|||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def setup_monitor_logging(monitor_name: str, logging_level=LOGGING_LEVEL):
|
def setup_monitor_logging(monitor: str, logging_level=LOGGING_LEVEL):
|
||||||
"""Configure JSON logging for a monitor and quiet noisy libraries.
|
"""Configure JSON logging for a monitor and quiet noisy libraries.
|
||||||
|
|
||||||
Call once at module import, at global scope, before creating module loggers,
|
Call once at module import, at global scope, before creating module loggers,
|
||||||
so logging is configured synchronously at startup rather than inside the
|
so logging is configured synchronously at startup rather than inside the
|
||||||
async init_monitor. Logs go to logs/latest.log with rotated history keyed by
|
async init_monitor. Logs go to logs/latest.log with rotated history keyed by
|
||||||
monitor_name.
|
monitor.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
monitor_name: Monitor name used as the rotated-history base.
|
monitor: Monitor name used as the rotated-history base.
|
||||||
logging_level: Root logging level.
|
logging_level: Root logging level.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
setup_logging(
|
setup_logging(
|
||||||
name="logs/latest.log",
|
name="logs/latest.log",
|
||||||
history_name=monitor_name,
|
history_name=monitor,
|
||||||
level=logging_level,
|
level=logging_level,
|
||||||
output="json",
|
output="json",
|
||||||
)
|
)
|
||||||
@@ -60,7 +60,7 @@ def setup_monitor_logging(monitor_name: str, logging_level=LOGGING_LEVEL):
|
|||||||
|
|
||||||
|
|
||||||
async def init_monitor(
|
async def init_monitor(
|
||||||
monitor_name: str,
|
monitor: str,
|
||||||
development_mode: DevelopmentMode,
|
development_mode: DevelopmentMode,
|
||||||
production_proxy_path: str = "proxies.txt",
|
production_proxy_path: str = "proxies.txt",
|
||||||
testing_proxy_path: str = "tests/configs/proxies.txt",
|
testing_proxy_path: str = "tests/configs/proxies.txt",
|
||||||
@@ -72,7 +72,7 @@ async def init_monitor(
|
|||||||
by setup_monitor_logging at module scope and is not touched here.
|
by setup_monitor_logging at module scope and is not touched here.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
monitor_name: Monitor identifier used for config and collection lookup.
|
monitor: Monitor identifier used for config and collection lookup.
|
||||||
development_mode: Selects production or testing config, collection,
|
development_mode: Selects production or testing config, collection,
|
||||||
and proxy path.
|
and proxy path.
|
||||||
production_proxy_path: Proxy file path used in production mode.
|
production_proxy_path: Proxy file path used in production mode.
|
||||||
@@ -84,7 +84,7 @@ async def init_monitor(
|
|||||||
|
|
||||||
use_production = is_production(development_mode)
|
use_production = is_production(development_mode)
|
||||||
category_names, _, collection = await setup_config(
|
category_names, _, collection = await setup_config(
|
||||||
monitor_name=monitor_name, development_mode=development_mode
|
monitor=monitor, development_mode=development_mode
|
||||||
)
|
)
|
||||||
proxy_path = production_proxy_path if use_production else testing_proxy_path
|
proxy_path = production_proxy_path if use_production else testing_proxy_path
|
||||||
proxies = AioProxies.from_file(path=proxy_path)
|
proxies = AioProxies.from_file(path=proxy_path)
|
||||||
@@ -92,7 +92,7 @@ async def init_monitor(
|
|||||||
|
|
||||||
|
|
||||||
async def setup_config(
|
async def setup_config(
|
||||||
monitor_name: str,
|
monitor: str,
|
||||||
development_mode: DevelopmentMode,
|
development_mode: DevelopmentMode,
|
||||||
mongo_client: AsyncIOMotorClient = None,
|
mongo_client: AsyncIOMotorClient = None,
|
||||||
) -> tuple[
|
) -> tuple[
|
||||||
@@ -105,7 +105,7 @@ async def setup_config(
|
|||||||
during development.
|
during development.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
monitor_name: Monitor identifier used for config and collection lookup.
|
monitor: Monitor identifier used for config and collection lookup.
|
||||||
development_mode: Selects the production or testing config and
|
development_mode: Selects the production or testing config and
|
||||||
collection.
|
collection.
|
||||||
mongo_client: Client the config read and the returned collection use;
|
mongo_client: Client the config read and the returned collection use;
|
||||||
@@ -117,14 +117,14 @@ async def setup_config(
|
|||||||
|
|
||||||
use_production = is_production(development_mode)
|
use_production = is_production(development_mode)
|
||||||
config = await get_config(
|
config = await get_config(
|
||||||
monitor_name=monitor_name,
|
monitor=monitor,
|
||||||
use_production=use_production,
|
use_production=use_production,
|
||||||
mongo_client=mongo_client,
|
mongo_client=mongo_client,
|
||||||
)
|
)
|
||||||
|
|
||||||
category_names = config["categories"]
|
category_names = config["categories"]
|
||||||
collection = get_collection(
|
collection = get_collection(
|
||||||
monitor_name if use_production else f"{monitor_name}_testing",
|
monitor if use_production else f"{monitor}_testing",
|
||||||
mongo_client=mongo_client,
|
mongo_client=mongo_client,
|
||||||
)
|
)
|
||||||
return category_names, config["channels"], collection
|
return category_names, config["channels"], collection
|
||||||
|
|||||||
Reference in new issue
Block a user