Merge branch 'feature/1790-oban-overuse' into 'develop'
[akkoma] / lib / pleroma / stats.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 "Performs collect stats"
35 def do_collect do
36 GenServer.cast(__MODULE__, :run_update)
37 end
38
39 @doc "Returns stats data"
40 @spec get_stats() :: %{
41 domain_count: non_neg_integer(),
42 status_count: non_neg_integer(),
43 user_count: non_neg_integer()
44 }
45 def get_stats do
46 %{stats: stats} = GenServer.call(__MODULE__, :get_state)
47
48 stats
49 end
50
51 @doc "Returns list peers"
52 @spec get_peers() :: list(String.t())
53 def get_peers do
54 %{peers: peers} = GenServer.call(__MODULE__, :get_state)
55
56 peers
57 end
58
59 @spec calculate_stat_data() :: %{
60 peers: list(),
61 stats: %{
62 domain_count: non_neg_integer(),
63 status_count: non_neg_integer(),
64 user_count: non_neg_integer()
65 }
66 }
67 def calculate_stat_data do
68 peers =
69 from(
70 u in User,
71 select: fragment("distinct split_part(?, '@', 2)", u.nickname),
72 where: u.local != ^true
73 )
74 |> Repo.all()
75 |> Enum.filter(& &1)
76
77 domain_count = Enum.count(peers)
78
79 status_count = Repo.aggregate(User.Query.build(%{local: true}), :sum, :note_count)
80
81 users_query =
82 from(u in User,
83 where: u.deactivated != true,
84 where: u.local == true,
85 where: not is_nil(u.nickname),
86 where: not u.invisible
87 )
88
89 user_count = Repo.aggregate(users_query, :count, :id)
90
91 %{
92 peers: peers,
93 stats: %{
94 domain_count: domain_count,
95 status_count: status_count || 0,
96 user_count: user_count
97 }
98 }
99 end
100
101 @spec get_status_visibility_count(String.t() | nil) :: map()
102 def get_status_visibility_count(instance \\ nil) do
103 if is_nil(instance) do
104 CounterCache.get_sum()
105 else
106 CounterCache.get_by_instance(instance)
107 end
108 end
109
110 @impl true
111 def handle_continue(:calculate_stats, _) do
112 stats = calculate_stat_data()
113 Process.send_after(self(), :run_update, @interval)
114 {:noreply, stats}
115 end
116
117 @impl true
118 def handle_call(:force_update, _from, _state) do
119 new_stats = calculate_stat_data()
120 {:reply, new_stats, new_stats}
121 end
122
123 @impl true
124 def handle_call(:get_state, _from, state) do
125 {:reply, state, state}
126 end
127
128 @impl true
129 def handle_cast(:run_update, _state) do
130 new_stats = calculate_stat_data()
131
132 {:noreply, new_stats}
133 end
134
135 @impl true
136 def handle_info(:run_update, _) do
137 new_stats = calculate_stat_data()
138 Process.send_after(self(), :run_update, @interval)
139 {:noreply, new_stats}
140 end
141 end