def alert_for_schema_change(db, check, conf): df = get_last_results(db, check, Metric.TABLE_METRIC, Metric.SCHEMA_CHANGE, conf, days=1) for index, row in df.iterrows(): changes = json.loads(row[0]) if changes["operation"] == "table detected": continue alert = Alert( text=f""" schema change detected - {changes['operation']}: {changes['column_name']} """, severity=2, table_id=check.table_id, alert_type=check.name, created_at=conf.for_time, ) metrics_session.add(alert) metrics_session.commit()
def volume_alert(db, table, conf): sql_df = get_last_results(db, table, 'metrics_data_volume', conf) filtered = sql_df[sql_df['time_interval'] == settings.INTERVAL_FOR_ALERTS] checked_txt = f'volume in interval: {settings.INTERVAL_FOR_ALERTS}' alert_on_z_score(filtered, table, 'count', 'volume_alert', checked_txt, conf)
def delay_alert(db): tables = MonitoredTable.get_monitored_tables(db.name) for table in tables: sql_df = get_last_results(db, table, 'metrics_data_delay') checked_txt = f'delay since last data' alert_on_z_score(sql_df, table, 'value', 'delay_alert', checked_txt)
def volume_alert(db): tables = MonitoredTable.get_monitored_tables(db.name) for table in tables: sql_df = get_last_results(db, table, 'metrics_data_volume') for interval in settings.VOLUME_INTERVAL: filtered = sql_df[sql_df['time_interval'] == interval] checked_txt = f'volume in interval: {interval}' alert_on_z_score(filtered, table, 'count', 'volume_alert', checked_txt)
def alert(db, check, conf): if check.name == Metric.SCHEMA_CHANGE: alert_for_schema_change(db, check, conf) else: metrics = check.metrics for column, metrics_for_col in metrics.items(): for metric in metrics_for_col: sql_df = get_last_results(db, check, column, metric, conf) sql_df["result"] = pd.to_numeric(sql_df["result"]) alert = column + "_" + metric checked_txt = alert + " is failing" alert_on_z_score(sql_df, check, alert, checked_txt, conf)
def alert(db, check, conf): if check.name == Metric.SCHEMA_CHANGE: alert_for_schema_change(db, check, conf) else: metrics = check.metrics for column, metrics_for_col in metrics.items(): for metric in metrics_for_col: sql_df = get_last_results(db, check, column, metric, conf) sql_df["result"] = pd.to_numeric(sql_df["result"]) if column == Metric.TABLE_METRIC: alert = metric else: alert = name_for(column, metric) checked_txt = alert alert_on_z_score(sql_df, check, alert, checked_txt, conf)
def values_alert(db): tables = MonitoredTable.get_monitored_tables(db.name) for table in tables: sql_df = get_last_results(db, table, 'metrics_data_values') checks_df = sql_df[['check_name', 'time_interval', 'column_name']].drop_duplicates() for i, row in checks_df.iterrows(): df = sql_df[(sql_df['check_name'] == row['check_name']) & (sql_df['time_interval'] == row['time_interval']) & (sql_df['column_name'] == row['column_name'])] check_text = f'values for {row.check_name} in column: {row.column_name}, interval: {row.time_interval}' alert_on_z_score(df, table, 'check_value', row['check_name'], check_text)
def values_alert(db, table, conf): sql_df = get_last_results(db, table, 'metrics_data_values', conf) sql_df = sql_df[sql_df['time_interval'] == settings.INTERVAL_FOR_ALERTS] checks_df = sql_df[['check_name', 'column_name', 'column_value']].drop_duplicates() for i, row in checks_df.iterrows(): df = sql_df[(sql_df['check_name'] == row['check_name']) & (sql_df['column_name'] == row['column_name'])] if row['column_value']: df = df[df['column_value'] == row['column_value']] check_text = f'column: {row.column_name}, values for {row.check_name}' alert_on_z_score(df, table, 'check_value', row['check_name'], check_text, conf)
def delay_alert(db, table, conf): sql_df = get_last_results(db, table, 'metrics_data_delay', conf) checked_txt = f'delay since last data' alert_on_z_score(sql_df, table, 'value', 'delay_alert', checked_txt, conf)