33import json
44import logging
55import os
6+ import time
67from decimal import Decimal
78
89import 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