Skip to content

Commit

Permalink
Identifiant de la source (traçabilité)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitfred committed Dec 20, 2024
1 parent 2d53a9a commit 87bcf13
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
21 changes: 12 additions & 9 deletions apps/transport/lib/registry/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Transport.Registry.Engine do

alias Transport.Registry.GTFS
alias Transport.Registry.NeTEx
alias Transport.Registry.Model.DataSource
alias Transport.Registry.Model.Stop
alias Transport.Registry.Result

Expand Down Expand Up @@ -45,15 +46,17 @@ defmodule Transport.Registry.Engine do
end

def prepare_extractor(%DB.Resource{} = resource) do
data_source_id = "PAN:resource:#{resource.id}"

case resource.format do
"GTFS" -> {:ok, {GTFS, resource.url}}
"NeTEx" -> {:ok, {NeTEx, resource.url}}
"GTFS" -> {:ok, {GTFS, data_source_id, resource.url}}
"NeTEx" -> {:ok, {NeTEx, data_source_id, resource.url}}
_ -> {:error, "Unsupported format"}
end
end

def download({extractor, url}) do
Logger.debug("download #{extractor} #{url}")
def download({extractor, data_source_id, url}) do
Logger.debug("download #{extractor} #{data_source_id} #{url}")
tmp_path = System.tmp_dir!() |> Path.join("#{Ecto.UUID.generate()}.dat")

safe_error = fn msg ->
Expand All @@ -75,7 +78,7 @@ defmodule Transport.Registry.Engine do
{:ok, %{status: status}} ->
cond do
status >= 200 && status < 300 ->
{:ok, {extractor, tmp_path}}
{:ok, {extractor, data_source_id, tmp_path}}

status > 400 ->
safe_error.("Error #{status} while downloading the resource from #{url}")
Expand All @@ -86,10 +89,10 @@ defmodule Transport.Registry.Engine do
end
end

@spec extract_from_archive({module(), Path.t()}) :: Result.t([Stop.t()])
def extract_from_archive({extractor, file}) do
Logger.debug("extract_from_archive #{extractor} #{file}")
extractor.extract_from_archive(file)
@spec extract_from_archive({module(), DataSource.data_source_id(), Path.t()}) :: Result.t([Stop.t()])
def extract_from_archive({extractor, data_source_id, file}) do
Logger.debug("extract_from_archive #{extractor} #{data_source_id} #{file}")
extractor.extract_from_archive(data_source_id, file)
end

def dump_to_csv(enumerable, output_file) do
Expand Down
4 changes: 3 additions & 1 deletion apps/transport/lib/registry/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ defmodule Transport.Registry.Extractor do

require Logger

alias Transport.Registry.Model.DataSource
alias Transport.Registry.Model.Stop
alias Transport.Registry.Result

@callback extract_from_archive(path :: Path.t()) :: Result.t([Stop.t()])
@callback extract_from_archive(data_source_id :: DataSource.data_source_id(), path :: Path.t()) ::
Result.t([Stop.t()])
end
10 changes: 6 additions & 4 deletions apps/transport/lib/registry/gtfs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Transport.Registry.GTFS do
@doc """
Extract stops from GTFS ressource.
"""
def extract_from_archive(archive) do
def extract_from_archive(data_source_id, archive) do
case file_stream(archive) do
{:error, error} ->
Logger.error(error)
Expand All @@ -26,13 +26,13 @@ defmodule Transport.Registry.GTFS do

content
|> Utils.to_stream_of_maps()
|> Stream.flat_map(&handle_stop/1)
|> Stream.flat_map(&handle_stop(data_source_id, &1))
|> Enum.to_list()
|> Result.ok()
end
end

defp handle_stop(record) do
defp handle_stop(data_source_id, record) do
latitude = Utils.fetch_position(record, "stop_lat")
longitude = Utils.fetch_position(record, "stop_lon")

Expand All @@ -44,7 +44,9 @@ defmodule Transport.Registry.GTFS do
latitude: latitude,
longitude: longitude,
projection: :utm_wgs84,
stop_type: record |> Utils.csv_get_with_default("location_type", "0") |> to_stop_type()
stop_type: record |> Utils.csv_get_with_default("location_type", "0") |> to_stop_type(),
data_source_format: :gtfs,
data_source_id: data_source_id
}
]
else
Expand Down
5 changes: 1 addition & 4 deletions apps/transport/lib/registry/netex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ defmodule Transport.Registry.NeTEx do
@doc """
Extract stops from a NeTEx archive.
"""
def extract_from_archive(archive) do
# FIXME: propagate some context
data_source_id = nil

def extract_from_archive(data_source_id, archive) do
archive
|> Transport.NeTEx.read_all_stop_places()
|> Enum.flat_map(&process_stop_places(data_source_id, &1))
Expand Down

0 comments on commit 87bcf13

Please sign in to comment.