Skip to content

Commit

Permalink
Fix and improve leaderboard table and graph
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed Mar 22, 2024
1 parent 4e691cc commit f4214e8
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 141 deletions.
24 changes: 17 additions & 7 deletions server/db/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions server/db/db_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package db

import (
"database/sql/driver"
"fmt"
"time"
)

type DateTime time.Time

// Time converts a DateTime to a time.Time.
func (d DateTime) Time() time.Time {
return time.Time(d)
}

func (d *DateTime) Scan(src any) error {
switch src := src.(type) {
case time.Time:
*d = DateTime(src)
return nil
case string:
t, err := time.Parse("2006-01-02 15:04:05", src)
if err != nil {
return fmt.Errorf("parsing time: %w", err)
}
*d = DateTime(t)
return nil
default:
return fmt.Errorf("unsupported type: %T", src)
}
}

func (d DateTime) Value() (driver.Value, error) {
return time.Time(d), nil
}
11 changes: 5 additions & 6 deletions server/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 21 additions & 16 deletions server/db/sql_queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ SELECT * FROM team_submit_attempts WHERE team_name = ? AND problem_id = ?
ORDER BY submitted_at ASC;

-- name: ListAllCorrectSubmissions :many
SELECT * FROM team_submit_attempts WHERE correct = TRUE
SELECT *
FROM team_submit_attempts
WHERE correct = TRUE
ORDER BY submitted_at ASC;

-- name: CountIncorrectSubmissions :one
Expand All @@ -42,30 +44,33 @@ DELETE FROM team_points WHERE team_name = ? AND reason = ? RETURNING *;
-- name: RemovePointsByTime :one
DELETE FROM team_points WHERE team_name = ? AND added_at = ? RETURNING *;

-- name: TeamPoints :many
SELECT
teams.team_name,
team_points.reason,
SUM(team_points.points) AS points
-- name: TeamPointsTotal :many
SELECT team_name, SUM(points) AS points
FROM team_points
RIGHT JOIN teams ON teams.team_name = team_points.team_name
GROUP BY teams.team_name, team_points.reason
ORDER BY COALESCE(SUM(team_points.points), 0) DESC;
GROUP BY team_name
ORDER BY COALESCE(SUM(points), 0) DESC;

-- name: TeamPointsHistory :many
SELECT
team_name,
added_at,
SUM(points) AS points
-- name: TeamPointsEach :many
SELECT team_name, reason, points
FROM team_points
GROUP BY team_name, added_at
GROUP BY team_name, reason;

-- name: TeamPointsHistory :many
SELECT *
FROM (
SELECT team_name, added_at, points FROM team_points
UNION ALL
SELECT team_name, MIN(joined_at) AS added_at, 0 AS points
FROM team_members
GROUP BY team_name
) AS history
ORDER BY added_at ASC;

-- name: ListTeams :many
SELECT team_name, created_at, accepting_members FROM teams;

-- name: ListTeamAndMembers :many
SELECT * FROM team_members ORDER BY joined_at ASC;
SELECT team_name, user_name FROM team_members ORDER BY joined_at ASC;

-- name: FindTeamWithInviteCode :one
SELECT team_name, created_at, accepting_members FROM teams WHERE invite_code = ? AND accepting_members = TRUE;
Expand Down
Loading

0 comments on commit f4214e8

Please sign in to comment.