timetable-mgr/stations/check.go

73 lines
1.5 KiB
Go
Raw Normal View History

2024-06-29 21:54:32 +01:00
package stations
import (
"time"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
)
2024-06-29 21:54:32 +01:00
func Check() {
oldMetadata, err := dbAccess.GetStationsMetadata()
if err != nil {
log.Error("Error reading Stations metadata", zap.Error(err))
}
if oldMetadata == nil {
log.Info("No old metadata for stations, rebuilding")
} else {
timeSinceLastUpdate := time.Since(oldMetadata.LastUpdate)
if timeSinceLastUpdate <= time.Hour*24*7 {
log.Info("Stations update not required")
return
}
}
ok := run()
if !ok {
log.Error("Error updating Stations data")
}
2024-06-29 21:54:32 +01:00
}
func run() bool {
// Download
2024-06-29 21:54:32 +01:00
data, data2, err := download()
2024-06-30 20:28:34 +01:00
log.Info("Downloaded station data from two sources")
2024-06-29 21:54:32 +01:00
if err != nil {
2024-06-30 20:28:34 +01:00
log.Error("error downloading station data", zap.Error(err))
return false
2024-06-29 21:54:32 +01:00
}
// Parse
stations, err := parseData(data, data2)
2024-06-30 20:28:34 +01:00
log.Info("Parsed station data")
2024-06-30 09:45:16 +01:00
if err != nil {
2024-06-30 20:28:34 +01:00
log.Error("error parsing station data", zap.Error(err))
return false
2024-06-30 09:45:16 +01:00
}
// Drop
2024-06-30 20:28:34 +01:00
err = dbAccess.DropCollection("stations")
if err != nil {
log.Error("Error dropping stations collection", zap.Error(err))
}
// Push
2024-06-30 20:28:34 +01:00
err = dbAccess.PutManyNewStations(&stations)
if err != nil {
log.Error("Error putting new station data", zap.Error(err))
}
err = dbAccess.CreateStationIndeces()
if err != nil {
log.Error("Error creating station indeces", zap.Error(err))
}
ok := dbAccess.SetStationsMetadata(time.Now())
if !ok {
log.Warn("Error setting new metadata for Stations")
}
return true
2024-06-29 21:54:32 +01:00
}