-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.py
127 lines (117 loc) · 4.06 KB
/
db.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import psycopg2
from config import DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST
from parse_message import extract_user_info
import pytz
import datetime
from prometheus_client import Counter
total_db_operations = Counter('total_db_operations', 'Count of total database ops occured')
def connect_to_database():
try:
conn = psycopg2.connect(
dbname=DATABASE_NAME,
user=DATABASE_USER,
password=DATABASE_PASSWORD,
host=DATABASE_HOST,
port=5432
)
print("Connection to the database was successful")
return conn
except psycopg2.Error as e:
print(f"Error connecting to the database: {e}")
return None
def save_log( message, discord_user_id, discord_message_id, sent_at, in_text_valid=-1):
try:
conn = connect_to_database()
cur = conn.cursor()
cur.execute(
"""
INSERT INTO participation_logs (
message, discord_user_id, discord_message_id, sent_at, in_text_valid
) VALUES (%s, %s, %s, %s, %s)
""",
(message, discord_user_id, discord_message_id, sent_at, in_text_valid)
)
conn.commit()
total_db_operations.inc()
except psycopg2.Error as e:
print(f"Error occurred while saving log: {e}")
conn.rollback() # Rollback the transaction if there is any error
finally:
cur.close()
conn.close()
def update_log(discord_message_id, message, in_text_valid, updated_at):
try:
conn = connect_to_database()
cur = conn.cursor()
cur.execute("""
UPDATE participation_logs
SET message = %s, in_text_valid = %s, updated_at = %s
WHERE discord_message_id = %s
""", (message, in_text_valid, updated_at, discord_message_id))
conn.commit()
total_db_operations.inc()
if cur.rowcount == 0:
conn.rollback() # Rollback the transaction if no rows were updated
print(f"No log found for message ID: {discord_message_id}")
return False
else:
return True
except Exception as e:
print(f"Error updating log: {e}")
conn.rollback()
finally:
cur.close()
conn.close()
def get_ist_time():
utc_now = datetime.datetime.now()
ist = pytz.timezone('Asia/Kolkata')
ist_now = utc_now.replace(tzinfo=pytz.utc).astimezone(ist)
return ist_now
def delete_log(discord_message_id):
try:
conn = connect_to_database()
cur = conn.cursor()
ist_time = get_ist_time()
cur.execute(
"""
UPDATE participation_logs
SET deleted_at = %s
WHERE discord_message_id = %s
""",
(ist_time, discord_message_id),
)
conn.commit()
print(f"Log marked as deleted for message ID: {discord_message_id}")
total_db_operations.inc()
except Exception as e:
print(f"Error updating log: {e}")
conn.rollback()
finally:
cur.close()
conn.close()
def check_intext_validity( message):
conn = connect_to_database()
cur = conn.cursor()
try:
college_id = extract_user_info(message)
if college_id:
cur.execute("SELECT name FROM student_list_2023 WHERE student_id = %s", (college_id.upper(),))
full_name = cur.fetchone()
total_db_operations.inc()
if full_name:
first_name = full_name[0].split()[0] # Assuming name is the first element of the tuple
if first_name.lower() in message.lower():
return 1
elif full_name[0].lower() in message.lower():
return 1
else:
print(f"No name found for student_id: {college_id}")
return 0
except Exception as e:
print(f"Error while checking intext validity: {e}")
return -1
finally:
cur.close()
conn.close()
if __name__ == "__main__":
print(check_intext_validity( ""))