-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
338 lines (269 loc) · 11.6 KB
/
database.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from datetime import datetime, timedelta
import pandas as pd
from sqlalchemy import create_engine, func, desc
from sqlalchemy.orm import sessionmaker
from structs import *
import os.path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_DIR = os.path.join(BASE_DIR, 'gwaff.db')
# logging.getLogger('sqlalchemy').disabled = True
# logging.getLogger('sqlalchemy.orm.mapper.Mapper').disabled = True
# logging.getLogger('sqlalchemy.engine.Engine').disabled = True
class BaseDatabase:
"""
Base class for database operations using SQLAlchemy.
"""
def __init__(self):
"""
Initializes the database engine and session.
"""
self.engine = create_engine(f'sqlite:///{DB_DIR}?charset=utf8mb4', echo=False)
Session = sessionmaker(bind=self.engine)
self.session = Session()
def commit(self):
"""
Commits the current transaction.
"""
self.session.commit()
def __del__(self):
"""
Closes the database session.
"""
self.session.close()
class DatabaseCreator(BaseDatabase):
"""
Class for creating and clearing the database.
"""
def clear_database(self):
"""
Drops all tables in the database.
"""
Profile.__table__.drop(self.engine, checkfirst=True)
Record.__table__.drop(self.engine, checkfirst=True)
Minecraft.__table__.drop(self.engine, checkfirst=True)
Event.__table__.drop(self.engine, checkfirst=True)
self.session.commit()
def create_database(self):
"""
Creates all tables in the database.
"""
Base.metadata.create_all(self.engine)
self.session.commit()
class DatabaseReader(BaseDatabase):
"""
Class for reading data from the database.
"""
def get_dates_in_range(self, start_date=None, end_date=None):
"""
Retrieves distinct timestamps within a specified date range.
Args:
start_date (datetime, optional): The start date of the range.
end_date (datetime, optional): The end date of the range.
Returns:
list: A list of timestamps within the specified range.
"""
query_result = self.session.query(Record.timestamp).distinct()
if start_date and end_date:
query_result = query_result.filter(Record.timestamp.between(start_date, end_date))
elif start_date:
query_result = query_result.filter(Record.timestamp >= start_date)
return [i.timestamp for i in query_result.order_by(Record.timestamp).all()]
def get_row(self, id: int, start_date: datetime = None, end_date: datetime = None) -> list:
"""
Retrieves records for a specific ID, optionally filtering by start date.
Args:
id (int): The ID of the record.
start_date (datetime, optional): The start date for filtering records.
end_date (datetime, optional): The end date for filtering records.
Returns:
list: A list of records for the specified ID.
"""
if id in {483515866319945728} and start_date < datetime(2024, 6, 1, 0, 0):
if end_date is None:
return []
if datetime(2024, 6, 1, 0, 0) < end_date:
return []
record_query = (self.session.query(Record)
.filter_by(id=id)
.order_by(Record.timestamp))
if start_date:
record_query = record_query.filter(Record.timestamp >= start_date)
if end_date:
record_query = record_query.filter(Record.timestamp <= end_date)
# Execute the query for records
return record_query.all()
def get_data_in_range(self, start_date: datetime = None, end_date: datetime = None,
limit: int = 15, include: set[int] = None) -> list[tuple]:
"""
Retrieves profile data and associated records within a specified date range.
Args:
start_date (datetime, optional): The start date for filtering records.
end_date (datetime, optional): The end date for filtering records.
limit (int, optional): The maximum number of profiles to retrieve. Defaults to 15.
include (set, optional): A list of profile IDs to include. Defaults to None.
Returns:
list: A list of tuples containing profile data and associated records.
"""
profile_query = (self.session.query(Profile)
.join(Record, Profile.id == Record.id)
.group_by(Profile.id)
.order_by(desc(func.max(Record.value))))
if include and hasattr(include, '__iter__'):
profile_query = profile_query.filter(Profile.id.in_(include))
if limit:
profile_query = profile_query.limit(limit)
result = []
for profile in profile_query:
# Query for records associated with the profile, optionally filtering by start_date
records = self.get_row(int(profile.id), start_date, end_date)
# Append the data to the result list
result.append((
(profile.id, profile.name, profile.colour, profile.avatar),
[record.timestamp for record in records],
[record.value for record in records]
))
return result
def get_growth_in_range(self, start_date: datetime = None, end_date: datetime = None,
limit: int = 15, include: set[int] = None) -> list[tuple]:
"""
Retrieves profile data and growth within a specified date range.
Args:
start_date (datetime, optional): The start date for filtering records.
end_date (datetime, optional): The end date for filtering records.
limit (int, optional): The maximum number of profiles to retrieve. Defaults to 15.
include (set, optional): A list of profile IDs to include. Defaults to None.
Returns:
list: A list of tuples containing profile data and growth values.
"""
profile_query = (self.session.query(Profile)
.join(Record, Profile.id == Record.id))
if start_date:
profile_query = profile_query.filter(Record.timestamp >= start_date)
if end_date:
profile_query = profile_query.filter(Record.timestamp <= end_date)
profile_query = (profile_query.group_by(Profile.id)
.order_by(desc(func.max(Record.value) - func.min(Record.value))))
if include and hasattr(include, '__iter__'):
profile_query = profile_query.filter(Profile.id.in_(include))
if limit:
profile_query = profile_query.limit(limit)
result = []
for profile in profile_query:
# Query for records associated with the profile, optionally filtering by start_date
records = self.get_row(profile.id, start_date, end_date)
# Append the data to the result list
result.append((
(profile.id, profile.name, profile.colour, profile.avatar),
[record.timestamp for record in records],
[record.value - records[0].value for record in records]
))
return result
def get_last_timestamp(self):
"""
Retrieves the most recent timestamp from the records.
Returns:
datetime: The most recent timestamp.
"""
return self.session.query(func.max(Record.timestamp)).first()[0]
def get_last_record(self):
"""
Retrieves the last record for each profile.
Returns:
list: A list of tuples containing profile data and the maximum record value.
"""
profile_query = (self.session.query(Profile, func.max(Record.value))
.join(Record, Profile.id == Record.id)
.group_by(Profile.id)
.order_by(desc(func.max(Record.value))).all())
# return [(i, i.records[-1]) for i in profile_query]
return profile_query
def get_profile_data(self, id=None):
"""
Retrieves profile data for a specific ID or all profiles if no ID is provided.
Args:
id (int, optional): The ID of the profile. Defaults to None.
Returns:
list or Profile: A list of all profiles or a specific profile.
"""
if id is None:
return self.session.query(Profile).all()
return self.session.query(Profile).filter_by(id=id).first() or None
class DatabaseSaver(BaseDatabase):
"""
Class for saving data to the database.
"""
def update_profile(self, id, name: str = None, colour: str = None, avatar: str = None) -> None:
"""
Updates or creates a profile in the database.
Args:
id (int): The ID of the profile.
name (str, optional): The name of the profile. Defaults to None.
colour (str, optional): The colour of the profile. Defaults to None.
avatar (str, optional): The avatar of the profile. Defaults to None.
"""
profile = self.session.query(Profile).filter_by(id=id).first()
if profile is None:
new_profile = Profile(id=id, name=name,
colour=colour, avatar=avatar)
self.session.add(new_profile)
return
profile.name = name or profile.name
profile.colour = colour or profile.colour
profile.avatar = avatar or profile.avatar
def insert_record(self, id: int, timestamp: datetime, value: int) -> None:
"""
Inserts a new record into the database.
Args:
id (int): The ID of the profile.
timestamp (datetime): The timestamp of the record.
value (int): The xp value at the timestamp.
Raises:
ValueError: If id, timestamp, or value is None.
"""
if id is None or timestamp is None or value is None:
raise ValueError('id, timestamp, and value are required')
new_record = Record(id=id, timestamp=timestamp, value=value)
self.session.add(new_record)
def load_from_csv(self, data):
"""
Loads data from a CSV file into the database.
Args:
data (DataFrame): The data to load from the CSV file.
"""
dates = data.columns
dates = list(dates)[4:]
for index, row in data.iterrows():
self.update_profile(*row.iloc[0:4])
for i in dates:
date = datetime.fromisoformat(i)
if row[i] is not None and not pd.isna(row[i]):
self.insert_record(row.iloc[0], date, int(row[i]))
self.commit()
if __name__ == '__main__':
# dbc = DatabaseCreator()
dbr = DatabaseReader()
# dbi = DatabaseSaver()
# dbc.clear_database()
# dbc.create_database()
# dbi.load_from_csv(pd.read_csv("gwaff.csv", index_col=0))
print(dbr.get_profile_data())
print(dbr.get_profile_data()[0].__dict__)
print(dbr.get_profile_data(92029863090728960).__dict__)
print(dbr.get_last_timestamp())
print(len(dbr.get_last_record()))
for p, v in dbr.get_last_record():
print(p.name, v)
# print(dbr.get_data_in_range(limit=1))
threshold = datetime.now() - timedelta(days=7)
# dates = dbr.get_dates_in_range(threshold)
# print(len(dates))
# for i in dates:
# print(i)
for i in dbr.get_data_in_range(threshold, limit=1):
print(i)
# for i in dbr.get_growth_in_range():
# print(i)
# print(i[0], i[2][-1])
# print(len(i[1]))
# for i in dbr.get_profile_data():
# print(i.__dict__)