-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql.py
67 lines (51 loc) · 2.07 KB
/
sql.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
import sqlite3
import datetime as dt
def convert_to_table_name(service_name):
table_name = service_name.replace('.', '_')
table_name = table_name.replace('-', '_')
return table_name
def get_latest_status(service_name, amount=12*12):
conn = sqlite3.connect('data/outage.sqlite3')
cursor = conn.cursor()
table_name = convert_to_table_name(service_name)
cursor.execute(f'''
SELECT * FROM {table_name} WHERE user = 0 ORDER BY timestamp DESC LIMIT {amount}
''')
rows = cursor.fetchall()
conn.close()
# convert the unix timestamp to human-readable time
time = [dt.datetime.fromtimestamp(row[0]).strftime('%H:%M:%S') for row in rows]
status = [row[1] for row in rows]
return time[::-1],status[::-1]
def get_latest_user_report(service_name, amount=100):
# will only take the latest 100 reports that happened in less than 24 hours
conn = sqlite3.connect('data/outage.sqlite3')
cursor = conn.cursor()
table_name = convert_to_table_name(service_name)
limit_unix = int((dt.datetime.now() - dt.timedelta(days=1)).timestamp())
cursor.execute(f'''
SELECT * FROM {table_name} WHERE user = 1 AND timestamp > {limit_unix} ORDER BY timestamp DESC LIMIT {amount}
''')
rows = cursor.fetchall()
conn.close()
time = [dt.datetime.fromtimestamp(row[0]).strftime('%H:%M:%S') for row in rows]
status = [row[1] for row in rows]
return time[::-1],status[::-1]
def get_percentage_uptime(service_name):
conn = sqlite3.connect('data/outage.sqlite3')
cursor = conn.cursor()
table_name = convert_to_table_name(service_name)
cursor.execute(f'''
SELECT COUNT(*) FROM {table_name} WHERE status = 1 AND user = 0
''')
up = cursor.fetchone()[0]
cursor.execute(f'''
SELECT COUNT(*) FROM {table_name} WHERE user = 0
''')
total = cursor.fetchone()[0]
conn.close()
return up/total
if __name__ == "__main__":
print(get_latest_status("ADE"))
print(get_latest_user_report("ADE"))
print(get_percentage_uptime("ADE"))