diff options
author | href <href@random.sh> | 2020-04-17 15:53:14 +0200 |
---|---|---|
committer | href <href@random.sh> | 2020-04-17 15:53:14 +0200 |
commit | 919725a6941830ce82c835ed3288c1722ddd8c9f (patch) | |
tree | 49a95b0ce716a24c7e056036d3353ceca1debe4a /lib/lsg_web | |
parent | welp (diff) |
bleh
Diffstat (limited to '')
-rw-r--r-- | lib/lsg_web/controllers/alcoolog_controller.ex | 48 | ||||
-rw-r--r-- | lib/lsg_web/router.ex | 2 | ||||
-rw-r--r-- | lib/lsg_web/templates/alcoolog/index.html.eex | 55 | ||||
-rw-r--r-- | lib/lsg_web/views/alcoolog_view.ex | 4 | ||||
-rw-r--r-- | lib/lsg_web/views/layout_view.ex | 17 |
5 files changed, 126 insertions, 0 deletions
diff --git a/lib/lsg_web/controllers/alcoolog_controller.ex b/lib/lsg_web/controllers/alcoolog_controller.ex new file mode 100644 index 0000000..9d5d9d9 --- /dev/null +++ b/lib/lsg_web/controllers/alcoolog_controller.ex @@ -0,0 +1,48 @@ +defmodule LSGWeb.AlcoologController do + use LSGWeb, :controller + require Logger + + def index(conn, %{"channel" => channel}) do + case LSG.Token.lookup(channel) do + {:ok, obj} -> index(conn, obj) + err -> + Logger.debug("AlcoologControler: token #{inspect err} invalid") + conn + |> put_status(404) + |> text("Page not found") + end + end + + def index(conn, {:alcoolog, :index, channel}) do + aday = 7*((24 * 60)*60) + now = DateTime.utc_now() + before = now + |> DateTime.add(-aday, :second) + |> DateTime.to_unix(:millisecond) + #match = :ets.fun2ms(fn(obj = {{^nick, date}, _, _, _, _}) when date > before -> obj end) + match = [ + {{{:_, :"$1"}, :_, :_, :_, :_}, + [ + {:>, :"$1", {:const, before}}, + ], [:"$_"]} + ] + + nicks_in_channel = IRC.UserTrack.channel(channel) + |> Enum.map(fn({_, nick, _, _, _, _, _}) -> String.downcase(nick) end) + # tuple ets: {{nick, date}, volumes, current, nom, commentaire} + drinks = :ets.select(LSG.IRC.AlcoologPlugin.ETS, match) + #|> Enum.filter(fn({{nick, _}, _, _, _, _}) -> Enum.member?(nicks_in_channel, nick) end) + |> Enum.sort_by(fn({{_, ts}, _, _, _, _}) -> ts end, &>/2) + + stats = LSG.IRC.AlcoologPlugin.get_channel_statistics(channel) + + top = Enum.reduce(drinks, %{}, fn({{nick, _}, vol, _, _, _}, acc) -> + all = Map.get(acc, nick, 0) + Map.put(acc, nick, all + vol) + end) + |> Enum.sort_by(fn({_nick, count}) -> count end, &>/2) + # {date, single_peak} + render(conn, "index.html", channel: channel, drinks: drinks, top: top, stats: stats) + end + +end diff --git a/lib/lsg_web/router.ex b/lib/lsg_web/router.ex index c47e422..a834083 100644 --- a/lib/lsg_web/router.ex +++ b/lib/lsg_web/router.ex @@ -22,6 +22,8 @@ defmodule LSGWeb.Router do get "/irc", IrcController, :index get "/irc/txt", IrcController, :txt get "/irc/txt/:name", IrcController, :txt + get "/irc/alcoolog/:channel", AlcoologController, :index + get "/irc/alcoolog/~/:nick", AlcoologController, :nick end scope "/api", LSGWeb do diff --git a/lib/lsg_web/templates/alcoolog/index.html.eex b/lib/lsg_web/templates/alcoolog/index.html.eex new file mode 100644 index 0000000..507be71 --- /dev/null +++ b/lib/lsg_web/templates/alcoolog/index.html.eex @@ -0,0 +1,55 @@ +<style type="text/css"> +h1 small { + font-size: 14px; +} +ol li { + margin-bottom: 5px +} +</style> + +<h1> + <small><a href="/irc"><%= Keyword.get(Application.get_env(:lsg, :irc), :name, "ircbot") %></a> › </small><br/> + alcoolog <%= @channel %> +</h1> + +<ul> + <%= for {nick, status} <- @stats do %> + <li><strong><%= nick %> <%= status.user_status %> - <%= status.trend_symbol %> <%= status.active %> g/l</strong><br/> + — 15m: <%= status.active15m %> g/l - 30m: <%= status.active30m %> g/l - 1h: <%= status.active1h %> g/l<br /> + — dernier verre: <%= status.last_type %> <%= status.last_descr %> (<%= status.last_points %>) + <%= LSGWeb.LayoutView.format_time(status.last_at) %> + <br /> + — sobre dans: <%= status.sober_in_s %> + <br /> + <small> + — aujourd'hui: <%= status.daily_volumes %> points, <%= status.daily_gl %> g/l + </small> + </li> + <% end %> +</ul> + +<p> + top consommateur par volume, les 7 derniers jours: <%= Enum.intersperse(for({nick, count} <- @top, do: "#{nick}: #{Float.round(count,4)}"), ", ") %> +</p> + +<table class="table"> + <thead> + <tr> + <th scope="col">date</th> + <th scope="col">nick</th> + <th scope="col">points</th> + <th scope="col">nom</th> + </tr> + </thead> + <tbody> + <%= for {{nick, date}, points, _, nom, comment} <- @drinks do %> + <% date = DateTime.from_unix!(date, :millisecond) %> + <th scope="row"><%= LSGWeb.LayoutView.format_time(date, false) %></th> + <td><%= nick %></td> + <td><%= Float.round(points+0.0, 5) %></td> + <td><%= nom||"" %> <%= comment||"" %></td> + </tr> + <% end %> + </tbody> +</table> + diff --git a/lib/lsg_web/views/alcoolog_view.ex b/lib/lsg_web/views/alcoolog_view.ex new file mode 100644 index 0000000..2c55c09 --- /dev/null +++ b/lib/lsg_web/views/alcoolog_view.ex @@ -0,0 +1,4 @@ +defmodule LSGWeb.AlcoologView do + use LSGWeb, :view +end + diff --git a/lib/lsg_web/views/layout_view.ex b/lib/lsg_web/views/layout_view.ex index 39216ed..956d703 100644 --- a/lib/lsg_web/views/layout_view.ex +++ b/lib/lsg_web/views/layout_view.ex @@ -1,3 +1,20 @@ defmodule LSGWeb.LayoutView do use LSGWeb, :view + + def format_time(date, with_relative \\ true) do + alias Timex.Format.DateTime.Formatters + alias Timex.Timezone + date = if is_integer(date) do + date + |> DateTime.from_unix!(:millisecond) + |> Timezone.convert("Europe/Paris") + else + date + end + {:ok, relative} = Formatters.Relative.relative_to(date, Timex.now("Europe/Paris"), "{relative}", "fr") + {:ok, detail} = Formatters.Default.lformat(date, "{D}/{M}/{YYYY} {h24}:{m}", "fr") + + content_tag(:time, if(with_relative, do: relative, else: detail), [title: detail]) + end + end |