Skip to content

Instantly share code, notes, and snippets.

@Zmey56
Last active January 24, 2023 21:20
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/03a7402597ef223c30b647558b8fdf40 to your computer and use it in GitHub Desktop.
Save Zmey56/03a7402597ef223c30b647558b8fdf40 to your computer and use it in GitHub Desktop.
def get_metric_alert(alert_df, metric, metric_alias, slice, group, day_threshhold=0.3):
alert_data = {}
is_alert = 0
day_0 = date.today().strftime("%Y-%m-%d")
day_1 = (date.today() - pd.DateOffset(days=1)).strftime("%Y-%m-%d")
day_7 = (date.today() - pd.DateOffset(days=7)).strftime("%Y-%m-%d")
day_0_value = alert_df[alert_df['date'] == day_0][metric].iloc[0]
day_1_value = alert_df[alert_df['date'] == day_1][metric].iloc[0]
day_7_value = alert_df[alert_df['date'] == day_7][metric].iloc[0]
if metric in ('users_feed', 'users_msg', 'likes', 'views', 'messages'):
day_1_value = day_1_value/3
day_7_value = day_7_value/3
if day_0_value <= day_7_value:
day_7_diff = abs(day_0_value / day_7_value - 1)
else:
day_7_diff = abs(day_7_value / day_0_value - 1)
if day_0_value <= day_1_value:
day_1_diff = abs(day_0_value / day_1_value - 1)
else:
day_1_diff = abs(day_1_value / day_0_value - 1)
alert_data['day_0_value'] = day_0_value
alert_data['day_1_value'] = day_1_value
alert_data['day_7_value'] = day_7_value
alert_data['day_1_diff'] = day_1_diff
alert_data['day_7_diff'] = day_7_diff
alert_data['is_alert'] = is_alert
q = '''SELECT max(time) as last_time FROM simulator.alerts_log
WHERE is_alert = 1 and slice = '{}' and metric = '{}' and group_level = '{}' '''.format(slice,
metric_alias,
group)
last_time = Getch(q).df
last_time = last_time['last_time'].iloc[0]
if last_time != '1970-01-01 03:00:00' and pd.Timedelta(pd.Timestamp('now') - pd.to_datetime(last_time)).seconds / 3600.0 < 3:
return alert_data
if (day_1_value <= day_0_value <= day_7_value) or (day_7_value <= day_0_value <= day_1_value):
return alert_data
if day_7_diff >= day_threshhold and day_1_diff >= day_threshhold:
is_alert = 1
alert_data['is_alert'] = is_alert
return alert_data
else:
return alert_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment