/
validate_rooms.go
538 lines (467 loc) · 17 KB
/
validate_rooms.go
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package plexams
import (
"context"
"fmt"
"strings"
"time"
set "github.com/deckarep/golang-set/v2"
"github.com/logrusorgru/aurora"
"github.com/obcode/plexams.go/graph/model"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/theckman/yacspin"
)
func (p *Plexams) ValidateRoomsPerSlot() error {
ctx := context.Background()
cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[69],
Suffix: aurora.Sprintf(aurora.Cyan(" validating rooms per slot (allowed and enough seats)")),
SuffixAutoColon: true,
StopCharacter: "✓",
StopColors: []string{"fgGreen"},
StopFailMessage: "error",
StopFailCharacter: "✗",
StopFailColors: []string{"fgRed"},
}
spinner, err := yacspin.New(cfg)
if err != nil {
log.Debug().Err(err).Msg("cannot create spinner")
}
err = spinner.Start()
if err != nil {
log.Debug().Err(err).Msg("cannot start spinner")
}
validationMessages := make([]string, 0)
slots := p.semesterConfig.Slots
for _, slot := range slots {
plannedExams, err := p.dbClient.GetExamsInSlot(ctx, slot.DayNumber, slot.SlotNumber)
if err != nil {
log.Error().Err(err).
Int("day", slot.DayNumber).
Int("time", slot.SlotNumber).
Msg("error while getting exams planned in slot")
return err
}
plannedRooms := make([]*model.PlannedRoom, 0)
for _, plannedExam := range plannedExams {
plannedRooms = append(plannedRooms, plannedExam.PlannedRooms...)
}
spinner.Message(aurora.Sprintf(aurora.Yellow("checking slot (%d/%d) with %d rooms in %d exams"),
slot.DayNumber, slot.SlotNumber, len(plannedRooms), len(plannedExams)))
allowedRooms, err := p.RoomsForSlot(ctx, slot.DayNumber, slot.SlotNumber)
if err != nil {
log.Error().Err(err).
Int("day", slot.DayNumber).
Int("time", slot.SlotNumber).
Msg("error while getting allowed rooms for slot")
return err
}
allAllowedRooms :=
append(allowedRooms.NormalRooms,
append(allowedRooms.LabRooms,
append(allowedRooms.ExahmRooms,
allowedRooms.NtaRooms...)...)...)
for _, plannedRoom := range plannedRooms {
if plannedRoom.RoomName == "ONLINE" {
continue
}
if plannedRoom.RoomName == "No Room" {
validationMessages = append(validationMessages, aurora.Sprintf(aurora.Red("No Room for %d students in slot (%d/%d)"),
aurora.Magenta(len(plannedRoom.StudentsInRoom)), aurora.Blue(slot.DayNumber), aurora.Blue(slot.SlotNumber)))
continue
}
isAllowed := false
for _, allowedRoom := range allAllowedRooms {
if allowedRoom.Name == plannedRoom.RoomName {
isAllowed = true
break
}
}
if !isAllowed {
validationMessages = append(validationMessages, aurora.Sprintf(aurora.Red("Room %s is not allowed in slot (%d/%d)"),
aurora.Magenta(plannedRoom.RoomName), aurora.Blue(slot.DayNumber), aurora.Blue(slot.SlotNumber)))
}
}
type roomSeats struct {
seatsPlanned, seats int
}
seats := make(map[string]roomSeats)
for _, plannedRoom := range plannedRooms {
entry, ok := seats[plannedRoom.RoomName]
// TODO: Remove this hack
if strings.HasPrefix(plannedRoom.RoomName, "ONLINE") {
continue
}
if !ok {
seats[plannedRoom.RoomName] = roomSeats{seatsPlanned: len(plannedRoom.StudentsInRoom), seats: p.GetRoomInfo(plannedRoom.RoomName).Seats}
} else {
seats[plannedRoom.RoomName] = roomSeats{seatsPlanned: len(plannedRoom.StudentsInRoom) + entry.seatsPlanned, seats: p.GetRoomInfo(plannedRoom.RoomName).Seats}
}
}
for roomName, roomSeats := range seats {
if roomSeats.seatsPlanned > roomSeats.seats {
validationMessages = append(validationMessages,
aurora.Sprintf(aurora.Red("Room %s is overbooked in slot (%d/%d): %d seats planned, but only %d available"),
aurora.Magenta(roomName), aurora.Blue(slot.DayNumber), aurora.Blue(slot.SlotNumber),
aurora.Cyan(roomSeats.seatsPlanned), aurora.Cyan(roomSeats.seats)))
}
}
}
if len(validationMessages) > 0 {
spinner.StopFailMessage(aurora.Sprintf(aurora.Red("%d problems found"), len(validationMessages)))
err = spinner.StopFail()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
for _, msg := range validationMessages {
fmt.Printf(" ↪ %s\n", msg)
}
} else {
err = spinner.Stop()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
return nil
}
func (p *Plexams) ValidateRoomsNeedRequest() error {
ctx := context.Background()
cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[69],
Suffix: aurora.Sprintf(aurora.Cyan(" validating rooms which needs requests")),
SuffixAutoColon: true,
StopCharacter: "✓",
StopColors: []string{"fgGreen"},
StopFailMessage: "error",
StopFailCharacter: "✗",
StopFailColors: []string{"fgRed"},
}
spinner, err := yacspin.New(cfg)
if err != nil {
log.Debug().Err(err).Msg("cannot create spinner")
}
err = spinner.Start()
if err != nil {
log.Debug().Err(err).Msg("cannot start spinner")
}
validationMessages := make([]string, 0)
roomTimetables, err := p.GetReservations()
if err != nil {
log.Error().Err(err).Msg("cannot get reservations")
return err
}
bookedEntries, err := p.ExahmRoomsFromBooked()
if err != nil {
log.Error().Err(err).Msg("cannot get booked entries")
return err
}
for _, bookedEntry := range bookedEntries {
for _, roomName := range bookedEntry.Rooms {
timeRanges, ok := roomTimetables[roomName]
if !ok {
timeRanges = make([]TimeRange, 0, 1)
}
roomTimetables[roomName] = append(timeRanges, TimeRange{
From: bookedEntry.From,
Until: bookedEntry.Until,
})
}
}
log.Debug().Interface("timetables", roomTimetables).Msg("found booked and reserved rooms")
plannedRooms, err := p.PlannedRooms(ctx)
if err != nil {
log.Error().Err(err).Msg("cannot get all planned rooms")
}
reservationFound := 0
PLANNEDROOM:
for _, plannedRoom := range plannedRooms {
if !p.roomInfo[plannedRoom.RoomName].NeedsRequest {
log.Debug().Str("room", plannedRoom.RoomName).Msg("room needs no request")
continue
}
spinner.Message(aurora.Sprintf(aurora.Yellow("checking room %s in slot (%d/%d)"),
plannedRoom.RoomName, plannedRoom.Day, plannedRoom.Slot))
startTime := p.getSlotTime(plannedRoom.Day, plannedRoom.Slot).Local()
endTime := startTime.Add(time.Duration(plannedRoom.Duration) * time.Minute)
for _, timerange := range roomTimetables[plannedRoom.RoomName] {
if timerange.From.Before(startTime) && timerange.Until.After(endTime) {
log.Debug().Str("room", plannedRoom.RoomName).Msg("found reservation")
reservationFound++
continue PLANNEDROOM
}
}
validationMessages = append(validationMessages, aurora.Sprintf(aurora.Red("No Reservation for room %s found in slot (%d/%d)"),
aurora.Magenta(plannedRoom.RoomName), aurora.Blue(plannedRoom.Day), aurora.Blue(plannedRoom.Slot)))
}
if len(validationMessages) > 0 {
spinner.StopFailMessage(aurora.Sprintf(aurora.Red("%d problems found"), len(validationMessages)))
err = spinner.StopFail()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
for _, msg := range validationMessages {
fmt.Printf(" ↪ %s\n", msg)
}
} else {
spinner.StopMessage(aurora.Sprintf(aurora.Green("found %d reservations"), reservationFound))
err = spinner.Stop()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
return nil
}
func (p *Plexams) ValidateRoomsPerExam() error {
ctx := context.Background()
cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[69],
Suffix: aurora.Sprintf(aurora.Cyan(" validating rooms per exam")),
SuffixAutoColon: true,
StopCharacter: "✓",
StopColors: []string{"fgGreen"},
StopFailMessage: "error",
StopFailCharacter: "✗",
StopFailColors: []string{"fgRed"},
}
spinner, err := yacspin.New(cfg)
if err != nil {
log.Debug().Err(err).Msg("cannot create spinner")
}
err = spinner.Start()
if err != nil {
log.Debug().Err(err).Msg("cannot start spinner")
}
validationMessages := make([]string, 0)
exams, err := p.PlannedExams(ctx)
if err != nil {
log.Error().Err(err).Msg("cannot get exams in plan")
}
for _, exam := range exams {
if exam.PlanEntry == nil {
continue
}
if exam.Constraints != nil && exam.Constraints.NotPlannedByMe {
continue
}
spinner.Message(aurora.Sprintf(aurora.Yellow("checking rooms for %d. %s (%s) with %d students and %d ntas"),
exam.Ancode, exam.ZpaExam.Module, exam.ZpaExam.MainExamer, exam.StudentRegsCount, len(exam.Ntas)))
// check if each student has a room
allStudentRegs := make([]*model.StudentReg, 0)
for _, primussExam := range exam.PrimussExams {
allStudentRegs = append(allStudentRegs, primussExam.StudentRegs...)
}
// rooms, err := p.dbClient.RoomsForAncode(ctx, exam.Exam.Ancode)
// if err != nil {
// log.Error().Err(err).Int("ancode", exam.Exam.Ancode).Msg("cannot get rooms for ancode")
// return err
// }
allStudentsInRooms := make([]string, 0)
for _, room := range exam.PlannedRooms {
if room.RoomName != "No Room" {
allStudentsInRooms = append(allStudentsInRooms, room.StudentsInRoom...)
}
}
for _, studentReg := range allStudentRegs {
studentHasSeat := false
for _, mtknr := range allStudentsInRooms {
if studentReg.Mtknr == mtknr {
studentHasSeat = true
break
}
}
if !studentHasSeat {
validationMessages = append(validationMessages, aurora.Sprintf(aurora.Red("Student %s (%s) has no seat for exam %d. %s (%s) in slot (%d,%d)"),
aurora.Magenta(studentReg.Name), aurora.Magenta(studentReg.Mtknr), aurora.Cyan(exam.Ancode), aurora.Cyan(exam.ZpaExam.Module), aurora.Cyan(exam.ZpaExam.MainExamer),
aurora.BrightBlue(exam.PlanEntry.DayNumber), aurora.BrightBlue(exam.PlanEntry.SlotNumber)))
}
}
// check if room constraints of exams are met
if exam.Constraints != nil && exam.Constraints.RoomConstraints != nil {
if exam.Constraints.RoomConstraints.ExahmRooms {
for _, room := range exam.PlannedRooms {
if !p.GetRoomInfo(room.RoomName).Exahm {
validationMessages = append(validationMessages, aurora.Sprintf(aurora.Red("Is not an exahm room %s for exam %d. %s (%s) in slot (%d,%d)"),
aurora.Magenta(room.RoomName), aurora.Cyan(exam.Ancode), aurora.Cyan(exam.ZpaExam.Module), aurora.Cyan(exam.ZpaExam.MainExamer),
aurora.BrightBlue(exam.PlanEntry.DayNumber), aurora.BrightBlue(exam.PlanEntry.SlotNumber)))
}
}
}
if exam.Constraints.RoomConstraints.Lab {
for _, room := range exam.PlannedRooms {
if !p.GetRoomInfo(room.RoomName).Lab {
validationMessages = append(validationMessages, aurora.Sprintf(aurora.Red("Is not a lab %s for exam %d. %s (%s) in slot (%d,%d)"),
aurora.Magenta(room.RoomName), aurora.Cyan(exam.Ancode), aurora.Cyan(exam.ZpaExam.Module), aurora.Cyan(exam.ZpaExam.MainExamer),
aurora.BrightBlue(exam.PlanEntry.DayNumber), aurora.BrightBlue(exam.PlanEntry.SlotNumber)))
}
}
}
if exam.Constraints.RoomConstraints.PlacesWithSocket {
for _, room := range exam.PlannedRooms {
if !p.GetRoomInfo(room.RoomName).PlacesWithSocket && !p.GetRoomInfo(room.RoomName).Lab {
validationMessages = append(validationMessages, aurora.Sprintf(aurora.Red("Is not a room with places with sockets %s for exam %d. %s (%s) in slot (%d,%d)"),
aurora.Magenta(room.RoomName), aurora.Cyan(exam.Ancode), aurora.Cyan(exam.ZpaExam.Module), aurora.Cyan(exam.ZpaExam.MainExamer),
aurora.BrightBlue(exam.PlanEntry.DayNumber), aurora.BrightBlue(exam.PlanEntry.SlotNumber)))
}
}
}
}
// check rooms for NTAs
// - needsRoomAlone okay
if len(exam.Ntas) > 0 {
spinner.Message(aurora.Sprintf(aurora.Yellow("checking rooms for ntas")))
for _, nta := range exam.Ntas {
if nta.NeedsRoomAlone {
var roomForNta *model.PlannedRoom
for _, room := range exam.PlannedRooms {
if room.NtaMtknr != nil && *room.NtaMtknr == nta.Mtknr {
roomForNta = room
break
}
}
OUTER:
for _, room := range exam.PlannedRooms {
if room.RoomName == roomForNta.RoomName {
for _, mtknr := range room.StudentsInRoom {
if mtknr != nta.Mtknr {
validationMessages = append(validationMessages, aurora.Sprintf(aurora.Red("NTA %s has room %s not alone for exam %d. %s (%s) in slot (%d,%d)"),
aurora.Magenta(nta.Name), aurora.Magenta(room.RoomName), aurora.Cyan(exam.Ancode), aurora.Cyan(exam.ZpaExam.Module), aurora.Cyan(exam.ZpaExam.MainExamer),
aurora.BrightBlue(exam.PlanEntry.DayNumber), aurora.BrightBlue(exam.PlanEntry.SlotNumber)))
break OUTER
}
}
}
}
}
}
}
}
if len(validationMessages) > 0 {
spinner.StopFailMessage(aurora.Sprintf(aurora.Red("%d problems found"), len(validationMessages)))
err = spinner.StopFail()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
for _, msg := range validationMessages {
fmt.Printf(" ↪ %s\n", msg)
}
} else {
err = spinner.Stop()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
return nil
}
func (p *Plexams) ValidateRoomsTimeDistance() error {
ctx := context.Background()
timelag := viper.GetInt("rooms.timelag")
cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[69],
Suffix: aurora.Sprintf(aurora.Cyan(" validating time lag of planned rooms (%d minutes)"), timelag),
SuffixAutoColon: true,
StopCharacter: "✓",
StopColors: []string{"fgGreen"},
StopFailMessage: "error",
StopFailCharacter: "✗",
StopFailColors: []string{"fgRed"},
}
spinner, err := yacspin.New(cfg)
if err != nil {
log.Debug().Err(err).Msg("cannot create spinner")
}
err = spinner.Start()
if err != nil {
log.Debug().Err(err).Msg("cannot start spinner")
}
validationMessages := make([]string, 0)
for _, day := range p.semesterConfig.Days {
spinner.Message(aurora.Sprintf(aurora.Yellow("checking day %d (%s)"), day.Number, day.Date.Format("02.01.06")))
for i := range p.semesterConfig.Starttimes {
if i == len(p.semesterConfig.Days)-1 {
continue
}
slot1, slot2 := i+1, i+2
log.Debug().Int("slot 1", slot1).Int("slot 2", slot2).Msg("checking slot")
plannedRoomsSlot1, err := p.dbClient.PlannedRoomsInSlot(ctx, day.Number, slot1)
if err != nil {
log.Error().Err(err).
Int("day", day.Number).
Int("time", slot1).
Msg("error while getting rooms planned in slot")
return err
}
rooms1 := make(map[string]int)
for _, room := range plannedRoomsSlot1 {
duration, ok := rooms1[room.RoomName]
if !ok {
rooms1[room.RoomName] = room.Duration
} else {
if duration < room.Duration {
rooms1[room.RoomName] = room.Duration
}
}
}
plannedRoomsSlot2, err := p.dbClient.PlannedRoomsInSlot(ctx, day.Number, slot2)
if err != nil {
log.Error().Err(err).
Int("day", day.Number).
Int("time", slot2).
Msg("error while getting rooms planned in slot")
return err
}
rooms2 := set.NewSet[string]()
for _, room := range plannedRoomsSlot2 {
rooms2.Add(room.RoomName)
}
for roomName, maxDuration := range rooms1 {
if rooms2.Contains(roomName) {
start, err := time.Parse("15:04", p.semesterConfig.Starttimes[i].Start)
if err != nil {
log.Error().Err(err).Str("starttime", p.semesterConfig.Starttimes[i].Start).Msg("cannot parse starttime")
return err
}
endSlot1 := start.Add(time.Duration(maxDuration) * time.Minute)
startSlot2, err := time.Parse("15:04", p.semesterConfig.Starttimes[i+1].Start)
if err != nil {
log.Error().Err(err).Str("starttime", p.semesterConfig.Starttimes[i].Start).Msg("cannot parse starttime")
return err
}
log.Debug().Str("room", roomName).Int("max duration", maxDuration).
Str("starttime slot 1", p.semesterConfig.Starttimes[i].Start).
Str("endtime slot 1", endSlot1.Format("15:04")).
Str("starttime slot 2", startSlot2.Format("15:04")).
Msg("checking")
diff := time.Duration(timelag) * time.Minute
if startSlot2.Before(endSlot1.Add(diff)) {
validationMessages = append(validationMessages, aurora.Sprintf(
"Not enough time in room %s between slot (%d/%d) ends %s and slot (%d/%d) begins %s: %g minutes between",
aurora.Magenta(roomName), aurora.BrightBlue(day.Number), aurora.BrightBlue(slot1), aurora.Magenta(endSlot1.Format("15:04")),
aurora.BrightBlue(day.Number), aurora.BrightBlue(slot2), aurora.Magenta(startSlot2.Format("15:04")),
aurora.Magenta(startSlot2.Sub(endSlot1).Minutes()),
))
}
}
}
}
}
if len(validationMessages) > 0 {
spinner.StopFailMessage(aurora.Sprintf(aurora.Red("%d problems found"), len(validationMessages)))
err = spinner.StopFail()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
for _, msg := range validationMessages {
fmt.Printf(" ↪ %s\n", msg)
}
} else {
err = spinner.Stop()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
return nil
}