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