-
Notifications
You must be signed in to change notification settings - Fork 31
add lb option to show-stats #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PaliC
wants to merge
2
commits into
main
Choose a base branch
from
palic/lb_option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -530,32 +530,36 @@ def get_leaderboard_submissions( | |
| for submission in self.cursor.fetchall() | ||
| ] | ||
|
|
||
| def generate_stats(self, last_day: bool): | ||
| def generate_stats(self, last_day: bool, leaderboard_name: Optional[str] = None): | ||
| try: | ||
| return self._generate_stats(last_day) | ||
| return self._generate_stats(last_day, leaderboard_name) | ||
| except Exception as e: | ||
| logging.exception("error generating stats", exc_info=e) | ||
| raise | ||
|
|
||
| def _generate_runner_stats(self, last_day: bool = False): | ||
| def _generate_runner_stats(self, last_day: bool = False, leaderboard_name: Optional[str] = None): | ||
|
||
| select_expr = "WHERE NOW() - s.submission_time <= interval '24 hours'" if last_day else "" | ||
| if leaderboard_name: | ||
| select_expr += f"AND s.leaderboard_id = (SELECT id FROM leaderboard.leaderboard WHERE name = '{leaderboard_name}')" | ||
| if not last_day: | ||
| select_expr = select_expr.replace("AND", "WHERE") | ||
| # per-runner stats | ||
| self.cursor.execute( | ||
| f""" | ||
| SELECT | ||
| runner, | ||
| COUNT(*), | ||
| COUNT(*) FILTER (WHERE passed), | ||
| COUNT(score), | ||
| COUNT(*) FILTER (WHERE secret), | ||
| MAX(runs.start_time - s.submission_time), | ||
| AVG(runs.start_time - s.submission_time), | ||
| SUM(runs.end_time - runs.start_time) | ||
| FROM leaderboard.runs JOIN leaderboard.submission s ON submission_id = s.id | ||
| {select_expr} | ||
| GROUP BY runner; | ||
| """ | ||
| ) | ||
| f""" | ||
| SELECT | ||
| runner, | ||
| COUNT(*), | ||
| COUNT(*) FILTER (WHERE passed), | ||
| COUNT(score), | ||
| COUNT(*) FILTER (WHERE secret), | ||
| MAX(runs.start_time - s.submission_time), | ||
| AVG(runs.start_time - s.submission_time), | ||
| SUM(runs.end_time - runs.start_time) | ||
| FROM leaderboard.runs JOIN leaderboard.submission s ON submission_id = s.id | ||
| {select_expr} | ||
| GROUP BY runner; | ||
| """ | ||
| ) | ||
|
|
||
| result = {} | ||
| for row in self.cursor.fetchall(): | ||
|
|
@@ -569,50 +573,71 @@ def _generate_runner_stats(self, last_day: bool = False): | |
|
|
||
| return result | ||
|
|
||
| def _generate_submission_stats(self, last_day: bool = False): | ||
| select_expr = "WHERE NOW() - submission_time <= interval '24 hours'" if last_day else "" | ||
| def _generate_submission_stats(self, last_day: bool = False, leaderboard_name: Optional[str] = None): | ||
| select_expr = "WHERE NOW() - s.submission_time <= interval '24 hours'" if last_day else "" | ||
| leaderboard_filter = "" | ||
| if leaderboard_name: | ||
| leaderboard_filter = f"WHERE leaderboard_id = (SELECT id FROM leaderboard.leaderboard WHERE name = '{leaderboard_name}')" | ||
| if last_day: | ||
| leaderboard_filter = leaderboard_filter.replace("WHERE", "AND", 1) | ||
| select_expr += leaderboard_filter | ||
| self.cursor.execute( | ||
| f""" | ||
| SELECT | ||
| COUNT(*), | ||
| COUNT(*) FILTER (WHERE NOT done), | ||
| COUNT(DISTINCT user_id) | ||
| FROM leaderboard.submission | ||
| {select_expr} | ||
| ; | ||
| """ | ||
| ) | ||
| f""" | ||
| SELECT | ||
| COUNT(*), | ||
| COUNT(*) FILTER (WHERE NOT done), | ||
| COUNT(DISTINCT user_id) | ||
| FROM leaderboard.submission s | ||
| {select_expr} | ||
| ; | ||
| """, | ||
| ) | ||
| num_sub, num_sub_wait, num_users = self.cursor.fetchone() | ||
| return { | ||
| "num_submissions": num_sub, | ||
| "sub_waiting": num_sub_wait, | ||
| "num_users": num_users, | ||
| } | ||
|
|
||
| def _generate_stats(self, last_day: bool = False): | ||
| result = self._generate_submission_stats(last_day) | ||
| result.update(self._generate_runner_stats(last_day)) | ||
| def _generate_stats(self, last_day: bool = False, leaderboard_name: Optional[str] = None): | ||
| result = self._generate_submission_stats(last_day, leaderboard_name) | ||
| result.update(self._generate_runner_stats(last_day, leaderboard_name)) | ||
|
|
||
| # code-level stats | ||
| if not last_day: | ||
| self.cursor.execute( | ||
| leaderboard_filter = "" | ||
| if leaderboard_name: | ||
| leaderboard_filter = f""" | ||
| WHERE id IN ( | ||
| SELECT code_id FROM leaderboard.submission s | ||
| JOIN leaderboard.leaderboard l ON s.leaderboard_id = l.id | ||
| WHERE l.name = '{leaderboard_name}' | ||
| ) | ||
| """ | ||
| SELECT COUNT(*) FROM leaderboard.code_files; | ||
| self.cursor.execute( | ||
| f""" | ||
| SELECT COUNT(*) FROM leaderboard.code_files | ||
| {leaderboard_filter}; | ||
| """ | ||
| ) | ||
| result["num_unique_codes"] = self.cursor.fetchone()[0] | ||
|
|
||
| else: | ||
| # calculate heavy hitters | ||
| leaderboard_filter = "" | ||
| if leaderboard_name: | ||
| leaderboard_filter = f"AND l.name = '{leaderboard_name}'" | ||
| self.cursor.execute( | ||
| """ | ||
| f""" | ||
| WITH run_durations AS ( | ||
| SELECT | ||
| s.user_id AS user_id, | ||
| r.end_time - r.start_time AS duration | ||
| FROM leaderboard.runs r | ||
| JOIN leaderboard.submission s ON r.submission_id = s.id | ||
| JOIN leaderboard.leaderboard l ON s.leaderboard_id = l.id | ||
| WHERE NOW() - s.submission_time <= interval '24 hours' | ||
| {leaderboard_filter} | ||
| ) | ||
| SELECT | ||
| user_id, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the docstring for
generate_statsto includeleaderboard_name, describe its purpose, and any behavior when it's omitted.