1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Stats do
7 alias Pleroma.CounterCache
21 @doc "Performs update stats"
23 GenServer.call(__MODULE__, :force_update)
26 @doc "Performs collect stats"
28 GenServer.cast(__MODULE__, :run_update)
31 @doc "Returns stats data"
32 @spec get_stats() :: %{domain_count: integer(), status_count: integer(), user_count: integer()}
34 %{stats: stats} = GenServer.call(__MODULE__, :get_state)
39 @doc "Returns list peers"
40 @spec get_peers() :: list(String.t())
42 %{peers: peers} = GenServer.call(__MODULE__, :get_state)
48 {:ok, calculate_stat_data()}
51 def handle_call(:force_update, _from, _state) do
52 new_stats = calculate_stat_data()
53 {:reply, new_stats, new_stats}
56 def handle_call(:get_state, _from, state) do
57 {:reply, state, state}
60 def handle_cast(:run_update, _state) do
61 new_stats = calculate_stat_data()
66 def calculate_stat_data do
70 select: fragment("distinct split_part(?, '@', 2)", u.nickname),
71 where: u.local != ^true
76 domain_count = Enum.count(peers)
78 status_count = Repo.aggregate(User.Query.build(%{local: true}), :sum, :note_count)
82 where: u.deactivated != true,
83 where: u.local == true,
84 where: not is_nil(u.nickname),
85 where: not u.invisible
88 user_count = Repo.aggregate(users_query, :count, :id)
93 domain_count: domain_count,
94 status_count: status_count || 0,
95 user_count: user_count
100 def get_status_visibility_count(instance \\ nil) do
101 if is_nil(instance) do
102 CounterCache.get_sum()
104 CounterCache.get_by_instance(instance)