timetable-mgr/dbAccess/cif.go

205 lines
5.9 KiB
Go
Raw Permalink Normal View History

2024-03-28 22:47:08 +00:00
package dbAccess
import (
"context"
"errors"
"time"
"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"
// 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
}
// 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) {
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-03-28 22:47:08 +00:00
return &result, nil
}
// 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 {
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{
"$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-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-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
}
// Handles 'Delete' tasks from CIF Schedule updates, accepts DeleteQuery types and batches deletions.
func DeleteCifEntries(deletions []database.DeleteQuery) error {
defer func() {
if r := recover(); r != nil {
2024-04-14 19:03:13 +01:00
log.Panic("Panic:", zap.Any("panic", r))
}
}()
// Skip if deletions is empty
if len(deletions) == 0 {
2024-04-14 19:03:13 +01:00
log.Info("No deletions required")
return nil
}
2024-04-14 22:17:44 +01:00
log.Debug("Running deletions against database", zap.Int("count", len(deletions)))
// Prepare deletion tasks
collection := MongoClient.Database(DatabaseName).Collection(TimetableCollection)
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))
}
bulkWriteOptions := options.BulkWrite().SetOrdered(false)
_, err := collection.BulkWrite(context.Background(), bulkDeletions, bulkWriteOptions)
if err != nil {
return err
}
return nil
}
// Clears all non-vstp services from the database. Used when a CIF full download is required.
func PurgeNonVstp() (int64, error) {
coll := MongoClient.Database(DatabaseName).Collection(TimetableCollection)
filter := bson.M{"serviceDetail.vstp": false}
result, err := coll.DeleteMany(context.Background(), filter)
if err != nil {
return result.DeletedCount, err
}
return result.DeletedCount, nil
}
// Handles 'Create' tasks for CIF Schedule updates, accepts Service structs and batches their creation.
func CreateCifEntries(schedules []database.Service) error {
// Skip if deletions is empty
if len(schedules) == 0 {
2024-04-14 19:03:13 +01:00
log.Info("No creations required")
return nil
}
2024-04-14 22:17:44 +01:00
log.Debug("Running creations against database", zap.Int("count", len(schedules)))
collection := MongoClient.Database(DatabaseName).Collection(TimetableCollection)
models := make([]mongo.WriteModel, 0, len(schedules))
for _, s := range schedules {
model := mongo.NewInsertOneModel().SetDocument(s)
models = append(models, model)
}
bulkWriteOptions := options.BulkWrite().SetOrdered(false)
_, err := collection.BulkWrite(context.Background(), models, bulkWriteOptions)
if err != nil {
return err
}
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}}
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 {
2024-04-23 20:10:04 +01:00
log.Info("Creating timetable indexes")
2024-04-23 00:27:33 +01:00
coll := MongoClient.Database(DatabaseName).Collection(TimetableCollection)
indexModels := []mongo.IndexModel{
{
2024-04-23 16:54:29 +01:00
Keys: bson.D{
{Key: "trainUid", Value: 1},
{Key: "stpIndicator", Value: 1},
{Key: "scheduleStartDate", Value: 1},
2024-04-23 00:27:33 +01:00
},
Options: options.Index().SetName("delete_query"),
2024-04-23 20:10:04 +01:00
}, // The find by UID Query can make use of the delete_query index
2024-04-23 00:31:03 +01:00
{
2024-04-23 16:54:29 +01:00
Keys: bson.D{
{Key: "headcode", Value: 1},
2024-04-23 20:10:04 +01:00
{Key: "daysRun", Value: 1},
2024-04-23 16:54:29 +01:00
{Key: "scheduleStartDate", Value: 1},
{Key: "scheduleEndDate", Value: 1},
2024-04-23 00:31:03 +01:00
},
Options: options.Index().SetName("find_by_headcode"),
},
2024-04-23 15:13:16 +01:00
{
2024-04-23 16:54:29 +01:00
Keys: bson.D{
{Key: "serviceDetail.vstp", Value: 1},
2024-04-23 15:13:16 +01:00
},
Options: options.Index().SetName("vstp"),
},
2024-04-23 00:27:33 +01:00
}
_, err := coll.Indexes().CreateMany(context.Background(), indexModels)
if err != nil {
return err
}
return nil
}