-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_outdated_keys.py
61 lines (48 loc) · 1.43 KB
/
remove_outdated_keys.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
from collections import namedtuple
import delphi.operations.secrets as secrets
import mysql.connector
API_USER_RECORD = namedtuple("APIUser", ("api_key", "email", "date_diff"))
def get_outdated_keys(cur):
cur.execute(
"""
SELECT
diff.api_key,
diff.email,
diff.date_diff
FROM (
SELECT
api_key,
email,
created,
last_time_used,
ABS(TIMESTAMPDIFF(MONTH, last_time_used, created)) as date_diff
FROM api_user
) diff
WHERE diff.date_diff >= 5;
"""
)
outdated_keys = cur.fetchall()
return outdated_keys
def remove_outdated_key(cur, api_key):
cur.execute(
f"""
DELETE FROM api_user WHERE api_key = "{api_key}"
"""
)
def send_notification(api_key, email):
pass
def main():
u, p = secrets.db.epi
cnx = mysql.connector.connect(database="epidata", user=u, password=p, host=secrets.db.host)
cur = cnx.cursor()
outdated_keys_list = [API_USER_RECORD(*item) for item in get_outdated_keys(cur)]
for item in outdated_keys_list:
if item.date_diff == 5:
send_notification(item.email)
else:
remove_outdated_key(cur, item.api_key)
cur.close()
cnx.commit()
cnx.close()
if __name__ == "__main__":
main()