From 20fc93f6084ff1962efd89a18193a2fe4d560582 Mon Sep 17 00:00:00 2001 From: Tim Beccue Date: Mon, 28 Oct 2024 10:58:42 -0400 Subject: [PATCH] Add endpoint for removing expired lco scheduler events --- handler.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ serverless.yml | 8 +++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/handler.py b/handler.py index 8a70d70..9aed054 100644 --- a/handler.py +++ b/handler.py @@ -127,6 +127,43 @@ def getProject(project_name, created_at): return "Project not found." +def remove_expired_scheduler_events(cutoff_time, site): + table = dynamodb.Table(calendar_table_name) + index_name = "site-end-index" + + # 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 = 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) + ) + items = query.get('Items', []) + + # Extract key attributes for deletion (use the primary key attributes, not the index keys) + key_names = [k['AttributeName'] for k in table.key_schema] + + with table.batch_writer() as batch: + for item in items: + batch.delete_item(Key={k: item[k] for k in key_names if k in item}) + + # Handle pagination if results exceed 1MB + while 'LastEvaluatedKey' in query: + query = 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), + ExclusiveStartKey=query['LastEvaluatedKey'] + ) + items = query.get('Items', []) + + with table.batch_writer() as batch: + for item in items: + batch.delete_item(Key={k: item[k] for k in key_names if k in item}) + + + #=========================================# #======= API Endpoints ========# #=========================================# @@ -392,6 +429,22 @@ def deleteEventById(event, context): print(f"success deleting event, message: {message}") return create_response(200, message) +def clearExpiredSchedule(event, context): + """Endpoint to delete calendar events with an event_id. + + Args: + event.body.site (str): + sitecode for the site we are dealing with + event.body.time (str): + UTC datestring (eg. '2022-05-14T17:30:00Z'). All events that start after this will be removed. + + Returns: + 200 status code + """ + event_body = json.loads(event.get("body", "")) + remove_expired_scheduler_events(event_body["cutoff_time"], event_body["site"]) + return create_response(200) + def getSiteEventsInDateRange(event, context): """Return calendar events within a specified date range at a given site. diff --git a/serverless.yml b/serverless.yml index cc2b80e..9224ecc 100644 --- a/serverless.yml +++ b/serverless.yml @@ -265,7 +265,13 @@ functions: - X-Amz-User-Agent - Access-Control-Allow-Origin - Access-Control-Allow-Credentials - + clearExpiredSchedule: + handler: handler.clearExpiredSchedule + events: + - http: + path: remove-expired-lco-schedule + method: post + cors: true getSiteEventsInDateRange: handler: handler.getSiteEventsInDateRange events: