1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Stats do
7 alias Pleroma.CounterCache
30 @doc "Performs update stats"
32 GenServer.call(__MODULE__, :force_update)
35 @doc "Performs collect stats"
37 GenServer.cast(__MODULE__, :run_update)
40 @doc "Returns stats data"
41 @spec get_stats() :: %{domain_count: integer(), status_count: integer(), user_count: integer()}
43 %{stats: stats} = GenServer.call(__MODULE__, :get_state)
48 @doc "Returns list peers"
49 @spec get_peers() :: list(String.t())
51 %{peers: peers} = GenServer.call(__MODULE__, :get_state)
60 def handle_call(:force_update, _from, _state) do
61 new_stats = get_stat_data()
62 {:reply, new_stats, new_stats}
65 def handle_call(:get_state, _from, state) do
66 {:reply, state, state}
69 def handle_cast(:run_update, _state) do
70 new_stats = get_stat_data()
79 select: fragment("distinct split_part(?, '@', 2)", u.nickname),
80 where: u.local != ^true
85 domain_count = Enum.count(peers)
87 status_count = Repo.aggregate(User.Query.build(%{local: true}), :sum, :note_count)
89 user_count = Repo.aggregate(User.Query.build(%{local: true, active: true}), :count, :id)
94 domain_count: domain_count,
95 status_count: status_count,
96 user_count: user_count
101 def get_status_visibility_count do
103 CounterCache.get_as_map([
104 "status_visibility_public",
105 "status_visibility_private",
106 "status_visibility_unlisted",
107 "status_visibility_direct"
111 public: counter_cache["status_visibility_public"] || 0,
112 unlisted: counter_cache["status_visibility_unlisted"] || 0,
113 private: counter_cache["status_visibility_private"] || 0,
114 direct: counter_cache["status_visibility_direct"] || 0