-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding backend changes for alerts view
- Loading branch information
1 parent
109e923
commit b30fbc7
Showing
8 changed files
with
764 additions
and
705 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import TypedDict | ||
from chalice import BadRequestError, ForbiddenError | ||
from chalicelib import dynamo | ||
from datetime import date, datetime, timedelta | ||
from chalicelib.constants import DATE_FORMAT_BACKEND | ||
|
||
|
||
TABLE_NAME = "AlertDelaysWeekly" | ||
MAX_DELTA = 5000 | ||
|
||
|
||
class AlertDelaysByLineParams(TypedDict): | ||
start_date: str | date | ||
end_date: str | date | ||
line: str | ||
|
||
|
||
def is_invalid_range(start_date, end_date, max_delta): | ||
"""Check if number of requested entries is more than maximum for the table""" | ||
start_datetime = datetime.strptime(start_date, DATE_FORMAT_BACKEND) | ||
end_datetime = datetime.strptime(end_date, DATE_FORMAT_BACKEND) | ||
return start_datetime + timedelta(days=max_delta) < end_datetime | ||
|
||
|
||
def delay_time_by_line(params: AlertDelaysByLineParams): | ||
try: | ||
start_date = params["start_date"] | ||
end_date = params["end_date"] | ||
line = params["line"] | ||
if line not in [ | ||
"Red", | ||
"Blue", | ||
"Orange", | ||
"Green-B", | ||
"Green-C", | ||
"Green-D", | ||
"Green-E", | ||
]: | ||
raise BadRequestError("Invalid Line key.") | ||
except KeyError: | ||
raise BadRequestError("Missing or invalid parameters.") | ||
# Prevent queries of more than 5000 items. | ||
if is_invalid_range(start_date, end_date, MAX_DELTA): | ||
raise ForbiddenError("Date range too long. The maximum number of requested values is 150.") | ||
# If querying for weekly/monthly data, can just return the query. | ||
return dynamo.query_agg_trip_metrics(start_date, end_date, TABLE_NAME, line) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.