64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
"""Config loading and mode helpers shared across monitors and the bot."""
|
|
|
|
import logging
|
|
from enum import Enum
|
|
|
|
from monitor_utils.constants import LOGGERS
|
|
from monitor_utils.database import get_collection
|
|
|
|
CONFIGS_COLLECTION_NAME = "configs"
|
|
|
|
|
|
class ConfigNotFoundError(Exception):
|
|
"""Raised when a monitor has no config document in Mongo."""
|
|
|
|
|
|
class DevelopmentMode(str, Enum):
|
|
"""Runtime mode selecting production or development behavior."""
|
|
|
|
PRODUCTION = "PRODUCTION"
|
|
DEVELOPMENT = "DEVELOPMENT"
|
|
|
|
|
|
def is_production(development_mode: DevelopmentMode) -> bool:
|
|
"""Return True when the mode is PRODUCTION."""
|
|
|
|
return development_mode == DevelopmentMode.PRODUCTION
|
|
|
|
|
|
async def get_config(monitor_name: str, use_production: bool = False) -> dict:
|
|
"""Load a monitor's config document from Mongo.
|
|
|
|
Reads the configs collection in production and configs_testing in
|
|
development, so a dev run never reads production routing.
|
|
|
|
Args:
|
|
monitor_name: Monitor identifier (the config document _id).
|
|
use_production: Read the production configs collection instead of
|
|
configs_testing.
|
|
|
|
Returns:
|
|
The config document, including its categories and channels.
|
|
|
|
Raises:
|
|
ConfigNotFoundError: When no document exists for the monitor.
|
|
"""
|
|
|
|
collection_name = (
|
|
CONFIGS_COLLECTION_NAME
|
|
if use_production
|
|
else f"{CONFIGS_COLLECTION_NAME}_testing"
|
|
)
|
|
config = await get_collection(collection_name).find_one({"_id": monitor_name})
|
|
if config is None:
|
|
raise ConfigNotFoundError(monitor_name)
|
|
|
|
return config
|
|
|
|
|
|
def set_logger_levels(loggers: list[str] = LOGGERS, level: int = logging.WARNING):
|
|
"""Set the given loggers to the given level to quiet noisy libraries."""
|
|
|
|
for name in loggers:
|
|
logging.getLogger(name).setLevel(level)
|