import datetime import logging from tadas.domain import constants from tadas.monitoring import sensors from tadas.snowflake import snowflake_publish logger = logging.getLogger(__name__) def get_report_date(): reports_dates = {} for table, df in snowflake_publish.load_final_tables_as_df().items(): report_dates = df['report_date'].unique().tolist() reports_dates[table] = report_dates assert len(report_dates) == 1, f'Table {table} should have single report_date. Actual: "{report_dates}"' assert len(reports_dates) == 2, f'Expected 2 reports, got {reports_dates}' assert reports_dates[constants.REPORT_COUNTRIES] == reports_dates[constants.REPORT_GLOBAL], ( f'Expected reports to be the same date, got {reports_dates}' ) return reports_dates[constants.REPORT_COUNTRIES][0] def run(): try: report_date = get_report_date() except AssertionError as e: logger.error(f'Failed to get report date: {e}') return logger.info(f'Current report date: {report_date}') report_timestamp = datetime.datetime.combine(report_date, datetime.time.max, tzinfo=datetime.timezone.utc) threshold = sensors.freshness_threshold() logger.info( f'Current report timestamp: {report_timestamp}, threshold: {report_timestamp + threshold}' ) sensors.run_freshness_check(name='tadas', report_date=report_timestamp, threshold=threshold)