Merge branch 'feature/caddyfile' into 'develop'
[akkoma] / lib / pleroma / 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 spawn(fn -> schedule_update() end)
9 agent
10 end
11
12 def get_stats do
13 Agent.get(__MODULE__, fn {_, stats} -> stats end)
14 end
15
16 def get_peers do
17 Agent.get(__MODULE__, fn {peers, _} -> peers end)
18 end
19
20 def schedule_update do
21 spawn(fn ->
22 Process.sleep(1000 * 60 * 60 * 1) # 1 hour
23 schedule_update()
24 end)
25 update_stats()
26 end
27
28 def update_stats do
29 peers = from(u in Pleroma.User,
30 select: fragment("distinct ?->'host'", u.info),
31 where: u.local != ^true)
32 |> Repo.all()
33 domain_count = Enum.count(peers)
34 status_query = from(u in User.local_user_query,
35 select: fragment("sum((?->>'note_count')::int)", u.info))
36 status_count = Repo.one(status_query) |> IO.inspect
37 user_count = Repo.aggregate(User.local_user_query, :count, :id)
38 Agent.update(__MODULE__, fn _ ->
39 {peers, %{domain_count: domain_count, status_count: status_count, user_count: user_count}}
40 end)
41 end
42 end