things kinda work

This commit is contained in:
Pablo Martin 2024-08-13 15:02:03 +02:00
parent c38ce5cfe6
commit 7eb697fecd
10 changed files with 342 additions and 29 deletions

View file

@ -1,7 +1,8 @@
import logging
import datetime
import pathlib
from typing import Dict
from anaxi.constants import PATHS
from anaxi.file_persistence import read_yaml
from anaxi.logging import get_anaxi_logger
@ -12,7 +13,9 @@ class CosmosDBDatabaseConfig:
config_root_key: str = "cosmos-databases"
def __init__(self, host: str, database_id: str, master_key: str) -> None:
def __init__(
self, host: str, database_id: str, master_key: str
) -> "CosmosDBDatabaseConfig":
self.host = host
self.database_id = database_id
self.master_key = master_key
@ -45,7 +48,9 @@ class PostgresDatabaseConfig:
config_root_key: str = "postgres-databases"
def __init__(self, host: str, port: int, dbname: str, user: str, password) -> None:
def __init__(
self, host: str, port: int, dbname: str, user: str, password
) -> "PostgresDatabaseConfig":
self.host = host
self.port = port
self.dbname = dbname
@ -74,3 +79,92 @@ class PostgresDatabaseConfig:
raise e
return database_configs
class StreamConfig:
config_root_key: str = "streams"
def __init__(
self,
stream_name: str,
cosmos_database_id: str,
cosmos_container_name: str,
cutoff_timestamp: datetime.datetime,
postgres_database: str,
postgres_schema_name: str,
) -> "StreamConfig":
self.stream_name = stream_name
self.cosmos_database_id = cosmos_database_id
self.cosmos_container_name = cosmos_container_name
self.cutoff_timestamp = cutoff_timestamp
self.postgres_database = postgres_database
self.postgres_schema_name = postgres_schema_name
@classmethod
def from_dict(cls, a_dict) -> "StreamConfig":
return StreamConfig(**a_dict)
@classmethod
def from_yaml(cls, path: pathlib.Path) -> Dict[str, "StreamConfig"]:
yaml_contents = read_yaml(path)
streams_contents = yaml_contents[StreamConfig.config_root_key]
try:
streams_configs = {
stream_id: StreamConfig.from_dict(stream_config)
for stream_id, stream_config in streams_contents.items()
}
except KeyError as e:
logger.error(
"Error reading streams config yaml file. The file seems malformed. Review the example in the repository."
)
raise e
return streams_configs
def get_postgres_database_config_from_file(postgres_database):
logger.info("Reading Postgres config file...")
postgres_configs = PostgresDatabaseConfig.from_yaml(PATHS.postgres_config_file_path)
logger.info(f"Found file with {len(postgres_configs)} entries.")
try:
relevant_postgres_config = postgres_configs[postgres_database]
except KeyError as e:
logger.error(
f"Couldn't find a config entry for database with id: {postgres_database}"
)
raise e
return relevant_postgres_config
def get_cosmos_database_config_from_file(cosmos_db_id):
logger.info("Reading Cosmos DB config file...")
cosmos_db_configs = CosmosDBDatabaseConfig.from_yaml(
PATHS.cosmos_db_config_file_path
)
logger.info(f"Found file with {len(cosmos_db_configs)} entries.")
try:
relevant_cosmos_db_config = cosmos_db_configs[cosmos_db_id]
except KeyError as e:
logger.error(
f"Couldn't find a config entry for database with id: {cosmos_db_id}"
)
raise e
return relevant_cosmos_db_config
def get_stream_config_from_file(stream_id):
logger.info("Reading streams config file...")
streams_configs = StreamConfig.from_yaml(PATHS.streams_config_file_path)
logger.info(f"Found file with {len(streams_configs)} entries.")
try:
stream_config = streams_configs[stream_id]
except KeyError:
logger.error(
f"Could not find the stream {stream_id} in the stream configurations file. You might be missing an entry in {PATHS.streams_config_file_path} or have made a typo."
)
return stream_config