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:27:05 +01:00
|
|
|
// REWRITING CifCheck into CheckCif (at bottom of file), CifCheck and parseMetadata become redundant
|
|
|
|
|
2024-03-29 13:45:58 +00:00
|
|
|
// Loads CifMetadata and passes it to parseMetadata, this function is what you should call to initiate the CifUpdate process.
|
2024-03-28 22:47:08 +00:00
|
|
|
func CifCheck(cfg *helpers.Configuration) error {
|
|
|
|
log.Msg.Debug("Checking age of CIF Data")
|
|
|
|
metadata, err := dbAccess.GetCifMetadata()
|
2024-03-26 22:33:11 +00:00
|
|
|
if err != nil {
|
2024-03-28 22:47:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-03-29 13:45:58 +00:00
|
|
|
|
|
|
|
err = parseMetadata(metadata, cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Error updating CIF Data", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requests a full update if no metadata exists, or a daily update if metadata does exist.
|
|
|
|
// The daily update function does further metadata parsing to determine what exactly needs downloading.
|
|
|
|
func parseMetadata(metadata *dbAccess.CifMetadata, cfg *helpers.Configuration) error {
|
2024-03-28 22:47:08 +00:00
|
|
|
if metadata == nil {
|
2024-03-29 13:45:58 +00:00
|
|
|
log.Msg.Info("No metadata, creating Timetable data")
|
|
|
|
newMeta, err := runFullUpdate(cfg)
|
2024-03-28 22:47:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ok := dbAccess.PutCifMetadata(*newMeta)
|
|
|
|
if !ok {
|
2024-03-29 13:45:58 +00:00
|
|
|
log.Msg.Error("CIF Data updated but Metadata Update failed")
|
2024-03-28 22:47:08 +00:00
|
|
|
}
|
2024-03-29 13:45:58 +00:00
|
|
|
return nil
|
2024-03-26 22:33:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 13:45:58 +00:00
|
|
|
log.Msg.Debug("Requesting CIF Data Update")
|
2024-04-03 22:25:27 +01:00
|
|
|
// When testing done, this function returns newMetadata which needs putting into DB
|
|
|
|
_, err := runUpdate(metadata, cfg)
|
2024-03-29 13:45:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-03 22:25:27 +01:00
|
|
|
//ok := dbAccess.PutCifMetadata(*newMeta)
|
|
|
|
//if !ok {
|
|
|
|
// log.Msg.Error("CIF Data updated but Metadata Update failed")
|
|
|
|
//}
|
2024-03-28 22:47:08 +00:00
|
|
|
return nil
|
2024-03-26 22:33:11 +00:00
|
|
|
}
|
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 {
|
|
|
|
log.Msg.Error("Unable to read last update time")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if metadata == nil {
|
|
|
|
log.Msg.Info("Full CIF download required")
|
|
|
|
err := runCifFullDownload(cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Unable to run full CIF Update")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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-03 22:25:27 +01:00
|
|
|
|
|
|
|
// Here I need to determine which days I need to update CIF data for, then pass to an appropriate function:
|
|
|
|
// newMetadata, err := runCifUpdate(days []time.Time, cfg)
|
|
|
|
}
|