From 2ca5a86030af0707d4203022fef976f6ff3c0821 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 5 Jun 2023 20:55:59 +0100 Subject: [PATCH] Bug Fixes: - Remove outdated timetable data, - Ensure a 'Delete' transaction only deletes required data - Bump version to 2023.6.6 --- src/main.py | 2 +- src/timetable.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main.py b/src/main.py index 14e59f2..be81281 100644 --- a/src/main.py +++ b/src/main.py @@ -14,7 +14,7 @@ # program. If not, see # https://git.fjla.uk/OwlBoard/db-manager/src/branch/main/LICENSE -version = "2023.6.5" +version = "2023.6.6" print(f"main.py: Initialising db-manager v{version}") #Third Party Imports diff --git a/src/timetable.py b/src/timetable.py index 1efb7e9..c11e712 100644 --- a/src/timetable.py +++ b/src/timetable.py @@ -141,6 +141,7 @@ def insertSchedule(sch_record): return document def _insertToDb(data :list, type :str): + pre_count = mongo.getLength("timetable") try: if type == "full": mongo.dropCollection("timetable") @@ -152,7 +153,9 @@ def _insertToDb(data :list, type :str): singleList = [item] mongo.putTimetable(singleList) elif item['transactionType'] == "Delete": - mongo.deleteTimetableData({'trainUid': item['trainUid']}) ## Also need to consider the STP indicator and end date here else I am deleting LTP services when an STP is meant to be deleted. + mongo.deleteTimetableData({'trainUid': item['trainUid'], 'scheduleStartDate': item['scheduleStartDate'], 'stpIndicator': item['stpIndicator']}) ## Also need to consider the STP indicator and end date here else I am deleting LTP services when an STP is meant to be deleted. + post_count = mongo.getLength("timetable") + log.out(f"timetable._insertToDb: Document count difference after processing: {pre_count - post_count}", "DBUG") return True #If Successfuls except Exception as e: log.out("timetable._insertToDb: Error inserting timetable data", "ERR") @@ -201,12 +204,12 @@ def _helpParseDate(string :str, time :str = "false"): def _removeOutdatedServices(): log.out("timetable._removeOutdatedServices: Removing out of date schedules", "INFO") - preCount = mongo.getLength("timetable") + pre_count = mongo.getLength("timetable") query = { "schedule_end_date": { "$lte": datetime.now() } } - ## Important to actually call delete here!!!! - postCount = mongo.getLength("timetable") - log.out(f"timetable._removeOutdatedServices: Removed {preCount - postCount} out of date services", "DBUG") \ No newline at end of file + mongo.deleteMany("timetable", query) + post_count = mongo.getLength("timetable") + log.out(f"timetable._removeOutdatedServices: Removed {pre_count - post_count} out of date services", "DBUG") \ No newline at end of file