Skip to content

Commit

Permalink
fix(ex/credo): Fix single-pipe issues (#2296)
Browse files Browse the repository at this point in the history
Co-authored-by: Kayla Firestack <[email protected]>
  • Loading branch information
joshlarson and firestack authored Dec 5, 2023
1 parent b893ffe commit 1361f4c
Show file tree
Hide file tree
Showing 48 changed files with 271 additions and 260 deletions.
2 changes: 1 addition & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
{Credo.Check.Readability.PreferImplicitTry, []},
{Credo.Check.Readability.RedundantBlankLines, []},
{Credo.Check.Readability.Semicolons, []},
{Credo.Check.Readability.SinglePipe, []},
{Credo.Check.Readability.SpaceAfterCommas, []},
{Credo.Check.Readability.StringSigils, []},
{Credo.Check.Readability.TrailingBlankLine, []},
Expand Down Expand Up @@ -177,7 +178,6 @@
{Credo.Check.Readability.NestedFunctionCalls, []},
{Credo.Check.Readability.SeparateAliasRequire, []},
{Credo.Check.Readability.SingleFunctionToBlockPipe, []},
{Credo.Check.Readability.SinglePipe, []},
{Credo.Check.Readability.Specs, []},
{Credo.Check.Readability.StrictModuleLayout, []},
{Credo.Check.Readability.WithCustomTaggedTuple, []},
Expand Down
5 changes: 2 additions & 3 deletions lib/api/json_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ defmodule JsonApi do
end

defp load_relationships(%{} = relationships, included) do
relationships
|> Helpers.map_values(&load_single_relationship(&1, included))
Helpers.map_values(relationships, &load_single_relationship(&1, included))
end

@spec load_single_relationship(any, map()) :: list()
Expand All @@ -129,7 +128,7 @@ defmodule JsonApi do
end

defp load_single_relationship(%{"data" => %{} = data}, included) do
case data |> match_included(included) do
case match_included(data, included) do
nil -> []
item -> [parse_data_item(item, included)]
end
Expand Down
4 changes: 1 addition & 3 deletions lib/concentrate/pipeline/vehicle_positions_pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ defmodule Concentrate.Pipeline.VehiclePositionsPipeline do
nil
end

children =
[realtime_enhanced_child, swiftly_child]
|> Enum.reject(&is_nil/1)
children = Enum.reject([realtime_enhanced_child, swiftly_child], &is_nil/1)

{child_ids(children), children}
end
Expand Down
3 changes: 1 addition & 2 deletions lib/geonames.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ defmodule Geonames do
"#{__MODULE__} got_intersection_response url=#{sanitized_url} time=#{time_in_ms}"
)

body
|> Jason.decode!(strings: :copy)
Jason.decode!(body, strings: :copy)

response ->
Logger.warn(
Expand Down
4 changes: 1 addition & 3 deletions lib/mix/tasks/cache.clean.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ defmodule Mix.Tasks.Cache.Clean do
@shortdoc "Delete the gtfs cache file"
@spec run([binary]) :: any
def run(_) do
filepath =
CacheFile.cache_filename()
|> CacheFile.generate_filepath()
filepath = CacheFile.generate_filepath(CacheFile.cache_filename())

IO.puts("Deleting cache file #{filepath}")
File.rm(filepath)
Expand Down
6 changes: 3 additions & 3 deletions lib/notifications/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ defmodule Notifications.Notification do
end

def get_or_create_from_bridge_movement(bridge_movement_values) do
created_at = DateTime.utc_now() |> DateTime.to_unix()
created_at = DateTime.to_unix(DateTime.utc_now())

{:ok, {source, db_record}} =
Skate.Repo.transaction(fn ->
Expand All @@ -125,7 +125,7 @@ defmodule Notifications.Notification do
# so we assume that, if we see two bridge movements within a blackout
# period, they are actually the same movement and what's happening is
# that we have multiple servers trying to insert a movement at once.
cutoff_time = NaiveDateTime.utc_now() |> NaiveDateTime.add(-@bridge_lowering_blackout)
cutoff_time = NaiveDateTime.add(NaiveDateTime.utc_now(), -@bridge_lowering_blackout)

existing_record =
Skate.Repo.one(
Expand Down Expand Up @@ -256,7 +256,7 @@ defmodule Notifications.Notification do
detail_fields =
cond do
block_waiver ->
Map.from_struct(block_waiver) |> Map.delete(:id)
Map.delete(Map.from_struct(block_waiver), :id)

bridge_movement ->
{reason, end_time} =
Expand Down
2 changes: 1 addition & 1 deletion lib/notifications/notification_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ defmodule Notifications.NotificationServer do
created_at = Util.Time.now()
route_ids = trips |> Enum.map(& &1.route_id) |> Enum.uniq()
run_ids = trips |> Enum.map(& &1.run_id) |> Enum.uniq()
trip_ids = trips |> Enum.map(& &1.id)
trip_ids = Enum.map(trips, & &1.id)

peek_at_vehicles_by_run_ids_fn =
Application.get_env(
Expand Down
3 changes: 1 addition & 2 deletions lib/realtime/vehicles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ defmodule Realtime.Vehicles do
interlining_vehicles_and_ghosts,
fn vehicle_or_ghost ->
incoming_trip =
incoming_trips
|> Enum.find(fn trip ->
Enum.find(incoming_trips, fn trip ->
trip.block_id == vehicle_or_ghost.block_id && trip.route_id == route_id
end)

Expand Down
5 changes: 3 additions & 2 deletions lib/report/notifications_count_estimate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ defmodule Report.NotificationsCountEstimate do
@impl Report
def run() do
[count] =
from(p in "pg_class", where: p.relname == "notifications", select: p.reltuples)
|> Skate.Repo.all()
Skate.Repo.all(
from(p in "pg_class", where: p.relname == "notifications", select: p.reltuples)
)

{:ok, [%{count: count}]}
end
Expand Down
5 changes: 3 additions & 2 deletions lib/report/notifications_users_count_estimate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ defmodule Report.NotificationsUsersCountEstimate do
@impl Report
def run() do
[count] =
from(p in "pg_class", where: p.relname == "notifications_users", select: p.reltuples)
|> Skate.Repo.all()
Skate.Repo.all(
from(p in "pg_class", where: p.relname == "notifications_users", select: p.reltuples)
)

{:ok, [%{count: count}]}
end
Expand Down
17 changes: 9 additions & 8 deletions lib/report/user_names_and_uuids.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ defmodule Report.UserNamesAndUuids do
@impl Report
def run() do
{:ok,
from(u in DbUser,
select: %{
"username" => u.username,
"uuid" => u.uuid,
"email" => u.email
}
)
|> Skate.Repo.all()}
Skate.Repo.all(
from(u in DbUser,
select: %{
"username" => u.username,
"uuid" => u.uuid,
"email" => u.email
}
)
)}
end

@impl Report
Expand Down
17 changes: 9 additions & 8 deletions lib/report/user_settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ defmodule Report.UserSettings do
@impl Report
def run() do
{:ok,
from(s in DbUserSettings,
select: %{
"ladder_page_vehicle_label" => s.ladder_page_vehicle_label,
"shuttle_page_vehicle_label" => s.shuttle_page_vehicle_label,
"vehicle_adherence_colors" => s.vehicle_adherence_colors
}
)
|> Skate.Repo.all()}
Skate.Repo.all(
from(s in DbUserSettings,
select: %{
"ladder_page_vehicle_label" => s.ladder_page_vehicle_label,
"shuttle_page_vehicle_label" => s.shuttle_page_vehicle_label,
"vehicle_adherence_colors" => s.vehicle_adherence_colors
}
)
)}
end

@impl Report
Expand Down
4 changes: 1 addition & 3 deletions lib/schedule/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ defmodule Schedule.Data do
|> (fn time -> time - 6 * 60 * 60 end).()
|> Util.Time.date_of_timestamp()

last_possible_service_date =
end_time
|> Util.Time.date_of_timestamp()
last_possible_service_date = Util.Time.date_of_timestamp(end_time)

date_range = Date.range(first_possible_service_date, last_possible_service_date)
Enum.to_list(date_range)
Expand Down
7 changes: 3 additions & 4 deletions lib/schedule/gtfs/stop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ defmodule Schedule.Gtfs.Stop do
"""
@spec parse(binary() | nil) :: [t()]
def parse(file_binary) do
file_binary
|> Csv.parse(
Csv.parse(
file_binary,
parse: &from_csv_row/1,
filter: fn row -> Map.has_key?(@location_type_map, row["location_type"]) end
)
Expand Down Expand Up @@ -104,8 +104,7 @@ defmodule Schedule.Gtfs.Stop do
routes_by_id = routes |> Enum.reject(&Route.shuttle_route?(&1)) |> Map.new(&{&1.id, &1})

routes_by_parent_or_stop_id =
route_patterns
|> Enum.reduce(%{}, fn route_pattern, acc_stop_id_to_routes ->
Enum.reduce(route_patterns, %{}, fn route_pattern, acc_stop_id_to_routes ->
route_pattern
|> parent_or_stop_id_to_routes_for_pattern(
routes_by_id,
Expand Down
3 changes: 1 addition & 2 deletions lib/schedule/hastus/trip.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ defmodule Schedule.Hastus.Trip do
|> Enum.filter(&Regex.match?(@through_routed_suffix_regex, &1))
|> Enum.group_by(&String.replace(&1, @through_routed_suffix_regex, ""))

trips
|> Enum.flat_map(fn trip ->
Enum.flat_map(trips, fn trip ->
through_routed_trip_ids = Map.get(original_id_to_through_routed_trip_ids, trip.trip_id)

if through_routed_trip_ids do
Expand Down
2 changes: 1 addition & 1 deletion lib/schedule/health/checkers/routes_checker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Schedule.Health.Checkers.RoutesChecker do
def healthy?(%{min_length: min_length}) do
routes_fn = Application.get_env(:skate_web, :routes_fn, &Schedule.all_routes/0)

length = routes_fn.() |> length()
length = length(routes_fn.())
pass? = length >= min_length

if !pass? do
Expand Down
2 changes: 1 addition & 1 deletion lib/schedule/health/checkers/timepoints_checker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Schedule.Health.Checkers.TimepointsChecker do
timepoints_on_route_fn =
Application.get_env(:skate_web, :timepoints_on_route_fn, &Schedule.timepoints_on_route/1)

length = timepoints_on_route_fn.(route_id) |> length()
length = route_id |> timepoints_on_route_fn.() |> length()
pass? = length >= min_length

if !pass? do
Expand Down
5 changes: 3 additions & 2 deletions lib/schedule/swing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ defmodule Schedule.Swing do
|> Map.values()
|> Enum.map(&{&1.pieces, &1.id})
|> Enum.flat_map(fn {pieces_for_block, block_id} ->
Enum.chunk_every(pieces_for_block, 2, 1, :discard)
pieces_for_block
|> Enum.chunk_every(2, 1, :discard)
|> Enum.map(fn [piece1, piece2] -> {piece1, piece2, block_id} end)
end)
|> Enum.map(fn {piece1, piece2, block_id} ->
Expand Down Expand Up @@ -124,7 +125,7 @@ defmodule Schedule.Swing do
defp first_trip_from_piece(piece, trips_by_id) do
case piece.start_mid_route? do
%{trip: mid_route_trip} ->
with %{} = first_trip <- mid_route_trip |> trip_or_trip_id_to_trip(trips_by_id) do
with %{} = first_trip <- trip_or_trip_id_to_trip(mid_route_trip, trips_by_id) do
%{first_trip | run_id: piece.run_id}
end

Expand Down
21 changes: 11 additions & 10 deletions lib/skate/oban/clean_up_notifications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ defmodule Skate.Oban.CleanUpNotifications do
cutoff_days = Map.get(args, "cutoff_days", 100)
limit = Map.get(args, "limit", 100)

oldest_date = DateTime.utc_now() |> DateTime.add(-cutoff_days * @seconds_per_day)
oldest_date = DateTime.add(DateTime.utc_now(), -cutoff_days * @seconds_per_day)

Logger.notice("#{__MODULE__} starting cleanup")

{time, {count, nil}} =
:timer.tc(fn ->
from(notification_indexed in Notifications.Db.Notification,
where:
notification_indexed.id in subquery(
from(notification_limited in Notifications.Db.Notification,
where: notification_limited.inserted_at < ^oldest_date,
limit: ^limit,
select: notification_limited.id
Skate.Repo.delete_all(
from(notification_indexed in Notifications.Db.Notification,
where:
notification_indexed.id in subquery(
from(notification_limited in Notifications.Db.Notification,
where: notification_limited.inserted_at < ^oldest_date,
limit: ^limit,
select: notification_limited.id
)
)
)
)
)
|> Skate.Repo.delete_all()
end)

Logger.notice(
Expand Down
4 changes: 2 additions & 2 deletions lib/skate/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Skate.Repo do

def add_prod_credentials(config, auth_token_fn \\ &ExAws.RDS.generate_db_auth_token/4) do
hostname = System.get_env("POSTGRES_HOSTNAME")
port = System.get_env("POSTGRES_PORT", "5432") |> String.to_integer()
port = String.to_integer(System.get_env("POSTGRES_PORT", "5432"))
username = System.get_env("POSTGRES_USERNAME")

token =
Expand All @@ -21,7 +21,7 @@ defmodule Skate.Repo do
if is_nil(token) do
Logger.info("#{__MODULE__} add_prod_credentials token_is_nil")
else
hash_string = :crypto.hash(:sha3_256, token) |> Base.encode16()
hash_string = Base.encode16(:crypto.hash(:sha3_256, token))

Logger.info("#{__MODULE__} add_prod_credentials token_hash=#{hash_string}")
end
Expand Down
8 changes: 5 additions & 3 deletions lib/skate_web/channels/vehicle_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule SkateWeb.VehicleChannel do
def join_authenticated("vehicle:run_ids:" <> run_ids, _message, socket) do
run_ids = String.split(run_ids, ",")

vehicle_or_ghost = Realtime.Server.peek_at_vehicles_by_run_ids(run_ids) |> List.first()
vehicle_or_ghost = run_ids |> Realtime.Server.peek_at_vehicles_by_run_ids() |> List.first()

socket =
if vehicle_or_ghost do
Expand All @@ -42,9 +42,11 @@ defmodule SkateWeb.VehicleChannel do

vehicle_or_ghost =
if user_in_test_group? do
Realtime.Server.peek_at_vehicle_by_id_with_logged_out(vehicle_or_ghost_id) |> List.first()
vehicle_or_ghost_id
|> Realtime.Server.peek_at_vehicle_by_id_with_logged_out()
|> List.first()
else
Realtime.Server.peek_at_vehicle_by_id(vehicle_or_ghost_id) |> List.first()
vehicle_or_ghost_id |> Realtime.Server.peek_at_vehicle_by_id() |> List.first()
end

{lookup_key, _vehicle_or_ghost} =
Expand Down
6 changes: 4 additions & 2 deletions lib/skate_web/controllers/admin_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ defmodule SkateWeb.AdminController do

@spec index(Plug.Conn.t(), map()) :: Plug.Conn.t()
def index(conn, _params) do
conn
|> render("index.html", layout: {SkateWeb.LayoutView, "barebones.html"}, title: "Skate Admin")
render(conn, "index.html",
layout: {SkateWeb.LayoutView, "barebones.html"},
title: "Skate Admin"
)
end
end
9 changes: 7 additions & 2 deletions lib/skate_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ defmodule SkateWeb.PageController do

def index(conn, _params) do
%{id: user_id} = AuthManager.Plug.current_resource(conn)
%{username: username} = user = User.get_by_id!(user_id) |> Skate.Repo.preload(:test_groups)

%{username: username} =
user =
user_id
|> User.get_by_id!()
|> Skate.Repo.preload(:test_groups)

_ = Logger.info("uid=#{username}")

Expand All @@ -33,7 +38,7 @@ defmodule SkateWeb.PageController do
base: Application.get_env(:skate, :base_tileset_url),
satellite: Application.get_env(:skate, :satellite_tileset_url)
})
|> assign(:user_test_groups, user.test_groups |> Enum.map(& &1.name))
|> assign(:user_test_groups, Enum.map(user.test_groups, & &1.name))
|> assign(:map_limits, map_limits)
|> assign(:sentry_org_slug, Application.get_env(:skate, :sentry_org_slug))
|> render("index.html")
Expand Down
11 changes: 7 additions & 4 deletions lib/skate_web/controllers/report_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ defmodule SkateWeb.ReportController do
@spec index(Plug.Conn.t(), map()) :: Plug.Conn.t()
def index(conn, _params) do
reports =
Report.all_reports()
|> Enum.map(fn {short_name, report} -> {report.description(), short_name} end)
Enum.map(Report.all_reports(), fn {short_name, report} ->
{report.description(), short_name}
end)

conn
|> assign(:reports, reports)
Expand All @@ -30,8 +31,10 @@ defmodule SkateWeb.ReportController do
{:ok, results} = Report.to_csv(report)

{:ok, dt} =
Util.Time.now()
|> FastLocalDatetime.unix_to_datetime(Application.get_env(:skate, :timezone))
FastLocalDatetime.unix_to_datetime(
Util.Time.now(),
Application.get_env(:skate, :timezone)
)

timestamp = DateTime.to_iso8601(dt, :basic)

Expand Down
Loading

0 comments on commit 1361f4c

Please sign in to comment.