~ihabunek/triglav

3a1876166aa129c76c1b09fc562af764d04df4e1 — Ivan Habunek 3 months ago 1c7461f
Fix detecting unexpected master route members

It was presumed that members would be relations, things broke when a way
was encountered. Also added error missing error message.
M lib/triglav/schemas/error.ex => lib/triglav/schemas/error.ex +9 -4
@@ 80,12 80,17 @@ defmodule Triglav.Schemas.Error do
      relation_id: relation.id
    }

  @spec unexpected_route_master_member_relation(Route.t(), Relation.t()) :: Error.t()
  def unexpected_route_master_member_relation(%Route{} = route, relation_id),
  @spec unexpected_route_master_member(Route.t(), Relation.t(), String.t(), integer()) ::
          Error.t()
  def unexpected_route_master_member(%Route{} = route, relation_id, member_type, member_id),
    do: %__MODULE__{
      key: "unexpected_route_master_member_relation",
      key: "unexpected_route_master_member",
      route_id: route.id,
      relation_id: relation_id
      relation_id: relation_id,
      params: %{
        member_type: member_type,
        member_id: member_id
      }
    }

  @spec relation_not_updated_to_ptv2(Route.t(), Relation.t()) :: Error.t()

M lib/triglav/zet/errors.ex => lib/triglav/zet/errors.ex +14 -2
@@ 91,7 91,19 @@ defmodule Triglav.Zet.Errors do
    "Platform #{sequence_id} is #{distance_from_route}m from route"
  end

  def render(%Error{key: key}) do
    "FIXME: missing render function for error '#{key}'"
  def render(%Error{
        key: "unexpected_route_master_member",
        params: %{"member_type" => member_type, "member_id" => member_id}
      }) do
    "Route master relation contains unexpected member #{member_type} #{member_id}"
  end

  def render(%Error{key: "relation_not_contained_in_route_master"}) do
    "Relation not contained in route master"
  end

  def render(%Error{} = error) do
    fields = Map.take(error, [:key, :params])
    "FIXME: missing render function for error '#{inspect(fields)}'"
  end
end

M lib/triglav/zet/validator.ex => lib/triglav/zet/validator.ex +20 -6
@@ 141,13 141,27 @@ defmodule Triglav.Zet.Validator do

    if length(route_masters) == 1 do
      [route_master] = route_masters
      routes = Enum.filter(relations, &Relation.is_route/1)
      contained_ids = route_master.members |> Enum.map(& &1.member_id) |> MapSet.new()
      route_ids = MapSet.new(routes, & &1.id)
      unexpected_ids = MapSet.difference(contained_ids, route_ids)

      for relation_id <- unexpected_ids do
        Error.unexpected_route_master_member_relation(route, relation_id)
      expected_members =
        relations
        |> Enum.filter(&Relation.is_route/1)
        |> Enum.map(&{"R", &1.id})
        |> MapSet.new()

      actual_members =
        route_master.members
        |> Enum.map(&{&1.member_type, &1.member_id})
        |> MapSet.new()

      unexpected_members = MapSet.difference(actual_members, expected_members)

      for {member_type, member_id} <- unexpected_members do
        Error.unexpected_route_master_member(
          route,
          route_master.id,
          member_type,
          member_id
        )
      end
    end
  end

M lib/triglav_web/views/osm_helpers.ex => lib/triglav_web/views/osm_helpers.ex +4 -0
@@ 120,6 120,10 @@ defmodule TriglavWeb.OsmHelpers do
    """
  end

  def osm_link(_, _) do
    ~E"<p>FIXME: osm_link failed</p>"
  end

  def josm_load_objects(objects, opts \\ []) do
    {title, opts} = Keyword.pop(opts, :title, "Load")
    link = Triglav.Josm.load_objects(objects, opts)