From c9f894bbe6db005e219bacc4b6f2fc9c351e95fb Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sun, 14 Apr 2024 21:21:35 +0100 Subject: [PATCH] Add RemoveOutdatedServices --- cif/update.go | 12 ++++++++++++ dbAccess/cif.go | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/cif/update.go b/cif/update.go index e952f0c..8995ad2 100644 --- a/cif/update.go +++ b/cif/update.go @@ -47,7 +47,19 @@ func runCifFullDownload(cfg *helpers.Configuration) error { log.Warn("CIF Data updated, but metadata write failed") } + // Set parsed to nil to encourage garbage collection parsed = nil + + // Clear out of date schedules + cutoff := time.Now().Add(-time.Hour * 24 * 7) + log.Debug("Attempting to remove outdated services", zap.Time("scheduleEnd before", cutoff)) + count, err := dbAccess.RemoveOutdatedServices(cutoff) + if err != nil { + log.Warn("Out of date services not removed", zap.Error(err)) + } else { + log.Info("Out of date services removed", zap.Int64("removal count", count)) + } + return nil } diff --git a/dbAccess/cif.go b/dbAccess/cif.go index 79a1059..6a1db98 100644 --- a/dbAccess/cif.go +++ b/dbAccess/cif.go @@ -138,3 +138,19 @@ func CreateCifEntries(schedules []database.Service) error { return nil } + +// 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) + + res, err := collection.DeleteMany(context.Background(), filter) + if err != nil { + return // Automatically returns named values + } + + count = res.DeletedCount + return // Automatically returns names values +}