2024-03-26 15:40:15 +00:00
|
|
|
package corpus
|
|
|
|
|
|
|
|
import (
|
2024-03-26 22:33:11 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Runs all stages of the CORPUS Update process
|
2024-03-26 22:33:11 +00:00
|
|
|
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))
|
2024-03-26 22:33:11 +00:00
|
|
|
return err
|
2024-03-26 15:40:15 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 21:07:21 +01: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))
|
2024-03-26 22:33:11 +00:00
|
|
|
return err
|
2024-03-26 15:40:15 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 16:04:42 +00:00
|
|
|
corpusData := pruneCorpusEntries(unsortedCorpusData)
|
|
|
|
stationData := createStationEntries(corpusData)
|
2024-03-26 15:40:15 +00:00
|
|
|
|
2024-03-26 22:33:11 +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
|
2024-03-26 22:33:11 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2024-03-26 22:33:11 +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
|
|
|
}
|
2024-03-26 22:33:11 +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
|
2024-03-26 22:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-03-26 15:40:15 +00:00
|
|
|
}
|