Fix delete activities not federating
[akkoma] / lib / pleroma / stats.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Stats do
6 import Ecto.Query
7 alias Pleroma.User
8 alias Pleroma.Repo
9
10 def start_link do
11 agent = Agent.start_link(fn -> {[], %{}} end, name: __MODULE__)
12 spawn(fn -> schedule_update() end)
13 agent
14 end
15
16 def get_stats do
17 Agent.get(__MODULE__, fn {_, stats} -> stats end)
18 end
19
20 def get_peers do
21 Agent.get(__MODULE__, fn {peers, _} -> peers end)
22 end
23
24 def schedule_update do
25 spawn(fn ->
26 # 1 hour
27 Process.sleep(1000 * 60 * 60)
28 schedule_update()
29 end)
30
31 update_stats()
32 end
33
34 def update_stats do
35 peers =
36 from(
37 u in Pleroma.User,
38 select: fragment("distinct split_part(?, '@', 2)", u.nickname),
39 where: u.local != ^true
40 )
41 |> Repo.all()
42 |> Enum.filter(& &1)
43
44 domain_count = Enum.count(peers)
45
46 status_query =
47 from(u in User.local_user_query(), select: fragment("sum((?->>'note_count')::int)", u.info))
48
49 status_count = Repo.one(status_query)
50 user_count = Repo.aggregate(User.active_local_user_query(), :count, :id)
51
52 Agent.update(__MODULE__, fn _ ->
53 {peers, %{domain_count: domain_count, status_count: status_count, user_count: user_count}}
54 end)
55 end
56 end