PIS module ready for testing
This commit is contained in:
@@ -5,3 +5,4 @@ const CorpusCollection string = "corpus"
|
||||
const StationsCollection string = "stations"
|
||||
const MetaCollection string = "meta"
|
||||
const TimetableCollection string = "timetable"
|
||||
const PisCollection string = "pis"
|
||||
|
||||
@@ -3,10 +3,14 @@ package dbAccess
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func GetPisMetadata() (*database.PisMetadata, error) {
|
||||
@@ -24,3 +28,61 @@ func GetPisMetadata() (*database.PisMetadata, error) {
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func PutPisMetadata(version string) error {
|
||||
coll := MongoClient.Database(DatabaseName).Collection(MetaCollection)
|
||||
options := options.Update().SetUpsert(true)
|
||||
filter := bson.M{"type": "PisMetadata"}
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"type": "PisMetadata",
|
||||
"lastUpdate": time.Now().Format(time.RFC3339),
|
||||
"lastVersion": version,
|
||||
},
|
||||
}
|
||||
|
||||
_, err := coll.UpdateOne(context.Background(), filter, update, options)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("New Stations Metadata written", zap.String("version", version))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Puts complete PIS dataset to database
|
||||
func PutPisData(pis *[]database.PIS) (int64, error) {
|
||||
coll := MongoClient.Database(DatabaseName).Collection(PisCollection)
|
||||
|
||||
var docs []interface{}
|
||||
for _, entry := range *pis {
|
||||
docs = append(docs, entry)
|
||||
}
|
||||
|
||||
res, err := coll.InsertMany(context.TODO(), docs)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int64(len(res.InsertedIDs)), nil
|
||||
}
|
||||
|
||||
func CreatePisIndeces() error {
|
||||
coll := MongoClient.Database(DatabaseName).Collection(PisCollection)
|
||||
|
||||
crsIndex := mongo.IndexModel{
|
||||
Keys: bson.D{{"stops", 1}},
|
||||
}
|
||||
|
||||
tiplocIndex := mongo.IndexModel{
|
||||
Keys: bson.D{{"tiplocs", 1}},
|
||||
}
|
||||
|
||||
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{crsIndex, tiplocIndex})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user