-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
147 lines (118 loc) · 4.23 KB
/
lambda_function.py
File metadata and controls
147 lines (118 loc) · 4.23 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import math
from datetime import datetime, timedelta
import pytz
import pg8000
DB_HOST = os.environ["DB_HOST"]
DB_USER = os.environ["DB_USER"]
DB_PASSWORD = os.environ["DB_PASSWORD"]
DB_NAME = os.environ["DB_NAME"]
def landslide_probability(rainfall_mm: float) -> float:
intercept = -13.7821
coefficient = 0.4294
z = intercept + coefficient * rainfall_mm
return math.exp(z) / (1 + math.exp(z))
def landslide_risk(rainfall_mm: float) -> int:
prob = landslide_probability(rainfall_mm)
if prob <= 0.01:
return 0
elif prob <= 0.7:
return 1
elif prob > 0.7:
return 2
def get_rainfall_last_3h(place_name: str) -> float:
# Placeholder: replace with real HTTP request / API logic
return 1.5
def get_places_from_event(event) -> list[str]:
if isinstance(event, dict):
if "place_name" in event and event["place_name"]:
return [event["place_name"]]
if "places" in event and isinstance(event["places"], list):
return event["places"]
return []
def get_place_id(place_name: str) -> int:
place_ids = {"Craig": "AK91", "Kasaan": "AK182"}
return place_ids.get(place_name, None)
def is_risk_elevated_from_previous(
cursor, place_name: str, current_prob: float
) -> bool:
sql = """
SELECT risk_prob FROM precip_risk
WHERE place_name = %s
ORDER BY ts DESC
LIMIT 1
"""
cursor.execute(sql, (place_name,))
result = cursor.fetchone()
if result is None:
return False
previous_prob = result[0]
return current_prob > previous_prob
def lambda_handler(event, context):
places_to_run = get_places_from_event(event)
alaska_tz = pytz.timezone("US/Alaska")
now = datetime.now(alaska_tz)
ts = now
expires_at = now + timedelta(hours=3)
expires_at_str = expires_at
conn = pg8000.connect(
host=DB_HOST, user=DB_USER, password=DB_PASSWORD, database=DB_NAME
)
conn.autocommit = True
try:
with conn.cursor() as cur:
for place_name in places_to_run:
rainfall_mm = get_rainfall_last_3h(place_name)
prob = landslide_probability(rainfall_mm)
risk = landslide_risk(rainfall_mm)
risk_is_elevated = is_risk_elevated_from_previous(cur, place_name, prob)
precip_inches = rainfall_mm / 25.4
# Place holders for 24hr, 2days, 3days rainfall and risk calculations
precip24hr = rainfall_mm * 8
risk24hr = landslide_risk(precip24hr)
precip2days = rainfall_mm * 16
risk2days = landslide_risk(precip2days)
precip3days = rainfall_mm * 24
risk3days = landslide_risk(precip3days)
place_id = get_place_id(place_name)
sql = """
INSERT INTO precip_risk (
ts, place_name, precip, precip_inches, hour,
risk_prob, risk_level, risk_is_elevated_from_previous,
precip24hr, risk24hr, precip2days, risk2days, precip3days, risk3days, expires_at, place_id
) VALUES (
%s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
)
"""
cur.execute(
sql,
(
ts,
place_name,
rainfall_mm,
precip_inches,
now.strftime("%I%p"),
prob,
risk,
risk_is_elevated,
precip24hr,
risk24hr,
precip2days,
risk2days,
precip3days,
risk3days,
expires_at_str,
place_id,
),
)
return {
"status": "ok",
"places_processed": places_to_run,
"timestamp": ts.isoformat(),
}
finally:
conn.close()
if __name__ == "__main__":
test_event = {"places": ["Kasaan", "Craig", "Anchorage"]}
print(lambda_handler(test_event, None))