2024-03-26 16:04:42 +00:00
|
|
|
package cif
|
|
|
|
|
2024-03-26 22:33:11 +00:00
|
|
|
import (
|
2024-04-04 13:58:18 +01:00
|
|
|
"time"
|
|
|
|
|
2024-03-26 22:33:11 +00:00
|
|
|
"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 {
|
|
|
|
log.Msg.Info("Too early to update CIF data, not published until 0600")
|
|
|
|
return
|
|
|
|
}
|
2024-04-03 22:25:27 +01:00
|
|
|
|
|
|
|
log.Msg.Info("Checking age of CIF Data")
|
|
|
|
|
|
|
|
// Load and read metadata from database
|
|
|
|
metadata, err := dbAccess.GetCifMetadata()
|
|
|
|
if err != nil {
|
2024-04-12 20:43:03 +01:00
|
|
|
log.Msg.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 {
|
|
|
|
log.Msg.Info("Full CIF download required")
|
|
|
|
err := runCifFullDownload(cfg)
|
|
|
|
if err != nil {
|
2024-04-04 22:39:09 +01:00
|
|
|
log.Msg.Error("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) {
|
|
|
|
log.Msg.Info("CIF Data has already been updated today, skipping")
|
|
|
|
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-08 21:08:07 +01:00
|
|
|
log.Msg.Debug("Full Update Requested due to time since last update", zap.Int("daysSinceLastUpdate", daysSinceLastUpdate))
|
2024-04-04 22:39:09 +01:00
|
|
|
log.Msg.Info("Full CIF download required")
|
|
|
|
err := runCifFullDownload(cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Unable to run full CIF Update", zap.Error(err))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
daysToUpdate := generateUpdateDays(daysSinceLastUpdate)
|
|
|
|
|
|
|
|
// Run the update
|
|
|
|
log.Msg.Info("CIF Update required", zap.Any("days to update", daysToUpdate))
|
|
|
|
err = runCifUpdateDownload(cfg, metadata, daysToUpdate)
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Unable to run CIF update", zap.Error(err))
|
|
|
|
}
|
2024-04-03 22:25:27 +01:00
|
|
|
}
|