[server]: Update return_newest_data and add test script

This commit is contained in:
2025-02-28 14:04:29 +00:00
parent 4aff5300e5
commit 508b86acd9
2 changed files with 14 additions and 2 deletions

View File

@ -8,15 +8,18 @@ table = dynamodb.Table(os.environ['TABLE_NAME'])
def lambda_handler(event, context): def lambda_handler(event, context):
try: try:
# Scan the table to get all items
response = table.scan( response = table.scan(
FilterExpression=Attr('timestamp').exists() FilterExpression=Attr('timestamp').exists()
) )
items = response['Items'] items = response['Items']
newest_timestamp = max([int(item['timestamp'] for item in items)]) newest_timestamp = max([int(item['timestamp']) for item in items])
newest_items = [item for item in items if int(item['timestamp']) == newest_timestamp] newest_items = [item for item in items if int(item['timestamp']) == newest_timestamp]
# assuming that filtering by timestamp first makes sense, as we expect to have a lot of historical data and not many object types
if 'queryStringParameters' in event and event['queryStringParameters'] and 'objectType' in event['queryStringParameters']:
newest_items = [item for item in items if item['objectType'] == event['queryStringParameters']['objectType']]
return { return {
'statusCode': 200, 'statusCode': 200,
'body': json.dumps(newest_items) 'body': json.dumps(newest_items)

View File

@ -0,0 +1,9 @@
#!/bin/sh
API_URL="https://281bc6mcm5.execute-api.us-east-1.amazonaws.com/transient_data"
if [ "$1" ]; then
query_string="?objectType=$1"
fi
curl "$API_URL$query_string"