[server]: Fix reserved keyword error

This commit is contained in:
2025-03-24 23:30:03 +00:00
parent 797ab3a915
commit f91a38d6ac

View File

@ -10,11 +10,19 @@ API_URL = "https://281bc6mcm5.execute-api.us-east-1.amazonaws.com/transient_data
def fetch_train_data(): def fetch_train_data():
response = requests.get(API_URL) try:
if response.status_code == 200: response = requests.get(API_URL)
return response.json() response.raise_for_status() # Raise an error for bad status codes
else: if response.text.strip(): # Ensure response is not empty
print(f"Failed to fetch data: {response.status_code}") return response.json()
else:
print("Error: Empty response from API")
return []
except requests.exceptions.RequestException as e:
print(f"API request failed: {e}")
return []
except json.JSONDecodeError as e:
print(f"JSON decoding failed: {e}")
return [] return []
@ -30,11 +38,12 @@ def update_punctuality(objectID, new_punctuality):
new_avg = ((old_avg * count) + new_punctuality) / (count + 1) new_avg = ((old_avg * count) + new_punctuality) / (count + 1)
count += 1 count += 1
# Update the DynamoDB table # Update the DynamoDB table, renaming 'count' to avoid using a reserved keyword
table.update_item( table.update_item(
Key={"objectID": objectID}, Key={"objectID": objectID},
UpdateExpression="SET average_punctuality = :avg, count = :cnt", UpdateExpression="SET average_punctuality = :avg, #cnt = :cnt",
ExpressionAttributeValues={":avg": new_avg, ":cnt": count} ExpressionAttributeValues={":avg": new_avg, ":cnt": count},
ExpressionAttributeNames={"#cnt": "count"} # Alias 'count' to avoid reserved keyword issue
) )
else: else:
# Insert new train punctuality record # Insert new train punctuality record
@ -56,3 +65,4 @@ def lambda_handler(event, context):
"statusCode": 200, "statusCode": 200,
"body": json.dumps("Punctuality data updated successfully") "body": json.dumps("Punctuality data updated successfully")
} }