Testing new stations data
All checks were successful
Go Test / test (push) Successful in 26s

This commit is contained in:
Fred Boniface 2024-06-30 20:28:34 +01:00
parent 9496c9aae0
commit 77eb22b837
4 changed files with 115 additions and 29 deletions

View File

@ -2,12 +2,14 @@ package dbAccess
import ( import (
"context" "context"
"fmt" "strings"
"git.fjla.uk/owlboard/go-types/pkg/database" "git.fjla.uk/owlboard/go-types/pkg/database"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
) )
// Puts an array of Corpus Documents into the database // Puts an array of Corpus Documents into the database
@ -85,27 +87,76 @@ func CreateCorpusIndexes() error {
func GetTiplocFromCrs(crs string) (tiploc string, err error) { func GetTiplocFromCrs(crs string) (tiploc string, err error) {
// Return TIPLOC from CRS code // Return TIPLOC from CRS code
crs = strings.ToUpper(crs)
// PIPELINE: // PIPELINE:
/* pipeline := bson.A{
bson.A{ bson.D{{"$match", bson.D{{"3ALPHA", crs}}}},
bson.D{{"$match", bson.D{{"3ALPHA", "BTH"}}}},
bson.D{ bson.D{
{"$project", {"$project",
bson.D{ bson.D{
{"TIPLOC", 1}, {"TIPLOC", 1},
{"_id", 0},
},
},
},
}
coll := MongoClient.Database(DatabaseName).Collection(StationsCollection)
cursor, err := coll.Aggregate(context.Background(), pipeline)
if err != nil {
return "", err
}
defer cursor.Close(context.Background())
var result struct {
TIPLOC string `bson:"TIPLOC"`
}
if cursor.Next(context.Background()) {
if err := cursor.Decode(&result); err != nil {
return "", err
}
return result.TIPLOC, nil
}
log.Warn("No TIPLOC Found", zap.String("CRS", crs))
return "", nil
}
func GetStanoxFromCrs(crs string) (stanox string, err error) {
// Return STANOX from CRS code
crs = strings.ToUpper(crs)
// PIPELINE:
pipeline := bson.A{
bson.D{{"$match", bson.D{{"3ALPHA", crs}}}},
bson.D{
{"$project",
bson.D{
{"STANOX", 1}, {"STANOX", 1},
{"_id", 0}, {"_id", 0},
}, },
}, },
}, },
} }
*/
err = fmt.Errorf("not yet written")
return "", err
}
func GetStanoxFromCrs(crs string) (stanox string, err error) { coll := MongoClient.Database(DatabaseName).Collection(StationsCollection)
// Return STANOX from CRS code cursor, err := coll.Aggregate(context.Background(), pipeline)
err = fmt.Errorf("not yet written") if err != nil {
return "", err return "", err
}
defer cursor.Close(context.Background())
var result struct {
STANOX string `bson:"STANOX"`
}
if cursor.Next(context.Background()) {
if err := cursor.Decode(&result); err != nil {
return "", err
}
return result.STANOX, nil
}
log.Warn("No STANOX Found", zap.String("CRS", crs))
return "", nil
} }

View File

@ -43,7 +43,7 @@ func SetStationsMetadata(time time.Time) bool {
database := MongoClient.Database(DatabaseName) database := MongoClient.Database(DatabaseName)
collection := database.Collection(MetaCollection) collection := database.Collection(MetaCollection)
options := options.Update().SetUpsert(true) options := options.Update().SetUpsert(true)
filter := bson.M{"type": Doctype} filter := bson.M{"type": StationsMetaDoctype}
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"type": StationsMetaDoctype, "type": StationsMetaDoctype,
@ -73,7 +73,6 @@ func PutManyNewStations(stationsData *[]database.Station) error {
return err return err
} }
SetUpdateTime(StationsCollection)
return nil return nil
} }
@ -86,6 +85,26 @@ func convertNewStationsToInterfaceSlice(stationsData *[]database.Station) *[]int
return &interfaceSlice return &interfaceSlice
} }
func CreateStationIndeces() bool { func CreateStationIndeces() error {
return false coll := MongoClient.Database(DatabaseName).Collection(StationsCollection)
locationIndex := mongo.IndexModel{
Keys: bson.D{{"location", "2dsphere"}},
Options: nil,
}
crsIndex := mongo.IndexModel{
Keys: bson.D{{"3ALPHA", 1}},
}
tiplocIndex := mongo.IndexModel{
Keys: bson.D{{"TIPLOC", 1}},
}
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{locationIndex, crsIndex, tiplocIndex})
if err != nil {
return err
}
return nil
} }

View File

@ -1,7 +1,6 @@
package stations package stations
import ( import (
"fmt"
"time" "time"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess" "git.fjla.uk/owlboard/timetable-mgr/dbAccess"
@ -33,23 +32,41 @@ func Check() {
func run() bool { func run() bool {
// Download // Download
data, data2, err := download() data, data2, err := download()
log.Info("Downloaded station data from two sources")
if err != nil { if err != nil {
fmt.Println(err) log.Error("error downloading station data", zap.Error(err))
return false return false
} }
// Parse // Parse
stations, err := parseData(data, data2) stations, err := parseData(data, data2)
log.Info("Parsed station data")
if err != nil { if err != nil {
fmt.Println(err) log.Error("error parsing station data", zap.Error(err))
return false return false
} }
// Drop // Drop
dbAccess.DropCollection("stations") err = dbAccess.DropCollection("stations")
if err != nil {
log.Error("Error dropping stations collection", zap.Error(err))
}
// Push // Push
dbAccess.PutManyNewStations(&stations) 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 return true
} }

View File

@ -29,7 +29,6 @@ func parseData(data ...[]byte) ([]database.Station, error) {
} }
output = append(output, outputStation) output = append(output, outputStation)
} }
fmt.Println(output)
return output, nil return output, nil
} }