diff options
| author | Roman Chvanikov <chvanikoff@pm.me> | 2020-05-09 01:20:50 +0300 | 
|---|---|---|
| committer | Roman Chvanikov <chvanikoff@pm.me> | 2020-05-09 01:20:50 +0300 | 
| commit | 39d2f2118aed7906cb352d8a37f22da73f3a3aa3 (patch) | |
| tree | f8f17f78d6ef5f6af14b857f991e199ffed92a4e /lib/mix | |
| parent | 0c2b09a9ba771b3b04a0a08ed940823bd8601a9f (diff) | |
| download | pleroma-39d2f2118aed7906cb352d8a37f22da73f3a3aa3.tar.gz pleroma-39d2f2118aed7906cb352d8a37f22da73f3a3aa3.zip | |
update counter_cache logic
Diffstat (limited to 'lib/mix')
| -rw-r--r-- | lib/mix/tasks/pleroma/refresh_counter_cache.ex | 42 | 
1 files changed, 29 insertions, 13 deletions
| 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 @@ -17,30 +17,46 @@ defmodule Mix.Tasks.Pleroma.RefreshCounterCache do    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 | 
