From 7d412d5432a6cc395306408084ea8ea0e5633df8 Mon Sep 17 00:00:00 2001 From: Oliver Braun Date: Fri, 17 Nov 2023 11:31:29 +0100 Subject: [PATCH] more added ancodes stuff --- db/primuss.go | 25 ++++++- db/zpa_exams.go | 169 ++++++++++++++++++++++++++++++++++++++++-------- plexams/exam.go | 116 ++++++++++++++++++++++----------- 3 files changed, 243 insertions(+), 67 deletions(-) diff --git a/db/primuss.go b/db/primuss.go index 3074341..588e17b 100644 --- a/db/primuss.go +++ b/db/primuss.go @@ -84,7 +84,26 @@ func (db *DB) GetAddedAncodes(ctx context.Context) (map[int][]model.ZPAPrimussAn return addedAcodesMap, nil } -func (db *DB) GetAddedAncodesForAncode(ctx context.Context, ancode int) ([]*model.ZPAPrimussAncodes, error) { - // IMPLEMENT ME - return nil, nil +func (db *DB) GetAddedAncodesForAncode(ctx context.Context, ancode int) ([]model.ZPAPrimussAncodes, error) { + collection := db.Client.Database(db.databaseName).Collection(collectionPrimussAncodes) + + cur, err := collection.Find(ctx, bson.D{{Key: "ancode", Value: ancode}}) + if err != nil { + log.Error().Err(err).Msg("cannot get added ancodes") + return nil, err + } + + var addedAncodes []model.AddedPrimussAncode + err = cur.All(ctx, &addedAncodes) + if err != nil { + log.Error().Err(err).Msg("cannot decode added ancodes") + return nil, err + } + + added := make([]model.ZPAPrimussAncodes, 0, len(addedAncodes)) + for _, addedAncode := range addedAncodes { + added = append(added, addedAncode.PrimussAncode) + } + + return added, nil } diff --git a/db/zpa_exams.go b/db/zpa_exams.go index 5479308..9d81ba3 100644 --- a/db/zpa_exams.go +++ b/db/zpa_exams.go @@ -2,6 +2,7 @@ package db import ( "context" + "sort" "time" set "github.com/deckarep/golang-set/v2" @@ -26,6 +27,12 @@ func (db *DB) GetZPAExams(ctx context.Context) ([]*model.ZPAExam, error) { } defer cur.Close(ctx) + addedAncodes, err := db.GetAddedAncodes(ctx) + if err != nil { + log.Error().Err(err).Msg("cannot get added ancodes") + return nil, err + } + for cur.Next(ctx) { var exam model.ZPAExam @@ -36,6 +43,19 @@ func (db *DB) GetZPAExams(ctx context.Context) ([]*model.ZPAExam, error) { return exams, err } + db.cleanupPrimussAncodes(&exam) + addedAncodesForAncode, ok := addedAncodes[exam.AnCode] + + if ok { + err := db.addAddedAncodesToExam(ctx, &exam, addedAncodesForAncode) + if err != nil { + log.Error().Err(err).Int("ancode", exam.AnCode). + Interface("added ancodes", addedAncodesForAncode). + Msg("error when trying to add added ancodes to exam") + return nil, err + } + } + exams = append(exams, &exam) } @@ -58,6 +78,22 @@ func (db *DB) GetZpaExamByAncode(ctx context.Context, ancode int) (*model.ZPAExa return nil, err } + db.cleanupPrimussAncodes(&result) + addedAncodes, err := db.GetAddedAncodesForAncode(ctx, result.AnCode) + if err != nil { + log.Error().Err(err).Str("semester", db.semester). + Int("ancode", ancode).Msg("cannot get added ancodes") + return nil, err + } + if addedAncodes != nil { + err := db.addAddedAncodesToExam(ctx, &result, addedAncodes) + if err != nil { + log.Error().Err(err).Str("semester", db.semester). + Int("ancode", ancode).Msg("cannot add added ancodes") + return nil, err + } + } + return &result, nil } @@ -198,39 +234,21 @@ func (db *DB) getZPAExamsPlannedOrNot(ctx context.Context, toPlan *bool) ([]*mod exams := make([]*model.ZPAExam, 0, (*ancodeSet).Cardinality()) - // add added ancodes for _, zpaExam := range zpaExams { if (*ancodeSet).Contains(zpaExam.AnCode) { + db.cleanupPrimussAncodes(zpaExam) addedAncodesForAncode, ok := addedAncodes[zpaExam.AnCode] - if ok { - rmNewAncodes := make([]model.ZPAPrimussAncodes, 0, len(zpaExam.PrimussAncodes)) - OUTER: - for _, ancode := range zpaExam.PrimussAncodes { - for _, newAncode := range addedAncodesForAncode { - if ancode.Program == newAncode.Program { - break OUTER - } - } - rmNewAncodes = append(rmNewAncodes, ancode) - } - zpaExam.PrimussAncodes = append(rmNewAncodes, addedAncodesForAncode...) - } - // add dummy ancodes if no ancode for group is present - OUTER2: - for _, group := range zpaExam.Groups { - groupShort := group[:2] - for _, ancodes := range zpaExam.PrimussAncodes { - if ancodes.Program == groupShort { - break OUTER2 - } + if ok { + err := db.addAddedAncodesToExam(ctx, zpaExam, addedAncodesForAncode) + if err != nil { + log.Error().Err(err).Int("ancode", zpaExam.AnCode). + Interface("added ancodes", addedAncodesForAncode). + Msg("error when trying to add added ancodes to exam") + return nil, err } - zpaExam.PrimussAncodes = append(zpaExam.PrimussAncodes, - model.ZPAPrimussAncodes{ - Program: groupShort, - Ancode: -1, - }) } + exams = append(exams, zpaExam) } } @@ -284,3 +302,100 @@ func (db *DB) getZpaAncodesPlannedOrNot(ctx context.Context, toPlan *bool) (*set return &resultSet, nil } + +func (db *DB) cleanupPrimussAncodes(zpaExam *model.ZPAExam) { + programs := set.NewSet[string]() + + ancodesMap := make(map[string]int) + for _, group := range zpaExam.Groups { + ancodesMap[group[:2]] = -1 + programs.Add(group[:2]) + } + + for _, primussAncode := range zpaExam.PrimussAncodes { + if programs.Contains(primussAncode.Program) { + ancodesMap[primussAncode.Program] = primussAncode.Ancode + } + } + + programSlice := programs.ToSlice() + sort.Strings(programSlice) + + newPrimussAncodes := make([]model.ZPAPrimussAncodes, 0, len(ancodesMap)) + + for _, program := range programSlice { + newPrimussAncodes = append(newPrimussAncodes, model.ZPAPrimussAncodes{ + Program: program, + Ancode: ancodesMap[program], + }) + } + + zpaExam.PrimussAncodes = newPrimussAncodes +} + +func (db *DB) addAddedAncodesToExam(ctx context.Context, zpaExam *model.ZPAExam, addedAncodesForAncode []model.ZPAPrimussAncodes) error { + if addedAncodesForAncode == nil { + var err error + addedAncodesForAncode, err = db.GetAddedAncodesForAncode(ctx, zpaExam.AnCode) + if err != nil { + log.Error().Err(err).Int("ancode", zpaExam.AnCode).Msg("cannot get added ancodes") + return err + } + if len(addedAncodesForAncode) == 0 { + return nil + } + } + + allPrimussAncodes := append(zpaExam.PrimussAncodes, addedAncodesForAncode...) + + ancodesMap := make(map[string]int) + programs := set.NewSet[string]() + for _, primussAncode := range allPrimussAncodes { + ancodesMap[primussAncode.Program] = primussAncode.Ancode + programs.Add(primussAncode.Program) + } + + programSlice := programs.ToSlice() + sort.Strings(programSlice) + + newPrimussAncodes := make([]model.ZPAPrimussAncodes, 0, len(ancodesMap)) + + for _, program := range programSlice { + newPrimussAncodes = append(newPrimussAncodes, model.ZPAPrimussAncodes{ + Program: program, + Ancode: ancodesMap[program], + }) + } + + zpaExam.PrimussAncodes = newPrimussAncodes + + // rmNewAncodes := make([]model.ZPAPrimussAncodes, 0, len(zpaExam.PrimussAncodes)) + // OUTER: + // for _, ancode := range zpaExam.PrimussAncodes { + // for _, newAncode := range addedAncodesForAncode { + // if ancode.Program == newAncode.Program { + // break OUTER + // } + // } + // rmNewAncodes = append(rmNewAncodes, ancode) + // } + // zpaExam.PrimussAncodes = append(rmNewAncodes, addedAncodesForAncode...) + + // // add dummy ancodes if no ancode for group is present + // OUTER2: + // for _, group := range zpaExam.Groups { + // groupShort := group[:2] + // for _, ancodes := range zpaExam.PrimussAncodes { + // if ancodes.Program == groupShort { + // break OUTER2 + // } + // } + // zpaExam.PrimussAncodes = append(zpaExam.PrimussAncodes, + // model.ZPAPrimussAncodes{ + // Program: groupShort, + // Ancode: -1, + // }) + // } + + return nil +} diff --git a/plexams/exam.go b/plexams/exam.go index a6988a2..4ff7f3a 100644 --- a/plexams/exam.go +++ b/plexams/exam.go @@ -22,62 +22,104 @@ func (p *Plexams) prepareConnectedExam(ctx context.Context, ancode int, allProgr return nil, err } - allKeys := make(map[string]bool) - programs := []string{} - for _, group := range zpaExam.Groups { - program := group[:2] - if _, value := allKeys[program]; !value { - allKeys[program] = true - programs = append(programs, program) - } - } + // allKeys := make(map[string]bool) + // programs := []string{} + // for _, group := range zpaExam.Groups { + // program := group[:2] + // if _, value := allKeys[program]; !value { + // allKeys[program] = true + // programs = append(programs, program) + // } + // } primussExams := make([]*model.PrimussExam, 0) var errors []string - for _, program := range programs { - primussExam, err := p.GetPrimussExam(ctx, program, ancode) + // Replace with primuss ancodes + for _, primussAncode := range zpaExam.PrimussAncodes { + primussExam, err := p.GetPrimussExam(ctx, primussAncode.Program, primussAncode.Ancode) if err != nil { if errors == nil { errors = make([]string, 0) } - errors = append(errors, fmt.Sprintf("%s/%d not found", program, ancode)) + errors = append(errors, fmt.Sprintf("%s/%d not found", primussAncode.Program, primussAncode.Ancode)) } else { primussExams = append(primussExams, primussExam) } } - otherPrograms := make([]string, 0, len(allPrograms)-len(programs)) -OUTER: - for _, aP := range allPrograms { - for _, p := range programs { - if aP == p { - continue OUTER - } - } - otherPrograms = append(otherPrograms, aP) - } + // FIXME: I do not need other programs? + // otherPrograms := make([]string, 0, len(allPrograms)-len(programs)) + // OUTER: + // for _, aP := range allPrograms { + // for _, p := range programs { + // if aP == p { + // continue OUTER + // } + // } + // otherPrograms = append(otherPrograms, aP) + // } - var otherPrimussExams []*model.PrimussExam + // var otherPrimussExams []*model.PrimussExam + + // for _, program := range otherPrograms { + // primussExam, err := p.GetPrimussExam(ctx, program, ancode) + // if err == nil { + // if otherPrimussExams == nil { + // otherPrimussExams = make([]*model.PrimussExam, 0) + // } + // if errors == nil { + // errors = make([]string, 0) + // } + // errors = append(errors, fmt.Sprintf("found %s/%d (%s: %s)", program, ancode, primussExam.MainExamer, primussExam.Module)) + // otherPrimussExams = append(otherPrimussExams, primussExam) + // } + // } - for _, program := range otherPrograms { - primussExam, err := p.GetPrimussExam(ctx, program, ancode) - if err == nil { - if otherPrimussExams == nil { - otherPrimussExams = make([]*model.PrimussExam, 0) - } - if errors == nil { - errors = make([]string, 0) - } - errors = append(errors, fmt.Sprintf("found %s/%d (%s: %s)", program, ancode, primussExam.MainExamer, primussExam.Module)) - otherPrimussExams = append(otherPrimussExams, primussExam) - } - } + // Old version: + // for _, program := range programs { + // primussExam, err := p.GetPrimussExam(ctx, program, ancode) + // if err != nil { + // if errors == nil { + // errors = make([]string, 0) + // } + // errors = append(errors, fmt.Sprintf("%s/%d not found", program, ancode)) + // } else { + // primussExams = append(primussExams, primussExam) + // } + // } + + // otherPrograms := make([]string, 0, len(allPrograms)-len(programs)) + // OUTER: + // for _, aP := range allPrograms { + // for _, p := range programs { + // if aP == p { + // continue OUTER + // } + // } + // otherPrograms = append(otherPrograms, aP) + // } + + // var otherPrimussExams []*model.PrimussExam + + // for _, program := range otherPrograms { + // primussExam, err := p.GetPrimussExam(ctx, program, ancode) + // if err == nil { + // if otherPrimussExams == nil { + // otherPrimussExams = make([]*model.PrimussExam, 0) + // } + // if errors == nil { + // errors = make([]string, 0) + // } + // errors = append(errors, fmt.Sprintf("found %s/%d (%s: %s)", program, ancode, primussExam.MainExamer, primussExam.Module)) + // otherPrimussExams = append(otherPrimussExams, primussExam) + // } + // } return &model.ConnectedExam{ ZpaExam: zpaExam, PrimussExams: primussExams, - OtherPrimussExams: otherPrimussExams, + OtherPrimussExams: nil, // otherPrimussExams, Errors: errors, }, nil }