Add logic to avoid timetable updates too early

This commit is contained in:
Fred Boniface 2024-03-27 23:35:03 +00:00
parent 848f4bf7bd
commit 46c82eefa6
4 changed files with 50 additions and 5 deletions

View File

@ -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")
}

View File

@ -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
}

17
src/cif/update.go Normal file
View File

@ -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")
}