-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducer.py
95 lines (75 loc) · 3.03 KB
/
reducer.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
import datetime
from database import BaseDatabase
from structs import Profile, Record
XP_SAFE_THRESHOLD = 500
class DatabaseReducer(BaseDatabase):
"""
Class used to reduce the number of date entries in the database.
"""
def reduce(self):
"""
Reduces the number of records in the database.
For each user, iterates over all their records and removes any row
with a timestamp that is less than two hours from the previous and
next timestamp.
"""
profile_query = self.session.query(Profile).all()
delete_count: int = 0
now = datetime.datetime.now()
for user in profile_query:
records = (self.session.query(Record)
.filter_by(id=user.id)
.order_by(Record.timestamp).all())
if len(records) <= 3:
continue
to_delete = set()
i = 1
prev_record = records[0]
while i < len(records) - 1:
curr_record = records[i]
next_record = records[i + 1]
# if any(record in to_delete for record in {prev_record, curr_record, next_record}):
# print("Skipping due to previous deletion")
# continue
timediff = next_record.timestamp - prev_record.timestamp
valdiff = next_record.value - prev_record.value
age = now - curr_record.timestamp
if valdiff < 0:
print(
f"user: {user.name}, prev: {prev_record.value}, curr: {curr_record.value}, next: {next_record.value}")
if (timediff < datetime.timedelta(hours=3)
and valdiff < XP_SAFE_THRESHOLD
and age > datetime.timedelta(days=30)):
to_delete.add(curr_record)
elif (timediff < datetime.timedelta(hours=6)
and valdiff < XP_SAFE_THRESHOLD
and age > datetime.timedelta(days=365)):
to_delete.add(curr_record)
elif (valdiff == 0
and timediff < datetime.timedelta(hours=12)
and age > datetime.timedelta(days=7)):
to_delete.add(curr_record)
else:
prev_record = curr_record
i += 1
delete_count += len(to_delete)
for record in to_delete:
self.session.delete(record)
return delete_count
if __name__ == '__main__':
dr = DatabaseReducer()
confirm = input('Do you want to run the reducer? (Y or n)\n')
# confirm = 'Y'
if confirm == 'Y':
count = dr.reduce()
if count > 0:
print(f'Deleting {count} records')
confirm = input('Are you really sure? (Y or n)\n')
if confirm == 'Y':
dr.commit()
else:
print('Aborted')
else:
print('0 records deleted')
else:
print('Aborted')