Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85628735
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/mix/tasks/pleroma/refresh_counter_cache.ex b/lib/mix/tasks/pleroma/refresh_counter_cache.ex
index 15b4dbfa6..280201bef 100644
--- a/lib/mix/tasks/pleroma/refresh_counter_cache.ex
+++ b/lib/mix/tasks/pleroma/refresh_counter_cache.ex
@@ -1,46 +1,62 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Mix.Tasks.Pleroma.RefreshCounterCache do
@shortdoc "Refreshes counter cache"
use Mix.Task
alias Pleroma.Activity
alias Pleroma.CounterCache
alias Pleroma.Repo
require Logger
import Ecto.Query
def run([]) do
Mix.Pleroma.start_pleroma()
- ["public", "unlisted", "private", "direct"]
- |> Enum.each(fn visibility ->
- count = status_visibility_count_query(visibility)
- name = "status_visibility_#{visibility}"
- CounterCache.set(name, count)
- Mix.Pleroma.shell_info("Set #{name} to #{count}")
+ Activity
+ |> distinct([a], true)
+ |> select([a], fragment("split_part(?, '/', 3)", a.actor))
+ |> Repo.all()
+ |> Enum.each(fn instance ->
+ counters = instance_counters(instance)
+ CounterCache.set(instance, counters)
+ Mix.Pleroma.shell_info("Setting #{instance} counters: #{inspect(counters)}")
end)
Mix.Pleroma.shell_info("Done")
end
- defp status_visibility_count_query(visibility) do
+ defp instance_counters(instance) do
+ counters = %{"public" => 0, "unlisted" => 0, "private" => 0, "direct" => 0}
+
Activity
- |> where(
+ |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
+ |> where([a], like(a.actor, ^"%#{instance}%"))
+ |> select(
+ [a],
+ {fragment(
+ "activity_visibility(?, ?, ?)",
+ a.actor,
+ a.recipients,
+ a.data
+ ), count(a.id)}
+ )
+ |> group_by(
[a],
fragment(
- "activity_visibility(?, ?, ?) = ?",
+ "activity_visibility(?, ?, ?)",
a.actor,
a.recipients,
- a.data,
- ^visibility
+ a.data
)
)
- |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
- |> Repo.aggregate(:count, :id, timeout: :timer.minutes(30))
+ |> Repo.all(timeout: :timer.minutes(30))
+ |> Enum.reduce(counters, fn {visibility, count}, acc ->
+ Map.put(acc, visibility, count)
+ end)
end
end
diff --git a/lib/pleroma/counter_cache.ex b/lib/pleroma/counter_cache.ex
index 4d348a413..b469e7b50 100644
--- a/lib/pleroma/counter_cache.ex
+++ b/lib/pleroma/counter_cache.ex
@@ -1,41 +1,79 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.CounterCache do
alias Pleroma.CounterCache
alias Pleroma.Repo
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
schema "counter_cache" do
- field(:name, :string)
- field(:count, :integer)
+ field(:instance, :string)
+ field(:public, :integer)
+ field(:unlisted, :integer)
+ field(:private, :integer)
+ field(:direct, :integer)
end
def changeset(struct, params) do
struct
- |> cast(params, [:name, :count])
- |> validate_required([:name])
- |> unique_constraint(:name)
+ |> cast(params, [:instance, :public, :unlisted, :private, :direct])
+ |> validate_required([:instance])
+ |> unique_constraint(:instance)
end
- def get_as_map(names) when is_list(names) do
+ def get_by_instance(instance) do
CounterCache
- |> where([cc], cc.name in ^names)
- |> Repo.all()
- |> Enum.group_by(& &1.name, & &1.count)
- |> Map.new(fn {k, v} -> {k, hd(v)} end)
+ |> select([c], %{
+ "public" => c.public,
+ "unlisted" => c.unlisted,
+ "private" => c.private,
+ "direct" => c.direct
+ })
+ |> where([c], c.instance == ^instance)
+ |> Repo.one()
+ |> case do
+ nil -> %{"public" => 0, "unlisted" => 0, "private" => 0, "direct" => 0}
+ val -> val
+ end
end
- def set(name, count) do
+ def get_as_map() do
+ CounterCache
+ |> select([c], %{
+ "public" => sum(c.public),
+ "unlisted" => sum(c.unlisted),
+ "private" => sum(c.private),
+ "direct" => sum(c.direct)
+ })
+ |> Repo.one()
+ end
+
+ def set(instance, values) do
+ params =
+ Enum.reduce(
+ ["public", "private", "unlisted", "direct"],
+ %{"instance" => instance},
+ fn param, acc ->
+ Map.put_new(acc, param, Map.get(values, param, 0))
+ end
+ )
+
%CounterCache{}
- |> changeset(%{"name" => name, "count" => count})
+ |> changeset(params)
|> Repo.insert(
- on_conflict: [set: [count: count]],
+ on_conflict: [
+ set: [
+ public: params["public"],
+ private: params["private"],
+ unlisted: params["unlisted"],
+ direct: params["direct"]
+ ]
+ ],
returning: true,
- conflict_target: :name
+ conflict_target: :instance
)
end
end
diff --git a/lib/pleroma/stats.ex b/lib/pleroma/stats.ex
index 6b3a8a41f..4e355bd5c 100644
--- a/lib/pleroma/stats.ex
+++ b/lib/pleroma/stats.ex
@@ -1,116 +1,110 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Stats do
import Ecto.Query
alias Pleroma.CounterCache
alias Pleroma.Repo
alias Pleroma.User
use GenServer
def start_link(_) do
GenServer.start_link(
__MODULE__,
nil,
name: __MODULE__
)
end
@doc "Performs update stats"
def force_update do
GenServer.call(__MODULE__, :force_update)
end
@doc "Performs collect stats"
def do_collect do
GenServer.cast(__MODULE__, :run_update)
end
@doc "Returns stats data"
@spec get_stats() :: %{domain_count: integer(), status_count: integer(), user_count: integer()}
def get_stats do
%{stats: stats} = GenServer.call(__MODULE__, :get_state)
stats
end
@doc "Returns list peers"
@spec get_peers() :: list(String.t())
def get_peers do
%{peers: peers} = GenServer.call(__MODULE__, :get_state)
peers
end
def init(_args) do
{:ok, calculate_stat_data()}
end
def handle_call(:force_update, _from, _state) do
new_stats = calculate_stat_data()
{:reply, new_stats, new_stats}
end
def handle_call(:get_state, _from, state) do
{:reply, state, state}
end
def handle_cast(:run_update, _state) do
new_stats = calculate_stat_data()
{:noreply, new_stats}
end
def calculate_stat_data do
peers =
from(
u in User,
select: fragment("distinct split_part(?, '@', 2)", u.nickname),
where: u.local != ^true
)
|> Repo.all()
|> Enum.filter(& &1)
domain_count = Enum.count(peers)
status_count = Repo.aggregate(User.Query.build(%{local: true}), :sum, :note_count)
users_query =
from(u in User,
where: u.deactivated != true,
where: u.local == true,
where: not is_nil(u.nickname),
where: not u.invisible
)
user_count = Repo.aggregate(users_query, :count, :id)
%{
peers: peers,
stats: %{
domain_count: domain_count,
status_count: status_count || 0,
user_count: user_count
}
}
end
def get_status_visibility_count do
- counter_cache =
- CounterCache.get_as_map([
- "status_visibility_public",
- "status_visibility_private",
- "status_visibility_unlisted",
- "status_visibility_direct"
- ])
+ counter_cache = CounterCache.get_as_map()
%{
public: counter_cache["status_visibility_public"] || 0,
unlisted: counter_cache["status_visibility_unlisted"] || 0,
private: counter_cache["status_visibility_private"] || 0,
direct: counter_cache["status_visibility_direct"] || 0
}
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Aug 8, 1:46 PM (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1723544
Default Alt Text
(7 KB)
Attached To
Mode
rPUBE pleroma-upstream
Attached
Detach File
Event Timeline
Log In to Comment