PIS module ready for testing

This commit is contained in:
Fred Boniface
2024-10-23 11:42:47 +01:00
parent 0320197147
commit e8e715aa37
7 changed files with 155 additions and 32 deletions

View File

@@ -5,3 +5,4 @@ const CorpusCollection string = "corpus"
const StationsCollection string = "stations"
const MetaCollection string = "meta"
const TimetableCollection string = "timetable"
const PisCollection string = "pis"

View File

@@ -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
}