Compare commits

...

2 Commits

Author SHA1 Message Date
Fred Boniface 9bd6bbde25 Prevent deleting VSTP services when completing full download.
Go Test / test (push) Successful in 24s Details
2024-04-23 20:49:28 +01:00
Fred Boniface a2c5bd49ff Improve CIF Indexes 2024-04-23 20:10:04 +01:00
2 changed files with 24 additions and 13 deletions

View File

@ -33,8 +33,14 @@ func runCifFullDownload(cfg *helpers.Configuration) error {
return err
}
// Drop timetable collection
dbAccess.DropCollection(dbAccess.TimetableCollection) // I should edit this to prevent removal of VSTP entries in the database.
// Try to remove all non-vstp entries, else give up and delete collection
count, err := dbAccess.PurgeNonVstp()
if err != nil {
log.Warn("Error purging non-vstp schedules, dropping collection", zap.Error(err))
dbAccess.DropCollection(dbAccess.TimetableCollection) // I should edit this to prevent removal of VSTP entries in the database.
} else {
log.Info("Removed non-vstp services", zap.Int64("deletion count", count))
}
// Process CIF file
err = ProcessParsedCif(parsed)

View File

@ -108,6 +108,19 @@ func DeleteCifEntries(deletions []database.DeleteQuery) error {
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
@ -154,6 +167,7 @@ func RemoveOutdatedServices(cutoff time.Time) (count int64, err error) {
// Creates indexes on the Timetable collection... Index suitability needs checking.
func CreateTimetableIndexes() error {
log.Info("Creating timetable indexes")
coll := MongoClient.Database(DatabaseName).Collection(TimetableCollection)
indexModels := []mongo.IndexModel{
{
@ -163,22 +177,13 @@ func CreateTimetableIndexes() error {
{Key: "scheduleStartDate", Value: 1},
},
Options: options.Index().SetName("delete_query"),
},
{
Keys: bson.D{
{Key: "trainUid", Value: 1},
{Key: "scheduleStartDate", Value: 1},
{Key: "scheduleEndDate", Value: 1},
{Key: "daysRun", Value: 1},
},
Options: options.Index().SetName("find_by_uid"),
},
}, // The find by UID Query can make use of the delete_query index
{
Keys: bson.D{
{Key: "headcode", Value: 1},
{Key: "daysRun", Value: 1},
{Key: "scheduleStartDate", Value: 1},
{Key: "scheduleEndDate", Value: 1},
{Key: "daysRun", Value: 1},
},
Options: options.Index().SetName("find_by_headcode"),
},