Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

add lb option to show-stats #283

wants to merge 2 commits into from

Conversation

PaliC
Copy link
Collaborator

@PaliC PaliC commented May 20, 2025

This is a pretty simple pr that adds an lb option to show-stats. Right now it just shows average runtime as an extra stat, but I'll add a baseline time in a future pr (this is involved enough I wanted to break it out)

Here's a pic of stats. First one is the default and the second is for a single leaderboard.

Screenshot 2025-05-23 at 12 30 20 PM

@PaliC PaliC marked this pull request as ready for review May 23, 2025 19:31
@PaliC PaliC requested review from Copilot, msaroufim and ngc92 May 23, 2025 19:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR extends the show-stats command with an optional leaderboard_name filter.

  • Adds a leaderboard_name parameter to generate_stats and internal stats methods
  • Updates SQL queries to apply leaderboard filtering when provided
  • Exposes the option in the Discord slash command and installs jq in the CI workflow

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/discord-cluster-manager/leaderboard_db.py Added leaderboard_name parameter and SQL filters in stats
src/discord-cluster-manager/cogs/admin_cog.py Exposed new command parameter with autocomplete
.github/workflows/nvidia_workflow.yml Installed jq for JSON parsing
Comments suppressed due to low confidence (2)

src/discord-cluster-manager/leaderboard_db.py:533

  • Add unit tests for generate_stats (and underlying methods) that cover scenarios with and without leaderboard_name to validate the filtering logic.
def generate_stats(self, last_day: bool, leaderboard_name: Optional[str] = None):

src/discord-cluster-manager/leaderboard_db.py:543

  • Add a leading space before 'AND' (e.g., f" AND ...") so the SQL clause doesn't run together and remains valid.
select_expr += f"AND s.leaderboard_id = (SELECT id FROM leaderboard.leaderboard WHERE name = '{leaderboard_name}')"

Comment on lines +543 to +548
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"""
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interpolating leaderboard_name directly into SQL with f-strings can lead to SQL injection. Use parameterized queries or query arguments instead.

Suggested change
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_expr += "AND s.leaderboard_id = (SELECT id FROM leaderboard.leaderboard WHERE name = %s)"
if not last_day:
select_expr = select_expr.replace("AND", "WHERE")
# per-runner stats
query = f"""

Copilot uses AI. Check for mistakes.

@@ -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):
Copy link
Preview

Copilot AI May 23, 2025

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_stats to include leaderboard_name, describe its purpose, and any behavior when it's omitted.

Suggested change
def generate_stats(self, last_day: bool, leaderboard_name: Optional[str] = None):
def generate_stats(self, last_day: bool, leaderboard_name: Optional[str] = None):
"""
Generate statistics for submissions and runs.
Args:
last_day (bool): If True, only include data from the last 24 hours.
leaderboard_name (Optional[str]): The name of the leaderboard to filter statistics for.
If omitted, statistics are generated for all leaderboards.
Returns:
dict: A dictionary containing the generated statistics.
"""

Copilot uses AI. Check for mistakes.

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):
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The leaderboard filter logic is duplicated across multiple methods; consider extracting a helper to build and append the WHERE/AND clauses.

Copilot uses AI. Check for mistakes.

Comment on lines 765 to +768
@discord.app_commands.describe(last_day_only="Only show stats for the last day")
async def show_bot_stats(self, interaction: discord.Interaction, last_day_only: bool):
@discord.app_commands.describe(leaderboard_name="Name of the leaderboard")
@app_commands.autocomplete(leaderboard_name=leaderboard_name_autocomplete)
async def show_bot_stats(self, interaction: discord.Interaction, last_day_only: bool, leaderboard_name: Optional[str] = None):
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider renaming last_day_only to last_day (or vice versa) to match the database method’s parameter name and improve consistency.

Copilot uses AI. Check for mistakes.

@msaroufim
Copy link
Member

msaroufim commented May 24, 2025

So main reason I'm pausing on this is because I think we should probably just create a table with admin stats and then we can have significantly simpler queries for both the per leaderboard stats and the per day stats. RIght now there's tons of code duplication between our sql queries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants