wiki/lib/wiki/metric_generator.ex

35 lines
803 B
Elixir

defmodule Wiki.MetricGenerator do
use GenServer
alias Wiki.Repo
alias Wiki.Content.{Page, PageLink, Upload}
def start_link(state) do
GenServer.start_link(__MODULE__, state, name: __MODULE__)
end
@impl true
def init(state) do
update_metrics()
# 5 minutes
Process.send_after(self(), :update, 5 * 60 * 1000)
{:ok, state}
end
@impl true
def handle_info(:update, state) do
update_metrics()
{:noreply, state}
end
def update_metrics do
pages = Repo.aggregate(Page, :count)
uploads = Repo.aggregate(Upload, :count)
links = Repo.aggregate(PageLink, :count)
:telemetry.execute([:wiki, :pages], %{count: pages})
:telemetry.execute([:wiki, :uploads], %{count: uploads})
:telemetry.execute([:wiki, :links], %{count: links})
end
end