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