From c956fc81ccc214cd10c1ac584dfa58d5128d90a0 Mon Sep 17 00:00:00 2001 From: Oliver Braun Date: Wed, 25 Jun 2025 15:52:52 +0200 Subject: [PATCH] chore: nices output and renamed some things --- cmd/email.go | 6 +++--- cmd/invigilation.go | 9 +++++++-- db/zpa.go | 13 +++++++++++++ plexams/email.go | 4 ++-- plexams/email_published.go | 2 +- ...shedEmail.tmpl => publishedEmailExams.tmpl} | 0 ...lHTML.tmpl => publishedEmailExamsHTML.tmpl} | 0 plexams/validate_invigilation.go | 18 +++++++++++++----- plexams/zpa_get.go | 4 ++++ 9 files changed, 43 insertions(+), 13 deletions(-) rename plexams/tmpl/{publishedEmail.tmpl => publishedEmailExams.tmpl} (100%) rename plexams/tmpl/{publishedEmailHTML.tmpl => publishedEmailExamsHTML.tmpl} (100%) diff --git a/cmd/email.go b/cmd/email.go index 0f46d2d..9c20373 100644 --- a/cmd/email.go +++ b/cmd/email.go @@ -19,7 +19,7 @@ primuss-data [all|] --- send emails to teachers about primuss data and n constraints --- ask for constraints prepared --- announce exams to plan and constraints draft --- announce draft plan -published --- announce published exams +published-exams --- announce published exams invigilations --- send email requesting invigilations constraints nta-with-room-alone --- send emails to students with room alone before planning nta-planned --- send emails about rooms to all students with nta after planning @@ -58,8 +58,8 @@ nta-planned --- send emails about rooms to all students with nta after plann if err != nil { log.Fatalf("got error: %v\n", err) } - case "published": - err := plexams.SendEmailPublished(context.Background(), run) + case "published-exams": + err := plexams.SendEmailPublishedExams(context.Background(), run) if err != nil { log.Fatalf("got error: %v\n", err) } diff --git a/cmd/invigilation.go b/cmd/invigilation.go index a4239c3..e86bda7 100644 --- a/cmd/invigilation.go +++ b/cmd/invigilation.go @@ -58,8 +58,13 @@ reserve [daynumber] [slotnumber] [invigilator ID] --- add reserve for slot (d invigilatorID, err := strconv.Atoi(args[3]) if err != nil { - fmt.Printf("cannot use %s as invigilators id", args[3]) - os.Exit(1) + // find invigilator by name + invigilatorName := args[3] + invigilatorID, err = plexams.GetTeacherIdByRegex(ctx, invigilatorName) + if err != nil || invigilatorID == 0 { + fmt.Printf("cannot find invigilator with regex %s", args[3]) + os.Exit(1) + } } oldInvigilator, err := plexams.GetInvigilatorInSlot(ctx, room, day, slot) diff --git a/db/zpa.go b/db/zpa.go index 2e16979..5d6db9a 100644 --- a/db/zpa.go +++ b/db/zpa.go @@ -28,6 +28,19 @@ func (db *DB) GetTeacher(ctx context.Context, id int) (*model.Teacher, error) { return &teacher, nil } +func (db *DB) GetTeacherIdByRegex(ctx context.Context, name string) (int, error) { + collection := db.Client.Database(db.databaseName).Collection("teachers") + + var teacher model.Teacher + err := collection.FindOne(ctx, bson.D{{Key: "fullname", Value: bson.D{{Key: "$regex", Value: name}}}}).Decode(&teacher) + if err != nil { + log.Error().Err(err).Str("name", name).Msg("cannot find teacher in db") + return 0, err + } + + return teacher.ID, nil +} + func (db *DB) GetTeachers(ctx context.Context) ([]*model.Teacher, error) { return db.getTeachers(ctx, func(model.Teacher) bool { return true }) } diff --git a/plexams/email.go b/plexams/email.go index 2fca641..ae55b60 100644 --- a/plexams/email.go +++ b/plexams/email.go @@ -33,8 +33,8 @@ import ( //go:embed tmpl/handicapEmailRoomAloneHTML.tmpl //go:embed tmpl/preparedEmail.tmpl //go:embed tmpl/preparedEmailHTML.tmpl -//go:embed tmpl/publishedEmail.tmpl -//go:embed tmpl/publishedEmailHTML.tmpl +//go:embed tmpl/publishedEmailExams.tmpl +//go:embed tmpl/publishedEmailExamsHTML.tmpl //go:embed tmpl/invigilationEmail.tmpl //go:embed tmpl/invigilationEmailHTML.tmpl var emailTemplates embed.FS diff --git a/plexams/email_published.go b/plexams/email_published.go index a3d569a..1b83bb1 100644 --- a/plexams/email_published.go +++ b/plexams/email_published.go @@ -12,7 +12,7 @@ import ( "github.com/theckman/yacspin" ) -func (p *Plexams) SendEmailPublished(ctx context.Context, run bool) error { +func (p *Plexams) SendEmailPublishedExams(ctx context.Context, run bool) error { cfg := yacspin.Config{ Frequency: 100 * time.Millisecond, CharSet: yacspin.CharSets[69], diff --git a/plexams/tmpl/publishedEmail.tmpl b/plexams/tmpl/publishedEmailExams.tmpl similarity index 100% rename from plexams/tmpl/publishedEmail.tmpl rename to plexams/tmpl/publishedEmailExams.tmpl diff --git a/plexams/tmpl/publishedEmailHTML.tmpl b/plexams/tmpl/publishedEmailExamsHTML.tmpl similarity index 100% rename from plexams/tmpl/publishedEmailHTML.tmpl rename to plexams/tmpl/publishedEmailExamsHTML.tmpl diff --git a/plexams/validate_invigilation.go b/plexams/validate_invigilation.go index 732e194..9775d14 100644 --- a/plexams/validate_invigilation.go +++ b/plexams/validate_invigilation.go @@ -272,8 +272,11 @@ func (p *Plexams) ValidateInvigilatorSlots() error { roomWithoutInvigilatorDay := make(map[int]int) slotWithoutReserveDay := make(map[int]int) + maxInvigsMissingInOneSlot := make(map[int]int) + // all rooms and reserve max one invigilator for _, slot := range p.semesterConfig.Slots { + invigsMissing := 0 spinner.Message(aurora.Sprintf(aurora.Magenta("checking slot (%d,%d)"), aurora.Cyan(slot.DayNumber), aurora.Cyan(slot.SlotNumber))) @@ -291,6 +294,7 @@ func (p *Plexams) ValidateInvigilatorSlots() error { if len(invigilations) == 0 { slotWithoutReserveDay[slot.DayNumber]++ + invigsMissing++ } else if len(invigilations) > 1 { validationMessages = append(validationMessages, aurora.Sprintf(aurora.Red("more than one reserve invigilator in slot (%d,%d)"), aurora.Magenta(slot.DayNumber), aurora.Magenta(slot.SlotNumber))) @@ -307,11 +311,15 @@ func (p *Plexams) ValidateInvigilatorSlots() error { } if len(invigilations) == 0 { roomWithoutInvigilatorDay[slot.DayNumber]++ + invigsMissing++ } else if len(invigilations) > 1 { - validationMessages = append(validationMessages, aurora.Sprintf(aurora.Red("more than one invigilator for room %s in slot (%d,%d)"), + validationMessages = append(validationMessages, aurora.Sprintf(aurora.Yellow("more than one invigilator for room %s in slot (%d,%d)"), aurora.Magenta(room), aurora.Magenta(slot.DayNumber), aurora.Magenta(slot.SlotNumber))) } } + if invigsMissing > maxInvigsMissingInOneSlot[slot.DayNumber] { + maxInvigsMissingInOneSlot[slot.DayNumber] = invigsMissing + } } if len(roomWithoutInvigilatorDay) > 0 || len(slotWithoutReserveDay) > 0 { @@ -332,16 +340,16 @@ func (p *Plexams) ValidateInvigilatorSlots() error { if roomsWithoutInvig+slotsWithoutReserve > 0 { var msg strings.Builder - msg.WriteString(aurora.Sprintf(aurora.Red("Day %2d: %2d open invigilations, "), - aurora.Magenta(day), aurora.Cyan(roomsWithoutInvig+slotsWithoutReserve))) + msg.WriteString(aurora.Sprintf(aurora.Yellow("Day %2d: %2d open invigilations, %2d max. in one Slot "), + aurora.Magenta(day), aurora.Cyan(roomsWithoutInvig+slotsWithoutReserve), aurora.Cyan(maxInvigsMissingInOneSlot[day]))) if roomsWithoutInvig > 0 { - msg.WriteString(aurora.Sprintf(aurora.Red("%2d rooms without invigilator,"), aurora.Cyan(roomsWithoutInvig))) + msg.WriteString(aurora.Sprintf(aurora.Yellow("%2d rooms without invigilator,"), aurora.Cyan(roomsWithoutInvig))) } else { msg.WriteString(" ") } if slotsWithoutReserve > 0 { - msg.WriteString(aurora.Sprintf(aurora.Red("%2d slots without reserve"), aurora.Cyan(slotsWithoutReserve))) + msg.WriteString(aurora.Sprintf(aurora.Yellow("%2d slots without reserve"), aurora.Cyan(slotsWithoutReserve))) } validationMessages = append(validationMessages, msg.String()) diff --git a/plexams/zpa_get.go b/plexams/zpa_get.go index aa4dece..5d71c0d 100644 --- a/plexams/zpa_get.go +++ b/plexams/zpa_get.go @@ -15,6 +15,10 @@ func (p *Plexams) GetTeacher(ctx context.Context, id int) (*model.Teacher, error return p.dbClient.GetTeacher(ctx, id) } +func (p *Plexams) GetTeacherIdByRegex(ctx context.Context, name string) (int, error) { + return p.dbClient.GetTeacherIdByRegex(ctx, name) +} + func (p *Plexams) GetTeachers(ctx context.Context, fromZpa *bool) ([]*model.Teacher, error) { if fromZpa != nil && *fromZpa { if err := p.SetZPA(); err != nil {