From b5c43ca50b1109dfefc06419ef554967d9837bdd Mon Sep 17 00:00:00 2001 From: Simon Li Date: Wed, 20 Mar 2024 10:54:06 +0000 Subject: [PATCH 1/2] Paginate list_requests DynamoDb.scan --- egress_backend/lambda/egress_api/list_requests.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/egress_backend/lambda/egress_api/list_requests.py b/egress_backend/lambda/egress_api/list_requests.py index 37aad4e..c4b3250 100644 --- a/egress_backend/lambda/egress_api/list_requests.py +++ b/egress_backend/lambda/egress_api/list_requests.py @@ -21,6 +21,10 @@ def list_requests(): ddb_table = ddb.Table(table) response = ddb_table.scan() + data = response["Items"] + while response.get("LastEvaluatedKey"): + response = ddb_table.scan(ExclusiveStartKey=response["LastEvaluatedKey"]) + data.extend(response["Items"]) - logger.debug("Succesful database scan of all egress requests") - return response["Items"] + logger.debug("Successful database scan of all egress requests") + return data From 79bec9faddedcd949c3ad2e059e5c605ddafe142 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Wed, 20 Mar 2024 11:55:37 +0000 Subject: [PATCH 2/2] Don't return eggress requests older than 90 days in list_requests --- egress_backend/lambda/egress_api/list_requests.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/egress_backend/lambda/egress_api/list_requests.py b/egress_backend/lambda/egress_api/list_requests.py index c4b3250..6ce76db 100644 --- a/egress_backend/lambda/egress_api/list_requests.py +++ b/egress_backend/lambda/egress_api/list_requests.py @@ -4,10 +4,13 @@ # agreement between Customer and Amazon Web Services, Inc. import os +from datetime import datetime import boto3 from aws_lambda_powertools import Logger, Tracer +MAX_REQUEST_AGE_DAYS = 90 + tracer = Tracer(service="ListRequestsAPI") logger = Logger(service="ListRequestsAPI") @@ -18,13 +21,19 @@ def list_requests(): logger.debug("List Requests API invoked") + now = datetime.now() + + def is_recent(item): + updated_dt = datetime.strptime(item["updated_dt"], "%Y-%m-%dT%H:%M:%S.%fZ") + return (now - updated_dt).days < MAX_REQUEST_AGE_DAYS + ddb_table = ddb.Table(table) response = ddb_table.scan() - data = response["Items"] + data = [item for item in response["Items"] if is_recent(item)] while response.get("LastEvaluatedKey"): response = ddb_table.scan(ExclusiveStartKey=response["LastEvaluatedKey"]) - data.extend(response["Items"]) + data.extend([item for item in response["Items"] if is_recent(item)]) logger.debug("Successful database scan of all egress requests") return data