Skip to content

Commit

Permalink
Extract and move up the Repo call in stats handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vdegove committed Jan 30, 2025
1 parent b0762ac commit 905d51b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
13 changes: 6 additions & 7 deletions apps/transport/lib/transport/stats_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -343,22 +343,22 @@ defmodule Transport.StatsHandler do
end

query
|> Repo.all()
|> features()
|> geojson()
|> Jason.encode!()
end

def rendered_geojson(:bike_scooter_sharing) do
bike_scooter_features_query()
|> Repo.all()
|> bike_scooter_sharing_features()
|> geojson()
|> Jason.encode!()
end

@spec features(Ecto.Query.t()) :: [map()]
def features(q) do
q
|> Repo.all()
def features(result) do
result
|> Enum.reject(fn aom -> is_nil(aom.geometry) or new_aom_without_datasets?(aom) end)
|> Enum.map(fn aom ->
dataset_types =
Expand Down Expand Up @@ -413,9 +413,8 @@ defmodule Transport.StatsHandler do
end

@spec bike_scooter_sharing_features(Ecto.Query.t()) :: [map()]
def bike_scooter_sharing_features(query) do
query
|> DB.Repo.all()
def bike_scooter_sharing_features(result) do
result
|> Enum.reject(fn r -> is_nil(r.geometry) end)
|> Enum.map(fn r ->
%{
Expand Down
12 changes: 7 additions & 5 deletions apps/transport/test/transport/stats_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ defmodule Transport.StatsHandlerTest do
}
]

assert Transport.StatsHandler.bike_scooter_sharing_features(Transport.StatsHandler.bike_scooter_features_query()) ==
assert Transport.StatsHandler.bike_scooter_features_query()
|> DB.Repo.all()
|> Transport.StatsHandler.bike_scooter_sharing_features() ==
expected
end

Expand Down Expand Up @@ -354,15 +356,15 @@ defmodule Transport.StatsHandlerTest do
assert DB.AOM.created_after_2021?(aom)

assert [] ==
Transport.StatsHandler.quality_features_query() |> Transport.StatsHandler.features()
Transport.StatsHandler.quality_features_query() |> DB.Repo.all() |> Transport.StatsHandler.features()

# If created before 2022, it is present even without a dataset
aom = aom |> Ecto.Changeset.change(%{composition_res_id: 500}) |> DB.Repo.update!()

refute DB.AOM.created_after_2021?(aom)

assert [%{"properties" => %{"dataset_count" => 0, "nom" => ^aom_nom}}] =
Transport.StatsHandler.quality_features_query() |> Transport.StatsHandler.features()
Transport.StatsHandler.quality_features_query() |> DB.Repo.all() |> Transport.StatsHandler.features()

# Created in 2022 but with a dataset
aom = aom |> Ecto.Changeset.change(%{composition_res_id: 1_200}) |> DB.Repo.update!()
Expand All @@ -371,7 +373,7 @@ defmodule Transport.StatsHandlerTest do
assert DB.AOM.created_after_2021?(aom)

assert [%{"properties" => %{"dataset_types" => %{pt: 1}, "nom" => ^aom_nom}}] =
Transport.StatsHandler.quality_features_query() |> Transport.StatsHandler.features()
Transport.StatsHandler.quality_features_query() |> DB.Repo.all() |> Transport.StatsHandler.features()
end
end

Expand Down Expand Up @@ -399,6 +401,6 @@ defmodule Transport.StatsHandlerTest do
"quality" => %{"error_level" => "Error"}
}
}
] = Transport.StatsHandler.quality_features_query() |> Transport.StatsHandler.features()
] = Transport.StatsHandler.quality_features_query() |> DB.Repo.all() |> Transport.StatsHandler.features()
end
end

0 comments on commit 905d51b

Please sign in to comment.