This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
forked from jaceksan/tiger-slack-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.py
76 lines (64 loc) · 2.4 KB
/
scheduler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# (C) 2021 GoodData Corporation
import uuid
from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
import os
from pathlib import Path
import csv
from tiger.metadata import Metadata
from tiger.report import Report
from tiger.slackclient import SlackClient
ENDPOINT = 'https://hackaton.anywhere.gooddata.com'
TOKEN = os.environ.get('TIGER_API_TOKEN')
WORKSPACE_ID = 'demo'
TARGET_CHANNEL_ID = 'C02M4PF2R8C'
THRESHOLD = 230000
DATA_SOURCE_ID = 'pg_staging-demo'
# Initialize a Web API client
slack_client = SlackClient()
metadata_client = Metadata(ENDPOINT, TOKEN)
metadata_client.workspace_id = WORKSPACE_ID
def alert():
print("Alert processing START. The time is: %s" % datetime.now())
done_file = '/tmp/scheduler_finished2.txt'
if Path(done_file).exists():
print('Alert already sent, do nothing')
return
report_client = Report(ENDPOINT, TOKEN, WORKSPACE_ID, metadata_client)
df = report_client.execute(
[{'id': 'metric/revenue', 'short_id': 'revenue', 'title': 'Revenue'}],
[{'id': 'label/customers.region', 'short_id': 'customers.region', 'title': 'Region'}]
)
print(f'Upload notification - invalid caches for DS {DATA_SOURCE_ID}')
metadata_client.invalid_caches(DATA_SOURCE_ID)
print(f'Execute report ...')
base_path = '/tmp/' + str(uuid.uuid4())
file_path = base_path + '.csv'
with open(file_path, 'wt') as fd:
df.to_csv(fd, index=False)
print(f'Evaluate alerts ...')
with open(file_path) as fp:
data = csv.reader(fp)
i = 1
for row in data:
if i > 1:
print(f'{float(row[1])} - {THRESHOLD} - {str(row)}')
if float(row[1]) > THRESHOLD:
msg = f'Region {row[0]} achieved {THRESHOLD} revenue, congratz!\n'
print(msg)
slack_client.send_markdown_message(
TARGET_CHANNEL_ID, [msg]
)
with open(done_file, 'w') as fp2:
fp2.write('1')
i += 1
print("Alert processing END. The time is: %s" % datetime.now())
if __name__ == "__main__":
scheduler = BlockingScheduler(timezone="Europe/Prague")
scheduler.add_job(alert, 'interval', seconds=30)
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
pass