timetable-mgr/stations/check.go

56 lines
970 B
Go

package stations
import (
"fmt"
"time"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
)
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")
}
}
func run() bool {
// Download
data, data2, err := download()
if err != nil {
fmt.Println(err)
return false
}
// Parse
stations, err := parseData(data, data2)
if err != nil {
fmt.Println(err)
return false
}
// Drop
dbAccess.DropCollection("stations")
// Push
dbAccess.PutManyNewStations(&stations)
return true
}