-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws_course_counts.go
More file actions
46 lines (39 loc) · 913 Bytes
/
ws_course_counts.go
File metadata and controls
46 lines (39 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"log/slog"
"net/http"
"strconv"
)
func (app *App) broadcastCourseCounts(r *http.Request, courseIDs []string) {
if len(courseIDs) == 0 {
return
}
dedup := make([]string, 0, len(courseIDs))
seen := make(map[string]struct{}, len(courseIDs))
for _, id := range courseIDs {
if id == "" {
continue
}
if _, ok := seen[id]; ok {
continue
}
seen[id] = struct{}{}
dedup = append(dedup, id)
}
if len(dedup) == 0 {
return
}
rows, err := app.queries.GetCourseCountsByIDs(r.Context(), dedup)
if err != nil {
app.logError(r, logMsgAdminCoursesCountsError, slog.Any("error", err))
return
}
counts := make(map[string]int64, len(dedup))
for _, row := range rows {
counts[row.ID] = row.CurrentStudents
}
for _, id := range dedup {
count := counts[id]
app.wsHub.Broadcast(WSMessage("course_count_update," + id + "," + strconv.FormatInt(count, 10)))
}
}