-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
178 lines (160 loc) · 6.32 KB
/
db.py
File metadata and controls
178 lines (160 loc) · 6.32 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import yaml
import psycopg2
from psycopg2.extras import DictCursor
from datetime import datetime
from logger_config import logger
from constants import VALID_SUMMARY_INTERVALS
class Database:
def __init__(self, config_file, table_suffix=None):
self.config = yaml.safe_load(open(config_file))
self._table_suffix = self.config['db'][
'table_suffix'] if table_suffix is None else 'test'
self._create_tables()
def _get_db_connection(self):
connection = psycopg2.connect(host=self.config['db']['host'],
user=self.config['db']['user'],
password=self.config['db']['password'],
dbname=self.config['db']['database'],
port=self.config['db']['port'],
cursor_factory=DictCursor)
return connection
def _create_tables(self):
connection = self._get_db_connection()
try:
with connection.cursor() as cursor:
cursor.execute(f"""
CREATE TABLE IF NOT EXISTS activity_{self._table_suffix} (
id SERIAL PRIMARY KEY,
activity_type VARCHAR(50) NOT NULL,
user_id VARCHAR(50) NOT NULL,
station TEXT,
timestamp TIMESTAMP NOT NULL
)
""")
# subscriptions table
cursor.execute(f"""
CREATE TABLE IF NOT EXISTS subscriptions_{self._table_suffix} (
id SERIAL PRIMARY KEY,
station TEXT NOT NULL,
user_id BIGINT NOT NULL,
UNIQUE (station, user_id)
)
""")
connection.commit()
finally:
connection.close()
def add_subscription(self, station, user_id):
sql = f"""
INSERT INTO subscriptions_{self._table_suffix} (station, user_id)
VALUES (%s, %s)
ON CONFLICT (station, user_id) DO NOTHING
"""
values = (station, user_id)
self._execute_query_with_value(sql, values)
def remove_subscription(self, station, user_id):
sql = f"""
DELETE FROM subscriptions_{self._table_suffix}
WHERE station = %s AND user_id = %s
"""
values = (station, user_id)
self._execute_query_with_value(sql, values)
def get_subscriptions_by_user(self, user_id) -> list[str]:
sql = f"""
SELECT station
FROM subscriptions_{self._table_suffix}
WHERE user_id = %s
"""
subscriptions = self._select_with_values(sql, (user_id, ))
if subscriptions:
return sorted(
[subscription['station'] for subscription in subscriptions])
else:
return []
def stations_with_subscribers(self):
sql = f"""
SELECT DISTINCT station
FROM subscriptions_{self._table_suffix}
"""
stations = self._select(sql)
return sorted([station['station'] for station in stations])
def get_subscriptions_by_station(self, station) -> list[int]:
sql = f"""
SELECT user_id
FROM subscriptions_{self._table_suffix}
WHERE station = %s
"""
subscriptions = self._select_with_values(sql, (station, ))
if subscriptions:
return sorted(
[subscription['user_id'] for subscription in subscriptions])
else:
return []
def count_unique_subscribers(self) -> list[int]:
sql = f"""
SELECT DISTINCT user_id
FROM subscriptions_{self._table_suffix}
"""
subscribers = self._select(sql)
return len([subscriber['user_id'] for subscriber in subscribers])
def get_subscription_summary(self) -> list[str]:
stations = self.stations_with_subscribers()
summary = []
for station in stations:
summary.append(
f"{station}: {len(self.get_subscriptions_by_station(station))}"
)
return summary
def _select_with_values(self, sql, values):
connection = self._get_db_connection()
try:
with connection.cursor() as cursor:
cursor.execute(sql, values)
return cursor.fetchall()
except Exception as e:
logger.error(f"{e} with SQL: {sql} and values: {values}")
finally:
connection.close()
def log_activity(self, activity_type, user_id, station):
sql = f"""
INSERT INTO activity_{self._table_suffix} (activity_type, user_id, station, timestamp)
VALUES (%s, %s, %s, %s)
"""
values = (activity_type, user_id, station, datetime.now())
self._execute_query_with_value(sql, values)
def get_activity_summary(self, interval: str) -> list[str]:
if interval not in VALID_SUMMARY_INTERVALS:
raise ValueError(
f"Invalid interval: {interval}. Must be one of {VALID_SUMMARY_INTERVALS}"
)
sql = f"""
SELECT activity_type, COUNT(*) AS count
FROM activity_{self._table_suffix}
WHERE timestamp >= NOW() - INTERVAL '{interval}'
GROUP BY activity_type
ORDER BY count DESC
"""
results = self._select(sql)
formatted_results = [
f'{row["activity_type"]}: {row["count"]}' for row in results
]
return formatted_results
def _select(self, sql):
connection = self._get_db_connection()
try:
with connection.cursor() as cursor:
cursor.execute(sql)
return cursor.fetchall()
except Exception as e:
logger.error(f"{e} with SQL: {sql}")
finally:
connection.close()
def _execute_query_with_value(self, sql, values):
connection = self._get_db_connection()
try:
with connection.cursor() as cursor:
cursor.execute(sql, values)
connection.commit()
except Exception as e:
logger.error(f"{e} with SQL: {sql} and values: {values}")
finally:
connection.close()