~ihabunek/triglav

ec1d133a79ce17f814444b2c77159ddb668d41dd — Ivan Habunek 8 months ago 21fae55
Show only unresloved errors
M lib/triglav/zet/errors.ex => lib/triglav/zet/errors.ex +13 -0
@@ 5,6 5,19 @@ defmodule Triglav.Zet.Errors do

  import Ecto.Query

  def list_unresolved(opts \\ []) do
    route_id = Keyword.get(opts, :route_id)

    from(e in Error, where: is_nil(e.resolved_at))
    |> maybe_filter_by_route(route_id)
    |> Repo.all()
  end

  defp maybe_filter_by_route(query, route_id) when is_binary(route_id),
    do: where(query, [e], e.route_id == ^route_id)

  defp maybe_filter_by_route(query, _), do: query

  def list_history() do
    from(h in ErrorHistory, order_by: [desc: :timestamp], limit: 50)
    |> Repo.all()

M lib/triglav/zet/gtfs.ex => lib/triglav/zet/gtfs.ex +2 -15
@@ 1,7 1,6 @@
defmodule Triglav.Zet.Gtfs do
  alias Triglav.Repo
  alias Triglav.Schemas.Zet
  alias Triglav.Schemas.Error

  import Ecto.Query
  import Geo.PostGIS


@@ 10,14 9,8 @@ defmodule Triglav.Zet.Gtfs do
    Zet.FeedInfo |> Repo.one!()
  end

  def list_routes(opts \\ []) do
    routes = Repo.all(Zet.Route)

    if Keyword.get(opts, :with_errors, false) do
      Repo.preload(routes, :errors, prefix: nil)
    else
      routes
    end
  def list_routes() do
    Zet.Route |> Repo.all()
  end

  def get_route(id) do


@@ 26,12 19,6 @@ defmodule Triglav.Zet.Gtfs do
    |> Repo.one!()
  end

  def get_errors(route_id) do
    from(e in Error, where: e.route_id == ^route_id)
    |> Repo.all()
    |> Repo.preload(:relation)
  end

  @spec list_gtfs_stop_ids() :: [Zet.DistinctTrip.t()]
  def list_gtfs_stop_ids() do
    from(t in Zet.DistinctTrip)

M lib/triglav_web/controllers/zet/routes_controller.ex => lib/triglav_web/controllers/zet/routes_controller.ex +16 -20
@@ 3,11 3,14 @@ defmodule TriglavWeb.Zet.RoutesController do

  alias Triglav.Import.Geofabrik
  alias Triglav.Schemas.Osmosis.Relation
  alias Triglav.Zet.Errors
  alias Triglav.Zet.Gtfs
  alias Triglav.Zet.Osmosis

  def index(conn, _params) do
    routes = Gtfs.list_routes(with_errors: true)
    routes = Gtfs.list_routes()
    errors = Errors.list_unresolved() |> Enum.group_by(& &1.route_id)

    relations = Osmosis.list_public_transport_relations(members: true)
    {:ok, osm_state} = Geofabrik.local_state()
    gtfs_info = Gtfs.get_feed_info()


@@ 21,6 24,7 @@ defmodule TriglavWeb.Zet.RoutesController do

    render(conn, "index.html",
      routes: annotate_routes(routes, relations),
      errors: errors,
      unmached_relations: unmached_relations,
      osm_state: osm_state,
      gtfs_info: gtfs_info


@@ 63,7 67,10 @@ defmodule TriglavWeb.Zet.RoutesController do
    route = Gtfs.get_route(id)
    relations = Osmosis.list_public_transport_relations(ref: id, members: true)
    hierarchy = make_hierarchy(relations)
    {errors, rel_errors} = Gtfs.get_errors(id) |> group_errors_by_relation()

    errors = Errors.list_unresolved(route_id: id)
    relation_errors = Enum.filter(errors, & &1.relation_id) |> Enum.group_by(& &1.relation_id)
    route_errors = Enum.filter(errors, &is_nil(&1.relation_id))

    platform_members =
      relations


@@ 97,15 104,15 @@ defmodule TriglavWeb.Zet.RoutesController do

    render(conn, "detail.html",
      conn: conn,
      route: route,
      relations: relations,
      hierarchy: hierarchy,
      errors: errors,
      rel_errors: rel_errors,
      trips: grouped_trips,
      platform_members: platform_members,
      relation_errors: relation_errors,
      relations: relations,
      route: route,
      route_errors: route_errors,
      stops_json: stops_json,
      ways_geojson: ways_geojson,
      platform_members: platform_members
      trips: grouped_trips,
      ways_geojson: ways_geojson
    )
  end



@@ 134,17 141,6 @@ defmodule TriglavWeb.Zet.RoutesController do
    }
  end

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

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

    {errors, rel_errors}
  end

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


M lib/triglav_web/templates/zet/routes/detail.html.eex => lib/triglav_web/templates/zet/routes/detail.html.eex +32 -31
@@ 27,6 27,38 @@
    <%= josm_load_objects(@relations) %>
  </section>

  <%= if length(@route_errors) > 0 or map_size(@relation_errors) > 0 do %>
    <section>
      <h3>Errors</h3>
      <%= if length(@route_errors) > 0 do %>
        <ul>
          <%= for error <- @route_errors do %>
            <li>
              <span class="text-red"><%= render_error(error) %></span>
            </li>
          <% end %>
        </ul>
      <% end %>
      <%= if map_size(@relation_errors) > 0 do %>
        <ul>
          <%= for {relation_id, errors} <- @relation_errors do %>
            <li>
              <% relation = Enum.find(@relations, & &1.id == relation_id) %>
              <%= osm_link(relation, tags: ["type"], name: true) %>
              <ul>
                <%= for error <- errors do %>
                  <li><span class="text-red"><%= render_error(error) %></span></li>
                <% end %>
              </ul>
            </li>
          <% end %>
        </ul>
      <% end %>
    </section>
  <% else %>
    <p class="text-green">No errors found.</p>
  <% end %>

  <section>
    <h3>Route relations</h3>



@@ 70,37 102,6 @@
    </div>
  </section>

  <%= if length(@errors) > 0 or map_size(@rel_errors) > 0 do %>
    <section>
      <h3>Errors</h3>
      <%= if length(@errors) > 0 do %>
        <ul>
          <%= for error <- @errors do %>
            <li>
              <span class="text-red"><%= render_error(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) %>
              <ul>
                <%= for error <- errors do %>
                  <li><span class="text-red"><%= render_error(error) %></span></li>
                <% end %>
              </ul>
            </li>
          <% end %>
        </ul>
      <% end %>
    </section>
  <% else %>
    <p class="text-green">No errors found.</p>
  <% end %>

  <hr />

  <h2>ZET GTFS routes</h2>

M lib/triglav_web/templates/zet/routes/index.html.eex => lib/triglav_web/templates/zet/routes/index.html.eex +3 -2
@@ 94,9 94,10 @@
          </span>
        </td>
        <td>
          <%= if !Enum.empty?(route.errors) do %>
          <% route_errors = Map.get(@errors, route.id, []) %>
          <%= if !Enum.empty?(route_errors) do %>
          <ul>
            <%= for error <- route.errors do %>
            <%= for error <- route_errors do %>
              <li>
                <span class="text-red"><%= render_error(error) %></span>
              </li>