[server]: Update punctuality_by_timestamp table
This commit is contained in:
@ -4,16 +4,20 @@ import requests
|
|||||||
import os
|
import os
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
|
# Initialize DynamoDB resource
|
||||||
dynamodb = boto3.resource("dynamodb")
|
dynamodb = boto3.resource("dynamodb")
|
||||||
table = dynamodb.Table("punctuality_by_objectID")
|
table_train = dynamodb.Table("punctuality_by_objectID")
|
||||||
|
table_timestamp = dynamodb.Table("punctuality_by_timestamp")
|
||||||
|
|
||||||
API_URL = "https://281bc6mcm5.execute-api.us-east-1.amazonaws.com/transient_data?objectType=IrishRailTrain"
|
API_URL = "https://281bc6mcm5.execute-api.us-east-1.amazonaws.com/transient_data?objectType=IrishRailTrain"
|
||||||
|
|
||||||
|
|
||||||
def fetch_train_data():
|
def fetch_train_data():
|
||||||
|
"""Fetch train data from API."""
|
||||||
try:
|
try:
|
||||||
response = requests.get(API_URL)
|
response = requests.get(API_URL)
|
||||||
response.raise_for_status() # Raise an error for bad status codes
|
response.raise_for_status()
|
||||||
if response.text.strip(): # Ensure response is not empty
|
if response.text.strip():
|
||||||
return response.json()
|
return response.json()
|
||||||
else:
|
else:
|
||||||
print("Error: Empty response from API")
|
print("Error: Empty response from API")
|
||||||
@ -27,8 +31,9 @@ def fetch_train_data():
|
|||||||
|
|
||||||
|
|
||||||
def update_punctuality(objectID, new_punctuality):
|
def update_punctuality(objectID, new_punctuality):
|
||||||
|
"""Update punctuality data for a specific train."""
|
||||||
new_punctuality = Decimal(str(new_punctuality)) # Ensure Decimal type for DynamoDB
|
new_punctuality = Decimal(str(new_punctuality)) # Ensure Decimal type for DynamoDB
|
||||||
response = table.get_item(Key={"objectID": objectID})
|
response = table_train.get_item(Key={"objectID": objectID})
|
||||||
if "Item" in response:
|
if "Item" in response:
|
||||||
item = response["Item"]
|
item = response["Item"]
|
||||||
old_avg = Decimal(str(item["average_punctuality"]))
|
old_avg = Decimal(str(item["average_punctuality"]))
|
||||||
@ -38,8 +43,8 @@ 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, renaming 'count' to avoid using a reserved keyword
|
# Update the DynamoDB table, renaming 'count' to avoid reserved keyword issues
|
||||||
table.update_item(
|
table_train.update_item(
|
||||||
Key={"objectID": objectID},
|
Key={"objectID": objectID},
|
||||||
UpdateExpression="SET average_punctuality = :avg, #cnt = :cnt",
|
UpdateExpression="SET average_punctuality = :avg, #cnt = :cnt",
|
||||||
ExpressionAttributeValues={":avg": new_avg, ":cnt": count},
|
ExpressionAttributeValues={":avg": new_avg, ":cnt": count},
|
||||||
@ -47,22 +52,52 @@ def update_punctuality(objectID, new_punctuality):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# Insert new train punctuality record
|
# Insert new train punctuality record
|
||||||
table.put_item(
|
table_train.put_item(
|
||||||
Item={"objectID": objectID, "average_punctuality": new_punctuality, "count": 1}
|
Item={"objectID": objectID, "average_punctuality": new_punctuality, "count": 1}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def update_punctuality_by_timestamp(timestamp, punctualities):
|
||||||
|
"""Update the average punctuality for a given timestamp."""
|
||||||
|
if not punctualities:
|
||||||
|
return
|
||||||
|
|
||||||
|
avg_punctuality = sum(punctualities) / len(punctualities)
|
||||||
|
|
||||||
|
# Insert or update record in DynamoDB
|
||||||
|
table_timestamp.put_item(
|
||||||
|
Item={
|
||||||
|
"timestamp": timestamp,
|
||||||
|
"average_punctuality": avg_punctuality
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def lambda_handler(event, context):
|
def lambda_handler(event, context):
|
||||||
|
"""AWS Lambda handler."""
|
||||||
train_data = fetch_train_data()
|
train_data = fetch_train_data()
|
||||||
|
|
||||||
|
if not train_data:
|
||||||
|
return {"statusCode": 500, "body": json.dumps("No train data available")}
|
||||||
|
|
||||||
|
# Extract timestamp (assuming all records share the same timestamp)
|
||||||
|
timestamp = train_data[0].get("timestamp") if train_data else None
|
||||||
|
if not timestamp:
|
||||||
|
return {"statusCode": 500, "body": json.dumps("Missing timestamp in train data")}
|
||||||
|
|
||||||
|
punctualities = []
|
||||||
for train in train_data:
|
for train in train_data:
|
||||||
objectID = train.get("objectID")
|
objectID = train.get("objectID")
|
||||||
punctuality = int(train.get("trainPunctuality", 0))
|
punctuality = int(train.get("trainPunctuality", 0))
|
||||||
|
|
||||||
if objectID:
|
if objectID:
|
||||||
update_punctuality(objectID, punctuality)
|
update_punctuality(objectID, punctuality)
|
||||||
|
punctualities.append(punctuality)
|
||||||
|
|
||||||
|
# Update average punctuality for the timestamp
|
||||||
|
update_punctuality_by_timestamp(timestamp, punctualities)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"statusCode": 200,
|
"statusCode": 200,
|
||||||
"body": json.dumps("Punctuality data updated successfully")
|
"body": json.dumps("Punctuality data updated successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user