From 259f514b3d452356f643af3dcd34361cfcd99340 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 9 Apr 2024 20:55:26 +0100 Subject: [PATCH] Add function to write full CIF download to filesystem --- cif/helpers.go | 23 +++++++++++++++++++++-- cif/update.go | 11 +++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cif/helpers.go b/cif/helpers.go index 9b86afa..87878f2 100644 --- a/cif/helpers.go +++ b/cif/helpers.go @@ -112,7 +112,7 @@ func debugWriteFile(header *upstreamApi.JsonTimetableV1, schedule *[]upstreamApi 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") + filename := time.Now().In(londonTimezone).Format("2006-01-02_15:04:05_ParsedCIF") data, err := json.MarshalIndent(map[string]interface{}{ "header": header, @@ -129,9 +129,28 @@ func debugWriteFile(header *upstreamApi.JsonTimetableV1, schedule *[]upstreamApi return } - writeErr := os.WriteFile(filepath+filename, data, 0777) + writeErr := os.WriteFile(filepath+filename+".json", data, 0777) if writeErr != nil { log.Msg.Error("Error writing debug file to disk", zap.Error(writeErr)) } } } + +func debugWriteDownload(input *[]byte) { + if helpers.Runtime == "debug" { + log.Msg.Debug("Writing CIF Download to file") + filepath := "./cif_debug_data/" + filename := time.Now().In(londonTimezone).Format("2006-01-02_15:04:05_RawCIF") + + err := os.MkdirAll(filepath, 0777) + if err != nil { + log.Msg.Error("Error creating directory", zap.Error(err)) + return + } + + err = os.WriteFile(filepath+filename+".jsonl", *input, 0777) + if err != nil { + log.Msg.Error("Error writing debug file to disk", zap.Error(err)) + } + } +} diff --git a/cif/update.go b/cif/update.go index acf212f..5be9bc7 100644 --- a/cif/update.go +++ b/cif/update.go @@ -25,6 +25,11 @@ func runCifFullDownload(cfg *helpers.Configuration) error { log.Msg.Error("Error downloading CIF data", zap.Error(err)) } + // If debug mode is on, call debugWriteDownload + if helpers.Runtime == "debug" { + debugWriteDownload(&data) + } + // Parse CIF file parsed, err := parseCifData(data) if err != nil { @@ -69,6 +74,12 @@ func runCifUpdateDownload(cfg *helpers.Configuration, metadata *dbAccess.CifMeta log.Msg.Error("Error fetching CIF update", zap.Error(err)) return err } + + // If debug mode is on, call debugWriteDownload + if helpers.Runtime == "debug" { + debugWriteDownload(&data) + } + // Parse CIF file parsed, err := parseCifData(data) if err != nil {