Skip to content

Commit

Permalink
more added ancodes stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
obcode committed Nov 17, 2023
1 parent cc506dd commit 7d412d5
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 67 deletions.
25 changes: 22 additions & 3 deletions db/primuss.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
169 changes: 142 additions & 27 deletions db/zpa_exams.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"sort"
"time"

set "github.com/deckarep/golang-set/v2"
Expand All @@ -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

Expand All @@ -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)
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
}
116 changes: 79 additions & 37 deletions plexams/exam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 7d412d5

Please sign in to comment.