-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_db.go
79 lines (69 loc) · 2.09 KB
/
validate_db.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
package plexams
import (
"context"
"fmt"
"time"
"github.com/logrusorgru/aurora"
"github.com/obcode/plexams.go/graph/model"
"github.com/rs/zerolog/log"
"github.com/theckman/yacspin"
)
// TODO: all planned_rooms okay? especially after moving an exam? check room -> slot -> ancode sameslot?
func (p *Plexams) ValidateDB() error {
ctx := context.Background()
cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[69],
Suffix: aurora.Sprintf(aurora.Cyan(" validating data base entries")),
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)
planEntries, err := p.dbClient.PlanEntries(ctx)
if err != nil {
log.Error().Err(err).Msg("cannot get planEntries")
}
spinner.Message(aurora.Sprintf(aurora.Yellow(" validating only one plan entry per ancode")))
planEntryMap := make(map[int]*model.PlanEntry)
for _, planEntry := range planEntries {
otherEntry, ok := planEntryMap[planEntry.Ancode]
if ok {
validationMessages = append(validationMessages, aurora.Sprintf(
aurora.Red("more than one plan entry for ancode %d: %v and %v"),
aurora.Magenta(planEntry.Ancode),
aurora.Cyan(otherEntry), aurora.Cyan(planEntry),
))
}
}
if len(validationMessages) > 0 {
spinner.StopFailMessage(aurora.Sprintf(aurora.Red("%d problems"),
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("%d plan entries are okay"),
len(planEntries)))
err = spinner.Stop()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
return nil
}