timetable-mgr/cif/check.go

67 lines
1.9 KiB
Go
Raw Normal View History

2024-03-26 16:04:42 +00:00
package cif
import (
2024-04-04 13:58:18 +01:00
"time"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
"git.fjla.uk/owlboard/timetable-mgr/helpers"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
)
2024-04-03 22:25:27 +01:00
// Checks if the CIF Data needs updating, and what type of update is needed (Full/Partial) and if partial
// what days data needs updating, then calls an update function to handle the update.
func CheckCif(cfg *helpers.Configuration) {
// Check that it is after 0600, if not then skip update
2024-04-04 13:58:18 +01:00
if time.Now().In(londonTimezone).Hour() <= dataAvailable {
2024-04-14 19:03:13 +01:00
log.Info("Too early to update CIF data, not published until 0600")
2024-04-04 13:58:18 +01:00
return
}
2024-04-03 22:25:27 +01:00
2024-04-14 20:07:56 +01:00
log.Debug("Checking age of CIF Data")
2024-04-03 22:25:27 +01:00
// Load and read metadata from database
metadata, err := dbAccess.GetCifMetadata()
if err != nil {
2024-04-14 19:03:13 +01:00
log.Error("Unable to read last update time", zap.Error(err))
2024-04-03 22:25:27 +01:00
return
}
2024-04-04 22:39:09 +01:00
// If no metadata is found in DB, presume no CIF data exists
2024-04-03 22:25:27 +01:00
if metadata == nil {
2024-04-14 19:03:13 +01:00
log.Info("Full CIF download required")
2024-04-03 22:25:27 +01:00
err := runCifFullDownload(cfg)
if err != nil {
log.Warn("Unable to run full CIF Update", zap.Error(err))
2024-04-03 22:25:27 +01:00
}
return
}
2024-04-04 13:58:18 +01:00
// Check if last update was today
if isSameToday(metadata.LastUpdate) {
2024-04-14 19:03:13 +01:00
log.Info("CIF Data has already been updated today, skipping")
2024-04-04 13:58:18 +01:00
return
}
2024-04-04 22:39:09 +01:00
// Check how many days since last update, if more than 5, run full update, else run update
daysSinceLastUpdate := howManyDaysAgo(metadata.LastUpdate)
if daysSinceLastUpdate > 5 {
2024-04-14 19:03:13 +01:00
log.Debug("Full Update Requested due to time since last update", zap.Int("daysSinceLastUpdate", daysSinceLastUpdate))
log.Info("Full CIF download required")
2024-04-04 22:39:09 +01:00
err := runCifFullDownload(cfg)
if err != nil {
log.Error("Full CIF update failed", zap.Error(err))
2024-04-04 22:39:09 +01:00
}
return
}
daysToUpdate := generateUpdateDays(daysSinceLastUpdate)
// Run the update
2024-04-14 19:03:13 +01:00
log.Info("CIF Update required", zap.Any("days to update", daysToUpdate))
2024-04-04 22:39:09 +01:00
err = runCifUpdateDownload(cfg, metadata, daysToUpdate)
if err != nil {
log.Warn("Daily CIF update failed", zap.Error(err))
2024-04-04 22:39:09 +01:00
}
2024-04-03 22:25:27 +01:00
}