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
14 require Pleroma.Constants
16 @interval 1000 * 60 * 60
19 GenServer.start_link(__MODULE__, initial_data(), name: __MODULE__)
23 GenServer.call(__MODULE__, :force_update)
27 %{stats: stats} = GenServer.call(__MODULE__, :get_state)
33 %{peers: peers} = GenServer.call(__MODULE__, :get_state)
39 Process.send(self(), :run_update, [])
43 def handle_call(:force_update, _from, _state) do
44 new_stats = get_stat_data()
45 {:reply, new_stats, new_stats}
48 def handle_call(:get_state, _from, state) do
49 {:reply, state, state}
52 def handle_info(:run_update, _state) do
53 new_stats = get_stat_data()
55 Process.send_after(self(), :run_update, @interval)
60 %{peers: [], stats: %{}}
67 select: fragment("distinct split_part(?, '@', 2)", u.nickname),
68 where: u.local != ^true
73 domain_count = Enum.count(peers)
75 user_count = Repo.aggregate(User.Query.build(%{local: true, active: true}), :count, :id)
79 stats: %{domain_count: domain_count, status_count: status_count(), user_count: user_count}
85 all: get_all_statuses_count(),
86 public: public_statuses_query() |> Repo.aggregate(:count, :id),
87 unlisted: unlisted_statuses_query() |> Repo.aggregate(:count, :id),
88 direct: direct_statuses_query() |> Repo.aggregate(:count, :id),
89 private: private_statuses_query() |> Repo.aggregate(:count, :id)
93 defp get_all_statuses_count do
94 Repo.aggregate(User.Query.build(%{local: true}), :sum, :note_count)
97 def public_statuses_query do
99 where: fragment("(?)->'to' \\? ?", o.data, ^Pleroma.Constants.as_public())
103 def unlisted_statuses_query do
105 where: not fragment("(?)->'to' \\? ?", o.data, ^Pleroma.Constants.as_public()),
106 where: fragment("(?)->'cc' \\? ?", o.data, ^Pleroma.Constants.as_public())
110 def direct_statuses_query do
111 private_statuses_ids = from(p in private_statuses_query(), select: p.id) |> Repo.all()
116 "? \\? 'directMessage' AND (?->>'directMessage')::boolean = true",
120 (not fragment("(?)->'to' \\? ?", o.data, ^Pleroma.Constants.as_public()) and
121 not fragment("(?)->'cc' \\? ?", o.data, ^Pleroma.Constants.as_public()) and
122 o.id not in ^private_statuses_ids)
126 def private_statuses_query do
127 from(o in subquery(recipients_query()),
128 where: ilike(o.recipients, "%/followers%")
132 defp recipients_query do
136 recipients: fragment("jsonb_array_elements_text((?)->'to')", o.data)
138 where: not fragment("(?)->'to' \\? ?", o.data, ^Pleroma.Constants.as_public()),
139 where: not fragment("(?)->'cc' \\? ?", o.data, ^Pleroma.Constants.as_public())