2024-03-28 22:47:08 +00:00
|
|
|
package dbAccess
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
2024-04-05 21:42:00 +01:00
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/database"
|
2024-03-28 22:47:08 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Doctype = "CifMetadata"
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// The type describing the CifMetadata 'type' in the database.
|
|
|
|
// This type will be moved to owlboard/go-types
|
2024-03-28 22:47:08 +00:00
|
|
|
type CifMetadata struct {
|
2024-04-11 20:59:02 +01:00
|
|
|
Doctype string `bson:"type"`
|
|
|
|
LastUpdate time.Time `bson:"lastUpdate"`
|
|
|
|
LastTimestamp int64 `bson:"lastTimestamp"`
|
|
|
|
LastSequence int64 `bson:"lastSequence"`
|
|
|
|
LastUpdateType string `bson:"lastUpdateType"`
|
2024-03-28 22:47:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Fetches the CifMetadata from the database, returns nil if no metadata exists - before first initialisation for example.
|
2024-03-28 22:47:08 +00:00
|
|
|
func GetCifMetadata() (*CifMetadata, error) {
|
2024-04-15 20:03:48 +01:00
|
|
|
database := MongoClient.Database(DatabaseName)
|
|
|
|
collection := database.Collection(MetaCollection)
|
2024-03-28 22:47:08 +00:00
|
|
|
filter := bson.M{"type": Doctype}
|
|
|
|
var result CifMetadata
|
|
|
|
err := collection.FindOne(context.Background(), filter).Decode(&result)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, mongo.ErrNoDocuments) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-14 19:03:13 +01:00
|
|
|
log.Debug("Fetched CIF Metadata from database", zap.Any("Metadata", result))
|
2024-04-08 21:08:07 +01:00
|
|
|
|
2024-03-28 22:47:08 +00:00
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:01:57 +00:00
|
|
|
// Uses upsert to Insert/Update the CifMetadata in the database
|
2024-04-11 20:59:02 +01:00
|
|
|
func PutCifMetadata(metadata *CifMetadata, lastUpdateType string) bool {
|
2024-04-15 20:03:48 +01:00
|
|
|
database := MongoClient.Database(DatabaseName)
|
|
|
|
collection := database.Collection(MetaCollection)
|
2024-03-28 22:47:08 +00:00
|
|
|
options := options.Update().SetUpsert(true)
|
|
|
|
filter := bson.M{"type": Doctype}
|
|
|
|
update := bson.M{
|
2024-04-08 21:08:07 +01:00
|
|
|
"$set": bson.M{
|
2024-04-11 20:59:02 +01:00
|
|
|
"type": Doctype,
|
|
|
|
"lastUpdate": metadata.LastUpdate,
|
|
|
|
"lastTimestamp": metadata.LastTimestamp,
|
|
|
|
"lastSequence": metadata.LastSequence,
|
|
|
|
"lastUpdateType": lastUpdateType,
|
2024-04-08 21:08:07 +01:00
|
|
|
},
|
2024-03-28 22:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := collection.UpdateOne(context.Background(), filter, update, options)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-04-14 19:03:13 +01:00
|
|
|
log.Error("Error updating CIF Metadata", zap.Error(err))
|
2024-03-28 22:47:08 +00:00
|
|
|
return false
|
|
|
|
}
|
2024-04-08 21:08:07 +01:00
|
|
|
|
2024-04-14 19:03:13 +01:00
|
|
|
log.Info("New CIF Metadata written", zap.Time("Update time", metadata.LastUpdate))
|
2024-03-28 22:47:08 +00:00
|
|
|
return true
|
|
|
|
}
|
2024-04-05 21:42:00 +01:00
|
|
|
|
2024-04-06 22:27:55 +01:00
|
|
|
// Handles 'Delete' tasks from CIF Schedule updates, accepts DeleteQuery types and batches deletions.
|
2024-04-05 21:42:00 +01:00
|
|
|
func DeleteCifEntries(deletions []database.DeleteQuery) error {
|
2024-04-13 21:45:24 +01:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2024-04-14 19:03:13 +01:00
|
|
|
log.Panic("Panic:", zap.Any("panic", r))
|
2024-04-13 21:45:24 +01:00
|
|
|
}
|
|
|
|
}()
|
2024-04-08 21:08:07 +01:00
|
|
|
// Skip if deletions is empty
|
|
|
|
if len(deletions) == 0 {
|
2024-04-14 19:03:13 +01:00
|
|
|
log.Info("No deletions required")
|
2024-04-08 21:08:07 +01:00
|
|
|
return nil
|
|
|
|
}
|
2024-04-14 22:17:44 +01:00
|
|
|
log.Debug("Running deletions against database", zap.Int("count", len(deletions)))
|
2024-04-06 22:27:55 +01:00
|
|
|
|
2024-04-05 21:42:00 +01:00
|
|
|
// Prepare deletion tasks
|
2024-04-15 20:03:48 +01:00
|
|
|
collection := MongoClient.Database(DatabaseName).Collection(TimetableCollection)
|
2024-04-05 21:42:00 +01:00
|
|
|
bulkDeletions := make([]mongo.WriteModel, 0, len(deletions))
|
|
|
|
|
|
|
|
for _, deleteQuery := range deletions {
|
|
|
|
filter := bson.M{
|
|
|
|
"trainUid": deleteQuery.TrainUid,
|
|
|
|
"scheduleStartDate": deleteQuery.ScheduleStartDate,
|
|
|
|
"stpIndicator": deleteQuery.StpIndicator,
|
|
|
|
}
|
|
|
|
bulkDeletions = append(bulkDeletions, mongo.NewDeleteManyModel().SetFilter(filter))
|
|
|
|
}
|
|
|
|
|
2024-04-11 21:06:10 +01:00
|
|
|
bulkWriteOptions := options.BulkWrite().SetOrdered(false)
|
|
|
|
|
|
|
|
_, err := collection.BulkWrite(context.Background(), bulkDeletions, bulkWriteOptions)
|
2024-04-06 22:27:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles 'Create' tasks for CIF Schedule updates, accepts Service structs and batches their creation.
|
|
|
|
func CreateCifEntries(schedules []database.Service) error {
|
2024-04-08 21:08:07 +01:00
|
|
|
// Skip if deletions is empty
|
|
|
|
if len(schedules) == 0 {
|
2024-04-14 19:03:13 +01:00
|
|
|
log.Info("No creations required")
|
2024-04-08 21:08:07 +01:00
|
|
|
return nil
|
|
|
|
}
|
2024-04-14 22:17:44 +01:00
|
|
|
log.Debug("Running creations against database", zap.Int("count", len(schedules)))
|
2024-04-08 21:08:07 +01:00
|
|
|
|
2024-04-15 20:03:48 +01:00
|
|
|
collection := MongoClient.Database(DatabaseName).Collection(TimetableCollection)
|
2024-04-06 22:27:55 +01:00
|
|
|
|
|
|
|
models := make([]mongo.WriteModel, 0, len(schedules))
|
|
|
|
|
|
|
|
for _, s := range schedules {
|
|
|
|
model := mongo.NewInsertOneModel().SetDocument(s)
|
|
|
|
models = append(models, model)
|
2024-04-05 21:42:00 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 22:27:55 +01:00
|
|
|
bulkWriteOptions := options.BulkWrite().SetOrdered(false)
|
|
|
|
|
2024-04-09 22:39:35 +01:00
|
|
|
_, err := collection.BulkWrite(context.Background(), models, bulkWriteOptions)
|
2024-04-06 22:27:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-05 21:42:00 +01:00
|
|
|
return nil
|
|
|
|
}
|
2024-04-14 21:21:35 +01:00
|
|
|
|
|
|
|
// Removes any schedules which ended before 'cutoff'
|
|
|
|
func RemoveOutdatedServices(cutoff time.Time) (count int64, err error) {
|
|
|
|
// Define filter
|
|
|
|
filter := bson.M{"scheduleEndDate": bson.M{"$lt": cutoff}}
|
|
|
|
|
2024-04-15 20:03:48 +01:00
|
|
|
collection := MongoClient.Database(DatabaseName).Collection(TimetableCollection)
|
2024-04-14 21:21:35 +01:00
|
|
|
|
|
|
|
res, err := collection.DeleteMany(context.Background(), filter)
|
|
|
|
if err != nil {
|
|
|
|
return // Automatically returns named values
|
|
|
|
}
|
|
|
|
|
|
|
|
count = res.DeletedCount
|
|
|
|
return // Automatically returns names values
|
|
|
|
}
|
2024-04-23 00:27:33 +01:00
|
|
|
|
2024-04-23 00:31:03 +01:00
|
|
|
// Creates indexes on the Timetable collection... Index suitability needs checking.
|
2024-04-23 00:27:33 +01:00
|
|
|
func CreateTimetableIndexes() error {
|
|
|
|
coll := MongoClient.Database(DatabaseName).Collection(TimetableCollection)
|
|
|
|
indexModels := []mongo.IndexModel{
|
|
|
|
{
|
|
|
|
Keys: bson.M{
|
|
|
|
"trainUid": 1,
|
|
|
|
"stpIndicator": 1,
|
|
|
|
"scheduleStartDate": 1,
|
|
|
|
},
|
|
|
|
Options: options.Index().SetName("delete_query"),
|
|
|
|
},
|
2024-04-23 00:31:03 +01:00
|
|
|
{
|
|
|
|
Keys: bson.M{
|
|
|
|
"trainUid": 1,
|
|
|
|
"scheduleStartDate": 1,
|
|
|
|
"scheduleEndDate": 1,
|
|
|
|
"daysRun": 1,
|
|
|
|
},
|
|
|
|
Options: options.Index().SetName("find_by_uid"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Keys: bson.M{
|
|
|
|
"headcode": 1,
|
|
|
|
"scheduleStartDate": 1,
|
|
|
|
"scheduleEndDate": 1,
|
|
|
|
"daysRun": 1,
|
|
|
|
},
|
|
|
|
Options: options.Index().SetName("find_by_headcode"),
|
|
|
|
},
|
2024-04-23 00:27:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := coll.Indexes().CreateMany(context.Background(), indexModels)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|