Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(yankee): make it actually write to dynamo. do some cleanup of stdout #90

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ingestor/.chalice/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
"update_yankee_shuttles": {
"iam_policy_file": "policy-yankee.json",
"lambda_memory_size": 1024,
"lambda_timeout": 900,
"max_ebs_size_gb": 2
"lambda_timeout": 60,
"max_ebs_size_gb": 5
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions ingestor/chalicelib/yankee.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def get_driving_distance(old_coords: Tuple[float, float], new_coords: Tuple[floa
return float(response_json["routes"][0]["distance"]) * METERS_PER_MILE


# TODO: this function is doing too much, trying to make it chill
@tracer.wrap()
def _update_shuttles(last_bus_positions: List[Dict], shuttle_shapes: ShapeDict, shuttle_stops: List[Stop]):
url = "https://api.samsara.com/fleet/vehicles/locations"
Expand Down Expand Up @@ -260,7 +261,6 @@ def _update_shuttles(last_bus_positions: List[Dict], shuttle_shapes: ShapeDict,
break

if detected_route is None:
print(f"Bus {name} at coordinates ({long}, {lat}) not detected on any route")
continue

last_detected_stop_id = -1
Expand All @@ -281,12 +281,11 @@ def _update_shuttles(last_bus_positions: List[Dict], shuttle_shapes: ShapeDict,
if detected_stop_id == -1:
detected_stop_id = last_detected_stop_id

update_date = last_update_date
# here, we've had the bus arrive at a new stop!
if detected_stop_id != last_detected_stop_id and last_detected_stop_id != -1:
# insert into table
print(f"Bus {name} arrived at stop {detected_stop_id} from stop {last_detected_stop_id}")
travel_time = create_travel_time(
travel_time = maybe_create_travel_time(
name, detected_route, last_detected_stop_id, detected_stop_id, last_update_date, shuttle_stops
)
travel_times.append(travel_time)
Expand All @@ -299,7 +298,7 @@ def _update_shuttles(last_bus_positions: List[Dict], shuttle_shapes: ShapeDict,
"longitude": long,
"detected_route": detected_route,
"detected_stop_id": detected_stop_id,
"last_update_date": update_date,
"last_update_date": datetime.now(),
}
)

Expand All @@ -308,7 +307,7 @@ def _update_shuttles(last_bus_positions: List[Dict], shuttle_shapes: ShapeDict,
return bus_positions


def create_travel_time(
def maybe_create_travel_time(
name: str,
route_id: str,
last_detected_stop_id: int,
Expand All @@ -318,7 +317,10 @@ def create_travel_time(
):
# don't write travel times with no start date
if last_update_date is None:
return
print(
f"Position of bus {name} on {route_id} from {last_detected_stop_id} to {detected_stop_id} had no last update date, cannot create travel time"
)
return None

last_update_datetime = datetime.strptime(last_update_date, TIME_FORMAT)
update_datetime = datetime.now()
Expand Down Expand Up @@ -374,6 +376,4 @@ def update_shuttles():
shuttle_shapes = get_shuttle_shapes(session)
shuttle_stops = get_shuttle_stops(session)

updated_positions = _update_shuttles(last_bus_positions, shuttle_shapes, shuttle_stops)

save_bus_positions(updated_positions)
_update_shuttles(last_bus_positions, shuttle_shapes, shuttle_stops)