Skip to content

Commit f00cd2a

Browse files
committed
recent submission display, preparation for difficulty bars
1 parent 26c6359 commit f00cd2a

File tree

1 file changed

+85
-3
lines changed

1 file changed

+85
-3
lines changed

leetcode/models/graphql_user_problems_solved.py

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from leetcode.models import *
22
import sys
3+
from datetime import datetime, timedelta
34

5+
# TODO: Add the submission ID into the table, so that the user can copy paste that to download the submitted code
6+
# TODO: think about round the time to the nearest
47
@dataclass
58
class DifficultyCount:
69
difficulty: str
@@ -46,20 +49,23 @@ def parse_args(self, args):
4649
else:
4750
console.print("Username neither provided nor configured. Head to --help.", style=ALERT)
4851
sys.exit(1)
49-
52+
53+
# TODO: add a bar chart for the stats
54+
# TODOL add last submissions
5055
def execute(self, args):
5156
try:
5257
with Loader('Fetching user stats...', ''):
5358
self.parse_args(args)
5459

5560
self.graphql_query = GraphQLQuery(self.query, self.params)
5661
self.result = self.leet_API.post_query(self.graphql_query)
57-
if self.result['errors']:
62+
if 'errors' in self.result:
5863
raise Exception(self.result['errors'][0]['message'])
5964
self.result = QueryResult.from_dict(self.result['data'])
6065
self.show()
6166
except Exception as e:
6267
console.print(f"{e.__class__.__name__}: {e}", style=ALERT)
68+
self.recent_submissions()
6369

6470
def show(self):
6571
difficulties = [x.difficulty for x in self.result.allQuestionsCount]
@@ -79,7 +85,83 @@ def show(self):
7985

8086
for diff, count, stats, subm in zip(difficulties, question_counts, beaten_stats, submit_counts):
8187
table.add_row(diff, str(count), str(stats), str(subm))
88+
console.print(table)
89+
90+
@staticmethod
91+
def time_ago(timestamp: int) -> str:
92+
current_time = datetime.now()
93+
timestamp_time = datetime.fromtimestamp(timestamp)
94+
time_difference = current_time - timestamp_time
95+
96+
if time_difference < timedelta(minutes=1):
97+
return "just now"
98+
elif time_difference < timedelta(hours=1):
99+
minutes = time_difference.seconds // 60
100+
return f"{minutes} minute{'s' if minutes != 1 else ''} ago"
101+
elif time_difference < timedelta(days=1):
102+
hours = time_difference.seconds // 3600
103+
return f"{hours} hour{'s' if hours != 1 else ''} ago"
104+
elif time_difference < timedelta(weeks=1):
105+
days = time_difference.days
106+
return f"{days} day{'s' if days != 1 else ''} ago"
107+
else:
108+
weeks = time_difference.days // 7
109+
return f"{weeks} week{'s' if weeks != 1 else ''} ago"
110+
111+
def recent_submissions(self):
112+
with Loader('Fetching recent submissions...', ''):
113+
self.submissions_query = self.parser.extract_query('recentAcSubmissions')
114+
self.subm_params = {'username': self.params['username'], 'limit': 10}
115+
self.subm_result = self.leet_API.post_query(GraphQLQuery(self.submissions_query, self.subm_params))
116+
self.subm_result = self.subm_result['data']['recentAcSubmissionList']
117+
118+
self.id_query = self.parser.extract_query('GetQuestionId')
119+
120+
table = LeetTable(title='Recent Submissions', width = 70)
121+
table.add_column('ID')
122+
table.add_column('Title')
123+
table.add_column('Time')
124+
125+
for subm in self.subm_result:
126+
self.subm_params = {'titleSlug': subm['titleSlug']}
127+
question_id = self.leet_API.post_query(GraphQLQuery(self.id_query, self.subm_params))['data']['question']['questionId']
128+
table.add_row(question_id, subm['title'], self.time_ago(int(subm['timestamp'])))
129+
82130
print(table)
131+
83132

133+
# if __name__ == '__main__':
134+
# from argparse import Namespace
135+
# user = UserProblemsSolved()
136+
# user.execute(Namespace(username='skygragon'))
137+
# result = user.result
138+
# question_counts = [x.count for x in result.allQuestionsCount][1:]
139+
140+
# submit_counts = []
141+
# for diff, subm in result.matchedUser.submitStatsGlobal.items():
142+
# for submission in subm:
143+
# submit_counts.append(submission.count)
144+
# submit_counts = submit_counts[1:]
145+
146+
# bars = []
147+
# for x, y in zip(question_counts, submit_counts):
148+
# bar = styles.CustomBar(end=(y/x) * 100)
149+
# bars.append(bar)
150+
151+
# result = user.recent_submissions()
152+
# result = result['data']['recentAcSubmissionList']
153+
154+
# table = LeetTable(title='Recent Submissions', width = 50)
155+
# table.add_column('ID')
156+
# table.add_column('Title')
157+
# table.add_column('Time')
158+
159+
# for subm in result:
160+
# question_id = GetQuestionDetail(subm['titleSlug']).question_id
161+
# table.add_row(question_id, subm['title'], time_ago(int(subm['timestamp'])))
162+
163+
# left_container = rich.containers.Renderables(bars)
164+
# right_container = rich.containers.Renderables([table])
84165

85-
166+
# columns = rich.columns.Columns([left_container, right_container], column_first=True)
167+
# console.print(columns)

0 commit comments

Comments
 (0)