~ihabunek/triglav

ref: 3a62b8feb3be418cf9f3fb176ac6c84a36e66a2f triglav/lib/triglav/zet/errors.ex -rw-r--r-- 3.6 KiB
3a62b8feIvan Habunek Validate tags on platforms 4 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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: "relation_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: "platform_invalid_tag_value",
        params: %{
          "sequence_id" => sequence_id,
          "name" => name,
          "expected" => expected,
          "actual" => actual
        }
      }) do
    if actual do
      "Platform #{sequence_id} has invalid tag [#{name}=#{actual}], expected [#{name}=#{expected}]"
    else
      "Platform #{sequence_id} is 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: "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