From f33b23d035a7ef1fe0df43baa4c6333d13db01b4 Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Wed, 9 Jan 2019 15:54:55 -0800 Subject: [PATCH] add post count estimates for bulk update requests --- app/logical/alias_and_implication_importer.rb | 22 +++++++++++++++++++ app/logical/moderator/tag_batch_change.rb | 4 ++++ app/models/bulk_update_request.rb | 4 ++++ app/models/tag_relationship.rb | 4 ++++ app/views/bulk_update_requests/show.html.erb | 1 + test/test_helpers/saved_search_test_helper.rb | 18 +-------------- .../alias_and_implication_importer_test.rb | 22 +++++++++++++++++++ test/unit/bulk_update_request_test.rb | 22 +++++++++++++++++++ test/unit/moderator/tag_batch_change_test.rb | 10 +++++++++ test/unit/tag_alias_test.rb | 11 ++++++++++ test/unit/tag_implication_test.rb | 11 ++++++++++ 11 files changed, 112 insertions(+), 17 deletions(-) diff --git a/app/logical/alias_and_implication_importer.rb b/app/logical/alias_and_implication_importer.rb index 86320e9cd..7fe6e5382 100644 --- a/app/logical/alias_and_implication_importer.rb +++ b/app/logical/alias_and_implication_importer.rb @@ -78,6 +78,28 @@ class AliasAndImplicationImporter end end + def estimate_update_count + tokens = self.class.tokenize(text) + tokens.inject(0) do |sum, token| + case token[0] + when :create_alias + sum + TagAlias.new(antecedent_name: token[1], consequent_name: token[2]).estimate_update_count + + when :create_implication + sum + TagImplication.new(antecedent_name: token[1], consequent_name: token[2]).estimate_update_count + + when :mass_update + sum + Moderator::TagBatchChange.new(token[1], token[2]).estimate_update_count + + when :change_category + sum + Tag.find_by_name(token[1]).try(:post_count) || 0 + + else + sum + 0 + end + end + end + private def parse(tokens, approver) diff --git a/app/logical/moderator/tag_batch_change.rb b/app/logical/moderator/tag_batch_change.rb index 3afd088cb..fb982eddb 100644 --- a/app/logical/moderator/tag_batch_change.rb +++ b/app/logical/moderator/tag_batch_change.rb @@ -20,6 +20,10 @@ module Moderator ModAction.log("processed mass update: #{antecedent} -> #{consequent}",:mass_update) end + def estimate_update_count + PostReadOnly.tag_match(antecedent).count + end + def migrate_posts(normalized_antecedent, normalized_consequent) ::Post.tag_match(normalized_antecedent.join(" ")).find_each do |post| post.reload diff --git a/app/models/bulk_update_request.rb b/app/models/bulk_update_request.rb index 81af0ff5e..0d72130e5 100644 --- a/app/models/bulk_update_request.rb +++ b/app/models/bulk_update_request.rb @@ -229,4 +229,8 @@ class BulkUpdateRequest < ApplicationRecord def is_rejected? status == "rejected" end + + def estimate_update_count + AliasAndImplicationImporter.new(script, nil).estimate_update_count + end end diff --git a/app/models/tag_relationship.rb b/app/models/tag_relationship.rb index 233807662..fe310bc5c 100644 --- a/app/models/tag_relationship.rb +++ b/app/models/tag_relationship.rb @@ -192,6 +192,10 @@ class TagRelationship < ApplicationRecord end end + def estimate_update_count + Post.fast_count(antecedent_name, skip_cache: true) + end + extend SearchMethods include MessageMethods end diff --git a/app/views/bulk_update_requests/show.html.erb b/app/views/bulk_update_requests/show.html.erb index 7d4ff758c..bb614213f 100644 --- a/app/views/bulk_update_requests/show.html.erb +++ b/app/views/bulk_update_requests/show.html.erb @@ -9,6 +9,7 @@
  • Creator <%= link_to_user @bulk_update_request.user %>
  • Date <%= @bulk_update_request.created_at %>
  • Status: <%= @bulk_update_request.status %>
  • +
  • Estimate: <%= @bulk_update_request.estimate_update_count %>
  • <% if CurrentUser.is_admin? && @bulk_update_request.is_pending? %>
  • Commands <%= link_to "Approve", approve_bulk_update_request_path(@bulk_update_request), :method => :post %>
  • diff --git a/test/test_helpers/saved_search_test_helper.rb b/test/test_helpers/saved_search_test_helper.rb index f36543852..ff924732a 100644 --- a/test/test_helpers/saved_search_test_helper.rb +++ b/test/test_helpers/saved_search_test_helper.rb @@ -1,21 +1,5 @@ module SavedSearchTestHelper def mock_saved_search_service! - mock_sqs_service = Class.new do - def initialize - @commands = [] - end - - def commands - @commands - end - - def send_message(msg) - @commands << msg.split(/\n/).first - end - end - - service = mock_sqs_service.new - SavedSearch.stubs(:sqs_service).returns(service) - Danbooru.config.stubs(:aws_sqs_saved_search_url).returns("http://localhost:3002") + SavedSearch.stubs(:enabled?).returns(true) end end diff --git a/test/unit/alias_and_implication_importer_test.rb b/test/unit/alias_and_implication_importer_test.rb index b7a371f7b..5690e11db 100644 --- a/test/unit/alias_and_implication_importer_test.rb +++ b/test/unit/alias_and_implication_importer_test.rb @@ -26,6 +26,28 @@ class AliasAndImplicationImporterTest < ActiveSupport::TestCase end end + context "#estimate_update_count" do + setup do + FactoryBot.create(:post, tag_string: "aaa") + FactoryBot.create(:post, tag_string: "bbb") + FactoryBot.create(:post, tag_string: "ccc") + FactoryBot.create(:post, tag_string: "ddd") + FactoryBot.create(:post, tag_string: "eee") + + @script = "create alias aaa -> 000\n" + + "create implication bbb -> 111\n" + + "remove alias ccc -> 222\n" + + "remove implication ddd -> 333\n" + + "mass update eee -> 444\n" + end + + subject { AliasAndImplicationImporter.new(@script, nil) } + + should "return the correct count" do + assert_equal(3, subject.estimate_update_count) + end + end + context "given a valid list" do setup do @list = "create alias abc -> def\ncreate implication aaa -> bbb\n" diff --git a/test/unit/bulk_update_request_test.rb b/test/unit/bulk_update_request_test.rb index 454b7f904..83c226465 100644 --- a/test/unit/bulk_update_request_test.rb +++ b/test/unit/bulk_update_request_test.rb @@ -13,6 +13,28 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + context "#estimate_update_count" do + setup do + FactoryBot.create(:post, tag_string: "aaa") + FactoryBot.create(:post, tag_string: "bbb") + FactoryBot.create(:post, tag_string: "ccc") + FactoryBot.create(:post, tag_string: "ddd") + FactoryBot.create(:post, tag_string: "eee") + + @script = "create alias aaa -> 000\n" + + "create implication bbb -> 111\n" + + "remove alias ccc -> 222\n" + + "remove implication ddd -> 333\n" + + "mass update eee -> 444\n" + end + + subject { BulkUpdateRequest.new(script: @script) } + + should "return the correct count" do + assert_equal(3, subject.estimate_update_count) + end + end + context "on approval" do setup do @script = %q( diff --git a/test/unit/moderator/tag_batch_change_test.rb b/test/unit/moderator/tag_batch_change_test.rb index 57b6bdd0b..141d6e394 100644 --- a/test/unit/moderator/tag_batch_change_test.rb +++ b/test/unit/moderator/tag_batch_change_test.rb @@ -20,6 +20,16 @@ module Moderator CurrentUser.ip_addr = nil end + context "#estimate_update_count" do + setup do + @change = TagBatchChange.new("aaa", "bbb", @user.id, "127.0.0.1") + end + + should "find the correct count" do + assert_equal(1, @change.estimate_update_count) + end + end + should "execute" do tag_batch_change = TagBatchChange.new("aaa", "bbb", @user.id, "127.0.0.1") tag_batch_change.perform diff --git a/test/unit/tag_alias_test.rb b/test/unit/tag_alias_test.rb index 937c0616c..2df827697 100644 --- a/test/unit/tag_alias_test.rb +++ b/test/unit/tag_alias_test.rb @@ -58,6 +58,17 @@ class TagAliasTest < ActiveSupport::TestCase end end + context "#estimate_update_count" do + setup do + FactoryBot.create(:post, tag_string: "aaa bbb ccc") + @alias = FactoryBot.create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", status: "pending") + end + + should "get the right count" do + assert_equal(1, @alias.estimate_update_count) + end + end + context "on secondary validation" do should "warn about missing wiki pages" do ti = FactoryBot.build(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", skip_secondary_validations: false) diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index 0a62450fa..76e474852 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -54,6 +54,17 @@ class TagImplicationTest < ActiveSupport::TestCase end end + context "#estimate_update_count" do + setup do + FactoryBot.create(:post, tag_string: "aaa bbb ccc") + @implication = FactoryBot.create(:tag_implication, antecedent_name: "aaa", consequent_name: "bbb", status: "pending") + end + + should "get the right count" do + assert_equal(1, @implication.estimate_update_count) + end + end + context "on secondary validation" do should "warn if either tag is missing a wiki" do ti = FactoryBot.build(:tag_implication, antecedent_name: "aaa", consequent_name: "bbb", skip_secondary_validations: false)