-
Notifications
You must be signed in to change notification settings - Fork 13
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(SiteWeb.StopController): don't show route patterns for services that aren't running today #1762
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fecaad0
feat(RoutePatterns.RoutePattern): add service_id
thecristen 43a0d36
feat(V3Api.Services): get by ID
thecristen 1af0d24
feat(Services.Repo): get by ID
thecristen fecced5
feat(Services.Service): evaluate if a service runs on a date
thecristen 15413a7
fix(StopController): remove some route patterns
thecristen d632823
hotfix(Services.Repo): handle errors
thecristen 6f69a92
fix(Service): correct parsing to YYYY-MM-DD
thecristen 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 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
defmodule Services.Service do | ||
@moduledoc "Processes Services, including dates and notes" | ||
alias JsonApi.Item | ||
use Timex | ||
|
||
defstruct added_dates: [], | ||
added_dates_notes: [], | ||
|
@@ -153,4 +154,57 @@ defmodule Services.Service do | |
defp typicality(5), do: :unplanned_disruption | ||
defp typicality(6), do: :canonical | ||
defp typicality(_), do: :unknown | ||
|
||
@spec serves_date?(t(), Date.t()) :: boolean | ||
def serves_date?(service, date) do | ||
date in all_valid_dates_for_service(service) | ||
end | ||
|
||
@spec all_valid_dates_for_service(t()) :: [Date.t()] | ||
defp all_valid_dates_for_service(%__MODULE__{ | ||
start_date: from, | ||
end_date: until, | ||
added_dates: added_dates, | ||
removed_dates: removed_dates, | ||
valid_days: valid_days | ||
}) do | ||
# fallback to today if either start or end date are nil | ||
from = from || Timex.today() | ||
until = until || Timex.today() | ||
|
||
dates = | ||
if from == until do | ||
[from] | ||
else | ||
[ | ||
from: from, | ||
until: until, | ||
right_open: false | ||
] | ||
|> Interval.new() | ||
|> Enum.map(& &1) | ||
end | ||
|
||
removed_dates = parse_listed_dates(removed_dates) | ||
|
||
(dates ++ parse_listed_dates(added_dates)) | ||
|> Enum.reject(&Enum.member?(removed_dates, &1)) | ||
|> Enum.reject(fn date -> | ||
# if valid_days is an empty array, the service's dates are likely those in | ||
# added_dates, so we can ignore evaluating the day of the week here | ||
if valid_days != [] do | ||
Timex.weekday(date) not in valid_days | ||
end | ||
end) | ||
|> Enum.map(&Timex.to_date/1) | ||
|> Enum.uniq() | ||
end | ||
|
||
@spec parse_listed_dates([String.t()]) :: [NaiveDateTime.t()] | ||
defp parse_listed_dates(date_strings) do | ||
date_strings | ||
|> Enum.map(&Timex.parse(&1, "{YYYY}-{0M}-{D}")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ..yes. I will update |
||
|> Enum.filter(&(elem(&1, 0) == :ok)) | ||
|> Enum.map(&elem(&1, 1)) | ||
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
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,20 @@ | ||
defmodule V3Api.ServicesTest do | ||
use ExUnit.Case | ||
|
||
alias V3Api.Services | ||
|
||
@opts ["page[limit]": 1, sort: "id"] | ||
|
||
describe "all/1" do | ||
test "gets all services" do | ||
assert %JsonApi{data: [%JsonApi.Item{}]} = Services.all(@opts) | ||
end | ||
end | ||
|
||
describe "get/1" do | ||
test "gets the services by ID" do | ||
%JsonApi{data: [%JsonApi.Item{} = service]} = Services.get("canonical") | ||
assert service.id == "canonical" | ||
end | ||
end | ||
end |
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.
This arraying of relationships is exactly what is happening in the
line
parsing I am working on. Should we keep this behavior around?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.
Yeah, I see what you mean.
Changing this (via removing
list()
in the relevant part of the%JsonApi.Item{}
struct here) would require a baffling amount of downstream changes in all of our V3 API parsing. I don't see the benefit in changing it.