Skip to content

Commit 7a15063

Browse files
committed
feat: export planned rooms to json
1 parent 038667f commit 7a15063

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

cmd/export.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var (
11+
exportCmd = &cobra.Command{
12+
Use: "export",
13+
Short: "export [subcommand]",
14+
Long: `Generate various CSVs.
15+
plannedRooms - export rooms of planned exams.`,
16+
Args: cobra.MinimumNArgs(1),
17+
Run: func(cmd *cobra.Command, args []string) {
18+
plexams := initPlexamsConfig()
19+
switch args[0] {
20+
case "plannedRooms":
21+
if len(jsonfile) == 0 {
22+
jsonfile = "PlannedRooms.json"
23+
}
24+
fmt.Printf("generating %s\n", jsonfile)
25+
err := plexams.ExportPlannedRooms(jsonfile)
26+
if err != nil {
27+
os.Exit(1)
28+
}
29+
30+
default:
31+
fmt.Println("export called with unknown sub command")
32+
}
33+
},
34+
}
35+
jsonfile string
36+
)
37+
38+
func init() {
39+
rootCmd.AddCommand(exportCmd)
40+
exportCmd.Flags().StringVarP(&jsonfile, "out", "o", "", "output (csv) file")
41+
}

plexams/export.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package plexams
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"os"
7+
8+
"github.com/obcode/plexams.go/graph/model"
9+
"github.com/rs/zerolog/log"
10+
)
11+
12+
type ExportNtas struct {
13+
Name string `json:"name"`
14+
Duration int `json:"duration"`
15+
}
16+
17+
type ExportPlannedRooms struct {
18+
MainExamer string `json:"mainExamer"`
19+
Module string `json:"module"`
20+
Room string `json:"room"`
21+
Date string `json:"date"`
22+
Starttime string `json:"starttime"`
23+
NumberOfStudents int `json:"numberOfStudents"`
24+
Duration int `json:"duration"`
25+
MaxDuration int `json:"maxDuration"`
26+
Invigilator string `json:"invigilator"`
27+
Ntas []ExportNtas `json:"ntas"`
28+
}
29+
30+
func (p *Plexams) ExportPlannedRooms(jsonfile string) error {
31+
ctx := context.Background()
32+
plannedExams, err := p.PlannedExams(ctx)
33+
if err != nil {
34+
log.Error().Err(err).Msg("cannot get planned exams")
35+
}
36+
37+
exportPlannedRooms := make([]*ExportPlannedRooms, 0)
38+
for _, exam := range plannedExams {
39+
if exam.Constraints != nil && exam.Constraints.NotPlannedByMe {
40+
continue
41+
}
42+
starttime := p.getSlotTime(exam.PlanEntry.DayNumber, exam.PlanEntry.SlotNumber)
43+
44+
plannedRoomsMap := make(map[string][]*model.PlannedRoom)
45+
for _, plannedRoom := range exam.PlannedRooms {
46+
if _, ok := plannedRoomsMap[plannedRoom.RoomName]; !ok {
47+
plannedRoomsMap[plannedRoom.RoomName] = make([]*model.PlannedRoom, 0)
48+
}
49+
plannedRoomsMap[plannedRoom.RoomName] = append(plannedRoomsMap[plannedRoom.RoomName], plannedRoom)
50+
}
51+
52+
for roomName, plannedRooms := range plannedRoomsMap {
53+
invigilator, err := p.GetInvigilatorInSlot(ctx, roomName, exam.PlanEntry.DayNumber, exam.PlanEntry.SlotNumber)
54+
if err != nil {
55+
log.Error().Err(err).Int("ancode", exam.Ancode).Str("room", roomName).
56+
Msg("cannot get invigilator for room")
57+
return err
58+
}
59+
60+
numberOfStudents := 0
61+
maxDuration := 0
62+
ntas := make([]ExportNtas, 0)
63+
64+
for _, plannedRoom := range plannedRooms {
65+
numberOfStudents += len(plannedRoom.StudentsInRoom)
66+
if plannedRoom.Duration > maxDuration {
67+
maxDuration = plannedRoom.Duration
68+
}
69+
if plannedRoom.NtaMtknr != nil && *plannedRoom.NtaMtknr != "" {
70+
nta, err := p.NtaByMtknr(ctx, *plannedRoom.NtaMtknr)
71+
if err != nil {
72+
log.Error().Err(err).Str("mtknr", *plannedRoom.NtaMtknr).Msg("cannot get nta")
73+
return err
74+
}
75+
ntas = append(ntas, ExportNtas{
76+
Name: nta.Name,
77+
Duration: plannedRoom.Duration,
78+
})
79+
}
80+
}
81+
82+
exportPlannedRooms = append(exportPlannedRooms, &ExportPlannedRooms{
83+
MainExamer: exam.ZpaExam.MainExamer,
84+
Module: exam.ZpaExam.Module,
85+
Room: roomName,
86+
Date: starttime.Local().Format("02.01.2006"),
87+
Starttime: starttime.Local().Format("15:04"),
88+
NumberOfStudents: numberOfStudents,
89+
Duration: exam.ZpaExam.Duration,
90+
MaxDuration: maxDuration,
91+
Invigilator: invigilator.Shortname,
92+
Ntas: ntas,
93+
})
94+
}
95+
}
96+
97+
file, err := os.Create(jsonfile)
98+
if err != nil {
99+
log.Error().Err(err).Msg("cannot create JSON file")
100+
return err
101+
}
102+
defer file.Close()
103+
104+
encoder := json.NewEncoder(file)
105+
encoder.SetIndent("", " ")
106+
if err := encoder.Encode(exportPlannedRooms); err != nil {
107+
log.Error().Err(err).Msg("cannot encode JSON")
108+
return err
109+
}
110+
111+
return nil
112+
}

0 commit comments

Comments
 (0)