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
|
defmodule LSGWeb.AlcoologController do
use LSGWeb, :controller
require Logger
plug LSGWeb.ContextPlug when action not in [:token]
plug LSGWeb.ContextPlug, [restrict: :public] when action in [:token]
def token(conn, %{"token" => token}) do
case LSG.Token.lookup(token) do
{:ok, {:alcoolog, :index, network, channel}} -> index(conn, nil, network, channel)
err ->
Logger.debug("AlcoologControler: token #{inspect err} invalid")
conn
|> put_status(404)
|> text("Page not found")
end
end
def index(conn = %{assigns: %{account: account}}, %{"network" => network, "chan" => channel}) do
index(conn, account, network, LSGWeb.reformat_chan(channel))
end
def index(conn = %{assigns: %{account: account}}, _) do
index(conn, account, nil, nil)
end
#def index(conn, params) do
# network = Map.get(params, "network")
# chan = if c = Map.get(params, "chan") do
# LSGWeb.reformat_chan(c)
# end
# irc_conn = if network do
# IRC.Connection.get_network(network, chan)
# end
# bot = if(irc_conn, do: irc_conn.nick)#
#
# conn
# |> put_status(403)
# |> render("auth.html", network: network, channel: chan, irc_conn: conn, bot: bot)
#end
def index(conn, account, network, channel) do
aday = 18*((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}},
], [:"$_"]}
]
# tuple ets: {{nick, date}, volumes, current, nom, commentaire}
members = IRC.Membership.expanded_members_or_friends(account, network, channel)
members_ids = Enum.map(members, fn({account, _, nick}) -> account.id end)
member_names = Enum.reduce(members, %{}, fn({account, _, nick}, acc) -> Map.put(acc, account.id, nick) end)
drinks = :ets.select(LSG.IRC.AlcoologPlugin.ETS, match)
|> Enum.filter(fn({{account, _}, _, _, _, _}) -> Enum.member?(members_ids, account) end)
|> Enum.map(fn({{account, _}, _, _, _, _} = object) -> {object, Map.get(member_names, account)} end)
|> Enum.sort_by(fn({{{_, ts}, _, _, _, _}, _}) -> ts end, &>/2)
stats = LSG.IRC.AlcoologPlugin.get_channel_statistics(account, network, channel)
top = Enum.reduce(drinks, %{}, fn({{{account_id, _}, vol, _, _, _}, _}, acc) ->
nick = Map.get(member_names, account_id)
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}
#
conn
|> assign(:title, "alcoolog")
|> render("index.html", network: network, channel: channel, drinks: drinks, top: top, stats: stats)
end
end
|