2024-03-26 16:32:00 +00:00
|
|
|
package dbAccess
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-30 20:28:34 +01:00
|
|
|
"strings"
|
2024-03-26 22:33:11 +00:00
|
|
|
|
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/database"
|
2024-06-30 20:28:34 +01:00
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/log"
|
2024-04-23 00:27:33 +01:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2024-06-30 20:28:34 +01:00
|
|
|
"go.uber.org/zap"
|
2024-03-26 16:32:00 +00:00
|
|
|
)
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Puts an array of Corpus Documents into the database
|
2024-04-10 20:56:13 +01:00
|
|
|
func PutManyCorpus(corpusData *[]database.CorpusEntry) error {
|
2024-04-15 20:03:48 +01:00
|
|
|
collection := MongoClient.Database(DatabaseName).Collection(CorpusCollection)
|
2024-03-26 22:33:11 +00:00
|
|
|
|
|
|
|
documents := convertCorpusToInterfaceSlice(corpusData)
|
2024-03-26 16:32:00 +00:00
|
|
|
|
2024-04-10 20:56:13 +01:00
|
|
|
_, err := collection.InsertMany(context.Background(), *documents)
|
2024-03-26 16:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-26 22:33:11 +00:00
|
|
|
SetUpdateTime(CorpusCollection)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Puts an array of Stations documents into the database
|
2024-04-10 20:56:13 +01:00
|
|
|
func PutManyStations(stationsData *[]database.StationEntry) error {
|
2024-04-15 20:03:48 +01:00
|
|
|
collection := MongoClient.Database(DatabaseName).Collection(StationsCollection)
|
2024-03-26 22:33:11 +00:00
|
|
|
|
|
|
|
documents := convertStationsToInterfaceSlice(stationsData)
|
|
|
|
|
2024-04-10 20:56:13 +01:00
|
|
|
_, err := collection.InsertMany(context.Background(), *documents)
|
2024-03-26 16:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-26 22:33:11 +00:00
|
|
|
SetUpdateTime(StationsCollection)
|
2024-03-26 16:32:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-03-26 22:33:11 +00:00
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Converts []database.CorpusEntry types into interface slices required to put them into the database
|
2024-04-10 20:56:13 +01:00
|
|
|
func convertCorpusToInterfaceSlice(corpusData *[]database.CorpusEntry) *[]interface{} {
|
|
|
|
interfaceSlice := make([]interface{}, len(*corpusData))
|
|
|
|
for i, doc := range *corpusData {
|
2024-03-26 22:33:11 +00:00
|
|
|
interfaceSlice[i] = doc
|
|
|
|
}
|
2024-04-10 20:56:13 +01:00
|
|
|
return &interfaceSlice
|
2024-03-26 22:33:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Converts []database.StationEntry types into interface slices required to put them into the database
|
2024-04-10 20:56:13 +01:00
|
|
|
func convertStationsToInterfaceSlice(stationsData *[]database.StationEntry) *[]interface{} {
|
|
|
|
interfaceSlice := make([]interface{}, len(*stationsData))
|
|
|
|
for i, doc := range *stationsData {
|
2024-03-26 22:33:11 +00:00
|
|
|
interfaceSlice[i] = doc
|
|
|
|
}
|
2024-04-10 20:56:13 +01:00
|
|
|
return &interfaceSlice
|
2024-03-26 22:33:11 +00:00
|
|
|
}
|
2024-04-23 00:27:33 +01:00
|
|
|
|
|
|
|
func CreateCorpusIndexes() error {
|
|
|
|
coll := MongoClient.Database(DatabaseName).Collection(CorpusCollection)
|
|
|
|
indexModels := []mongo.IndexModel{
|
|
|
|
{
|
|
|
|
Keys: bson.M{
|
|
|
|
"tiploc": 1,
|
|
|
|
},
|
|
|
|
Options: options.Index().SetName("tiploc"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Keys: bson.M{
|
|
|
|
"3alpha": 1,
|
|
|
|
},
|
|
|
|
Options: options.Index().SetName("3alpha"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := coll.Indexes().CreateMany(context.Background(), indexModels)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-30 09:45:16 +01:00
|
|
|
|
|
|
|
func GetTiplocFromCrs(crs string) (tiploc string, err error) {
|
|
|
|
// Return TIPLOC from CRS code
|
2024-06-30 19:36:37 +01:00
|
|
|
|
2024-06-30 20:28:34 +01:00
|
|
|
crs = strings.ToUpper(crs)
|
2024-06-30 19:36:37 +01:00
|
|
|
// PIPELINE:
|
2024-06-30 20:28:34 +01:00
|
|
|
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
|
2024-06-30 09:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetStanoxFromCrs(crs string) (stanox string, err error) {
|
|
|
|
// Return STANOX from CRS code
|
2024-06-30 20:28:34 +01:00
|
|
|
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
|
2024-06-30 09:45:16 +01:00
|
|
|
}
|