Skip to content

Instantly share code, notes, and snippets.

@Zmey56
Last active January 24, 2023 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zmey56/e938c56218731b9c2e19d08a7beb475a to your computer and use it in GitHub Desktop.
Save Zmey56/e938c56218731b9c2e19d08a7beb475a to your computer and use it in GitHub Desktop.
def get_alert(alert_data, time, chat):
chat_id = chat
metrics_list, group_by_slices_list, where_expression_template, alert_plots, responsible_users = get_alert_info()
mertics = alert_data['mertics']
slice = alert_data['slice']
query = '''SELECT {select_vars}
FROM {from_table_name}
WHERE {where_expression}
GROUP BY {group_by_expression}'''
select_vars = get_select_vars(mertics, metrics_list, group_by_slices_list, slice)
from_table_name = get_from_table_name(mertics, metrics_list)
where_expression = get_where_expression(where_expression_template, group_by_slices_list, time, slice)
group_by_expression = get_group_by_expression(slice, group_by_slices_list)
alert_query = query.format(select_vars=select_vars,
from_table_name=from_table_name,
where_expression=where_expression,
group_by_expression=group_by_expression)
all_data = Getch(alert_query).df
data_to_alerts = {}
if slice != 'total':
group_levels = group_by_slices_list[slice]['group_levels']
alias = group_by_slices_list[slice]['alias']
for group_level in group_levels:
alert_df = all_data[all_data[alias] == group_level].copy()
data_to_alerts[group_level] = {'df': alert_df, 'slice': slice, 'group_level': group_level}
else:
data_to_alerts['total'] = {'df': all_data, 'slice': 'total', 'group_level': 'total'}
for group in data_to_alerts:
alert_df = data_to_alerts[group]['df']
slice_name = data_to_alerts[group]['slice']
for metric in mertics:
metric_alias = metrics_list[metric]['alias']
metric_name = metrics_list[metric]['metric_name']
alert_data = get_metric_alert(alert_df, metric, metric_alias, slice, group, day_threshhold=0.5)
alert_data['metric'] = metric
alert_data['metric_name'] = metric_name
alert_data['slice'] = slice
alert_data['group'] = group
alert_data['group_level'] = group
alert_data['time'] = time
alerts_log_df = pd.DataFrame(alert_data, index=[0]).fillna(0)
alerts_log_df = alerts_log_df[['time', 'day_0_value', 'day_1_value', 'day_7_value',
'day_1_diff', 'day_7_diff', 'slice', 'group_level',
'metric', 'metric_name', 'is_alert']]
Insertch(alerts_log_df, 'alerts_log').insert
if not alert_data['is_alert']:
continue
select_vars = get_select_vars(mertics, metrics_list, group_by_slices_list, slice, True)
from_table_name = get_from_table_name(mertics, metrics_list)
where_expression = get_where_expression(where_expression_template, group_by_slices_list, time, slice, True)
group_by_expression = get_group_by_expression(slice, group_by_slices_list, True)
plot_df_query = query.format(select_vars=select_vars,
from_table_name=from_table_name,
where_expression=where_expression,
group_by_expression=group_by_expression)
plot_df = Getch(plot_df_query).df
if slice != 'total':
plot_df = plot_df.query("{slice} == '{group}'".format(group=group, slice=slice))
plot_df['time'] = pd.to_datetime(plot_df['time'])
plot_df = plot_df[plot_df['time'] <= time]
alert_plot = alert_plots[slice][metric]
send_alert(plot_df, group, metric_name, metric_alias, slice_name, alert_plot, responsible_users, chat=chat_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment