-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda_function.py
75 lines (64 loc) · 2.51 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import boto3
import json
from decimal import Decimal
from datetime import datetime
print('Loading function')
dynamo = boto3.resource('dynamodb')
def respond(err, res=None):
return {
'statusCode': '400' if err else '200',
'body': json.dumps({'error': str(err)}) if err else json.dumps(res, default=str),
'headers': {
'Content-Type': 'application/json',
},
}
def people_in_jasper(items):
#calculates the number of people still in jasper by Sunday June 7th @5pm
# The target date and time (June 27, 2024 at 3:00 PM)
target_datetime = datetime(2024, 6, 27, 15, 0, 0)
sum = 0
for item in items:
longitude = float(item['longitude'])
latitude = float(item['latitude'])
date = str(item['timestamp'])
date = datetime.fromisoformat(date)
if (longitude > -118.7) and (longitude <-118.5):
if (latitude > 53.8) and (latitude < 54.0):
if date > target_datetime:
#then this person is in the area of jasper past this time
sum += 1
return sum
def stuck_in_traffic(items):
#number of people in jasper alsos stuck in traffic
sum = 0
for item in items:
#if person made "slow traffic" report:
if str(item['reportType']) == "Slow Area":
#if person is in Jasper
longitude = float(item['longitude'])
latitude = float(item['latitude'])
date = str(item['timestamp'])
date = datetime.fromisoformat(date)
if (longitude > -118.7) and (longitude <-118.5):
if (latitude > 53.8) and (latitude < 54.0):
#if this person's speed is indeeded slow (misinformation prevention)
if float(item['currentSpeed_kmh']) < 20.0:
sum += 1
return sum
def lambda_handler(event, context):
#operation = event['httpMethod']
operation = "GET"
if operation:
try:
table = dynamo.Table("reports-table")
response = table.scan()
items = response['Items']
#perform calculations
result1 = people_in_jasper(items)
result2 = stuck_in_traffic(items)
#return calculated results
return [result1, result2]
except Exception as e:
return respond(e)
else:
return respond(ValueError('Unsupported method "{}"'.format(operation)))