2024-03-26 16:04:42 +00:00
|
|
|
package cif
|
|
|
|
|
2024-03-26 22:33:11 +00:00
|
|
|
import (
|
|
|
|
"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-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-02 21:07:01 +01:00
|
|
|
newMeta, err := runUpdate(metadata, cfg)
|
2024-03-29 13:45:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ok := dbAccess.PutCifMetadata(*newMeta)
|
|
|
|
if !ok {
|
|
|
|
log.Msg.Error("CIF Data updated but Metadata Update failed")
|
2024-03-26 22:33:11 +00:00
|
|
|
}
|
2024-03-28 22:47:08 +00:00
|
|
|
return nil
|
2024-03-26 22:33:11 +00:00
|
|
|
}
|