summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/alcoolog_announcer_plugin.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lsg_irc/alcoolog_announcer_plugin.ex')
-rw-r--r--lib/lsg_irc/alcoolog_announcer_plugin.ex57
1 files changed, 50 insertions, 7 deletions
diff --git a/lib/lsg_irc/alcoolog_announcer_plugin.ex b/lib/lsg_irc/alcoolog_announcer_plugin.ex
index 28973ca..3902d5f 100644
--- a/lib/lsg_irc/alcoolog_announcer_plugin.ex
+++ b/lib/lsg_irc/alcoolog_announcer_plugin.ex
@@ -24,7 +24,26 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do
def irc_doc, do: nil
- def start_link(), do: GenServer.start_link(__MODULE__, [])
+ def start_link(), do: GenServer.start_link(__MODULE__, [], name: __MODULE__)
+
+ def log(account) do
+ dets_filename = (LSG.data_path() <> "/" <> "alcoologlog.dets") |> String.to_charlist
+ {:ok, dets} = :dets.open_file(dets_filename, [{:type,:bag}])
+ from = ~U[2020-08-23 19:41:40.524154Z]
+ to = ~U[2020-08-24 19:41:40.524154Z]
+ select = [
+ {{:"$1", :"$2", :_},
+ [
+ {:andalso,
+ {:andalso, {:==, :"$1", {:const, account.id}},
+ {:>, :"$2", {:const, DateTime.to_unix(from)}}},
+ {:<, :"$2", {:const, DateTime.to_unix(to)}}}
+ ], [:"$_"]}
+ ]
+ res = :dets.select(dets, select)
+ :dets.close(dets)
+ res
+ end
def init(_) do
{:ok, _} = Registry.register(IRC.PubSub, "account", [])
@@ -32,21 +51,42 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do
Process.send_after(self(), :stats, :timer.seconds(30))
dets_filename = (LSG.data_path() <> "/" <> "alcoologlog.dets") |> String.to_charlist
{:ok, dets} = :dets.open_file(dets_filename, [{:type,:bag}])
+ ets = nil # :ets.new(__MODULE__.ETS, [:ordered_set, :named_table, :protected, {:read_concurrency, true}])
#:ok = LSG.IRC.SettingPlugin.declare("alcoolog.alerts", __MODULE__, true, :boolean)
#:ok = LSG.IRC.SettingPlugin.declare("alcoolog.aperoalert", __MODULE__, true, :boolean)
- {:ok, {stats, now(), dets}}
+ #
+ {:ok, {stats, now(), dets, ets}}#, {:continue, :traverse}}
+ end
+
+ def handle_continue(:traverse, state = {_, _, dets, ets}) do
+ traverse_fun = fn(obj, dets) ->
+ case obj do
+ {nick, %DateTime{} = dt, active} ->
+ :dets.delete_object(dets, obj)
+ :dets.insert(dets, {nick, DateTime.to_unix(dt), active})
+ IO.puts("ok #{inspect obj}")
+ dets
+ {nick, ts, value} ->
+ :ets.insert(ets, { {nick, ts}, value })
+ dets
+ end
+ end
+ :dets.foldl(traverse_fun, dets, dets)
+ :dets.sync(dets)
+ IO.puts("alcoolog announcer fixed")
+ {:noreply, state}
end
def alcohol_reached(old, new, level) do
(old.active < level && new.active >= level) && (new.active5m >= level)
end
-
+
def alcohol_below(old, new, level) do
(old.active > level && new.active <= level) && (new.active5m <= level)
end
- def handle_info(:stats, {old_stats, old_now, dets}) do
+ def handle_info(:stats, {old_stats, old_now, dets, ets}) do
stats = get_stats()
now = now()
@@ -73,10 +113,13 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do
new = Map.get(stats, acct, nil)
#IO.puts "#{acct}: #{inspect(old)} -> #{inspect(new)}"
+ now = DateTime.to_unix(DateTime.utc_now())
if new && new[:active] do
- :dets.insert(dets, {acct, DateTime.utc_now(), new[:active]})
+ :dets.insert(dets, {acct, now, new[:active]})
+ :ets.insert(ets, {{acct, now}, new[:active]})
else
- :dets.insert(dets, {acct, DateTime.utc_now(), 0.0})
+ :dets.insert(dets, {acct, now, 0.0})
+ :ets.insert(ets, {{acct, now}, new[:active]})
end
event = cond do
@@ -206,7 +249,7 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do
timer()
#IO.puts "tick stats ok"
- {:noreply, {stats,now,dets}}
+ {:noreply, {stats,now,dets,ets}}
end
def handle_info(_, state) do