~ihabunek/triglav

ref: 3a62b8feb3be418cf9f3fb176ac6c84a36e66a2f triglav/lib/triglav_web/controllers/zet/errors_controller.ex -rw-r--r-- 2.2 KiB
3a62b8feIvan Habunek Validate tags on platforms 10 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
defmodule TriglavWeb.Zet.ErrorsController do
  use TriglavWeb, :controller

  alias Triglav.Zet.Errors
  alias Atomex.{Feed, Entry}

  def index(conn, _params) do
    history = Errors.list_history()
    render(conn, "index.html", history: history)
  end

  def atom(conn, _params) do
    history = Errors.list_history()
    self_url = TriglavWeb.Router.Helpers.zet_errors_url(conn, :atom)
    base_url = TriglavWeb.Router.Helpers.zet_errors_url(conn, :index)

    last_updated =
      if length(history) > 0,
        do: history |> List.first() |> Map.get(:timestamp),
        else: ~U"2000-01-01T00:00:00Z"

    feed =
      Feed.new(base_url, last_updated, "ZET error history")
      |> Feed.link(self_url, rel: "self")
      |> Feed.link(base_url, rel: "alternate", type: "text/html")
      |> Feed.entries(Enum.map(history, &to_entry(&1, base_url)))
      |> Feed.build(%{"xmlns:triglav" => "http://triglav.bezdomni.net/atom"})
      |> Atomex.generate_document()

    conn
    |> put_resp_content_type("text/xml")
    |> send_resp(conn.status || 200, feed)
  end

  defp to_entry(history_item, base_url) do
    counts = %{
      current: history_item.count,
      previous: history_item.previous_count,
      created: history_item.created_count,
      resolved: history_item.resolved_count,
      diff: history_item.created_count - history_item.resolved_count
    }

    id = "#{base_url}/#{history_item.id}"
    title = "Errors count changed #{counts.current} (+#{counts.created} -#{counts.resolved})"

    content = """
      <p>
        Created count: #{counts.created}<br />
        Resolved count: #{counts.resolved}<br />
        Current count: #{counts.current}
      </p>

      <p>
        ZET GTFS feed version: #{history_item.zet_feed_version}<br />
        OSM sequence number: #{history_item.osm_sequence_number}<br />
        Timestamp: #{history_item.timestamp}
      </p>
    """

    Entry.new(id, history_item.timestamp, title)
    |> Entry.content(content, type: "html")
    |> Entry.add_field("triglav:counts", counts, nil)
    |> Entry.add_field("triglav:osm_sequence_number", nil, history_item.osm_sequence_number)
    |> Entry.add_field("triglav:zet_feed_version", nil, history_item.zet_feed_version)
    |> Entry.build()
  end
end