diff --git a/app/controllers/tag_corrections_controller.rb b/app/controllers/tag_corrections_controller.rb deleted file mode 100644 index 21b9c82e6..000000000 --- a/app/controllers/tag_corrections_controller.rb +++ /dev/null @@ -1,25 +0,0 @@ -class TagCorrectionsController < ApplicationController - respond_to :html, :json, :xml - before_action :builder_only, only: [:new, :create] - - def new - @correction = TagCorrection.new(params[:tag_id]) - respond_with(@correction) - end - - def show - @correction = TagCorrection.new(params[:tag_id]) - respond_with(@correction) - end - - def create - @correction = TagCorrection.new(params[:tag_id]) - - if params[:commit] == "Fix" - @correction.fix! - redirect_to tags_path(:search => {:name_matches => @correction.tag.name, :hide_empty => "no"}), :notice => "Tag will be fixed in a few seconds" - else - redirect_to tags_path(:search => {:name_matches => @correction.tag.name}) - end - end -end diff --git a/app/jobs/fix_tag_post_count_job.rb b/app/jobs/fix_tag_post_count_job.rb deleted file mode 100644 index 3f6ab4ad7..000000000 --- a/app/jobs/fix_tag_post_count_job.rb +++ /dev/null @@ -1,8 +0,0 @@ -class FixTagPostCountJob < ApplicationJob - queue_as :default - queue_with_priority 20 - - def perform(tag) - tag.fix_post_count - end -end diff --git a/app/logical/danbooru_maintenance.rb b/app/logical/danbooru_maintenance.rb index 5ab771b2e..4a6e62232 100644 --- a/app/logical/danbooru_maintenance.rb +++ b/app/logical/danbooru_maintenance.rb @@ -15,7 +15,7 @@ module DanbooruMaintenance ForumSubscription.process_all! TagAlias.update_cached_post_counts_for_all PostDisapproval.dmail_messages! - Tag.clean_up_negative_post_counts! + regenerate_post_counts! SuperVoter.init! TokenBucket.prune! TagChangeRequestPruner.warn_all @@ -38,6 +38,13 @@ module DanbooruMaintenance rescue_exception(exception) end + def regenerate_post_counts! + updated_tags = Tag.regenerate_post_counts! + updated_tags.each do |tag| + DanbooruLogger.info("Updated tag count", tag.attributes) + end + end + def rescue_exception(exception) DanbooruLogger.log(exception) raise exception diff --git a/app/logical/tag_correction.rb b/app/logical/tag_correction.rb index 6b326a89e..343075ad0 100644 --- a/app/logical/tag_correction.rb +++ b/app/logical/tag_correction.rb @@ -4,14 +4,14 @@ class TagCorrection include ActiveModel::Serializers::Xml attr_reader :tag - delegate :category, :post_count, :real_post_count, to: :tag + delegate :category, :post_count, to: :tag def initialize(tag_id) @tag = Tag.find(tag_id) end def attributes - { post_count: post_count, real_post_count: real_post_count, category: category, category_cache: category_cache, tag: tag } + { post_count: post_count, category: category, category_cache: category_cache, tag: tag } end def category_cache @@ -19,7 +19,6 @@ class TagCorrection end def fix! - FixTagPostCountJob.perform_later(tag) tag.update_category_cache end end diff --git a/app/models/tag.rb b/app/models/tag.rb index bbe47aea9..db3ebd243 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -99,24 +99,26 @@ class Tag < ApplicationRecord Tag.where(:name => tag_names).update_all("post_count = post_count - 1") end - def clean_up_negative_post_counts! - Tag.where("post_count < 0").find_each do |tag| - tag_alias = TagAlias.where("status in ('active', 'processing') and antecedent_name = ?", tag.name).first - tag.fix_post_count - if tag_alias - tag_alias.consequent_tag.fix_post_count - end - end + def regenerate_post_counts! + sql = <<~SQL + UPDATE tags + SET post_count = true_count + FROM ( + SELECT tag, COUNT(*) AS true_count + FROM posts, unnest(string_to_array(tag_string, ' ')) AS tag + GROUP BY tag + ) true_counts, tags AS old_tags + WHERE + tags.name = tag + AND tags.post_count != true_count + AND old_tags.id = tags.id + RETURNING tags.*, old_tags.post_count AS old_post_count + SQL + + updated_tags = Tag.find_by_sql(sql) + updated_tags end end - - def real_post_count - @real_post_count ||= Post.raw_tag_match(name).where("true /* Tag#real_post_count */").count - end - - def fix_post_count - update_column(:post_count, real_post_count) - end end module CategoryMethods diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index ad3fe6b90..d50b9da23 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -145,9 +145,6 @@ class TagAlias < TagRelationship post.update(tag_string: fixed_tags) end end - - antecedent_tag.fix_post_count if antecedent_tag - consequent_tag.fix_post_count if consequent_tag end end diff --git a/app/views/tag_corrections/new.html.erb b/app/views/tag_corrections/new.html.erb deleted file mode 100644 index 1aee04715..000000000 --- a/app/views/tag_corrections/new.html.erb +++ /dev/null @@ -1,19 +0,0 @@ -