From dd1d9375a04a1d29412f8a0533e12b64a0ac9abe Mon Sep 17 00:00:00 2001 From: Tim Beccue Date: Wed, 4 Dec 2024 12:37:48 -0500 Subject: [PATCH] Change logic for how to clear expired schedules Instead of clearing events that haven't started yet, clear all events that haven't ended yet. In other words, include currently running events in the candidates for deletion. This avoids creating duplicates if the scheduler changes the current event. --- handler.py | 4 +--- import_schedules.py | 12 +++++------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/handler.py b/handler.py index 33ac1f5..1d7c13a 100644 --- a/handler.py +++ b/handler.py @@ -231,9 +231,7 @@ def deleteEventById(event, context): print(f"userRoles: {userRoles}") # Check if the requester is an admin - requesterIsAdmin="false" - if 'admin' in userRoles: - requesterIsAdmin="true" + requesterIsAdmin = 'admin' in userRoles print(f"requesterIsAdmin: {requesterIsAdmin}") # Specify the event with our pk (eventId) and sk (startTime) diff --git a/import_schedules.py b/import_schedules.py index 8d4c235..94a6e0e 100644 --- a/import_schedules.py +++ b/import_schedules.py @@ -212,14 +212,14 @@ def clear_old_schedule(site, cutoff_time=None): This method takes a site and a cutoff time, and deletes all events that satisfy the following conditions: - the event belongs to the given site - - the event starts after the cutoff_time (specifically, the event start is greater than the cutoff_time) - - the event origin is 'lco' + - the event ends after the cutoff_time (specifically, the event end is greater than the cutoff_time) + - the event origin is 'LCO' Then it gathers a list of project IDs that were associated with the deleted events, and delete them too. Args: cutoff_time (str): Formatted yyyy-MM-ddTHH:mmZ (UTC, 24-hour format) - Any events that start before this time are not deleted. + Any events that end before this time are not deleted. site (str): Only delete events from the given site (e.g. 'mrc') @@ -233,12 +233,10 @@ def clear_old_schedule(site, cutoff_time=None): # Query items from the secondary index with 'site' as the partition key and 'end' greater than the specified end_date - # We're using 'end' time for the query because it's part of a pre-existing GSI that allows for efficient queries. - # But ultimately we want this to apply to events that start after the cutoff, so add that as a filter condition too. query = calendar_table.query( IndexName=index_name, KeyConditionExpression=Key('site').eq(site) & Key('end').gt(cutoff_time), - FilterExpression=Attr('origin').eq('LCO') & Attr('start').gt(cutoff_time) + FilterExpression=Attr('origin').eq('LCO') ) items = query.get('Items', []) print(f"Removing expired scheduled events: {items}") @@ -255,7 +253,7 @@ def clear_old_schedule(site, cutoff_time=None): query = calendar_table.query( IndexName=index_name, KeyConditionExpression=Key('site').eq(site) & Key('end').gt(cutoff_time), - FilterExpression=Attr('origin').eq('lco') & Attr('start').gt(cutoff_time), + FilterExpression=Attr('origin').eq('LCO'), ExclusiveStartKey=query['LastEvaluatedKey'] ) items = query.get('Items', [])