~ihabunek/triglav

09e417f4f6b75ded5ee6f65a5f19cd9d28e63f57 — Ivan Habunek 1 year, 26 days ago 4847bb6
Group errors by relation
M lib/triglav_web/controllers/zet/routes_controller.ex => lib/triglav_web/controllers/zet/routes_controller.ex +16 -1
@@ 43,7 43,10 @@ defmodule TriglavWeb.Zet.RoutesController do
    relations = Osm.list_public_transport_relations(ref: id)
    ways = Osm.list_ways(relations)
    hierarchy = make_hierarchy(relations)
    errors = Validator.validate_route(route, relations, ways)

    {errors, rel_errors} =
      Validator.validate_route(route, relations, ways)
      |> group_errors_by_relation()

    trips =
      Gtfs.fetch_distinct_trips(route.id)


@@ 54,10 57,22 @@ defmodule TriglavWeb.Zet.RoutesController do
      relations: relations,
      hierarchy: hierarchy,
      errors: errors,
      rel_errors: rel_errors,
      trips: trips
    )
  end

  def group_errors_by_relation(errors) do
    {errors, rel_errors} = Enum.split_with(errors, &is_binary/1)

    rel_errors =
      Enum.reduce(rel_errors, %{}, fn {rel, error}, acc ->
        Map.update(acc, rel, [error], fn errors -> [error | errors] end)
      end)

    {errors, rel_errors}
  end

  defp make_hierarchy(relations) do
    {route_masters, routes} = Enum.split_with(relations, &(&1.tags["type"] == "route_master"))


M lib/triglav_web/templates/zet/routes/detail.html.eex => lib/triglav_web/templates/zet/routes/detail.html.eex +24 -16
@@ 7,24 7,32 @@
    <%= render_relation_hierarchy(@hierarchy) %>
  </section>

  <%= if length(@errors) > 0 do %>
  <%= if length(@errors) > 0 or map_size(@rel_errors) > 0 do %>
    <section>
      <h3>Errors</h3>
      <ul>
        <%= for error <- @errors do %>
          <li>
            <span class="red">
              <%= case error do
                {relation, error} -> [
                  osm_link(relation, tags: ["type"]),
                  error
                ]
                error -> error
              end %>
            </span>
          </li>
        <% end %>
      </ul>
      <%= if length(@errors) > 0 do %>
        <ul>
          <%= for error <- @errors do %>
            <li>
              <span class="red"><%= error %></span>
            </li>
          <% end %>
        </ul>
      <% end %>
      <%= if map_size(@rel_errors) > 0 do %>
        <ul>
          <%= for {relation, errors} <- @rel_errors do %>
            <li>
              <%= osm_link(relation, tags: ["type"], name: true, josm: true) %>
              <ul>
                <%= for error <- errors do %>
                  <li><span class="red"><%= error %></span></li>
                <% end %>
              </ul>
            </li>
          <% end %>
        </ul>
      <% end %>
    </section>
  <% else %>
    <p class="green">No errors found.</p>