-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathactivities_db.py
403 lines (290 loc) · 11.6 KB
/
activities_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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# Copyright (C) 2023 David Mossakowski
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import json
import sqlite3 as lite
import uuid
import copy
from threading import RLock
import csv
import skala_db
import logging
import os
sql_lock = RLock()
DATA_DIRECTORY = os.getenv('DATA_DIRECTORY')
if DATA_DIRECTORY is None:
DATA_DIRECTORY = os.getcwd()
#PLAYLISTS_DB = DATA_DIRECTORY + "/db/playlists.sqlite"
COMPETITIONS_DB = DATA_DIRECTORY + "/db/competitions.sqlite"
activities_TABLE = "activities"
route_finish_status = {0: "attempt", 1: "flash", 2: "redpoint", 3: "toprope"}
def init():
logging.info('initializing skala_activity...')
if os.path.exists(DATA_DIRECTORY) and os.path.exists(COMPETITIONS_DB):
db = lite.connect(COMPETITIONS_DB)
# ptype 0-public
cursor = db.cursor()
cursor.execute('''CREATE TABLE if not exists ''' + activities_TABLE + '''(
id text NOT NULL UNIQUE,
user_id text NOT NULL,
gym_id text NOT NULL,
routes_id text NOT NULL,
added_at DATETIME DEFAULT CURRENT_TIMESTAMP not null,
jsondata json NOT NULL
)''')
db.commit()
print('created ' + activities_TABLE)
def add_activity(user, gym, routesid, name, date):
activity_id = str(uuid.uuid4().hex)
gym_id = gym.get('id')
#routes_id = gym.get('routesid')
activity = {"id": activity_id, "gym_id": gym_id, "routes_id": routesid, "starttime": date, "name": name,
"gym_name": gym.get('name'),
"routes": []
}
# write this competition to db
_add_activity(activity_id, user.get('id'), gym_id, routesid, date, activity)
return activity_id
def get_activity(session_id):
return _get_activity(session_id)
def get_activities(user_id):
return _get_activities_by_user_id(user_id)
def get_activities_by_date_by_user(date, user_id):
return _get_activities_by_date_by_user_id(date, user_id)
def get_activities_by_gym_routes(gym_id, routes_id):
try:
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
result = cursor.execute("SELECT jsondata FROM " + activities_TABLE + " WHERE gym_id = ? AND routes_id = ?",
[str(gym_id), str(routes_id)])
activities = []
if result is not None and result.arraysize > 0:
for row in result.fetchall():
activities.append(json.loads(row[0]))
return activities
finally:
db.close()
# add an entry to an existing session
def add_activity_entry(activity_id, route, status, note, user_grade):
entry_id = str(uuid.uuid4().hex)
route_id = route.get('id')
grade = route.get('grade')
activity = get_activity(activity_id)
session_entry = {"id": entry_id, "route_id": route_id, "status": status, "note": note,
"grade": grade, "user_grade": user_grade,
}
session_entry = {**route, **session_entry}
activity.get('routes').append(session_entry)
# write this competition to db
_update_activity(activity_id, activity.get("user_id"), activity.get("gym_id"), activity.get("routes_id"), activity);
return activity
def update_activity(activity_id, activity_json):
if activity_json is None or activity_id is None:
return None
# write this competition to db
_update_activity_jsondata(activity_id, activity_json)
def delete_activity(activity_id):
activity = get_activity(activity_id)
if activity is None:
return None
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
cursor.execute("delete from " + activities_TABLE + " where id =? ",
[str(activity_id)])
finally:
db.commit()
db.close()
sql_lock.release()
logging.info("deleted activity for user:"+str(activity_id))
return activity
def delete_activity_route(activity_id, entry_id):
activity = get_activity(activity_id)
if activity is None:
return None
for route_index, route in enumerate(activity['routes']):
if route['id'] == entry_id:
activity['routes'].pop(int(route_index))
_update_activity(activity_id, activity.get("user_id"), activity.get("gym_id"), activity.get("routes_id"), activity);
return activity
def get_activities_by_routes_id(routes_id):
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
# Query to retrieve activities that contain the given route_id
cursor.execute(f"SELECT jsondata FROM {activities_TABLE} WHERE routes_id = ?", [str(routes_id)])
rows = cursor.fetchall()
matching_entries = []
for row in rows:
activity = json.loads(row[0])
for session_entry in activity.get('routes', []):
matching_entries.append(session_entry)
return matching_entries
finally:
db.close()
sql_lock.release()
def get_activity_routes_by_gym_id(gym_id):
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
# Query to retrieve activities that contain the given route_id
cursor.execute(f"SELECT jsondata FROM {activities_TABLE} WHERE gym_id = ?", [str(gym_id)])
rows = cursor.fetchall()
matching_entries = []
for row in rows:
activity = json.loads(row[0])
for session_entry in activity.get('routes', []):
matching_entries.append(session_entry)
return matching_entries
finally:
db.close()
sql_lock.release()
def get_activities_by_gym_id(gym_id):
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
# Query to retrieve activities that contain the given route_id
cursor.execute(f"SELECT jsondata FROM {activities_TABLE} WHERE gym_id = ?", [str(gym_id)])
rows = cursor.fetchall()
matching_entries = []
for row in rows:
activity = json.loads(row[0])
matching_entries.append(activity)
return matching_entries
finally:
db.close()
sql_lock.release()
def get_activities_all_anonymous():
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
# Query to retrieve activities that contain the given route_id
cursor.execute(f"SELECT jsondata FROM {activities_TABLE} order by added_at desc")
rows = cursor.fetchall()
matching_entries = []
for row in rows:
activity = json.loads(row[0])
activity.pop('user_id')
activity.pop('name')
matching_entries.append(activity)
return matching_entries
finally:
db.close()
sql_lock.release()
def _add_activity(activity_id, user_id, gym_id, routes_id, date, jsondata):
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
jsondata['id']=activity_id
jsondata['user_id']=user_id
jsondata['gym_id']=gym_id
jsondata['routes_id']=routes_id
jsondata['date']=date
cursor.execute("INSERT INTO " + activities_TABLE + " (id, user_id, gym_id, routes_id, added_at, jsondata ) "
"values (?, ?, ?, ?, ?, ?)",
[str(activity_id), str(user_id), str(gym_id), str(routes_id), date, json.dumps(jsondata)])
finally:
db.commit()
db.close()
sql_lock.release()
logging.info("added climbing session for user:"+str(user_id))
def _update_activity_jsondata(activity_id, new_jsondata):
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
# Convert new_jsondata to a JSON string
new_jsondata_str = json.dumps(new_jsondata)
cursor.execute(f"UPDATE {activities_TABLE} SET jsondata = ? WHERE id = ?", (new_jsondata_str, str(activity_id)))
finally:
db.commit()
db.close()
sql_lock.release()
logging.info(f"Updated jsondata for activity: {activity_id}")
def _get_activity(activity_id):
try:
#sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
result = cursor.execute("select jsondata from " + activities_TABLE + " where id =? ",
[str(activity_id)])
result = result.fetchone()
if result is None or result[0] is None:
return None
if result[0] is not None:
return json.loads(result[0])
else:
return None
finally:
db.commit()
db.close()
#sql_lock.release()
#logging.info("retrieved climbing session for user:"+str(session_id))
def _get_activities_by_user_id(user_id):
try:
#sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
result = cursor.execute("select jsondata from " + activities_TABLE + " where user_id =? order by added_at desc",
[str(user_id)])
#result = result.fetchall()
activities=[]
if result is not None and result.arraysize > 0:
for row in result.fetchall():
# comp = row[0]
activities.append(json.loads(row[0]))
# gyms[gym['id']] = gym
return activities
finally:
db.commit()
db.close()
#sql_lock.release()
#logging.info("retrieved climbing session for user:"+str(session_id))
def _get_activities_by_date_by_user_id(date, user_id):
try:
#sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
result = cursor.execute("select jsondata from " + activities_TABLE + " where user_id =? and added_at =? ",
(str(user_id), date))
#result = result.fetchall()
activities=[]
if result is not None and result.arraysize > 0:
for row in result.fetchall():
# comp = row[0]
activities.append(json.loads(row[0]))
# gyms[gym['id']] = gym
return activities
finally:
db.commit()
db.close()
#sql_lock.release()
#logging.info("retrieved climbing session for user:"+str(session_id))
def _update_activity(activity_id, user_id, gym_id, routes_id, jsondata):
try:
sql_lock.acquire()
db = lite.connect(COMPETITIONS_DB)
cursor = db.cursor()
cursor.execute(
"update " + activities_TABLE + " set user_id = ?, gym_id = ?, routes_id = ?, jsondata = ? where id=?",
[str(user_id), str(gym_id), str(routes_id), json.dumps(jsondata), str(activity_id)])
finally:
db.commit()
db.close()
sql_lock.release()
logging.info("updated climbing session for user:" + str(user_id))