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
feat: filter to fill in stop times from schedules #243
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
37f5c23
doc: fix broken guide link in README
digitalcora b61c59f
refactor: move `GTFS` out of `Filter` namespace
digitalcora 666cc90
fix: potential periodic `GTFS.PickupDropOff` issue
digitalcora c3ca297
feat: filter to fill in stop times from schedules
digitalcora edbc809
refactor: combine stop-time-related GTFS servers
digitalcora 3b1f93e
fix: StopTimes.get is probably supposed to be StopTimes.arrival_depar…
losvedir File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,78 @@ | ||
defmodule Concentrate.GroupFilter.ScheduledStopTimes do | ||
@moduledoc """ | ||
Uses the static GTFS schedule to fill in missing arrival/departure times on stop time updates | ||
that have specific `status` values. | ||
|
||
The desired status values must be set in app configuration at compile time. For example: | ||
|
||
config :concentrate, | ||
group_filters: [ | ||
{ | ||
Concentrate.GroupFilter.ScheduledStopTimes, | ||
on_time_statuses: ["status 1", "status 2", "status 3"] | ||
} | ||
] | ||
|
||
If no status values are configured, enabling this filter has no effect. | ||
""" | ||
@behaviour Concentrate.GroupFilter | ||
alias Concentrate.{StopTimeUpdate, TripDescriptor}, warn: false | ||
|
||
@impl Concentrate.GroupFilter | ||
def filter(trip_group, gtfs_stop_times \\ Concentrate.GTFS.StopTimes) | ||
|
||
config_path = [:group_filters, __MODULE__, :on_time_statuses] | ||
@on_time_statuses Application.compile_env(:concentrate, config_path, []) | ||
|
||
if @on_time_statuses == [] do | ||
def filter(trip_group, _), do: trip_group | ||
else | ||
def filter({trip_descriptor, vehicle_positions, [_ | _] = stop_time_updates}, gtfs_stop_times) | ||
when not is_nil(trip_descriptor) do | ||
{ | ||
trip_descriptor, | ||
vehicle_positions, | ||
filter_stop_time_updates( | ||
stop_time_updates, | ||
TripDescriptor.start_date(trip_descriptor), | ||
gtfs_stop_times | ||
) | ||
} | ||
end | ||
|
||
def filter(trip_group, _), do: trip_group | ||
|
||
defp filter_stop_time_updates(stop_time_updates, nil, _), do: stop_time_updates | ||
|
||
defp filter_stop_time_updates(stop_time_updates, trip_date, gtfs_stop_times) do | ||
Enum.map(stop_time_updates, fn stop_time_update -> | ||
fill_in_arrival_departure( | ||
stop_time_update, | ||
trip_date, | ||
StopTimeUpdate.arrival_time(stop_time_update), | ||
StopTimeUpdate.departure_time(stop_time_update), | ||
StopTimeUpdate.status(stop_time_update), | ||
gtfs_stop_times | ||
) | ||
end) | ||
end | ||
|
||
defp fill_in_arrival_departure(stop_time_update, trip_date, nil, nil, status, gtfs_stop_times) | ||
when status in @on_time_statuses do | ||
trip_id = StopTimeUpdate.trip_id(stop_time_update) | ||
stop_sequence = StopTimeUpdate.stop_sequence(stop_time_update) | ||
|
||
case gtfs_stop_times.arrival_departure(trip_id, stop_sequence, trip_date) do | ||
{arrival_time, departure_time} -> | ||
stop_time_update | ||
|> StopTimeUpdate.update_arrival_time(arrival_time) | ||
|> StopTimeUpdate.update_departure_time(departure_time) | ||
|
||
:unknown -> | ||
stop_time_update | ||
end | ||
end | ||
|
||
defp fill_in_arrival_departure(stop_time_update, _, _, _, _, _), do: stop_time_update | ||
end | ||
end |
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.
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.
question: should this note that it needs to come before
RemoveUnneededTimes
in the filter list, or not include arrival/departure times in cases when we'd drop them?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.
I don't think it needs to, strictly speaking. It does need to if you want
RemoveUnneededTimes
to remove times it generates, but I think that's a logical consequence of its position in the filter list. If it's generally understood that filters are run in the order they're configured, I think we don't have to specifically call it out here. (Maybe we do want to call that out somewhere in the general documentation?)