Skip to content

Commit

Permalink
refactor: total activity time query
Browse files Browse the repository at this point in the history
  • Loading branch information
corypride committed Jan 22, 2025
1 parent 6d8a270 commit 98c3b7f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
10 changes: 4 additions & 6 deletions backend/src/database/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (db *DB) GetTotalStudentsEnrolled(facilityID *uint) (int, error) {
query = query.Where("u.facility_id = ?", facilityID)
}

err := query.Debug().Find(&totalStudents).Error
err := query.Find(&totalStudents).Error
if err != nil {
return 0, NewDBError(err, "error getting total students enrolled")
}
Expand All @@ -219,11 +219,9 @@ func (db *DB) GetTotalStudentsEnrolled(facilityID *uint) (int, error) {

func (db *DB) GetTotalHourlyActivity(facilityID *uint) (int, error) {
var totalActivity int
subQry := db.Table("users u").
Select("CASE WHEN SUM(a.total_time) IS NULL THEN 0 ELSE ROUND(SUM(a.total_time)/3600, 0) END AS total_time").
Joins("LEFT JOIN activities a ON u.id = a.user_id").
Where("u.role = ?", "student")

subQry := db.Table("activities a").
Select("COALESCE(ROUND(SUM(a.total_time)/3600, 0), 0) AS total_activity_time").
Joins("INNER JOIN users u ON u.id = a.user_id")
if facilityID != nil {
subQry = subQry.Where("u.facility_id = ?", facilityID)
}
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/Pages/AdminLayer2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ export default function AdminLayer2() {
/>
<StatsCard
title="Total Activity Time"
number={layer2_metrics.total_hourly_activity.toLocaleString(
'en-US'
)}
number={layer2_metrics.total_hourly_activity.toString()}
label="Hours"
/>
</div>
Expand Down

0 comments on commit 98c3b7f

Please sign in to comment.