-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_leaderboard.py
60 lines (50 loc) · 2.23 KB
/
update_leaderboard.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
import firebase_admin
from firebase_admin import firestore
app = firebase_admin.initialize_app()
db = firestore.client()
drillInfoRef = db.collection("teams").document("1").collection("drills")
userInfoRef = db.collection("teams").document("1").collection("users")
attemptsRef = db.collection("teams").document("1").collection("attempts")
newBestAttempts = {}
# reset leaderboard
drills = [drill.to_dict() for drill in drillInfoRef.stream()]
users = [user.to_dict() for user in userInfoRef.stream()]
for drill in drills:
drill_id = drill["did"]
leaderboardEntry = {}
for user in users:
user_id = user["uid"]
leaderboardEntry[user_id] = None
newBestAttempts[drill_id] = leaderboardEntry
# set data from attempts into newBestAttempts
attempts = [attempt.to_dict() for attempt in attemptsRef.stream()]
for attempt in attempts:
attempt_id = attempt["id"]
drill_id = attempt["did"]
user_id = attempt["uid"]
mainOutputAttempt = ""
lowerIsBetter = False
for drill in drills:
if drill["did"] == drill_id:
mainOutputAttempt = drill["mainOutputAttempt"]
lowerIsBetter = drill["aggOutputs"][mainOutputAttempt]["lowerIsBetter"]
break
if mainOutputAttempt == "":
continue
attempt_score = attempt[mainOutputAttempt]
if newBestAttempts[drill_id][user_id] is None:
newBestAttempts[drill_id][user_id] = {mainOutputAttempt: {
"id": attempt_id,
"value": attempt_score
}}
else:
isScoreBetter = attempt_score < newBestAttempts[drill_id][user_id][mainOutputAttempt]["value"] if lowerIsBetter else attempt_score > newBestAttempts[drill_id][user_id][mainOutputAttempt]["value"]
if isScoreBetter:
newBestAttempts[drill_id][user_id] = {mainOutputAttempt: {
"id": attempt_id,
"value": attempt_score
}}
for drillId in newBestAttempts:
bestAttemptDrillRef = db.collection("teams").document("1").collection("best_attempts").document(drillId)
print(newBestAttempts[drillId])
bestAttemptDrillRef.set(newBestAttempts[drillId])