about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-08-18 14:55:03 +0200
committerGitHub <noreply@github.com>2019-08-18 14:55:03 +0200
commit96702e7f6727d9c688b2c6e2527c4a5bfefff886 (patch)
tree3e682e173d7bca9b3f610e01c31352214358a355 /lib
parent8a555534ec0fbee3e1b842ca84f9a1c6e1160c5d (diff)
Add `tootctl cache recount` command (#11597)
Diffstat (limited to 'lib')
-rw-r--r--lib/mastodon/cache_cli.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/mastodon/cache_cli.rb b/lib/mastodon/cache_cli.rb
index e9b6667b3..5b0eea91b 100644
--- a/lib/mastodon/cache_cli.rb
+++ b/lib/mastodon/cache_cli.rb
@@ -15,5 +15,50 @@ module Mastodon
       Rails.cache.clear
       say('OK', :green)
     end
+
+    desc 'recount TYPE', 'Update hard-cached counters'
+    long_desc <<~LONG_DESC
+      Update hard-cached counters of TYPE by counting referenced
+      records from scratch. TYPE can be "accounts" or "statuses".
+
+      It may take a very long time to finish, depending on the
+      size of the database.
+    LONG_DESC
+    def recount(type)
+      processed = 0
+
+      case type
+      when 'accounts'
+        Account.local.includes(:account_stat).find_each do |account|
+          account_stat                 = account.account_stat
+          account_stat.following_count = account.active_relationships.count
+          account_stat.followers_count = account.passive_relationships.count
+          account_stat.statuses_count  = account.statuses.where.not(visibility: :direct).count
+
+          account_stat.save if account_stat.changed?
+
+          processed += 1
+          say('.', :green, false)
+        end
+      when 'statuses'
+        Status.includes(:status_stat).find_each do |status|
+          status_stat                  = status.status_stat
+          status_stat.replies_count    = status.replies.where.not(visibility: :direct).count
+          status_stat.reblogs_count    = status.reblogs.count
+          status_stat.favourites_count = status.favourites.count
+
+          status_stat.save if status_stat.changed?
+
+          processed += 1
+          say('.', :green, false)
+        end
+      else
+        say("Unknown type: #{type}", :red)
+        exit(1)
+      end
+
+      say
+      say("OK, recounted #{processed} records", :green)
+    end
   end
 end