~ihabunek/triglav

a7ad66eb4976948efca7b48c8974ac64f25da6aa — Ivan Habunek 8 months ago e5d55f7
Make errors persistent
M lib/mix/tasks/triglav/osmosis_update.ex => lib/mix/tasks/triglav/osmosis_update.ex +1 -5
@@ 1,16 1,12 @@
defmodule Mix.Tasks.Triglav.OsmosisUpdate do
  use Mix.Task

  alias Triglav.Import.Osmosis
  alias Mix.Tasks.Triglav.ValidateRoutes

  @shortdoc "Updates the osmosis data to latest available."

  @impl Mix.Task
  def run(_args) do
    Application.put_env(:triglav, :repo_only, true)
    {:ok, _} = Application.ensure_all_started(:triglav)
    :ok = Osmosis.update()
    {:ok, _} = ValidateRoutes.validate()
    :ok = Triglav.Import.Osmosis.update()
  end
end

M lib/mix/tasks/triglav/validate_routes.ex => lib/mix/tasks/triglav/validate_routes.ex +96 -15
@@ 1,9 1,16 @@
defmodule Mix.Tasks.Triglav.ValidateRoutes do
  alias Triglav.Zet.Validator
  alias Triglav.Repo
  alias Ecto.Multi
  use Mix.Task

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

  import Ecto.Query
  require Logger

  @shortdoc "Validates routes and saves errors to the database, replaces any existing errors"



@@ 11,21 18,95 @@ defmodule Mix.Tasks.Triglav.ValidateRoutes do
  def run(_args) do
    Application.put_env(:triglav, :repo_only, true)
    {:ok, _} = Application.ensure_all_started(:triglav)
    validate()
    validate_routes()
  end

  @spec validate_routes() :: :ok | :error
  def validate_routes() do
    osm_state = Geofabrik.get_local_state()
    zet_feed_info = Zet.get_local_feed_info()

    case {osm_state, zet_feed_info} do
      {nil, _} ->
        Logger.warn("OSM data not loaded. Skipping validation.")

      {_, nil} ->
        Logger.warn("ZET data not loaded. Skipping validation.")

      _ ->
        do_validate(osm_state, zet_feed_info)
    end
  end

  #
  # TODO: Make validation fail gracefully if osm or zet data is not loaded.
  #
  defp 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)

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

    old_sigs = old_errors |> Map.keys() |> MapSet.new()
    new_sigs = new_errors |> 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))

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

    now = DateTime.utc_now() |> DateTime.truncate(:second)
    multi = Multi.new()

    multi =
      to_resolve
      |> Enum.with_index()
      |> Enum.reduce(multi, fn {error, index}, multi ->
        changeset = Error.changeset(error, %{resolved_at: now})
        Multi.update(multi, {:resolve, index}, changeset)
      end)

    multi =
      to_create
      |> Enum.with_index()
      |> Enum.reduce(multi, fn {error, index}, multi ->
        Multi.insert(multi, {:create, index}, error)
      end)

  def validate() do
    multi = Multi.new() |> Multi.delete_all(:delete, Error)
    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()
        else
          {:ok, nil}
        end
      end)

    Validator.validate_all_routes()
    |> Enum.with_index()
    |> Enum.reduce(multi, fn {error, index}, multi ->
      Multi.insert(multi, "error_#{index}", error)
    end)
    |> Repo.transaction()
    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
  end
end

M lib/triglav/import/geofabrik.ex => lib/triglav/import/geofabrik.ex +6 -1
@@ 45,7 45,7 @@ defmodule Triglav.Import.Geofabrik do
    end
  end

  @spec local_state() :: OsmState.t() | nil
  @spec local_state() :: {:ok, OsmState.t()} | {:error, :not_found}
  def local_state() do
    case Repo.get(OsmState, 1) do
      nil -> {:error, :not_found}


@@ 53,6 53,11 @@ defmodule Triglav.Import.Geofabrik do
    end
  end

  @spec get_local_state() :: OsmState.t() | nil
  def get_local_state() do
    Repo.get(OsmState, 1)
  end

  @spec save_state(map()) :: {:ok, OsmState.t()} | {:error, Changeset.t()}
  def save_state(attrs) do
    %OsmState{id: 1}

M lib/triglav/import/osm.ex => lib/triglav/import/osm.ex +1 -1
@@ 76,7 76,7 @@ defmodule Triglav.Import.Osm do
      File.rm("croatia-latest.osm.pbf")
      File.rm("croatia-latest.osm.pbf.md5")

      ValidateRoutes.validate()
      ValidateRoutes.validate_routes()

      IO.puts("Done.")
    else

M lib/triglav/import/zet.ex => lib/triglav/import/zet.ex +2 -2
@@ 41,7 41,7 @@ defmodule Triglav.Import.Zet do

      File.rm_rf!(temp_dir)

      ValidateRoutes.validate()
      ValidateRoutes.validate_routes()
    else
      IO.puts("You already have the latest data. Use :force option to import anyway.")
    end


@@ 83,7 83,7 @@ defmodule Triglav.Import.Zet do
    end)
  end

  defp get_local_feed_info() do
  def get_local_feed_info() do
    if zet_schema_exists() do
      Repo.one(FeedInfo)
    end

M lib/triglav/schemas/error.ex => lib/triglav/schemas/error.ex +43 -5
@@ 5,19 5,57 @@ defmodule Triglav.Schemas.Error do
  alias Triglav.Schemas.Zet.Route
  alias Triglav.Schemas.Osmosis.Relation

  @derive {Inspect, only: [:id, :key]}
  @type t :: %__MODULE__{}

  schema "errors" do
    field :key, :string
    belongs_to :route, Route, type: :string
    belongs_to :relation, Relation
    field :key, :string
    field :params, :map, default: %{}
    field :created_at, :utc_datetime
    field :resolved_at, :utc_datetime
  end

  def changeset(error, params) do
  @spec changeset(t(), map()) :: Ecto.Changeset.t()
  def changeset(%__MODULE__{} = error, params) do
    error
    |> cast(params, [:key, :params, :route_id, :relation_id])
    |> validate_required([:key, :params, :route_id])
    |> cast(params, [:route_id, :relation_id, :key, :params, :created_at, :resolved_at])
    |> validate_required([:route_id, :key, :params, :created_at])
  end

  @doc """
  For a given error returns a map of fields which describe the error, without
  the metadata (primary key, timestamps). This map can be used to compare two
  errors to see if they are the same.

  Map keys in `params` are strings when fetched from the db, so they are
  converted to atoms so they can be compared with errors created by factory
  methods below which use atom keys.
  """
  @spec signature(Error.t()) :: map()
  def signature(%__MODULE__{} = error) do
    error
    |> Map.take([:route_id, :relation_id, :key, :params])
    |> Map.update!(:params, &keys_to_atoms/1)
  end

  # Map keys are strings when fetched from the db, convert them to atoms
  defp keys_to_atoms(%{} = map) do
    for {k, v} <- map, into: %{} do
      k =
        cond do
          is_binary(k) -> String.to_existing_atom(k)
          is_atom(k) -> k
        end

      v =
        cond do
          is_map(v) -> keys_to_atoms(v)
          true -> v
        end

      {k, v}
    end
  end

  @spec missing_route_master(Route.t()) :: Error.t()

A lib/triglav/schemas/error_history.ex => lib/triglav/schemas/error_history.ex +15 -0
@@ 0,0 1,15 @@
defmodule Triglav.Schemas.ErrorHistory do
  use Ecto.Schema

  @type t :: %__MODULE__{}

  schema "error_history" do
    field :count, :integer, null: false
    field :previous_count, :integer, null: false
    field :created_count, :integer, null: false
    field :resolved_count, :integer, null: false
    field :zet_feed_version, :string, null: false
    field :osm_sequence_number, :integer, null: false
    field :timestamp, :utc_datetime, null: false
  end
end

M lib/triglav/zet/errors.ex => lib/triglav/zet/errors.ex +3 -0
@@ 1,5 1,8 @@
defmodule Triglav.Zet.Errors do
  alias Triglav.Schemas.Error
  alias Triglav.Repo

  import Ecto.Query

  def render(%Error{key: "missing_route_master"}) do
    "Missing route_master relation"

A priv/repo/migrations/20210130085020_alter_errors_add_timestamps.exs => priv/repo/migrations/20210130085020_alter_errors_add_timestamps.exs +10 -0
@@ 0,0 1,10 @@
defmodule Triglav.Repo.Migrations.ModifyErrorsAddTimestamps do
  use Ecto.Migration

  def change do
    alter table("errors") do
      add :created_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :resolved_at, :utc_datetime
    end
  end
end

A priv/repo/migrations/20210130155502_create_errors_history.exs => priv/repo/migrations/20210130155502_create_errors_history.exs +15 -0
@@ 0,0 1,15 @@
defmodule Triglav.Repo.Migrations.CreateErrorHistory do
  use Ecto.Migration

  def change do
    create table("error_history") do
      add :count, :integer, null: false
      add :previous_count, :integer, null: false
      add :created_count, :integer, null: false
      add :resolved_count, :integer, null: false
      add :zet_feed_version, :string, null: false
      add :osm_sequence_number, :integer, null: false
      add :timestamp, :utc_datetime, null: false, default: fragment("now()")
    end
  end
end