1+ from datetime import datetime , timedelta
2+ from pydantic import BaseModel
3+ from typing import List
4+ from collections import defaultdict
5+ from src .common import submission_link
6+ from random import randint
7+
8+
9+
10+ START_DATE = datetime (2024 , 9 , 16 )
11+
12+
13+ class DisplaySubmission (BaseModel ):
14+ level : int
15+ url : str
16+ text : str
17+
18+
19+ class Participant (BaseModel ):
20+ name : str
21+ reputation : int
22+ submissions : List [DisplaySubmission ]
23+
24+
25+ def load_dummy_data ():
26+ return [
27+ Participant (
28+ name = "Bob Marley" ,
29+ reputation = 55 ,
30+ submissions = [
31+ # week 1
32+ DisplaySubmission (level = 4 , text = "Submission" , url = "" ),
33+ DisplaySubmission (level = 4 , text = "Submission" , url = "" ),
34+ DisplaySubmission (level = 1 , text = "Submission" , url = "" ),
35+ DisplaySubmission (level = 1 , text = "Submission" , url = "" ),
36+ DisplaySubmission (level = 0 , text = "Submission" , url = "" ),
37+ DisplaySubmission (level = 0 , text = "Submission" , url = "" ),
38+ DisplaySubmission (level = 0 , text = "Submission" , url = "" ),
39+
40+ # week 2
41+ DisplaySubmission (level = 2 , text = "Submission" , url = "" ),
42+ DisplaySubmission (level = 2 , text = "Submission" , url = "" ),
43+ DisplaySubmission (level = 1 , text = "Submission" , url = "" ),
44+ DisplaySubmission (level = 5 , text = "Submission" , url = "" ),
45+ DisplaySubmission (level = 3 , text = "Submission" , url = "" ),
46+ DisplaySubmission (level = 4 , text = "Submission" , url = "" ),
47+ DisplaySubmission (level = 0 , text = "Submission" , url = "" ),
48+
49+ ],
50+ ).dict (),
51+
52+ Participant (
53+ name = "John McClane" ,
54+ reputation = 45 ,
55+ submissions = [
56+ # week 1
57+ DisplaySubmission (level = 4 , text = "Submission" , url = "" ),
58+ DisplaySubmission (level = 4 , text = "Submission" , url = "" ),
59+ DisplaySubmission (level = 1 , text = "Submission" , url = "" ),
60+ DisplaySubmission (level = 1 , text = "Submission" , url = "" ),
61+ DisplaySubmission (level = 0 , text = "Submission" , url = "" ),
62+ DisplaySubmission (level = 0 , text = "Submission" , url = "" ),
63+ DisplaySubmission (level = 0 , text = "Submission" , url = "" ),
64+
65+ # week 2
66+ DisplaySubmission (level = 2 , text = "Submission" , url = "" ),
67+ DisplaySubmission (level = 2 , text = "Submission" , url = "" ),
68+ DisplaySubmission (level = 1 , text = "Submission" , url = "" ),
69+ DisplaySubmission (level = 5 , text = "Submission" , url = "" ),
70+ DisplaySubmission (level = 3 , text = "Submission" , url = "" ),
71+ DisplaySubmission (level = 4 , text = "Submission" , url = "" ),
72+ DisplaySubmission (level = 0 , text = "Submission" , url = "" ),
73+
74+ ],
75+ ).dict (),
76+ ]
77+
78+
79+ def load_data_from_cursor (cursor ):
80+ data = defaultdict (lambda : defaultdict (str ))
81+ users = defaultdict (str )
82+ for record in cursor :
83+ k = record ['chat_id' ] if 'chat_id' in record else record ['username' ]
84+ if 'username' in record :
85+ users [k ] = record ['username' ]
86+ data [k ][record ['date' ]] = record ['text' ]
87+
88+
89+ start_date = START_DATE
90+ end_date = datetime .today ()
91+
92+ result = []
93+ days_of_week = ["Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" , "Sun" ]
94+ for k , p in data .items ():
95+ current_date = start_date
96+ submissions = []
97+ while current_date <= end_date :
98+ d = current_date .strftime ("%Y-%m-%d" )
99+ display_date = days_of_week [current_date .weekday ()] + ", " + d
100+ if d in p :
101+ submissions .append (DisplaySubmission (level = 4 , text = display_date , url = submission_link (p [d ])))
102+ else :
103+ submissions .append (DisplaySubmission (level = 0 , text = display_date , url = '' ))
104+ current_date += timedelta (days = 1 )
105+
106+ result .append (Participant (name = users [k ].split ()[0 ], reputation = len (p ), submissions = submissions ).dict ())
107+
108+ result .sort (key = lambda p : (p ['reputation' ], randint (0 , 100 )), reverse = True )
109+ return result
110+
111+ def load_data (cursor = None , dummy = False ):
112+ return load_dummy_data () if dummy else load_data_from_cursor (cursor )
0 commit comments