Skip to content

Commit

Permalink
fix: modified student_enrollment metric
Browse files Browse the repository at this point in the history
  • Loading branch information
corypride committed Jan 21, 2025
1 parent a6fa174 commit 6d8a270
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions backend/src/database/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,15 @@ func (db *DB) GetTotalCoursesOffered(facilityID *uint) (int, error) {

func (db *DB) GetTotalStudentsEnrolled(facilityID *uint) (int, error) {
var totalStudents int
subQry := db.Table("user_enrollments ue").
Select(`u.facility_id,
c.name AS course_name,
COUNT(DISTINCT u.id) AS num_enrolled`).
query := db.Table("user_enrollments ue").
Select(`COUNT(DISTINCT u.id) AS num_enrolled`).
Joins("INNER JOIN users u ON ue.user_id = u.id").
Joins("INNER JOIN courses c ON ue.course_id = c.id").
Group("ue.course_id, u.facility_id, c.name")
Group("ue.course_id, c.name")
if facilityID != nil {
subQry = subQry.Where("u.facility_id = ?", facilityID)
query = query.Where("u.facility_id = ?", facilityID)
}

query := db.Table("(?) AS sub", subQry).
Select(`COALESCE(SUM(sub.num_enrolled), 0) AS totalStudents`).
Group("sub.course_name")

err := query.Debug().Find(&totalStudents).Error
if err != nil {
return 0, NewDBError(err, "error getting total students enrolled")
Expand Down

0 comments on commit 6d8a270

Please sign in to comment.