-
Notifications
You must be signed in to change notification settings - Fork 3
Track long outages #61
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
Draft
hamima-halim
wants to merge
8
commits into
main
Choose a base branch
from
track-long-outages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6fc1cec
inital draft to track outages
hamima-halim 3dd4e7c
upd8
hamima-halim 346c588
quick container to keep track of degenerate vehicles
hamima-halim a88be0d
comment
hamima-halim ddf3e51
the purge
hamima-halim 1ca6670
commetn
hamima-halim eed70e3
comment
hamima-halim 6a85aab
upd8
hamima-halim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,75 @@ | ||
| from datetime import datetime | ||
| import pandas as pd | ||
| from typing import Dict, List, Optional | ||
|
|
||
| # a data structure of negligent vehicles. | ||
| # purge this on a new service date. i dont like that this is a dict--should this be a redis cache? dynamodb? also route shapes | ||
|
|
||
| # TODO: should we track degenerate vehicles? the knowledge isnt actionable but it is neat | ||
| cache_key_fmt = "{vehicle_label}_{trip_id}" | ||
| OUTAGES_BY_VEHICLE_AND_TRIP: Dict[str, List[Dict]] = {} | ||
|
|
||
| SAME_OUTAGE_TIMEDELTA = datetime.timedelta(seconds=30) | ||
| LONG_OUTAGE_TIMEDELTA = datetime.timedelta(minutes=1) | ||
|
|
||
|
|
||
| def _add_update_to_sequence(cache_key: str, update: Dict) -> None: | ||
| if cache_key in OUTAGES_BY_VEHICLE_AND_TRIP: | ||
| OUTAGES_BY_VEHICLE_AND_TRIP[cache_key].append(update) | ||
| else: | ||
| OUTAGES_BY_VEHICLE_AND_TRIP[cache_key] = [update] | ||
|
|
||
|
|
||
| def _remove(cache_key: str) -> None: | ||
| OUTAGES_BY_VEHICLE_AND_TRIP[cache_key] = [] | ||
|
|
||
|
|
||
| def purge_cache(): | ||
| nonlocal OUTAGES_BY_VEHICLE_AND_TRIP | ||
| OUTAGES_BY_VEHICLE_AND_TRIP = {} | ||
|
|
||
|
|
||
| def attempt_enrich_update(update: Dict) -> Optional[Dict]: | ||
| # if direction is none, attempt pull from trips.txt | ||
| # if stop is null, interpolate along shapes.txt (trip-shape relationship in trips.txt) | ||
| # maybe fetch and cache shape? | ||
| # if there are a couple of options, tie-break with the schedule? | ||
| # if current_status is IN_TRANSIT... | ||
| # check previous location stamp. if its TOO CLOSE, it might be stopped. | ||
| # if its TOO CLOSE to a stop location, its stopped at a stop. | ||
| # otherwise, it might clogged in traffic, sitting at a red, whatever. | ||
| # update event_type accordingly | ||
| return None | ||
|
|
||
|
|
||
| def report_outage(update: Dict) -> Optional[pd.DataFrame]: | ||
| """Given an outage event, cache it and potentially try fill the missing information. | ||
|
|
||
| If the outage duration is small (<1 minute,) it will return nothing. | ||
| It will then attempt to fill the missing information using shape interpolation and gtfs data | ||
| This might still fail and return nothing. | ||
| """ | ||
| cache_key = cache_key_fmt.format(vehicle_label=update["vehicle_label"], trip_id=update["trip_id"]) | ||
| outage_sequence = OUTAGES_BY_VEHICLE_AND_TRIP.get(cache_key, []) | ||
| if len(outage_sequence) == 0: | ||
| _add_update_to_sequence(cache_key, update) | ||
| return None | ||
|
|
||
| # compare latest outage timestamp.... | ||
| last_ts = outage_sequence[-1]["updated_at"] | ||
| first_ts = outage_sequence[0]["updated_at"] | ||
| current_ts = update["updated_at"] | ||
|
|
||
| # if timestamps are not close, remove entire cache entry. this is now the first instance of a new outage. | ||
| if current_ts - last_ts >= SAME_OUTAGE_TIMEDELTA: | ||
| _remove(cache_key) | ||
| _add_update_to_sequence(cache_key, update) | ||
| return None | ||
| else: | ||
| _add_update_to_sequence(cache_key, update) | ||
| # if the outage is <1 min, not long enough to bother with shape interpolation yet. | ||
| if current_ts - first_ts <= LONG_OUTAGE_TIMEDELTA: | ||
| return None | ||
| else: | ||
| # else, attempt to intuit the stop information via shapes and trip info | ||
| return attempt_enrich_update(update) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fake news-ass comment