~ihabunek/triglav

ref: 1a8ba58407fc58b933347c9bec5bcb4358191cbb triglav/lib/triglav/zet/errors.ex -rw-r--r-- 2.7 KiB
1a8ba584Ivan Habunek Use derived data for route detail view 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
defmodule Triglav.Zet.Errors do
  alias Triglav.Repo
  alias Triglav.Schemas.Error
  alias Triglav.Schemas.ErrorHistory

  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()
  end

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

  def render(%Error{key: "multiple_route_masters"}) do
    "Multiple route_master relations"
  end

  def render(%Error{key: "no_relations"}) do
    "No OSM relations found"
  end

  def render(%Error{key: "relation_not_updated_to_ptv2"}) do
    "Relation not updated to [public_transport:version=2]"
  end

  def render(%Error{
        key: "invalid_relation_name",
        params: %{"expected" => expected, "actual" => actual}
      }) do
    "Expected name based on tags: '#{expected}', actual name: '#{actual}"
  end

  def render(%Error{key: "relation_missing_required_tags", params: %{"tags" => tags}}) do
    "Missing required tags: #{Enum.join(tags, ", ")}"
  end

  def render(%Error{key: "relation_contains_unexpected_tags", params: %{"tags" => tags}}) do
    "Unexpected tags: #{Enum.join(tags, ", ")}"
  end

  def render(%Error{key: "relation_missing_gtfs_stop_ids", params: %{"count" => count}}) do
    "Missing gtfs:stop_id tags on #{count} platforms"
  end

  def render(%Error{
        key: "invalid_tag_value",
        params: %{"name" => name, "expected" => expected, "actual" => actual}
      }) do
    if actual do
      "Invalid tag [#{name}=#{actual}], expected [#{name}=#{expected}]"
    else
      "Missing tag [#{name}=#{expected}]"
    end
  end

  def render(%Error{key: "broken_route", params: %{"way_id" => way_id}}) do
    "Broken route starting at way ##{way_id}"
  end

  def render(%Error{
        key: "unexpected_member_role",
        params: %{
          "member_role" => member_role,
          "member_type" => member_type,
          "member_id" => member_id
        }
      }) do
    "Unexpected role \"#{member_role}\" on relation member #{member_type}#{member_id}"
  end

  def render(%Error{
        key: "platform_too_far_from_route",
        params: %{"sequence_id" => sequence_id, "distance_from_route" => distance_from_route}
      }) 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}'"
  end
end