45fa27b557755fa7c61bc664e9750e329d49ea4f
[akkoma] / stats.ex
1 defmodule Pleroma.Stats do
2 use Agent
3 import Ecto.Query
4 alias Pleroma.{User, Repo, Activity}
5
6 def start_link do
7 agent = Agent.start_link(fn -> %{} end, name: __MODULE__)
8 schedule_update()
9 agent
10 end
11
12 def get do
13 Agent.get(__MODULE__, fn stats -> stats end)
14 end
15
16 def schedule_update do
17 update_stats()
18 spawn(fn ->
19 Process.sleep(1000 * 60 * 60 * 1) # 1 hour
20 schedule_update()
21 end)
22 end
23
24 def update_stats do
25 peers = from(u in Pleroma.User,
26 select: fragment("?->'host'", u.info),
27 where: u.local != ^true)
28 |> Repo.all() |> Enum.uniq()
29 domain_count = Enum.count(peers)
30 status_query = from p in Activity,
31 where: p.local == ^true,
32 where: fragment("?->'object'->>'type' = ?", p.data, ^"Note")
33 status_count = Repo.aggregate(status_query, :count, :id)
34 Agent.update(__MODULE__, fn _ ->
35 %{peers: peers, domain_count: domain_count, status_count: status_count}
36 end)
37 end
38 end