[server]: Make return_newest_data accept a list of objectTypes

This commit is contained in:
2025-03-08 16:04:21 +00:00
parent 7274d3952f
commit 615864d8ec

View File

@ -1,6 +1,7 @@
import boto3 import boto3
import json import json
import os import os
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb') dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ['TABLE_NAME']) table = dynamodb.Table(os.environ['TABLE_NAME'])
@ -9,52 +10,92 @@ gsi_name = "objectType-index"
def lambda_handler(event, context): def lambda_handler(event, context):
try: try:
query_params = event.get('queryStringParameters', {}) or {} query_params = event.get('queryStringParameters', {}) or {}
object_type = query_params.get('objectType') object_type_param = query_params.get('objectType')
# Handle multiple object types if provided
if object_type_param:
object_types = [obj.strip() for obj in object_type_param.split(',')]
else:
object_types = []
# Step 1: Retrieve the latest timestamp # Step 1: Retrieve the latest timestamp
if object_type: items = []
response = table.query(
IndexName=gsi_name, if object_types:
KeyConditionExpression="objectType = :obj", # Scan with a filter for multiple object types to get timestamps
response = table.scan(
FilterExpression=Attr('objectType').is_in(object_types),
ProjectionExpression="#ts",
ExpressionAttributeNames={"#ts": "timestamp"}
)
items.extend(response.get('Items', []))
# Handle pagination if necessary
while 'LastEvaluatedKey' in response:
response = table.scan(
FilterExpression=Attr('objectType').is_in(object_types),
ProjectionExpression="#ts", ProjectionExpression="#ts",
ExpressionAttributeNames={"#ts": "timestamp"}, ExpressionAttributeNames={"#ts": "timestamp"},
ExpressionAttributeValues={":obj": object_type}, ExclusiveStartKey=response['LastEvaluatedKey']
Limit=1,
ScanIndexForward=False # Get the latest timestamp
) )
items.extend(response.get('Items', []))
else: else:
# Scan the entire table if no object types are specified
response = table.scan( response = table.scan(
ProjectionExpression="#ts", ProjectionExpression="#ts",
ExpressionAttributeNames={"#ts": "timestamp"} ExpressionAttributeNames={"#ts": "timestamp"}
) )
items.extend(response.get('Items', []))
if not response.get('Items'): # Handle pagination if necessary
while 'LastEvaluatedKey' in response:
response = table.scan(
ProjectionExpression="#ts",
ExpressionAttributeNames={"#ts": "timestamp"},
ExclusiveStartKey=response['LastEvaluatedKey']
)
items.extend(response.get('Items', []))
if not items:
return {'statusCode': 200, 'body': json.dumps([])} return {'statusCode': 200, 'body': json.dumps([])}
# Extract the newest timestamp # Extract the newest timestamp
newest_timestamp = max(int(item['timestamp']) for item in response['Items']) newest_timestamp = max(int(item['timestamp']) for item in items)
# Convert newest_timestamp to the correct type for DynamoDB comparison # Step 2: Fetch items with the newest timestamp for the specified object types
if object_type: items_with_latest_timestamp = []
response = table.query(
IndexName=gsi_name, if object_types:
KeyConditionExpression="objectType = :obj AND #ts = :ts",
ExpressionAttributeNames={"#ts": "timestamp"},
ExpressionAttributeValues={
":obj": object_type,
":ts": str(newest_timestamp) # Ensure correct type
}
)
else:
response = table.scan( response = table.scan(
FilterExpression="#ts = :ts", FilterExpression=Attr('objectType').is_in(object_types) & Attr('timestamp').eq(str(newest_timestamp))
ExpressionAttributeNames={"#ts": "timestamp"},
ExpressionAttributeValues={":ts": str(newest_timestamp)} # Ensure correct type
) )
items_with_latest_timestamp.extend(response.get('Items', []))
# Handle pagination if necessary
while 'LastEvaluatedKey' in response:
response = table.scan(
FilterExpression=Attr('objectType').is_in(object_types) & Attr('timestamp').eq(str(newest_timestamp)),
ExclusiveStartKey=response['LastEvaluatedKey']
)
items_with_latest_timestamp.extend(response.get('Items', []))
else:
# Scan the entire table for the latest timestamp if no object types are specified
response = table.scan(
FilterExpression=Attr('timestamp').eq(str(newest_timestamp))
)
items_with_latest_timestamp.extend(response.get('Items', []))
# Handle pagination if necessary
while 'LastEvaluatedKey' in response:
response = table.scan(
FilterExpression=Attr('timestamp').eq(str(newest_timestamp)),
ExclusiveStartKey=response['LastEvaluatedKey']
)
items_with_latest_timestamp.extend(response.get('Items', []))
return { return {
'statusCode': 200, 'statusCode': 200,
'body': json.dumps(response['Items']) 'body': json.dumps(items_with_latest_timestamp)
} }
except Exception as e: except Exception as e: