[server]: Make return_newest_data way more efficient
This commit is contained in:
@ -1,31 +1,62 @@
|
|||||||
import boto3
|
import boto3
|
||||||
import json
|
import json
|
||||||
from boto3.dynamodb.conditions import Attr
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
dynamodb = boto3.resource('dynamodb')
|
dynamodb = boto3.resource('dynamodb')
|
||||||
table = dynamodb.Table(os.environ['TABLE_NAME'])
|
table = dynamodb.Table(os.environ['TABLE_NAME'])
|
||||||
|
gsi_name = "objectType-index"
|
||||||
|
|
||||||
def lambda_handler(event, context):
|
def lambda_handler(event, context):
|
||||||
try:
|
try:
|
||||||
response = table.scan(
|
query_params = event.get('queryStringParameters', {}) or {}
|
||||||
FilterExpression=Attr('timestamp').exists()
|
object_type = query_params.get('objectType')
|
||||||
)
|
|
||||||
items = response['Items']
|
|
||||||
|
|
||||||
newest_timestamp = max([int(item['timestamp']) for item in items])
|
# Step 1: Retrieve the latest timestamp
|
||||||
newest_items = [item for item in items if int(item['timestamp']) == newest_timestamp]
|
if object_type:
|
||||||
|
response = table.query(
|
||||||
|
IndexName=gsi_name,
|
||||||
|
KeyConditionExpression="objectType = :obj",
|
||||||
|
ProjectionExpression="#ts",
|
||||||
|
ExpressionAttributeNames={"#ts": "timestamp"},
|
||||||
|
ExpressionAttributeValues={":obj": object_type},
|
||||||
|
Limit=1,
|
||||||
|
ScanIndexForward=False # Get the latest timestamp
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
response = table.scan(
|
||||||
|
ProjectionExpression="#ts",
|
||||||
|
ExpressionAttributeNames={"#ts": "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 not response.get('Items'):
|
||||||
if 'queryStringParameters' in event and event['queryStringParameters'] and 'objectType' in event['queryStringParameters']:
|
return {'statusCode': 200, 'body': json.dumps([])}
|
||||||
objectType = event['queryStringParameters']['objectType']
|
|
||||||
newest_items = [item for item in newest_items if item['objectType'] == objectType]
|
# Extract the newest timestamp
|
||||||
|
newest_timestamp = max(int(item['timestamp']) for item in response['Items'])
|
||||||
|
|
||||||
|
# Convert newest_timestamp to the correct type for DynamoDB comparison
|
||||||
|
if object_type:
|
||||||
|
response = table.query(
|
||||||
|
IndexName=gsi_name,
|
||||||
|
KeyConditionExpression="objectType = :obj AND #ts = :ts",
|
||||||
|
ExpressionAttributeNames={"#ts": "timestamp"},
|
||||||
|
ExpressionAttributeValues={
|
||||||
|
":obj": object_type,
|
||||||
|
":ts": str(newest_timestamp) # Ensure correct type
|
||||||
|
}
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
response = table.scan(
|
||||||
|
FilterExpression="#ts = :ts",
|
||||||
|
ExpressionAttributeNames={"#ts": "timestamp"},
|
||||||
|
ExpressionAttributeValues={":ts": str(newest_timestamp)} # Ensure correct type
|
||||||
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'statusCode': 200,
|
'statusCode': 200,
|
||||||
'body': json.dumps(newest_items)
|
'body': json.dumps(response['Items'])
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {
|
return {
|
||||||
'statusCode': 500,
|
'statusCode': 500,
|
||||||
|
Reference in New Issue
Block a user