Merge branch 'refactor/deactivated_user_field' into 'develop'
[akkoma] / lib / pleroma / stats.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Stats do
6 use GenServer
7
8 import Ecto.Query
9
10 alias Pleroma.CounterCache
11 alias Pleroma.Repo
12 alias Pleroma.User
13
14 @interval :timer.seconds(60)
15
16 def start_link(_) do
17 GenServer.start_link(
18 __MODULE__,
19 nil,
20 name: __MODULE__
21 )
22 end
23
24 @impl true
25 def init(_args) do
26 {:ok, nil, {:continue, :calculate_stats}}
27 end
28
29 @doc "Performs update stats"
30 def force_update do
31 GenServer.call(__MODULE__, :force_update)
32 end
33
34 @doc "Returns stats data"
35 @spec get_stats() :: %{
36 domain_count: non_neg_integer(),
37 status_count: non_neg_integer(),
38 user_count: non_neg_integer()
39 }
40 def get_stats do
41 %{stats: stats} = GenServer.call(__MODULE__, :get_state)
42
43 stats
44 end
45
46 @doc "Returns list peers"
47 @spec get_peers() :: list(String.t())
48 def get_peers do
49 %{peers: peers} = GenServer.call(__MODULE__, :get_state)
50
51 peers
52 end
53
54 @spec calculate_stat_data() :: %{
55 peers: list(),
56 stats: %{
57 domain_count: non_neg_integer(),
58 status_count: non_neg_integer(),
59 user_count: non_neg_integer()
60 }
61 }
62 def calculate_stat_data do
63 peers =
64 from(
65 u in User,
66 select: fragment("distinct split_part(?, '@', 2)", u.nickname),
67 where: u.local != ^true
68 )
69 |> Repo.all()
70 |> Enum.filter(& &1)
71
72 domain_count = Enum.count(peers)
73
74 status_count = Repo.aggregate(User.Query.build(%{local: true}), :sum, :note_count)
75
76 users_query =
77 from(u in User,
78 where: u.is_active == true,
79 where: u.local == true,
80 where: not is_nil(u.nickname),
81 where: not u.invisible
82 )
83
84 user_count = Repo.aggregate(users_query, :count, :id)
85
86 %{
87 peers: peers,
88 stats: %{
89 domain_count: domain_count,
90 status_count: status_count || 0,
91 user_count: user_count
92 }
93 }
94 end
95
96 @spec get_status_visibility_count(String.t() | nil) :: map()
97 def get_status_visibility_count(instance \\ nil) do
98 if is_nil(instance) do
99 CounterCache.get_sum()
100 else
101 CounterCache.get_by_instance(instance)
102 end
103 end
104
105 @impl true
106 def handle_continue(:calculate_stats, _) do
107 stats = calculate_stat_data()
108
109 unless Pleroma.Config.get(:env) == :test do
110 Process.send_after(self(), :run_update, @interval)
111 end
112
113 {:noreply, stats}
114 end
115
116 @impl true
117 def handle_call(:force_update, _from, _state) do
118 new_stats = calculate_stat_data()
119 {:reply, new_stats, new_stats}
120 end
121
122 @impl true
123 def handle_call(:get_state, _from, state) do
124 {:reply, state, state}
125 end
126
127 @impl true
128 def handle_info(:run_update, _) do
129 new_stats = calculate_stat_data()
130 Process.send_after(self(), :run_update, @interval)
131 {:noreply, new_stats}
132 end
133 end