Skip to content

Commit

Permalink
fix(elixir/credo): convert single with statements to cases
Browse files Browse the repository at this point in the history
  • Loading branch information
firestack committed Nov 30, 2023
1 parent b56fd72 commit 5e4417d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 64 deletions.
104 changes: 52 additions & 52 deletions lib/schedule/fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,41 +62,41 @@ defmodule Schedule.Fetcher do

Logger.info("#{__MODULE__}: Polling for new schedule data")

with {:ok, data, gtfs_timestamp, hastus_timestamp, continue_polling?} <-
fetch_gtfs(
state[:files_source],
state[:latest_gtfs_timestamp],
state[:latest_hastus_timestamp]
) do
schedule_state = {:loaded, data}

update_start_time = Time.utc_now()
:ok = state[:updater_function].(schedule_state)

Logger.info(
"#{__MODULE__}: Sent updated schedule data to receiving process, time_in_ms=#{Time.diff(Time.utc_now(), update_start_time, :millisecond)}"
)

Logger.info(
"#{__MODULE__}: Successfully loaded schedule data, time_in_ms=#{Time.diff(Time.utc_now(), start_time, :millisecond)}"
)

if notify_health_server? && state[:health_server] do
Health.Server.loaded(state[:health_server])
end
case fetch_gtfs(
state[:files_source],
state[:latest_gtfs_timestamp],
state[:latest_hastus_timestamp]
) do
{:ok, data, gtfs_timestamp, hastus_timestamp, continue_polling?} ->
schedule_state = {:loaded, data}

if continue_polling? do
Process.send_after(self(), :check_gtfs, state[:poll_interval_ms])
update_start_time = Time.utc_now()
:ok = state[:updater_function].(schedule_state)

Logger.info(
"#{__MODULE__}: Sent updated schedule data to receiving process, time_in_ms=#{Time.diff(Time.utc_now(), update_start_time, :millisecond)}"
)

Logger.info(
"#{__MODULE__}: Successfully loaded schedule data, time_in_ms=#{Time.diff(Time.utc_now(), start_time, :millisecond)}"
)

if notify_health_server? && state[:health_server] do
Health.Server.loaded(state[:health_server])
end

if continue_polling? do
Process.send_after(self(), :check_gtfs, state[:poll_interval_ms])

{:noreply,
Map.merge(state, %{
latest_gtfs_timestamp: gtfs_timestamp,
latest_hastus_timestamp: hastus_timestamp
}), :hibernate}
else
{:stop, :normal, state}
end

{:noreply,
Map.merge(state, %{
latest_gtfs_timestamp: gtfs_timestamp,
latest_hastus_timestamp: hastus_timestamp
}), :hibernate}
else
{:stop, :normal, state}
end
else
:no_update ->
Process.send_after(self(), :check_gtfs, state[:poll_interval_ms])

Expand Down Expand Up @@ -130,16 +130,16 @@ defmodule Schedule.Fetcher do
if CacheFile.should_use_file?() do
Logger.info("#{__MODULE__}: Loading schedule data from cached file")

with {:ok, data} <- CacheFile.load_gtfs() do
{:ok, data, nil, nil, false}
else
_ ->
with {:ok, data, gtfs_timestamp, hastus_timestamp, continue_polling?} <-
gtfs_from_url(latest_gtfs_timestamp, latest_hastus_timestamp) do
CacheFile.save_gtfs(data)
case CacheFile.load_gtfs() do
{:ok, data} ->
{:ok, data, nil, nil, false}

{:error, _} ->
case gtfs_from_url(latest_gtfs_timestamp, latest_hastus_timestamp) do
{:ok, data, gtfs_timestamp, hastus_timestamp, continue_polling?} ->
CacheFile.save_gtfs(data)
{:ok, data, gtfs_timestamp, hastus_timestamp, continue_polling?}

{:ok, data, gtfs_timestamp, hastus_timestamp, continue_polling?}
else
:no_update ->
:no_update

Expand All @@ -159,17 +159,17 @@ defmodule Schedule.Fetcher do
defp gtfs_from_url(latest_gtfs_timestamp, latest_hastus_timestamp) do
Logger.info("#{__MODULE__}: Querying schedule data remote files")

with {:files, files, gtfs_timestamp, hastus_timestamp} <-
fetch_remote_files(latest_gtfs_timestamp, latest_hastus_timestamp) do
Logger.info("#{__MODULE__}: Updated schedule data found, parsing")
case fetch_remote_files(latest_gtfs_timestamp, latest_hastus_timestamp) do
{:files, files, gtfs_timestamp, hastus_timestamp} ->
Logger.info("#{__MODULE__}: Updated schedule data found, parsing")

try do
data = Data.parse_files(files)
{:ok, data, gtfs_timestamp, hastus_timestamp, true}
rescue
error -> {:error, error}
end

try do
data = Data.parse_files(files)
{:ok, data, gtfs_timestamp, hastus_timestamp, true}
rescue
error -> {:error, error}
end
else
:no_update ->
:no_update

Expand Down
25 changes: 13 additions & 12 deletions lib/schedule/health/checkers/trip_stop_times_checker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ defmodule Schedule.Health.Checkers.TripStopTimesChecker do

@spec healthy_route?(timepoint_config()) :: boolean
defp healthy_route?(%{route_id: route_id, min_length: min_length}) do
with {:trip, %Trip{stop_times: stop_times, id: id}} <- trip(route_id) do
length = length(stop_times)
pass? = length >= min_length

if !pass? do
Logger.warning(
"#{__MODULE__} failed on trip_id=#{id} of route_id=#{route_id}. min_length=#{min_length} length=#{length}"
)
end

pass?
else
case trip(route_id) do
{:trip, %Trip{stop_times: stop_times, id: id}} ->
length = length(stop_times)
pass? = length >= min_length

if !pass? do
Logger.warning(
"#{__MODULE__} failed on trip_id=#{id} of route_id=#{route_id}. min_length=#{min_length} length=#{length}"
)
end

pass?

_ ->
false
end
Expand Down

0 comments on commit 5e4417d

Please sign in to comment.