Skip to content

Commit

Permalink
Fix stats 500 error on empty db (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelZhao21 authored Sep 5, 2024
1 parent 971014e commit 85875e6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const AddProjectsStatsPanel = () => {
<div className="flex flex-col justify-evenly w-full mt-8">
<div className="flex justify-evenly basis-2/5">
<StatBlock name="Active Projects" value={stats.num} />
<StatBlock name="Average Votes" value={stats.avg_votes} />
<StatBlock name="Average Seen" value={stats.avg_seen} />
</div>
</div>
Expand Down
14 changes: 11 additions & 3 deletions server/database/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ func AggregateStats(db *mongo.Database) (*models.Stats, error) {
projCursor.Next(context.Background())
err = projCursor.Decode(&projAvgSeen)
if err != nil {
return nil, err
// This means no documents were found
if err.Error() == "EOF" {
projAvgSeen = AvgSeenAgg{AvgSeen: 0}
} else {
return nil, err
}
}

// Get the average judge seen using an aggregation pipeline
Expand All @@ -64,9 +69,12 @@ func AggregateStats(db *mongo.Database) (*models.Stats, error) {
var judgeAvgSeen AvgSeenAgg
judgeCursor.Next(context.Background())
err = judgeCursor.Decode(&judgeAvgSeen)
println(judgeAvgSeen.AvgSeen)
if err != nil {
return nil, err
if err.Error() == "EOF" {
judgeAvgSeen = AvgSeenAgg{AvgSeen: 0}
} else {
return nil, err
}
}

// Create the stats object
Expand Down
6 changes: 5 additions & 1 deletion server/database/judge.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ func AggregateJudgeStats(db *mongo.Database) (*models.JudgeStats, error) {
cursor.Next(context.Background())
err = cursor.Decode(&stats)
if err != nil {
return nil, err
if err.Error() == "EOF" {
stats = models.JudgeStats{Num: 0, AvgSeen: 0, NumActive: 0}
} else {
return nil, err
}
}

// Set the total number of judges
Expand Down
6 changes: 5 additions & 1 deletion server/database/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ func AggregateProjectStats(db *mongo.Database) (*models.ProjectStats, error) {
cursor.Next(context.Background())
err = cursor.Decode(&stats)
if err != nil {
return nil, err
if err.Error() == "EOF" {
stats = models.ProjectStats{Num: 0, AvgSeen: 0, NumActive: 0}
} else {
return nil, err
}
}

// Set the total number of projects
Expand Down

0 comments on commit 85875e6

Please sign in to comment.