summaryrefslogtreecommitdiff
path: root/lib/lsg_web/controllers/alcoolog_controller.ex
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/lsg_web/controllers/alcoolog_controller.ex48
1 files changed, 48 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