-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourses.go
More file actions
165 lines (152 loc) · 3.79 KB
/
courses.go
File metadata and controls
165 lines (152 loc) · 3.79 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Course data structures and locking
*
* Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package main
import (
"context"
"fmt"
"log/slog"
"strings"
"sync"
"sync/atomic"
"github.com/coder/websocket"
)
type courseT struct {
/*
* Selected is usually accessed atomically, but a lock is still
* necessary as we need to sequentialize compare-with-Max-and-increment
* operations.
* We put Selected before other values to ensure 64-bit alignment on
* all systems, because it needs to be accessed atomically. See the
* "Bugs" section of sync/atomic.
*/
Selected uint32 /* atomic */
SelectedLock sync.Mutex
ID int
Max uint32
Title string
Type string
Group string
Teacher string
Location string
CourseID string
SectionID string
YearGroups uint8
Usems sync.Map /* string, *usemT */
Forced bool
LegalSexReq string
}
var courses sync.Map /* int, *courseT */
var numCourses uint32 /* atomic */
const staffDepartment = "Staff"
/*
* Read course information from the database. This should be called during
* setup.
*/
func setupCourses(ctx context.Context) error {
rows, err := db.Query(
ctx,
"SELECT id, nmax, title, ctype, cgroup, teacher, location, course_id, section_id, year_groups, forced, COALESCE(legal_sex_requirements, '') FROM courses",
)
if err != nil {
return fmt.Errorf("get courses from database: %w", err)
}
for {
if !rows.Next() {
err := rows.Err()
if err != nil {
return fmt.Errorf("read next course: %w", err)
}
break
}
currentCourse := courseT{} //exhaustruct:ignore
err = rows.Scan(
¤tCourse.ID,
¤tCourse.Max,
¤tCourse.Title,
¤tCourse.Type,
¤tCourse.Group,
¤tCourse.Teacher,
¤tCourse.Location,
¤tCourse.CourseID,
¤tCourse.SectionID,
¤tCourse.YearGroups,
¤tCourse.Forced,
¤tCourse.LegalSexReq,
)
if err != nil {
return fmt.Errorf("scan course: %w", err)
}
if !checkCourseType(currentCourse.Type) {
return fmt.Errorf("invalid course type in database: %d %s", currentCourse.ID, currentCourse.Type)
}
if !checkCourseGroup(currentCourse.Group) {
return fmt.Errorf("invalid course group in database: %d %s", currentCourse.ID, currentCourse.Group)
}
var selected1 uint32
err := db.QueryRow(
ctx,
"SELECT COUNT (*) FROM choices WHERE courseid = $1",
currentCourse.ID,
).Scan(&selected1)
if err != nil {
return fmt.Errorf("get selected count: %w", err)
}
/*
err = db.QueryRow(
ctx,
"SELECT COUNT (*) FROM pre_selected WHERE course_id = $1",
currentCourse.ID,
).Scan(&selected2)
*/
currentCourse.Selected = selected1
courses.Store(currentCourse.ID, ¤tCourse)
atomic.AddUint32(&numCourses, 1)
}
return nil
}
func (course *courseT) decrementSelectedAndPropagate(
ctx context.Context,
conn *websocket.Conn,
) error {
func() {
course.SelectedLock.Lock()
defer course.SelectedLock.Unlock()
atomic.AddUint32(&course.Selected, ^uint32(0))
}()
go func() {
defer func() {
if e := recover(); e != nil {
slog.Error("panic", "arg", e)
}
}()
propagateSelectedUpdate(course)
}()
err := sendSelectedUpdate(ctx, conn, course.ID)
if err != nil {
return fmt.Errorf("send selected update: %w", err)
}
return nil
}
var yearGroupsNumberBits = map[string]uint8{"Y9": 1, "Y10": 2, "Y11": 4, "Y12": 8}
func yearGroupsStringToNumber(s string) (uint8, error) {
var spec uint8
if s == "" {
for _, v := range yearGroupsNumberBits {
spec |= v
}
return spec, nil
}
ss := strings.Split(s, " ")
for _, yg := range ss {
v, ok := yearGroupsNumberBits[yg]
if !ok {
return spec, fmt.Errorf("invalid year group: %s", yg)
}
spec |= v
}
return spec, nil
}