-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparkrunDbLib.py
executable file
·732 lines (669 loc) · 29.5 KB
/
parkrunDbLib.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#!/usr/bin/python
"""
Library to handle parkrun data in an sqlite database
"""
import sqlite3
import json
import dateutil.parser
import datetime, time
import numpy as np
import pandas as pd
class parkrunDbLib:
def __init__(self,dbFname, idFname = None, Debug=True):
""" Initialise the library using database file named dbFname.
Optional idFname parameter specifies a file containing a JSON
array of {id, name} objects to use as a lookup for unknown volunteer
runners.
FIXME: Handle file not found errors, and incorreclty initialised
database files - this will just crash!
"""
self.DEBUG = Debug
if (self.DEBUG): print("parkrunDbLib.__init()__: fname=%s" % dbFname)
self.conn = sqlite3.connect(dbFname)
self.idFname = idFname
self.iddb = None # We cache the contents of idFname the first time it is used.
########################################
# Utilities
def dateStr2ts(self,dateStr):
""" Convert a string date dd/mm/yyyy to unix timestamp """
print("dateStr2ts(dateStr=%s)" % dateStr)
dt = dateutil.parser.parse(dateStr,dayfirst=True)
ts = time.mktime(dt.timetuple())
return ts
def ts2dateStr(self,ts):
""" Convert a unix timestamp to dd/mm/yyyy string
"""
print(type(ts),ts)
return datetime.datetime.fromtimestamp(ts).strftime('%d/%m/%Y')
#########################################
# Initialise database
def initialiseDb(self,initFname):
""" Initialise the database with the sql scriptfile initFname.
**** This is likely to wipe all the data in the database, so use
carefully!!!! ****
"""
print("Initialising Database with file %s" % initFname)
f = open(initFname,"r")
sqlStr = f.read()
f.close()
cur = self.conn.executescript(sqlStr)
self.conn.commit()
#########################################
# Parkruns
def getParkruns(self):
sqlStr = "select id, parkrunRef, name from parkruns"
cur = self.conn.execute(sqlStr,())
rows = cur.fetchall()
parkruns = []
for row in rows:
parkruns.append({'id':row[0], 'parkrunRef':row[1], 'name':row[2]})
return parkruns
def getParkrunName(self,id):
sqlStr = "select name from parkruns where id=?"
cur = self.conn.execute(sqlStr,(id,))
rows = cur.fetchall()
if (len(rows)>0):
parkrunName = rows[0][0]
else:
parkrunName = "unknown"
return parkrunName
def getParkrunId(self,prName):
sqlStr = "select id from parkruns where name=?"
cur = self.conn.execute(sqlStr,(prName,))
rows = cur.fetchall()
if (len(rows)>0):
parkrunId = rows[0][0]
else:
parkrunId = -1
return parkrunId
def addParkrun(self,prName):
sqlStr = ("insert into parkruns "
"(parkrunRef,name,created,modified) "
"values(?, ? ,date('now'),date('now'));")
if (self.DEBUG): print(type(prName), prName)
if (self.DEBUG): print(sqlStr)
cur = self.conn.execute(sqlStr,(prName,prName,))
prId = cur.lastrowid
self.conn.commit()
if (self.DEBUG): print("addParkrun - created parkrun %s with ID %d" % (prName,prId))
return prId
#########################################
# Events
def getEvents(self,parkrunId,dateMin='1970-01-01',dateMax='2100-01-01'):
sqlStr = "select dateVal, parkruns.name, eventNo from events, parkruns where (parkruns.id=events.parkrunId and events.dateVal>=date(?) and events.dateVal<=date(?)) order by events.dateVal"
cur = self.conn.execute(sqlStr,(dateMin,dateMax,))
rows = cur.fetchall()
parkruns = []
for row in rows:
parkruns.append({'date':row[0], 'name':row[1], 'eventNo':row[2]})
return parkruns
def getEventId(self,parkrunId,dateVal):
""" Check the database to see if we already have an event for parkrunId
on date DateVal. DateVal must be a unix timestamp
If so, return the event Id, or -1 if not.
"""
#Check if the event exists, if not create it.
sqlStr = "select id from events where parkrunId=? and dateVal=?"
sqlParams = (parkrunId,dateVal,)
if (self.DEBUG): print("sqlStr=%s." % sqlStr)
if (self.DEBUG): print(sqlParams)
cur = self.conn.execute(sqlStr,sqlParams)
rows = cur.fetchall()
if (len(rows)>0): # event already exists
eventId = rows[0][0]
if (self.DEBUG): print("Found event id %d" % (eventId))
else:
eventId = -1
return eventId
def getEventIdFromEventNo(self,parkrunId,eventNo):
""" Check the database to see if we already have an event for parkrunId
with number eventNo.
If so, return the event Id, or -1 if not.
"""
sqlStr = "select id from events where parkrunId=? and eventNo=?"
sqlParams = (parkrunId,eventNo,)
if (self.DEBUG): print("sqlStr=%s." % sqlStr)
if (self.DEBUG): print(sqlParams)
cur = self.conn.execute(sqlStr,sqlParams)
rows = cur.fetchall()
if (len(rows)>0): # event already exists
eventId = rows[0][0]
if (self.DEBUG): print("Found event id %d" % (eventId))
else:
eventId = -1
return eventId
def addEvent(self,eventNo, parkrunId, dateVal):
""" Create event number eventNo for parkrun parkrunId on date dateVal.
DateVal must be a unix timestamp
Returns the new event ID
"""
# create an event
sqlStr = "insert into events (eventNo, parkrunId, dateVal, created," \
"modified) " \
" values (?,?,?,date('now'),date('now'))"
sqlParams = (eventNo,
parkrunId,
dateVal,
)
if(self.DEBUG): print("addEvent - sqlStr =%s." % sqlStr)
if(self.DEBUG): print(sqlParams)
cur = self.conn.execute(sqlStr,
sqlParams
)
eventId = cur.lastrowid
self.conn.commit()
if (self.DEBUG): print("addEvent - created event %d for date %d (%s)" % (eventId,dateVal,self.ts2dateStr(dateVal)))
return eventId
#########################################
# Runners
def getRunner(self,runnerId):
"""return the data for runner id runnerId. """
sqlStr = "select runnerNo, name, club, gender from runners where id=?"
sqlParams=(runnerId,)
cur = self.conn.execute(sqlStr,sqlParams)
rows = cur.fetchall()
if (len(rows)>0):
return rows[0]
else:
return None
def getRunnerId(self,runnerNo):
""" Look in the runners table to see if runnerNo exists. Return -1
if not.
"""
sqlStr = "select id from runners where runnerNo=?"
cur = self.conn.execute(sqlStr,(runnerNo,))
rows = cur.fetchall()
if (len(rows)>0): # event already exists
runnerId = rows[0][0]
if (self.DEBUG): print("Found runner id %d" % (runnerId))
else:
runnerId = -1
return runnerId
def getRunnerNoFromName(self,nameStr):
""" Look up the runner Name in the runners table to return the runner Number.
if it is not found, the self.idFname file is used to attempt to look it
up, and then add it to the main database.
Returns the runner Number (=parkrun barcode no) or -1 if not found.
"""
sqlStr = "select runnerNo from runners where name=?"
sqlParams=(nameStr,)
cur = self.conn.execute(sqlStr,sqlParams)
rows = cur.fetchall()
if (len(rows)>0): # event already exists
runnerNo = rows[0][0]
if (self.DEBUG): print("getRunnerNoFromName() Found runner No %d in database" % (runnerNo))
elif (self.idFname != None):
# Attempt to look up the name in idFname file
# idFname should contain a json array of {id,name} objects.
if (self.DEBUG): print("getRunnerNoFromName(): Attempting to use external runner id database.")
if (self.iddb == None):
f = open(self.idFname,'r')
self.iddb = json.load(f)
f.close()
runnerNo = -1
for idrec in self.iddb:
if idrec['name'] == nameStr and idrec['id']!= 'unknown':
runnerNo = int(idrec['id'])
if (self.DEBUG): print("getRunnerNoFromName() Found runner No %d in id database" % (runnerNo))
else:
if (self.DEBUG): print("getRunnerNoFromName() failed to find runner %s" % nameStr)
runnerNo = -1
return runnerNo
def addRunner(self,runnerNo, nameStr, clubStr, genderStr):
""" Create runner record for runnerId
Returns the new runner ID
"""
# create a runner
sqlStr = "insert into runners (runnerNo, name, club, gender, created," \
"modified) " \
" values (?,?,?,?,date('now'), date('now'))"
sqlParams = (runnerNo,nameStr,clubStr,genderStr,)
if(self.DEBUG): print("createRunner - sqlStr =%s." % sqlStr)
if(self.DEBUG): print(sqlParams)
cur = self.conn.execute(sqlStr, sqlParams)
runnerId = cur.lastrowid
self.conn.commit()
if (self.DEBUG): print("createRunner - created runner %d" % (runnerId))
return runnerId
def updateRunner(self,runnerId, runnerNo, nameStr, clubStr, genderStr):
""" update runner record for runnerId
Returns the runnerId
"""
# update runner
sqlStr = ("update runners set"
" runnerNo = ?, "
" name = ?, "
" club = ?, "
" gender = ?,"
" modified = date('now') "
" where id = ?"
)
sqlParams = (runnerNo,nameStr,clubStr,genderStr,runnerId,)
if(self.DEBUG): print("updateRunner - sqlStr =%s." % sqlStr)
if(self.DEBUG): print(sqlParams)
cur = self.conn.execute(sqlStr, sqlParams)
self.conn.commit()
if (self.DEBUG): print("updateRunner - updated %d rows" % (cur.rowcount))
return cur.rowcount
#############################################
# Runs
def getRunId(self,eventId,runnerId, roleId):
""" Check the database to see if we already have runner runnerId
taking part as role RoleId for event eventId.
If so, return the run Id, or -1 if not.
"""
sqlStr = "select id from runs where eventId=? and runnerId=? and roleId=?"
cur = self.conn.execute(sqlStr,(eventId,runnerId,roleId,))
rows = cur.fetchall()
if (len(rows)>0): # event already exists
runId = rows[0][0]
if (self.DEBUG): print("Found event id %d" % (eventId))
else:
runId = -1
return runId
def addRun(self,eventId, runnerId, roleId, runTime,ageCat,ageGrade,finishPos,genderPos,note):
""" Create run for runner runnerId at event eventId doing role roleID
in runTime seconds.
Returns the new run ID
"""
# create an event
sqlStr = "insert into runs (eventId, runnerId, roleId, finishPos, genderPos, ageCat,ageGrade,note,runTime, created," \
"modified) " \
" values (?,?,?,?,?,?,?,?,?,date('now'),date('now'))"
sqlParams = (eventId,runnerId, roleId, finishPos,genderPos,
ageCat,ageGrade,note,runTime,)
if(self.DEBUG): print("addRun - sqlStr =%s." % sqlStr)
if(self.DEBUG): print(sqlParams)
cur = self.conn.execute(sqlStr,sqlParams)
runId = cur.lastrowid
self.conn.commit()
if (self.DEBUG): print("createRun - created run %d" % (runId))
return runId
#############################
# Queries
def getEventsListSql(self,prIdArr,startTs,endTs):
""" Returns (sql,paramDict) which is the
SQL query to return the list of event IDs for parkrun prId
beween the specified dates, and a dictionary of the parameters.
"""
# Make sure our ID array list is really a list, not a single value.
if not isinstance(prIdArr, (list, tuple)):
prIdArr= [prIdArr]
# FIXME - I am sure this is a bad idea but can't manage to
# set a list as a parameter to sqlite3?
prIdTuple = tuple(prIdArr)
sqlStr = 'select id from events where parkrunId in (' + ','.join((str(n) for n in prIdTuple)) + ' ) and dateVal>=:startTs and dateVal<=:endTs order by dateVal'
paramDict = {"startTs":startTs,"endTs":endTs}
return sqlStr,paramDict
def getEventHistory(self,parkrunStr,startTs,endTs):
""" returns a cursor pointing to the event history for the given parkrun
between timestamps startTs and endTs
"""
prId = self.getParkrunId(parkrunStr)
if (prId==-1):
return None
else:
#strftime('%d-%m-%Y', (date/1000)) as_string
sqlStr = ("select eventNo, id, dateVal, "
"strftime('%d-%m-%Y',datetime(dateVal, 'unixepoch',"
"'localtime')) as dateStr, "
"(select count(id) from runs "
" where runs.eventId=events.Id and runs.roleId=0) "
" as runners, "
"(select count(id) from runs "
" where runs.eventId=events.Id and runs.roleId=1) "
" as volunteers "
", "
"(select count(id) from runs "
" where runs.eventId=events.Id and runs.note='New PB!') "
" as PBcount "
", "
"(select count(id) from runs "
" where runs.eventId=events.Id and runs.note='First Timer!') "
" as FirstTimecount "
"from events "
"where parkrunId = ? "
" and dateVal>=? and dateVal<=? "
"order by dateVal desc"
)
sqlParams = (prId,startTs,endTs)
cur = self.conn.execute(sqlStr,sqlParams)
rows = cur.fetchall()
return rows
def getEventAttendanceSummary(self,parkrunStr,startTs,endTs):
""" returns a cursor pointing to the event attendance summary
(annual number of runs)
between timestamps startTs and endTs
"""
prId = self.getParkrunId(parkrunStr)
if (prId==-1):
return None
else:
#strftime('%d-%m-%Y', (date/1000)) as_string
sqlStr = (
"select yearStr, count(eventNo), sum(runners), sum(volunteers), "
"sum(PBcount), sum(FirstTimeCount), sum(totalRunTime), "
"sum(numValidRunners) "
"from "
"(select eventNo, id, dateVal, "
"strftime('%Y',datetime(dateVal, 'unixepoch',"
"'localtime')) as yearStr, "
"strftime('%d-%m-%Y',datetime(dateVal, 'unixepoch',"
"'localtime')) as dateStr, "
"(select count(id) from runs "
" where runs.eventId=events.Id and runs.roleId=0) "
" as runners, "
"(select count(id) from runs "
" where runs.eventId=events.Id and runs.roleId=1) "
" as volunteers "
", "
"(select count(id) from runs "
" where runs.eventId=events.Id and runs.note='New PB!') "
" as PBcount "
", "
"(select count(id) from runs "
" where runs.eventId=events.Id and runs.note='First Timer!') "
" as FirstTimecount "
", "
"(select sum(runTime) from runs "
" where runs.eventId=events.Id and runs.roleId=0 "
" and runs.runTime<9000) "
" as totalRunTime "
", "
"(select count(runTime) from runs "
" where runs.eventId=events.Id and runs.roleId=0 "
" and runs.runTime<9000) "
" as numValidRunners "
"from events "
"where parkrunId = ? "
" and dateVal>=? and dateVal<=? "
"order by dateVal desc) "
"group by yearStr"
)
sqlParams = (prId,startTs,endTs)
print("sqlStr=%s" % sqlStr)
cur = self.conn.execute(sqlStr,sqlParams)
rows = cur.fetchall()
return rows
def getEventResults(self,parkrunStr,eventNo):
""" returns set of rows containing results for the given parkrun event.
"""
prId = self.getParkrunId(parkrunStr)
eventId = self.getEventIdFromEventNo(prId,eventNo)
# print ("getEventResults - parkrunStr=%s (id=%d), eventNo=%d (id=%d)"
# % (parkrunStr,prId, eventNo, eventId))
if (prId==-1 | eventId==-1):
return None
else:
sqlStr = ("select finishPos, runners.name, runTime "
"from runs, runners "
"where runs.eventId = ? and runs.runnerId=runners.Id"
" order by runs.finishPos asc"
)
sqlParams = (eventId,)
cur = self.conn.execute(sqlStr,sqlParams)
rows = cur.fetchall()
return rows
def getResultsDf(self,parkrunStrArr,startTs,endTs):
""" returns a pandas dataframe containing results for the given
list of parkruns between the specified dates (which should be unix
timestamps - use dateStr2ts to generate these from strings).
"""
# If we are passed a single parkrun string, turn it into a
# list, so we know we have a list of parkruns to deal with.
if not isinstance(parkrunStrArr, (list, tuple)):
print("converting parkrunStr into an array")
parkrunStrArr = [parkrunStrArr]
# Now get the IDs of the parkruns
prIdArr = []
for parkrunStr in parkrunStrArr:
prId = self.getParkrunId(parkrunStr)
if (prId == -1):
print("ERROR - Parkrun %s not found" % parkrunStr)
else:
prIdArr.append(prId)
print("getVolStats - parkrunStr=%s (id=%d)"
% (parkrunStr,prId))
if (len(prIdArr) == 0):
print("ERROR - no valid parkruns found.")
return None
else:
print("prIdArr=",prIdArr)
# Get the SQL string to give us a list of event IDs to use
# in queries.
selEventsSql,sqlParams = self.getEventsListSql(prIdArr[0],startTs,endTs)
print("selEventsSql="+selEventsSql,sqlParams)
sqlStr = ("select events.dateVal, "
" events.parkrunId, parkruns.name, "
" events.eventNo, "
" runs.finishPos, runners.name, runners.runnerNo, "
" runs.runTime, runners.gender, runners.club, "
" runs.roleId, runs.note, runs.genderPos, "
" runs.ageCat, runs.ageGrade "
" from runs, runners, events, parkruns "
" where runs.runnerId=runners.Id "
" and events.id = runs.eventId "
" and events.parkrunId = parkruns.id "
" and runs.eventId in (%s) "
" order by events.dateVal" % selEventsSql)
df = pd.read_sql_query(sqlStr,self.conn, params=sqlParams)
#df.describe()
#print(df)
#exit(-1)
return(df)
def getVolStats2(self, parkrunStrArr, startTs, endTs,
thresh, limit, orderBy):
df = self.getResultsDf(parkrunStrArr, startTs, endTs)
volFilter = df['roleId'] == 1
volDf = df[volFilter]
volDf.describe()
return(volDf)
def getVolStats(self,parkrunStrArr,startTs,endTs,thresh,limit,orderBy):
""" returns set of rows containing
volunteering statistics for the given parkrun between the specified
dates.
Only runners who have participated in at least thresh number of events
are included.
Returns 'limit' number of rows
OrderBy is an integer 1 = total activities, 2=runs, 3= volunteers
"""
# If we are passed a single parkrun string, turn it into a
# list, so we know we have a list of parkruns to deal with.
if not isinstance(parkrunStrArr, (list, tuple)):
parkrunStrArr = [parkrunStrArr]
# Now get the IDs of the parkruns
prIdArr = []
for parkrunStr in parkrunStrArr:
prId = self.getParkrunId(parkrunStr)
if (prId != -1):
prIdArr.append(prId)
#print ("getVolStats - parkrunStr=%s (id=%d)"
# % (parkrunStr,prId))
else:
print("ERROR - Parkrun %s not found" % parkrunStr)
if (len(prIdArr) == 0):
print("ERROR - no valid parkruns found.")
return None
else:
# Get the SQL string to give us a list of event IDs to use
# in queries.
selEventsSql,sqlParams = self.getEventsListSql(prIdArr,startTs,endTs)
print(selEventsSql, sqlParams)
#cur = self.conn.execute(selEventsSql,sqlParams)
#rows = cur.fetchall()
#print(rows)
# calculate number of runs for each runner
runsSqlStr = (
" select name, runnerNo, count(runs.id) as nr, "
" sum(runs.runTime) as tr"
" from runners, runs "
" where runs.eventId in "
" ("+selEventsSql+") "
" and runs.roleId = 0 "
" "
" and runs.runnerId=runners.Id "
" group by runnerId"
" order by count(runs.id) desc"
)
# calculate number of volunteerings for each runner
# set time on feet to zero for volunteers, because otherwise
# we were getting the top volunteers winning time on feet too.
volsSqlStr = (
" select name, runnerNo, count(runs.id) as nv, "
" 0 as tv"
" from runners, runs "
" where runs.eventId in "
" ("+selEventsSql+") "
" and runs.roleId = 1 "
" "
" and runs.runnerId=runners.Id "
" group by runnerId"
" order by count(runs.id) desc "
)
orderByStr = ""
if (orderBy==1):
orderByStr = " order by total desc, nv desc"
elif (orderBy==2):
orderByStr = " order by nr desc, nv desc "
elif (orderBy==3):
orderByStr = " order by nv desc, nr desc "
elif (orderBy==4):
orderByStr = " order by timeOnFeet desc, nv desc "
# We want all runners who have:
# - ran but not volunteered
# - volunteered but not ran
# - volunteered and ran
# so we have to union together two queries to make sure we get
# them all.
# The 'coalesce' statements are to force Null to be returned as zero
# so that calculations work.
sqlStr = (
"select r.name, r.runnerNo, coalesce(r.nr,0) as nr, "
" coalesce(v.nv,0) as nv, "
" coalesce(r.nr,0) + coalesce(v.nv,0) as total, "
" coalesce(r.tr,0), coalesce(v.tv,0), "
" coalesce(r.tr,0) + coalesce(v.tv,0) as timeOnFeet "
" from "
" (" +runsSqlStr + ") r"
" left join "
" (" +volsSqlStr + ") v"
" on r.runnerNo = v.runnerNo"
" where total >= :thresh "
" union "
"select v.name, v.runnerNo, coalesce(r.nr,0) as nr, "
" coalesce(v.nv,0) as nv, "
" coalesce(r.nr,0) + coalesce(v.nv,0) as total, "
" coalesce(r.tr,0), coalesce(v.tv,0), "
" coalesce(r.tr,0) + coalesce(v.tv,0) as timeOnFeet "
" from "
" (" +volsSqlStr + ") v"
" left outer join "
" (" +runsSqlStr + ") r"
" on v.runnerNo = r.runnerNo"
" where total >= :thresh "
+ orderByStr +
" limit :limit"
)
sqlParams['thresh']=thresh
sqlParams['limit']=limit
if (self.DEBUG): print(sqlStr,sqlParams)
cur = self.conn.execute(sqlStr,sqlParams)
rows = cur.fetchall()
return rows
def getRunnerList(self,parkrunStr,startTs,endTs,thresh,limit):
""" returns a list of runnerIds
of runners for the given parkrun between the specified
dates.
Only runners who have participated in at least thresh number of events
are included.
Returns 'limit' number of rows
"""
prId = self.getParkrunId(parkrunStr)
if (self.DEBUG): print("getRunnerList - parkrunStr=%s (id=%d)"
% (parkrunStr,prId))
if (prId==-1 ):
print("ERROR - Parkrun %s not found" % parkrunStr)
return None
else:
# Get the SQL string to give us a list of event IDs to use
# in queries.
selEventsSql,sqlParams = self.getEventsListSql(prId,startTs,endTs)
# calculate number of runs for each runner
sqlStr = (
" select runners.id, runners.name, runners.runnerNo, "
" count(runs.id) as nr, "
" sum(runs.runTime) as tr"
" from runners, runs "
" where runs.eventId in "
" ("+selEventsSql+") "
" and runs.roleId = 0 "
" "
" and runs.runnerId=runners.Id "
" group by runnerId"
" having count(runs.id)>=:thresh "
" order by count(runs.id) desc"
" limit :limit "
)
sqlParams['thresh']=thresh
sqlParams['limit']=limit
if (self.DEBUG): print(sqlStr,sqlParams)
print(sqlStr,sqlParams)
cur = self.conn.execute(sqlStr,sqlParams)
rows = cur.fetchall()
return rows
def getRunnerHistory(self,runnerId, parkrunStr,startTs,endTs):
""" returns the run history for runner id runnerId
for the given parkrun between the specified dates
"""
prId = self.getParkrunId(parkrunStr)
if (self.DEBUG): print("getRunnerList - parkrunStr=%s (id=%d)"
% (parkrunStr,prId))
if (prId==-1 ):
print("ERROR - Parkrun %s not found" % parkrunStr)
return None
else:
# Get the SQL string to give us a list of event IDs to use
# in queries.
selEventsSql,sqlParams = self.getEventsListSql(prId,startTs,endTs)
if (self.DEBUG):
print("EVENTS LIST TO PROCESS: ",selEventsSql,sqlParams)
cur = self.conn.execute(selEventsSql,sqlParams)
rows = cur.fetchall()
for r in rows:
print(r)
# calculate number of runs for each runner
sqlStr = (
" select runs.eventId, events.dateVal, "
"strftime('%d-%m-%Y',datetime(dateVal, 'unixepoch',"
"'localtime')) as dateStr, "
" runs.runnerId, runs.finishPos, "
" runs.genderPos, "
" runs.ageGrade, runs.runTime "
" from events, runs "
" where runs.eventId in "
" ("+selEventsSql+") "
" and events.id=runs.eventId "
" and runs.roleId = 0 "
" and runs.runnerId = :runnerId "
" "
" order by events.dateVal asc"
)
sqlParams['runnerId']=runnerId
if (self.DEBUG): print(sqlStr,sqlParams)
cur = self.conn.execute(sqlStr,sqlParams)
rows = cur.fetchall()
return rows
if __name__ == "__main__":
db = parkrunDbLib("parkrun.db")
print("Parkruns: ", db.getParkruns())
print("Parkrun 0 = ", db.getParkrunName(1))
print("Parkrun 0 events= ", db.getEvents(1))
print("Parkrun 0 event on 01/01/2018 is event no ", db.getEventId(1,"01/01/2018"))
print(db.getEventId(1,"01/01/2018"))
#print db.getEventId(0,"01/01/2018",True)
#print db.getEventId(0,"01/01/2018")
parkrunStrArr=("Hartlepool", "Rossmere")
results = db.getVolStats2(parkrunStrArr,
db.dateStr2ts("01/01/1970"),
db.dateStr2ts("01/01/2030"), 1, 10, 1)
print(results)