Testing new stations data

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 (
"context"
"fmt"
"strings"
"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/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
)
// Puts an array of Corpus Documents into the database
@@ -85,27 +87,76 @@ func CreateCorpusIndexes() error {
func GetTiplocFromCrs(crs string) (tiploc string, err error) {
// Return TIPLOC from CRS code
crs = strings.ToUpper(crs)
// PIPELINE:
/*
bson.A{
bson.D{{"$match", bson.D{{"3ALPHA", "BTH"}}}},
bson.D{
{"$project",
bson.D{
{"TIPLOC", 1},
{"STANOX", 1},
{"_id", 0},
},
},
},
}
*/
err = fmt.Errorf("not yet written")
return "", err
pipeline := bson.A{
bson.D{{"$match", bson.D{{"3ALPHA", crs}}}},
bson.D{
{"$project",
bson.D{
{"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
err = fmt.Errorf("not yet written")
return "", err
crs = strings.ToUpper(crs)
// PIPELINE:
pipeline := bson.A{
bson.D{{"$match", bson.D{{"3ALPHA", crs}}}},
bson.D{
{"$project",
bson.D{
{"STANOX", 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 {
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)
collection := database.Collection(MetaCollection)
options := options.Update().SetUpsert(true)
filter := bson.M{"type": Doctype}
filter := bson.M{"type": StationsMetaDoctype}
update := bson.M{
"$set": bson.M{
"type": StationsMetaDoctype,
@@ -73,7 +73,6 @@ func PutManyNewStations(stationsData *[]database.Station) error {
return err
}
SetUpdateTime(StationsCollection)
return nil
}
@@ -86,6 +85,26 @@ func convertNewStationsToInterfaceSlice(stationsData *[]database.Station) *[]int
return &interfaceSlice
}
func CreateStationIndeces() bool {
return false
func CreateStationIndeces() error {
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
}