Fix all count
[akkoma] / lib / pleroma / stats.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Object
11 alias Pleroma.Repo
12 alias Pleroma.User
13
14 require Pleroma.Constants
15
16 @interval 1000 * 60 * 60
17
18 def start_link(_) do
19 GenServer.start_link(__MODULE__, initial_data(), name: __MODULE__)
20 end
21
22 def force_update do
23 GenServer.call(__MODULE__, :force_update)
24 end
25
26 def get_stats do
27 %{stats: stats} = GenServer.call(__MODULE__, :get_state)
28
29 stats
30 end
31
32 def get_peers do
33 %{peers: peers} = GenServer.call(__MODULE__, :get_state)
34
35 peers
36 end
37
38 def init(args) do
39 Process.send(self(), :run_update, [])
40 {:ok, args}
41 end
42
43 def handle_call(:force_update, _from, _state) do
44 new_stats = get_stat_data()
45 {:reply, new_stats, new_stats}
46 end
47
48 def handle_call(:get_state, _from, state) do
49 {:reply, state, state}
50 end
51
52 def handle_info(:run_update, _state) do
53 new_stats = get_stat_data()
54
55 Process.send_after(self(), :run_update, @interval)
56 {:noreply, new_stats}
57 end
58
59 defp initial_data do
60 %{peers: [], stats: %{}}
61 end
62
63 def get_stat_data do
64 peers =
65 from(
66 u in User,
67 select: fragment("distinct split_part(?, '@', 2)", u.nickname),
68 where: u.local != ^true
69 )
70 |> Repo.all()
71 |> Enum.filter(& &1)
72
73 domain_count = Enum.count(peers)
74
75 user_count = Repo.aggregate(User.Query.build(%{local: true, active: true}), :count, :id)
76
77 %{
78 peers: peers,
79 stats: %{domain_count: domain_count, status_count: status_count(), user_count: user_count}
80 }
81 end
82
83 defp status_count do
84 %{
85 all: all_statuses_query() |> Repo.aggregate(:count, :id),
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)
90 }
91 end
92
93 defp all_statuses_query do
94 from(o in Object, where: fragment("(?)->>'type' = 'Note'", o.data))
95 end
96
97 def public_statuses_query do
98 from(o in Object,
99 where: fragment("(?)->'to' \\? ?", o.data, ^Pleroma.Constants.as_public())
100 )
101 end
102
103 def unlisted_statuses_query do
104 from(o in Object,
105 where: not fragment("(?)->'to' \\? ?", o.data, ^Pleroma.Constants.as_public()),
106 where: fragment("(?)->'cc' \\? ?", o.data, ^Pleroma.Constants.as_public())
107 )
108 end
109
110 def direct_statuses_query do
111 private_statuses_ids = from(p in private_statuses_query(), select: p.id) |> Repo.all()
112
113 from(o in Object,
114 where:
115 fragment(
116 "? \\? 'directMessage' AND (?->>'directMessage')::boolean = true",
117 o.data,
118 o.data
119 ) or
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)
123 )
124 end
125
126 def private_statuses_query do
127 from(o in subquery(recipients_query()),
128 where: ilike(o.recipients, "%/followers%")
129 )
130 end
131
132 defp recipients_query do
133 from(o in Object,
134 select: %{
135 id: o.id,
136 recipients: fragment("jsonb_array_elements_text((?)->'to')", o.data)
137 },
138 where: not fragment("(?)->'to' \\? ?", o.data, ^Pleroma.Constants.as_public()),
139 where: not fragment("(?)->'cc' \\? ?", o.data, ^Pleroma.Constants.as_public())
140 )
141 end
142 end