package cif import ( "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" ) // REWRITING CifCheck into CheckCif (at bottom of file), CifCheck and parseMetadata become redundant // Loads CifMetadata and passes it to parseMetadata, this function is what you should call to initiate the CifUpdate process. func CifCheck(cfg *helpers.Configuration) error { log.Msg.Debug("Checking age of CIF Data") metadata, err := dbAccess.GetCifMetadata() if err != nil { return err } 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 { if metadata == nil { log.Msg.Info("No metadata, creating Timetable data") newMeta, err := runFullUpdate(cfg) if err != nil { return err } ok := dbAccess.PutCifMetadata(*newMeta) if !ok { log.Msg.Error("CIF Data updated but Metadata Update failed") } return nil } log.Msg.Debug("Requesting CIF Data Update") // When testing done, this function returns newMetadata which needs putting into DB _, err := runUpdate(metadata, cfg) if err != nil { return err } //ok := dbAccess.PutCifMetadata(*newMeta) //if !ok { // log.Msg.Error("CIF Data updated but Metadata Update failed") //} return nil } // 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 if time.Now().In(londonTimezone).Hour() <= dataAvailable { log.Msg.Info("Too early to update CIF data, not published until 0600") return } 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 } // Check if last update was today if isSameToday(metadata.LastUpdate) { log.Msg.Info("CIF Data has already been updated today, skipping") return } // // 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) }