tags: automatically fix incorrect tag counts during maintenance.

* Automatically fix all tags with incorrect counts during daily
  maintenance (previously only tags with negative counts were fixed).
* Log fixed tags to NewRelic.
* Remove the ability to manually fix tag counts with the "Fix" button on
  the /tags listing. This is no longer necessary now that tags are
  fixed automatically.
This commit is contained in:
evazion
2019-09-25 17:33:15 -05:00
parent 03d9b3feca
commit bc34fb16a4
12 changed files with 36 additions and 89 deletions

View File

@@ -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

View File

@@ -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