From 39a6c401549d214210d189649ea6fe5c95a8e8e7 Mon Sep 17 00:00:00 2001 From: TPV Date: Sun, 30 Aug 2026 17:54:40 -0700 Subject: [PATCH] updated the mongo_url to dsqls new url/small type hint fixes --- src/monitor_utils/config.py | 14 ++++++++++++-- src/monitor_utils/constants.py | 4 +--- src/monitor_utils/database.py | 8 ++++++-- src/monitor_utils/runner.py | 11 +++++++++-- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/monitor_utils/config.py b/src/monitor_utils/config.py index d189712..5f88731 100644 --- a/src/monitor_utils/config.py +++ b/src/monitor_utils/config.py @@ -3,6 +3,8 @@ import logging from enum import Enum +from motor.motor_asyncio import AsyncIOMotorClient + from monitor_utils.constants import LOGGERS from monitor_utils.database import get_collection @@ -26,7 +28,11 @@ def is_production(development_mode: DevelopmentMode) -> bool: return development_mode == DevelopmentMode.PRODUCTION -async def get_config(monitor_name: str, use_production: bool = False) -> dict: +async def get_config( + monitor_name: str, + use_production: bool = False, + mongo_client: AsyncIOMotorClient = None, +) -> dict: """Load a monitor's config document from Mongo. Reads the configs collection in production and configs_testing in @@ -36,6 +42,8 @@ async def get_config(monitor_name: str, use_production: bool = False) -> dict: monitor_name: Monitor identifier (the config document _id). use_production: Read the production configs collection instead of configs_testing. + mongo_client: Client to read through; the shared cluster client when + None. Returns: The config document, including its categories and channels. @@ -49,7 +57,9 @@ async def get_config(monitor_name: str, use_production: bool = False) -> dict: if use_production else f"{CONFIGS_COLLECTION_NAME}_testing" ) - config = await get_collection(collection_name).find_one({"_id": monitor_name}) + config = await get_collection(collection_name, mongo_client=mongo_client).find_one( + {"_id": monitor_name} + ) if config is None: raise ConfigNotFoundError(monitor_name) diff --git a/src/monitor_utils/constants.py b/src/monitor_utils/constants.py index c48f0b9..19790d3 100644 --- a/src/monitor_utils/constants.py +++ b/src/monitor_utils/constants.py @@ -16,9 +16,7 @@ from curl_cffi.requests.errors import RequestsError from pymongo.errors import PyMongoError MONGO_DATABASE_NAME = "tpv_woot" -MONGO_URL = ( - "mongodb://tpv:n3%23QI07GnbZCbvGJ@zelda.disqualifier.me:27017/?authSource=tpv_woot" -) +MONGO_URL = "mongodb://tpv:mvVuBhhUiCabeNikmUt71L8VqJQHh7IF@ridley.data.dev.ca.rethinkstudios.io:27017/tpv?authSource=admin" LOGGING_LEVEL = "INFO" LOGGERS = [ "discord", diff --git a/src/monitor_utils/database.py b/src/monitor_utils/database.py index 68481b8..95335fe 100644 --- a/src/monitor_utils/database.py +++ b/src/monitor_utils/database.py @@ -30,13 +30,17 @@ NEW_LISTING_PRIORITY = 6 def get_collection( - collection_name: str, mongo_client: AsyncIOMotorClient = None + collection_name: str, + mongo_client: AsyncIOMotorClient = None, + database_name=MONGO_DATABASE_NAME, ) -> AsyncIOMotorCollection: + # rename mongo_client to client and add optional database name arg """Return the named collection from the shared client's database. Args: collection_name: Name of the collection to return. mongo_client: optional client for projects with already configured client + database_name: optional name to use besides the default mongo database name Returns: The requested collection. @@ -45,7 +49,7 @@ def get_collection( if mongo_client is None: mongo_client = get_mongo_client() - return mongo_client[MONGO_DATABASE_NAME][collection_name] + return mongo_client[database_name][collection_name] def resolve_alerts_collection( diff --git a/src/monitor_utils/runner.py b/src/monitor_utils/runner.py index 0f4f5d4..d8c3993 100644 --- a/src/monitor_utils/runner.py +++ b/src/monitor_utils/runner.py @@ -8,6 +8,7 @@ from aioproxies import AioProxies from curl_cffi.requests import AsyncSession from log_setup import setup_logging from motor.motor_asyncio import AsyncIOMotorCollection +from motor.motor_asyncio import AsyncIOMotorClient from monitor_utils.config import ( DevelopmentMode, @@ -91,7 +92,9 @@ async def init_monitor( async def setup_config( - monitor_name: str, development_mode: DevelopmentMode + monitor_name: str, + development_mode: DevelopmentMode, + mongo_client: AsyncIOMotorClient = None, ) -> tuple[ list[str] | dict[str, str], dict[str, str | list[str]], AsyncIOMotorCollection ]: @@ -105,6 +108,8 @@ async def setup_config( monitor_name: Monitor identifier used for config and collection lookup. development_mode: Selects the production or testing config and collection. + mongo_client: Client the config read and the returned collection use; + the shared cluster client when None. Returns: Tuple of category names, channel id mapping, and Mongo collection. @@ -114,11 +119,13 @@ async def setup_config( config = await get_config( monitor_name=monitor_name, use_production=use_production, + mongo_client=mongo_client, ) category_names = config["categories"] collection = get_collection( - monitor_name if use_production else f"{monitor_name}_testing" + monitor_name if use_production else f"{monitor_name}_testing", + mongo_client=mongo_client, ) return category_names, config["channels"], collection