Reorganise logging
This commit is contained in:
@@ -11,10 +11,10 @@ import (
|
||||
|
||||
// Checks if the CORPUS Data needs updating, and calls an updater function if needed.
|
||||
func CheckCorpus(cfg *helpers.Configuration) {
|
||||
log.Msg.Debug("Checking age of CORPUS Data")
|
||||
log.Debug("Checking age of CORPUS Data")
|
||||
utime, err := dbAccess.CheckUpdateTime(dbAccess.CorpusCollection)
|
||||
if err != nil {
|
||||
log.Msg.Error("Error checking last CORPUS update", zap.Error(err))
|
||||
log.Error("Error checking last CORPUS update", zap.Error(err))
|
||||
}
|
||||
|
||||
lastUpdate := time.Unix(utime, 0)
|
||||
@@ -22,17 +22,17 @@ func CheckCorpus(cfg *helpers.Configuration) {
|
||||
dataAge := currentTime.Sub(lastUpdate)
|
||||
fortnight := 14 * 24 * time.Hour
|
||||
|
||||
log.Msg.Debug("CORPUS Data", zap.Duration("Data Age", dataAge), zap.Duration("Max Age", 14*24*time.Hour))
|
||||
log.Debug("CORPUS Data", zap.Duration("Data Age", dataAge), zap.Duration("Max Age", 14*24*time.Hour))
|
||||
|
||||
if dataAge >= fortnight {
|
||||
log.Msg.Info("CORPUS Data is more than two weeks old")
|
||||
log.Info("CORPUS Data is more than two weeks old")
|
||||
err := RunCorpusUpdate(cfg)
|
||||
if err != nil {
|
||||
log.Msg.Warn("CORPUS Update did not run")
|
||||
log.Warn("CORPUS Update did not run")
|
||||
} else {
|
||||
log.Msg.Info("CORPUS data has been updated")
|
||||
log.Info("CORPUS data has been updated")
|
||||
}
|
||||
} else {
|
||||
log.Msg.Info("CORPUS Data is less than two weeks old, not updating")
|
||||
log.Info("CORPUS Data is less than two weeks old, not updating")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,14 +14,14 @@ import (
|
||||
func parseCorpusData(stream io.ReadCloser) (*[]database.CorpusEntry, error) {
|
||||
defer stream.Close()
|
||||
|
||||
log.Msg.Debug("Starting CORPUS Data parsing")
|
||||
log.Debug("Starting CORPUS Data parsing")
|
||||
|
||||
var corpusEntries []database.CorpusEntry
|
||||
decoder := json.NewDecoder(stream)
|
||||
|
||||
// Expect an object at the root of the JSON stream
|
||||
if _, err := decoder.Token(); err != nil {
|
||||
log.Msg.Error("Error parsing CORPUS Data", zap.Error(err))
|
||||
log.Error("Error parsing CORPUS Data", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -29,19 +29,19 @@ func parseCorpusData(stream io.ReadCloser) (*[]database.CorpusEntry, error) {
|
||||
for decoder.More() {
|
||||
// Decode the next JSON token
|
||||
if tok, err := decoder.Token(); err != nil {
|
||||
log.Msg.Error("Error parsing CORPUS Data", zap.Error(err))
|
||||
log.Error("Error parsing CORPUS Data", zap.Error(err))
|
||||
return nil, err
|
||||
} else if tok == "TIPLOCDATA" {
|
||||
// Found the "TIPLOCDATA" key, expect the associated array
|
||||
if !decoder.More() {
|
||||
err := errors.New("missing array after TIPLOCDATA key")
|
||||
log.Msg.Error("Error parsing CORPUS Data", zap.Error(err))
|
||||
log.Error("Error parsing CORPUS Data", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Start reading the array associated with the "TIPLOCDATA" key
|
||||
if _, err := decoder.Token(); err != nil {
|
||||
log.Msg.Error("Error parsing CORPUS Data", zap.Error(err))
|
||||
log.Error("Error parsing CORPUS Data", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func parseCorpusData(stream io.ReadCloser) (*[]database.CorpusEntry, error) {
|
||||
for decoder.More() {
|
||||
var corpusEntry database.CorpusEntry
|
||||
if err := decoder.Decode(&corpusEntry); err != nil {
|
||||
log.Msg.Error("Error parsing CORPUS Data", zap.Error(err))
|
||||
log.Error("Error parsing CORPUS Data", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
corpusEntries = append(corpusEntries, corpusEntry)
|
||||
@@ -58,7 +58,7 @@ func parseCorpusData(stream io.ReadCloser) (*[]database.CorpusEntry, error) {
|
||||
}
|
||||
}
|
||||
|
||||
log.Msg.Debug("CORPUS parsing complete")
|
||||
log.Debug("CORPUS parsing complete")
|
||||
|
||||
return &corpusEntries, nil
|
||||
}
|
||||
|
||||
@@ -12,13 +12,13 @@ import (
|
||||
func RunCorpusUpdate(cfg *helpers.Configuration) error {
|
||||
resp, err := nrod.NrodStream(url, cfg)
|
||||
if err != nil {
|
||||
log.Msg.Error("Failed to fetch CORPUS data", zap.Error(err))
|
||||
log.Error("Failed to fetch CORPUS data", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
unsortedCorpusData, err := parseCorpusData(resp)
|
||||
if err != nil {
|
||||
log.Msg.Error("Error parsing Corpus data", zap.Error(err))
|
||||
log.Error("Error parsing Corpus data", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -26,24 +26,24 @@ func RunCorpusUpdate(cfg *helpers.Configuration) error {
|
||||
stationData := createStationEntries(corpusData)
|
||||
|
||||
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))
|
||||
log.Warn("CORPUS data may be incomplete")
|
||||
log.Error("Error dropping CORPUS Data", zap.Error(err))
|
||||
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))
|
||||
log.Warn("Stations data may be incomplete")
|
||||
log.Error("Error dropping stations Data", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
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))
|
||||
log.Warn("CORPUS data may be incomplete")
|
||||
log.Error("Error inserting CORPUS Data", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
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))
|
||||
log.Warn("Stations data may be incomplete")
|
||||
log.Error("Error inserting stations data", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user