2019-05-04 01:02:57 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-05-24 11:55:40 +02:00
|
|
|
require_relative 'base'
|
2019-05-04 01:02:57 +02:00
|
|
|
|
2023-05-23 16:08:26 +02:00
|
|
|
module Mastodon::CLI
|
2023-05-24 11:55:40 +02:00
|
|
|
class Cache < Base
|
2019-05-04 01:02:57 +02:00
|
|
|
desc 'clear', 'Clear out the cache storage'
|
|
|
|
def clear
|
|
|
|
Rails.cache.clear
|
|
|
|
say('OK', :green)
|
|
|
|
end
|
2019-08-18 14:55:03 +02:00
|
|
|
|
2019-09-10 13:48:48 +02:00
|
|
|
option :concurrency, type: :numeric, default: 5, aliases: [:c]
|
|
|
|
option :verbose, type: :boolean, aliases: [:v]
|
2019-08-18 14:55:03 +02:00
|
|
|
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)
|
|
|
|
case type
|
|
|
|
when 'accounts'
|
2023-06-10 18:36:09 +02:00
|
|
|
processed, = parallelize_with_progress(accounts_with_stats) do |account|
|
|
|
|
recount_account_stats(account)
|
2019-08-18 14:55:03 +02:00
|
|
|
end
|
|
|
|
when 'statuses'
|
2023-06-10 18:36:09 +02:00
|
|
|
processed, = parallelize_with_progress(statuses_with_stats) do |status|
|
|
|
|
recount_status_stats(status)
|
2019-08-18 14:55:03 +02:00
|
|
|
end
|
|
|
|
else
|
2024-01-26 09:53:44 +01:00
|
|
|
fail_with_message "Unknown type: #{type}"
|
2019-08-18 14:55:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
say
|
|
|
|
say("OK, recounted #{processed} records", :green)
|
|
|
|
end
|
2023-06-10 18:36:09 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def accounts_with_stats
|
|
|
|
Account.local.includes(:account_stat)
|
|
|
|
end
|
|
|
|
|
|
|
|
def statuses_with_stats
|
|
|
|
Status.includes(:status_stat)
|
|
|
|
end
|
|
|
|
|
|
|
|
def recount_account_stats(account)
|
|
|
|
account.account_stat.tap do |account_stat|
|
|
|
|
account_stat.following_count = account.active_relationships.count
|
|
|
|
account_stat.followers_count = account.passive_relationships.count
|
2024-11-07 11:32:33 +01:00
|
|
|
account_stat.statuses_count = account.statuses.not_direct_visibility.count
|
2023-06-10 18:36:09 +02:00
|
|
|
|
|
|
|
account_stat.save if account_stat.changed?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def recount_status_stats(status)
|
|
|
|
status.status_stat.tap do |status_stat|
|
2024-11-07 11:32:33 +01:00
|
|
|
status_stat.replies_count = status.replies.not_direct_visibility.count
|
2023-06-10 18:36:09 +02:00
|
|
|
status_stat.reblogs_count = status.reblogs.count
|
|
|
|
status_stat.favourites_count = status.favourites.count
|
|
|
|
|
|
|
|
status_stat.save if status_stat.changed?
|
|
|
|
end
|
|
|
|
end
|
2019-05-04 01:02:57 +02:00
|
|
|
end
|
|
|
|
end
|