aboutsummaryrefslogtreecommitdiff
path: root/lib/mix/tasks
diff options
context:
space:
mode:
authorHaelwenn <contact+git.pleroma.social@hacktivis.me>2020-02-24 19:21:29 +0000
committerHaelwenn <contact+git.pleroma.social@hacktivis.me>2020-02-24 19:21:29 +0000
commit908b6ee49a049e60d705e7f923e71326a965f360 (patch)
treeedfad3b8ea92b2037ef737a97acaa87d2ee00a34 /lib/mix/tasks
parentd240ce41b55a557366746fb7e313d3810bd8fe16 (diff)
parent7ad5c51f23102e89c491a2ef731e108873a09d71 (diff)
downloadpleroma-908b6ee49a049e60d705e7f923e71326a965f360.tar.gz
Merge branch 'status-visibility-count-stats' into 'develop'
Status count by visibility scope (`GET /api/pleroma/admin/stats` ) See merge request pleroma/pleroma!2097
Diffstat (limited to 'lib/mix/tasks')
-rw-r--r--lib/mix/tasks/pleroma/refresh_counter_cache.ex46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/mix/tasks/pleroma/refresh_counter_cache.ex b/lib/mix/tasks/pleroma/refresh_counter_cache.ex
new file mode 100644
index 000000000..bc2571efd
--- /dev/null
+++ b/lib/mix/tasks/pleroma/refresh_counter_cache.ex
@@ -0,0 +1,46 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 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}")
+ end)
+
+ Mix.Pleroma.shell_info("Done")
+ end
+
+ defp status_visibility_count_query(visibility) do
+ Activity
+ |> where(
+ [a],
+ fragment(
+ "activity_visibility(?, ?, ?) = ?",
+ a.actor,
+ a.recipients,
+ a.data,
+ ^visibility
+ )
+ )
+ |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
+ |> Repo.aggregate(:count, :id, timeout: :timer.minutes(30))
+ end
+end