Last active
January 24, 2023 21:17
-
-
Save Zmey56/a587d245960ae877823aa50d7a4fa721 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def send_alert(plot_df, group, metric_name, metric_alias, slice_name, alert_plot, responsible_users, chat): | |
bot = telegram.Bot(token=os.environ.get('REPORT_BOT_TOKEN')) | |
dahsboard_link = 'https://superset.lab.karpov.courses/superset/dashboard/52/' | |
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") | |
sns.set(rc={'figure.figsize': (16, 10)}) | |
sns.set(font_scale=2) | |
sns.set_style("white") | |
plt.tight_layout() | |
ax = sns.lineplot(data=plot_df[plot_df['date'].isin([day_0, day_1, day_7])].sort_values(['date', 'time_']), | |
x='time_', y=metric_alias, | |
hue='date', | |
hue_order=[day_7, day_1, day_0], | |
style='date', | |
style_order=[day_0, day_1, day_7], | |
size='date', | |
sizes=[4, 3, 3], | |
size_order=[day_0, day_1, day_7]) | |
ax.legend([day_7, day_1, day_0]) | |
for ind, label in enumerate(ax.get_xticklabels()): | |
if ind % 15 == 0: | |
label.set_visible(True) | |
else: | |
label.set_visible(False) | |
ax.set(xlabel='time') | |
ax.set(ylabel=metric_name) | |
ax.set_title('{}\n{}'.format(group, metric_name)) | |
ax.set(ylim=(0, None)) | |
plot_object = io.BytesIO() | |
plt.savefig(plot_object) | |
plot_object.name = '{}.png'.format(metric_name) | |
plot_object.seek(0) | |
plt.close() | |
if slice_name == 'total': | |
text = '''The problem with the metric {} is a strong deviation from yesterday/a week ago!\alert schedule {}\Dashboard {}'''.format(metric_name, alert_plot, dahsboard_link) | |
else: | |
text = '''The problem with the metric {} is a strong deviation from yesterday/a week ago\in the slice {} - {}\alert schedule {}\dashboard {}'''.format(metric_name, slice_name, group, alert_plot, dahsboard_link) | |
responsible_users_metric = responsible_users['metrics'].get(metric_alias) | |
if responsible_users_metric: | |
text = text + '''\n\n{} please see if everything is ok?'''.format(responsible_users_metric) | |
responsible_users_group = responsible_users['groups'].get(slice_name) | |
if responsible_users_group: | |
text = text + '''\n\n{} please see if everything is ok?'''.format(responsible_users_group) | |
bot.sendMessage(chat_id=chat, text=text) | |
bot.sendPhoto(chat_id=chat, photo=plot_object) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment