~ihabunek/triglav

7bdb88d4d88886e08c456f7ee012d7a343a73731 — Ivan Habunek 3 months ago 5725e40
Split validation from persisting errors
2 files changed, 87 insertions(+), 46 deletions(-)

M lib/mix/tasks/triglav/validate_routes.ex
M lib/triglav/import/osmosis.ex
M lib/mix/tasks/triglav/validate_routes.ex => lib/mix/tasks/triglav/validate_routes.ex +87 -45
@@ 2,12 2,14 @@ defmodule Mix.Tasks.Triglav.ValidateRoutes do
  use Mix.Task

  alias Ecto.Multi
  alias Triglav.Import.Geofabrik
  alias Triglav.Import.Zet
  alias Triglav.Repo
  alias Triglav.Schemas.Error
  alias Triglav.Schemas.ErrorHistory
  alias Triglav.Schemas.OsmState
  alias Triglav.Zet.Validator
  alias Triglav.Import.Geofabrik
  alias Triglav.Import.Zet
  alias Triglav.Schemas.Zet.FeedInfo

  import Ecto.Query
  require Logger


@@ 15,21 17,40 @@ defmodule Mix.Tasks.Triglav.ValidateRoutes do
  @shortdoc "Validates routes and saves errors to the database, replaces any existing errors"

  @impl Mix.Task
  def run(_args) do
  def run(args) do
    Application.put_env(:triglav, :repo_only, true)
    {:ok, _} = Application.ensure_all_started(:triglav)
    {opts, _} = OptionParser.parse!(args, strict: [dry_run: :boolean])

    Logger.info("Starting validation...")
    {time, _} = :timer.tc(fn -> validate_routes(opts) end)
    time_seconds = :erlang.float_to_binary(time / 1_000_000, decimals: 2)
    Logger.info("Done. Took #{time_seconds} seconds.")
  end

    {time, _} = :timer.tc(&validate_routes/0)
  # TODO: extract code from mix task into own module

    time_seconds = :erlang.float_to_binary(time / 1_000_000, decimals: 2)
  defmodule ValidationResults do
    @type t :: %__MODULE__{
            osm_state: OsmState.t(),
            zet_feed_info: FeedInfo.t(),
            to_create: [Error.t()],
            to_resolve: [Error.t()],
            history: ErrorHistory.t()
          }

    Logger.info("Done. Took #{time_seconds} seconds.")
    defstruct [
      :osm_state,
      :zet_feed_info,
      :to_create,
      :to_resolve,
      :history
    ]
  end

  @spec validate_routes() :: :ok | :error
  def validate_routes() do
  @spec validate_routes(Keyword.t()) :: :ok | :error
  def validate_routes(opts) do
    dry_run = Keyword.get(opts, :dry_run, false)

    osm_state = Geofabrik.get_local_state()
    zet_feed_info = Zet.get_local_feed_info()



@@ 41,33 62,73 @@ defmodule Mix.Tasks.Triglav.ValidateRoutes do
        Logger.warn("ZET data not loaded. Skipping validation.")

      _ ->
        do_validate(osm_state, zet_feed_info)
        %{history: history} = results = do_validate(osm_state, zet_feed_info)

        Logger.info("Routes validated")
        Logger.info("Found #{history.created_count} new error(s)")
        Logger.info("Found #{history.resolved_count} resolved error(s)")

        if dry_run do
          Logger.info("Dry run. Not persisting errors.")
        else
          Logger.info("Persisting errors.")

          case persist_errors(results) do
            {:ok, _} ->
              Logger.info("Successfully persisted errors.")

            {:error, failed_operation, failed_value, _changes} ->
              Logger.error("Failed persisting errors")
              Logger.error("Failed operation:\n#{inspect(failed_operation, pretty: true)}")
              Logger.error("Failed value:\n#{inspect(failed_value, pretty: true)}")
          end
        end
    end
  end

  defp do_validate(osm_state, zet_feed_info) do
  def do_validate(osm_state, zet_feed_info) do
    # Existing unresolved errors
    old_errors =
      from(e in Error, where: is_nil(e.resolved_at))
      |> Repo.all()
      |> Map.new(fn e -> {Error.signature(e), e} end)
    old_errors = from(e in Error, where: is_nil(e.resolved_at)) |> Repo.all()
    old_errors_map = Map.new(old_errors, fn e -> {Error.signature(e), e} end)

    # Current set of errors from the validator
    new_errors =
      Validator.validate_all_routes()
      |> Map.new(fn e -> {Error.signature(e), e} end)
    new_errors = Validator.validate_all_routes()
    new_errors_map = Map.new(new_errors, fn e -> {Error.signature(e), e} end)

    old_sigs = old_errors |> Map.keys() |> MapSet.new()
    new_sigs = new_errors |> Map.keys() |> MapSet.new()
    old_sigs = old_errors_map |> Map.keys() |> MapSet.new()
    new_sigs = new_errors_map |> Map.keys() |> MapSet.new()

    to_create = MapSet.difference(new_sigs, old_sigs) |> Enum.map(&Map.get(new_errors, &1))
    to_resolve = MapSet.difference(old_sigs, new_sigs) |> Enum.map(&Map.get(old_errors, &1))
    to_create = MapSet.difference(new_sigs, old_sigs) |> Enum.map(&Map.get(new_errors_map, &1))
    to_resolve = MapSet.difference(old_sigs, new_sigs) |> Enum.map(&Map.get(old_errors_map, &1))

    old_count = map_size(old_errors)
    old_count = length(old_errors)
    created_count = length(to_create)
    resolved_count = length(to_resolve)
    new_count = old_count + created_count - resolved_count

    history = %ErrorHistory{
      count: new_count,
      previous_count: old_count,
      created_count: created_count,
      resolved_count: resolved_count,
      zet_feed_version: zet_feed_info.version,
      osm_sequence_number: osm_state.sequence_number
    }

    %ValidationResults{
      osm_state: osm_state,
      zet_feed_info: zet_feed_info,
      to_create: to_create,
      to_resolve: to_resolve,
      history: history
    }
  end

  def persist_errors(%ValidationResults{
        history: history,
        to_resolve: to_resolve,
        to_create: to_create
      }) do
    now = DateTime.utc_now() |> DateTime.truncate(:second)
    multi = Multi.new()



@@ 88,32 149,13 @@ defmodule Mix.Tasks.Triglav.ValidateRoutes do

    multi =
      Multi.run(multi, :history, fn _repo, _changes ->
        if created_count > 0 or resolved_count > 0 do
          %ErrorHistory{
            count: new_count,
            previous_count: old_count,
            created_count: created_count,
            resolved_count: resolved_count,
            zet_feed_version: zet_feed_info.version,
            osm_sequence_number: osm_state.sequence_number
          }
          |> Repo.insert()
        if history.created_count > 0 or history.resolved_count > 0 do
          Repo.insert(history)
        else
          {:ok, nil}
        end
      end)

    with {:ok, _} <- Repo.transaction(multi) do
      Logger.info("Routes validated")
      Logger.info("Created #{created_count} error(s)")
      Logger.info("Resolved #{resolved_count} error(s)")
      :ok
    else
      {:error, failed_operation, failed_value, _changes} ->
        Logger.error("Validation failed")
        Logger.error("Failed operation:\n#{inspect(failed_operation, pretty: true)}")
        Logger.error("Failed value:\n#{inspect(failed_value, pretty: true)}")
        :error
    end
    Repo.transaction(multi)
  end
end

M lib/triglav/import/osmosis.ex => lib/triglav/import/osmosis.ex +0 -1
@@ 6,7 6,6 @@ defmodule Triglav.Import.Osmosis do
  https://download.geofabrik.de/europe/croatia.html
  """

  alias Triglav.Derived.PublicTransport
  alias Triglav.Import.Geofabrik
  alias Triglav.Repo