From 46c82eefa678917513015eec5c7a01823a39710d Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 27 Mar 2024 23:35:03 +0000 Subject: [PATCH] Add logic to avoid timetable updates too early --- src/cif/check.go | 23 ++++++++++++++++++----- src/cif/helpers.go | 15 +++++++++++++++ src/cif/{runner.go => process.go} | 0 src/cif/update.go | 17 +++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) rename src/cif/{runner.go => process.go} (100%) create mode 100644 src/cif/update.go diff --git a/src/cif/check.go b/src/cif/check.go index 72bfdf1..8e12768 100644 --- a/src/cif/check.go +++ b/src/cif/check.go @@ -17,15 +17,28 @@ func CifCheck(cfg *helpers.Configuration) { } lastUpdate := time.Unix(utime, 0) - currentTime := time.Now() + currentTime := time.Now().In(time.UTC) + london, _ := time.LoadLocation("Europe/London") + londonTime := currentTime.In(london) dataAge := currentTime.Sub(lastUpdate) - day := 24 * time.Hour + maxAge := 22 * time.Hour - log.Msg.Debug("CIF Data", zap.Duration("Data Age", dataAge), zap.Duration("Max Age", day)) + log.Msg.Debug("CIF Data", zap.Duration("Data Age", dataAge), zap.Duration("Max Age", maxAge)) - if dataAge >= day { + if dataAge >= maxAge { log.Msg.Warn("Timetable data is more than 24 hours old") - // Call Update Here + if londonTime.Hour() >= updateHour { + runUpdate("daily") + } else { + log.Msg.Debug("Timetable update is required but data is not yet available") + } + } else if dataAge > 48 { + log.Msg.Warn("Timetable data is more than 48 hours old") + if londonTime.Hour() >= updateHour { + runUpdate("full") + } else { + log.Msg.Debug("Waiting until todays data is available before updating") + } } else { log.Msg.Info("Timetable data is less than 24 hours old") } diff --git a/src/cif/helpers.go b/src/cif/helpers.go index af2cc4a..66faf5f 100644 --- a/src/cif/helpers.go +++ b/src/cif/helpers.go @@ -1,12 +1,17 @@ package cif import ( + "errors" "time" "git.fjla.uk/owlboard/timetable-mgr/log" "go.uber.org/zap" ) +const updateHour = 7 +const dailyUpdateUrl = "https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_ALL_UPDATE_DAILY&day=toc-update-" +const fullUpdateUrl = "https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_ALL_FULL_DAILY&day=toc-full" + func getDayString() string { london, err := time.LoadLocation("Europe/London") if err != nil { @@ -20,3 +25,13 @@ func getDayString() string { return dayStrings[day] } + +func getUpdateUrl(updateType string) (string, error) { + if updateType == "daily" { + return dailyUpdateUrl + getDayString(), nil + } else if updateType == "full" { + return fullUpdateUrl, nil + } + err := errors.New("Invalid update type provided, must be one of 'daily' or 'full'") + return "", err +} diff --git a/src/cif/runner.go b/src/cif/process.go similarity index 100% rename from src/cif/runner.go rename to src/cif/process.go diff --git a/src/cif/update.go b/src/cif/update.go new file mode 100644 index 0000000..0488c21 --- /dev/null +++ b/src/cif/update.go @@ -0,0 +1,17 @@ +package cif + +import ( + "errors" + + "git.fjla.uk/owlboard/timetable-mgr/log" + "go.uber.org/zap" +) + +func runUpdate(updateType string) error { + url, err := getUpdateUrl(updateType) + if err != nil { + log.Msg.Error("Unable to get the update URL", zap.Error(err)) + return err + } + return errors.New("This function is not yet defined") +}