timetable-mgr/corpus/update.go

52 lines
1.5 KiB
Go
Raw Normal View History

2024-03-26 15:40:15 +00:00
package corpus
import (
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
2024-03-26 15:40:15 +00:00
"git.fjla.uk/owlboard/timetable-mgr/helpers"
"git.fjla.uk/owlboard/timetable-mgr/log"
2024-04-03 22:25:27 +01:00
"git.fjla.uk/owlboard/timetable-mgr/nrod"
2024-03-26 15:40:15 +00:00
"go.uber.org/zap"
)
// Runs all stages of the CORPUS Update process
func RunCorpusUpdate(cfg *helpers.Configuration) error {
2024-04-03 22:25:27 +01:00
resp, err := nrod.NrodDownload(url, cfg)
2024-03-26 15:40:15 +00:00
if err != nil {
2024-04-03 22:25:27 +01:00
log.Msg.Error("Failed to fetch CORPUS data", zap.Error(err))
return err
2024-03-26 15:40:15 +00:00
}
unsortedCorpusData, err := parseCorpusData(resp)
2024-03-26 15:40:15 +00:00
if err != nil {
log.Msg.Error("Error parsing Corpus data", zap.Error(err))
return err
2024-03-26 15:40:15 +00:00
}
corpusData := pruneCorpusEntries(*unsortedCorpusData)
2024-03-26 16:04:42 +00:00
stationData := createStationEntries(corpusData)
2024-03-26 15:40:15 +00:00
if err := dbAccess.DropCollection(dbAccess.CorpusCollection); err != nil {
log.Msg.Warn("CORPUS data may be incomplete")
log.Msg.Error("Error dropping CORPUS Data", zap.Error(err))
2024-04-03 22:25:27 +01:00
return err
}
if err := dbAccess.DropCollection(dbAccess.StationsCollection); err != nil {
log.Msg.Warn("Stations data may be incomplete")
log.Msg.Error("Error dropping stations Data", zap.Error(err))
2024-04-03 22:25:27 +01:00
return err
2024-03-26 15:40:15 +00:00
}
2024-03-26 16:04:42 +00:00
if err := dbAccess.PutManyCorpus(corpusData); err != nil {
log.Msg.Warn("CORPUS data may be incomplete")
log.Msg.Error("Error inserting CORPUS Data", zap.Error(err))
2024-04-03 22:25:27 +01:00
return err
2024-03-26 16:04:42 +00:00
}
if err := dbAccess.PutManyStations(stationData); err != nil {
log.Msg.Warn("Stations data may be incomplete")
log.Msg.Error("Error inserting stations data", zap.Error(err))
2024-04-03 22:25:27 +01:00
return err
}
return nil
2024-03-26 15:40:15 +00:00
}