73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
package stations
|
|
|
|
import (
|
|
"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()
|
|
log.Info("Downloaded station data from two sources")
|
|
if err != nil {
|
|
log.Error("error downloading station data", zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
// Parse
|
|
stations, err := parseData(data, data2)
|
|
log.Info("Parsed station data")
|
|
if err != nil {
|
|
log.Error("error parsing station data", zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
// Drop
|
|
err = dbAccess.DropCollection("stations")
|
|
if err != nil {
|
|
log.Error("Error dropping stations collection", zap.Error(err))
|
|
}
|
|
|
|
// Push
|
|
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
|
|
}
|