This commit is contained in:
parent
9496c9aae0
commit
77eb22b837
@ -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"}}}},
|
||||
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
|
||||
crs = strings.ToUpper(crs)
|
||||
// PIPELINE:
|
||||
pipeline := bson.A{
|
||||
bson.D{{"$match", bson.D{{"3ALPHA", crs}}}},
|
||||
bson.D{
|
||||
{"$project",
|
||||
bson.D{
|
||||
{"STANOX", 1},
|
||||
{"_id", 0},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
*/
|
||||
err = fmt.Errorf("not yet written")
|
||||
return "", err
|
||||
}
|
||||
|
||||
func GetStanoxFromCrs(crs string) (stanox string, err error) {
|
||||
// Return STANOX from CRS code
|
||||
err = fmt.Errorf("not yet written")
|
||||
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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package stations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
||||
@ -33,23 +32,41 @@ func Check() {
|
||||
func run() bool {
|
||||
// Download
|
||||
data, data2, err := download()
|
||||
log.Info("Downloaded station data from two sources")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.Error("error downloading station data", zap.Error(err))
|
||||
return false
|
||||
}
|
||||
|
||||
// Parse
|
||||
stations, err := parseData(data, data2)
|
||||
log.Info("Parsed station data")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.Error("error parsing station data", zap.Error(err))
|
||||
return false
|
||||
}
|
||||
|
||||
// Drop
|
||||
dbAccess.DropCollection("stations")
|
||||
err = dbAccess.DropCollection("stations")
|
||||
if err != nil {
|
||||
log.Error("Error dropping stations collection", zap.Error(err))
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
@ -29,7 +29,6 @@ func parseData(data ...[]byte) ([]database.Station, error) {
|
||||
}
|
||||
output = append(output, outputStation)
|
||||
}
|
||||
fmt.Println(output)
|
||||
return output, nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user