From 2c34c529e169b834126a322c58e997677cc44791 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 9 Apr 2024 20:38:40 +0100 Subject: [PATCH] Add debug option to write CIF data to file. --- .gitignore | 3 +++ cif/helpers.go | 32 ++++++++++++++++++++++++++++++++ cif/update.go | 10 ++++++++++ 3 files changed, 45 insertions(+) diff --git a/.gitignore b/.gitignore index 3697f25..7886a44 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,9 @@ message-logs main timetable-mgr +# Debug data +cif_debug_data + # Test binary, built with `go test -c` *.test diff --git a/cif/helpers.go b/cif/helpers.go index b3e1e9a..9b86afa 100644 --- a/cif/helpers.go +++ b/cif/helpers.go @@ -1,9 +1,13 @@ package cif import ( + "encoding/json" "errors" + "os" "time" + "git.fjla.uk/owlboard/go-types/pkg/upstreamApi" + "git.fjla.uk/owlboard/timetable-mgr/helpers" "git.fjla.uk/owlboard/timetable-mgr/log" "go.uber.org/zap" ) @@ -103,3 +107,31 @@ func parseDaysRun(daysBinary *string) []string { } return result } + +func debugWriteFile(header *upstreamApi.JsonTimetableV1, schedule *[]upstreamApi.JsonScheduleV1) { + if helpers.Runtime == "debug" { + log.Msg.Debug("Writing CIF Header and Schedule elements to file") + filepath := "./cif_debug_data/" + filename := time.Now().In(londonTimezone).Format("2006-01-02_15:04:05_CIF") + + data, err := json.MarshalIndent(map[string]interface{}{ + "header": header, + "schedule": schedule, + }, "", " ") + if err != nil { + log.Msg.Error("Error marshalling data", zap.Error(err)) + return + } + + err = os.MkdirAll(filepath, 0777) + if err != nil { + log.Msg.Error("Error creating directory", zap.Error(err)) + return + } + + writeErr := os.WriteFile(filepath+filename, data, 0777) + if writeErr != nil { + log.Msg.Error("Error writing debug file to disk", zap.Error(writeErr)) + } + } +} diff --git a/cif/update.go b/cif/update.go index fa6c324..acf212f 100644 --- a/cif/update.go +++ b/cif/update.go @@ -35,6 +35,11 @@ func runCifFullDownload(cfg *helpers.Configuration) error { // Drop timetable collection dbAccess.DropCollection(dbAccess.TimetableCollection) // I should edit this to prevent removal of VSTP entries in the database. + // If debug mode is on, call debugWriteFile + if helpers.Runtime == "debug" { + debugWriteFile(&parsed.header, &parsed.sched) + } + // Process CIF file err = processParsedCif(parsed) if err != nil { @@ -71,6 +76,11 @@ func runCifUpdateDownload(cfg *helpers.Configuration, metadata *dbAccess.CifMeta return err } + // If debug mode is on, call debugWriteFile + if helpers.Runtime == "debug" { + debugWriteFile(&parsed.header, &parsed.sched) + } + // Check CIF Sequence // Skip if LastSequence is >= to this sequence if metadata.LastSequence >= parsed.header.Metadata.Sequence {