Skip to content

Commit 2009efc

Browse files
committed
update format
1 parent 2935bfa commit 2009efc

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

sds_data_manager/constructs/ialirt_api_manager_construct.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,14 @@ def __init__(
223223
handler="IAlirtCode.ialirt_db_query_api.lambda_handler",
224224
runtime=lambda_.Runtime.PYTHON_3_12,
225225
timeout=cdk.Duration.minutes(1),
226-
memory_size=1000,
226+
# Lambda allocates CPU proportionally to memory,
227+
# and DynamoDB queries are often CPU-bound due to
228+
# JSON parsing and network serialization.
229+
memory_size=2048, # MB
227230
environment={
228231
"ALGORITHM_TABLE": algorithm_table.table_name,
229232
"REGION": env.region,
230233
},
231-
layers=layers,
232234
)
233235

234236
# Grant the lambda function read/write permissions on the DynamoDB table.
@@ -250,12 +252,14 @@ def __init__(
250252
handler="IAlirtCode.ialirt_db_query_api_formatted.lambda_handler",
251253
runtime=lambda_.Runtime.PYTHON_3_12,
252254
timeout=cdk.Duration.minutes(1),
253-
memory_size=1000,
255+
# Lambda allocates CPU proportionally to memory,
256+
# and DynamoDB queries are often CPU-bound due to
257+
# JSON parsing and network serialization.
258+
memory_size=2048,
254259
environment={
255260
"ALGORITHM_TABLE": algorithm_table.table_name,
256261
"REGION": env.region,
257262
},
258-
layers=layers,
259263
)
260264

261265
# Grant the lambda function read/write permissions on the DynamoDB table.

sds_data_manager/lambda_code/IAlirtCode/ialirt_db_query_api.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import logging
55
import os
6+
import time
67
from decimal import Decimal
78

89
import boto3
@@ -50,7 +51,7 @@ def process_item_types(item: dict) -> dict:
5051
return result
5152

5253

53-
def lambda_handler(event, context): # noqa: PLR0912
54+
def lambda_handler(event, context): # noqa: PLR0912, PLR0915
5455
"""Create metadata and add it to the database.
5556
5657
This function is an event handler for s3 ingest bucket.
@@ -67,12 +68,16 @@ def lambda_handler(event, context): # noqa: PLR0912
6768
and runtime environment.
6869
6970
"""
71+
t0 = time.perf_counter()
7072
table_name = os.environ.get("ALGORITHM_TABLE")
7173
region = os.environ.get("AWS_DEFAULT_REGION", "us-west-2")
7274
dynamodb = boto3.resource("dynamodb", region_name=region)
7375
table = dynamodb.Table(table_name)
76+
t1 = time.perf_counter()
7477

7578
logger.info(f"Received event: {json.dumps(event)}")
79+
80+
# --- Parse event ---
7681
params = event.get("queryStringParameters", {})
7782

7883
if not params:
@@ -83,7 +88,9 @@ def lambda_handler(event, context): # noqa: PLR0912
8388

8489
key_expr = Key("apid").eq(478)
8590
query_kwargs = {"KeyConditionExpression": key_expr}
91+
t2 = time.perf_counter()
8692

93+
# --- Determine key condition ---
8794
allowed_params = {
8895
"met_start",
8996
"met_end",
@@ -183,10 +190,30 @@ def lambda_handler(event, context): # noqa: PLR0912
183190
}
184191

185192
query_kwargs["KeyConditionExpression"] = key_expr
193+
t3 = time.perf_counter()
186194

195+
# --- Query DynamoDB ---
187196
response = table.query(**query_kwargs)
197+
t4 = time.perf_counter()
188198

199+
# --- Process items ---
189200
items = response.get("Items", [])
190201
processed_items = [process_item_types(item) for item in items]
202+
t5 = time.perf_counter()
203+
204+
# --- Serialize to JSON ---
205+
json_body = json.dumps(processed_items)
206+
t6 = time.perf_counter()
207+
208+
num_items = len(processed_items)
209+
210+
text = (
211+
f"[TIMER SUMMARY] DynamoDB init: {t1 - t0:.3f}s | "
212+
f"Param parse: {t2 - t1:.3f}s | KeyCondition setup: {t3 - t2:.3f}s | "
213+
f"Query: {t4 - t3:.3f}s | Process: {t5 - t4:.3f}s | "
214+
f"JSON: {t6 - t5:.3f}s | TOTAL: {t6 - t0:.3f}s | "
215+
f"Items: {num_items}"
216+
)
217+
logger.info(text)
191218

192-
return {"statusCode": 200, "body": json.dumps(processed_items)}
219+
return {"statusCode": 200, "body": json_body}

0 commit comments

Comments
 (0)