timetable-mgr/stations/check.go

56 lines
970 B
Go
Raw Normal View History

2024-06-29 21:54:32 +01:00
package stations
import (
"fmt"
"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()
if err != nil {
fmt.Println(err)
return false
2024-06-29 21:54:32 +01:00
}
// Parse
stations, err := parseData(data, data2)
2024-06-30 09:45:16 +01:00
if err != nil {
fmt.Println(err)
return false
2024-06-30 09:45:16 +01:00
}
// Drop
dbAccess.DropCollection("stations")
// Push
dbAccess.PutManyNewStations(&stations)
return true
2024-06-29 21:54:32 +01:00
}