From 0e209046aa964ab4379399e1417ac5d6841e736e Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Tue, 6 Dec 2022 16:18:32 +0000 Subject: [PATCH 1/4] Add diagnostics tasks --- lib/mix/tasks/pleroma/diagnostics.ex | 72 ++++++++++++++++++++ lib/pleroma/web/activity_pub/activity_pub.ex | 4 +- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 lib/mix/tasks/pleroma/diagnostics.ex diff --git a/lib/mix/tasks/pleroma/diagnostics.ex b/lib/mix/tasks/pleroma/diagnostics.ex new file mode 100644 index 000000000..fe1f34620 --- /dev/null +++ b/lib/mix/tasks/pleroma/diagnostics.ex @@ -0,0 +1,72 @@ +defmodule Mix.Tasks.Pleroma.Diagnostics do + alias Pleroma.Repo + alias Pleroma.User + + require Logger + require Pleroma.Constants + + import Mix.Pleroma + + use Mix.Task + + def run(["home_timeline", nickname]) do + start_pleroma() + user = Repo.get_by!(User, nickname: nickname) + Logger.info("Home timeline query #{user.nickname}") + + followed_hashtags = + user + |> User.followed_hashtags() + |> Enum.map(& &1.id) + + params = + %{limit: 20} + |> Map.put(:type, ["Create", "Announce"]) + |> Map.put(:blocking_user, user) + |> Map.put(:muting_user, user) + |> Map.put(:reply_filtering_user, user) + |> Map.put(:announce_filtering_user, user) + |> Map.put(:user, user) + |> Map.put(:followed_hashtags, followed_hashtags) + |> Map.delete(:local) + + list_memberships = Pleroma.List.memberships(user) + recipients = [user.ap_id | User.following(user)] + + query = + Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query( + recipients ++ list_memberships, + params + ) + + Ecto.Adapters.SQL.explain(Repo, :all, query, analyze: true, timeout: :infinity) + |> IO.puts() + end + + def run(["user_timeline", nickname, reading_nickname]) do + start_pleroma() + user = Repo.get_by!(User, nickname: nickname) + reading_user = Repo.get_by!(User, nickname: reading_nickname) + Logger.info("User timeline query #{user.nickname}") + + params = + %{ limit: 20 } + |> Map.put(:type, ["Create", "Announce"]) + |> Map.put(:user, reading_user) + |> Map.put(:actor_id, user.ap_id) + |> Map.put(:pinned_object_ids, Map.keys(user.pinned_objects)) + + list_memberships = Pleroma.List.memberships(user) + + query = + %{ + godmode: params[:godmode], + reading_user: reading_user + } + |> Pleroma.Web.ActivityPub.ActivityPub.user_activities_recipients() + |> Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query(params) + + Ecto.Adapters.SQL.explain(Repo, :all, query, analyze: true, timeout: :infinity) + |> IO.puts() + end +end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 3f46a8ecb..d700128c0 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -739,9 +739,9 @@ defp fetch_activities_for_reading_user(reading_user, params) do |> fetch_activities(params, :offset) end - defp user_activities_recipients(%{godmode: true}), do: [] + def user_activities_recipients(%{godmode: true}), do: [] - defp user_activities_recipients(%{reading_user: reading_user}) do + def user_activities_recipients(%{reading_user: reading_user}) do if not is_nil(reading_user) and reading_user.local do [ Constants.as_public(), -- 2.43.0 From 3a875fc95293649bf71eff9bddc21a915aae282d Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Tue, 6 Dec 2022 16:21:43 +0000 Subject: [PATCH 2/4] add limits --- lib/mix/tasks/pleroma/diagnostics.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/mix/tasks/pleroma/diagnostics.ex b/lib/mix/tasks/pleroma/diagnostics.ex index fe1f34620..0036c3fe2 100644 --- a/lib/mix/tasks/pleroma/diagnostics.ex +++ b/lib/mix/tasks/pleroma/diagnostics.ex @@ -6,7 +6,7 @@ defmodule Mix.Tasks.Pleroma.Diagnostics do require Pleroma.Constants import Mix.Pleroma - + import Ecto.Query use Mix.Task def run(["home_timeline", nickname]) do @@ -38,6 +38,7 @@ def run(["home_timeline", nickname]) do recipients ++ list_memberships, params ) + |> limit(20) Ecto.Adapters.SQL.explain(Repo, :all, query, analyze: true, timeout: :infinity) |> IO.puts() @@ -65,6 +66,7 @@ def run(["user_timeline", nickname, reading_nickname]) do } |> Pleroma.Web.ActivityPub.ActivityPub.user_activities_recipients() |> Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query(params) + |> limit(20) Ecto.Adapters.SQL.explain(Repo, :all, query, analyze: true, timeout: :infinity) |> IO.puts() -- 2.43.0 From 9b2e6eab213cf952c3809a54496f6eef0915748b Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Tue, 6 Dec 2022 16:23:17 +0000 Subject: [PATCH 3/4] use list_recipients --- lib/mix/tasks/pleroma/diagnostics.ex | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/mix/tasks/pleroma/diagnostics.ex b/lib/mix/tasks/pleroma/diagnostics.ex index 0036c3fe2..6e83bf6f0 100644 --- a/lib/mix/tasks/pleroma/diagnostics.ex +++ b/lib/mix/tasks/pleroma/diagnostics.ex @@ -51,7 +51,7 @@ def run(["user_timeline", nickname, reading_nickname]) do Logger.info("User timeline query #{user.nickname}") params = - %{ limit: 20 } + %{limit: 20} |> Map.put(:type, ["Create", "Announce"]) |> Map.put(:user, reading_user) |> Map.put(:actor_id, user.ap_id) @@ -59,12 +59,15 @@ def run(["user_timeline", nickname, reading_nickname]) do list_memberships = Pleroma.List.memberships(user) - query = + recipients = %{ godmode: params[:godmode], reading_user: reading_user } |> Pleroma.Web.ActivityPub.ActivityPub.user_activities_recipients() + + query = + (recipients ++ list_memberships) |> Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query(params) |> limit(20) -- 2.43.0 From 85d4ea6f20ffb77083d9d29692f99f0f03c43f83 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Wed, 7 Dec 2022 11:12:18 +0000 Subject: [PATCH 4/4] Add docs for diagnostics --- .../administration/CLI_tasks/diagnostics.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 docs/docs/administration/CLI_tasks/diagnostics.md diff --git a/docs/docs/administration/CLI_tasks/diagnostics.md b/docs/docs/administration/CLI_tasks/diagnostics.md new file mode 100644 index 000000000..25572da8a --- /dev/null +++ b/docs/docs/administration/CLI_tasks/diagnostics.md @@ -0,0 +1,30 @@ +# Diagnostics + +A few tasks to help with debugging, troubleshooting, and diagnosing problems. + +They mostly relate to common postgres queries. + +## Home timeline query plan + +This task will print a query plan for the home timeline of a given user. + +=== "OTP" + + `./bin/pleroma_ctl diagnostics home_timeline ` + +=== "From Source" + + `mix pleroma.diagnostics home_timeline ` + +## User timeline query plan + +This task will print a query plan for the user timeline of a given user, +from the perspective of another given user. + +=== "OTP" + + `./bin/pleroma_ctl diagnostics user_timeline ` + +=== "From Source" + + `mix pleroma.diagnostics user_timeline ` \ No newline at end of file -- 2.43.0