~ihabunek/triglav

149ed279b8c34a9c17065c41e6eea29d54129b5b — Ivan Habunek 1 year, 7 months ago dc4683c errors
WIP
M lib/mix/tasks/triglav/validate_routes.ex => lib/mix/tasks/triglav/validate_routes.ex +29 -6
@@ 4,6 4,7 @@ defmodule Mix.Tasks.Triglav.ValidateRoutes do
  alias Ecto.Multi
  alias Triglav.Repo
  alias Triglav.Schemas.Error
  alias Triglav.Schemas.ErrorAudit
  alias Triglav.Zet.Validator

  import Ecto.Query


@@ 46,26 47,48 @@ defmodule Mix.Tasks.Triglav.ValidateRoutes do
      sigs_to_resolve
      |> Enum.with_index()
      |> Enum.reduce(multi, fn {sig, index}, multi ->
        changeset =
          old_errors
          |> Map.get(sig)
          |> Error.changeset(%{resolved_at: now})
        error = Map.get(old_errors, sig)
        error_changeset = Error.changeset(error, %{resolved_at: now})

        error_audit = %ErrorAudit{
          error_id: error.id,
          timestamp: now,
          action: "resolve"
        }

        Multi.update(multi, {:resolve, index}, changeset)
        multi
        |> Multi.update({:resolve, index}, error_changeset)
        |> Multi.insert({:resolve_audit, index}, error_audit)
      end)

    multi =
      sigs_to_create
      |> Enum.with_index()
      |> Enum.reduce(multi, fn {sig, index}, multi ->
        error_id = Ecto.UUID.generate()

        error =
          new_errors
          |> Map.get(sig)
          |> Map.put(:id, error_id)
          |> Map.put(:created_at, now)

        Multi.insert(multi, {:create, index}, error)
        error_audit = %ErrorAudit{
          error_id: error_id,
          timestamp: now,
          action: "create"
        }

        multi
        |> Multi.insert({:create, index}, error)
        |> Multi.insert({:create_audit, index}, error_audit)
      end)

    Repo.transaction(multi)

    %{
      created: MapSet.size(sigs_to_create),
      resolved: MapSet.size(sigs_to_resolve)
    }
  end
end

A lib/triglav/schemas/error_audit.ex => lib/triglav/schemas/error_audit.ex +13 -0
@@ 0,0 1,13 @@
defmodule Triglav.Schemas.ErrorAudit do
  use Ecto.Schema

  alias Triglav.Schemas.Error

  @type t :: %__MODULE__{}

  schema "error_audit" do
    field :timestamp, :utc_datetime
    field :action, :string
    belongs_to :error, Error, type: :binary_id
  end
end

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

  import Ecto.Query

  def list_audit() do
    resolved =
      from(e in Error,
        where: not is_nil(e.resolved_at),
        select: %{id: e.id, timestamp: e.resolved_at, action: "resolve"}
      )

    from(e in Error, select: %{id: e.id, timestamp: e.created_at, action: "create"})
    # |> union(^resolved)
    |> order_by(desc: :timestamp)
    |> limit(20)
    |> Repo.all()
  end

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

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

  def change do
    create table("error_audit") do
      add :error_id, references("errors", type: :binary_id), null: false
      add :timestamp, :utc_datetime, null: false
      add :action, :string, null: false
    end

    create index("error_audit", [:timestamp], name: "idx_error_audit_timestamp")
  end
end