From 6c4b92f7f6bd2799b6b2c4864a8d6ea0bec4ee82 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Fri, 27 Oct 2023 10:45:48 -0700 Subject: [PATCH] fix: Revert the changes for SyncCustomFilterCountJob (#8238) --- ...custom_filters_records_count_update_job.rb | 9 -------- app/jobs/sync_custom_filter_count_job.rb | 9 -------- app/models/custom_filter.rb | 23 ------------------- .../v1/models/_custom_filter.json.jbuilder | 1 - lib/redis/redis_keys.rb | 1 - .../custom_filters_controller_spec.rb | 16 ------------- 6 files changed, 59 deletions(-) delete mode 100644 app/jobs/custom_filters_records_count_update_job.rb delete mode 100644 app/jobs/sync_custom_filter_count_job.rb diff --git a/app/jobs/custom_filters_records_count_update_job.rb b/app/jobs/custom_filters_records_count_update_job.rb deleted file mode 100644 index 49b26a396..000000000 --- a/app/jobs/custom_filters_records_count_update_job.rb +++ /dev/null @@ -1,9 +0,0 @@ -class CustomFiltersRecordsCountUpdateJob < ApplicationJob - queue_as :low - - def perform - CustomFilter.find_each(batch_size: 25) do |filter| - SyncCustomFilterCountJob.perform_later(filter) - end - end -end diff --git a/app/jobs/sync_custom_filter_count_job.rb b/app/jobs/sync_custom_filter_count_job.rb deleted file mode 100644 index f89c196d6..000000000 --- a/app/jobs/sync_custom_filter_count_job.rb +++ /dev/null @@ -1,9 +0,0 @@ -class SyncCustomFilterCountJob < ApplicationJob - queue_as :low - - def perform(filter) - Redis::Alfred.set(filter.filter_count_key, 0) if filter.filter_records.nil? - - filter.set_record_count_in_redis - end -end diff --git a/app/models/custom_filter.rb b/app/models/custom_filter.rb index 2089d42c3..b3d58fd17 100644 --- a/app/models/custom_filter.rb +++ b/app/models/custom_filter.rb @@ -24,29 +24,6 @@ class CustomFilter < ApplicationRecord enum filter_type: { conversation: 0, contact: 1, report: 2 } validate :validate_number_of_filters - def records_count - fetch_record_count_from_redis - end - - def filter_records - Conversations::FilterService.new(query.with_indifferent_access, user, account).perform - end - - def set_record_count_in_redis - records = filter_records - Redis::Alfred.set(filter_count_key, records[:count][:all_count]) - end - - def fetch_record_count_from_redis - number_of_records = Redis::Alfred.get(filter_count_key) - SyncCustomFilterCountJob.perform_later(self) if number_of_records.nil? - number_of_records.to_i - end - - def filter_count_key - format(::Redis::Alfred::CUSTOM_FILTER_RECORDS_COUNT_KEY, account_id: account_id, filter_id: id, user_id: user_id) - end - def validate_number_of_filters return true if account.custom_filters.where(user_id: user_id).size < MAX_FILTER_PER_USER diff --git a/app/views/api/v1/models/_custom_filter.json.jbuilder b/app/views/api/v1/models/_custom_filter.json.jbuilder index c1c032e85..473f2d2c2 100644 --- a/app/views/api/v1/models/_custom_filter.json.jbuilder +++ b/app/views/api/v1/models/_custom_filter.json.jbuilder @@ -4,4 +4,3 @@ json.filter_type resource.filter_type json.query resource.query json.created_at resource.created_at json.updated_at resource.updated_at -json.count resource.records_count diff --git a/lib/redis/redis_keys.rb b/lib/redis/redis_keys.rb index 8c5b92548..01c1fc0b5 100644 --- a/lib/redis/redis_keys.rb +++ b/lib/redis/redis_keys.rb @@ -31,7 +31,6 @@ module Redis::RedisKeys LATEST_CHATWOOT_VERSION = 'LATEST_CHATWOOT_VERSION'.freeze # Check if a message create with same source-id is in progress? MESSAGE_SOURCE_KEY = 'MESSAGE_SOURCE_KEY::%s'.freeze - CUSTOM_FILTER_RECORDS_COUNT_KEY = 'CUSTOM_FILTER::%d::%d::%d'.freeze OPENAI_CONVERSATION_KEY = 'OPEN_AI_CONVERSATION_KEY::%s::%d::%d'.freeze ## Sempahores / Locks diff --git a/spec/controllers/api/v1/accounts/custom_filters_controller_spec.rb b/spec/controllers/api/v1/accounts/custom_filters_controller_spec.rb index 0f40cd789..760100cf3 100644 --- a/spec/controllers/api/v1/accounts/custom_filters_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/custom_filters_controller_spec.rb @@ -41,17 +41,6 @@ RSpec.describe 'Custom Filters API', type: :request do expect(response_body.first['name']).to eq(custom_filter.name) expect(response_body.first['query']).to eq(custom_filter.query) end - - it 'returns custom_filter conversations count when set in redis' do - get "/api/v1/accounts/#{account.id}/custom_filters", - headers: user.create_new_auth_token, - as: :json - - expect(response).to have_http_status(:success) - response_body = response.parsed_body - expect(response_body.first['name']).to eq(custom_filter.name) - expect(response_body.first['count']).to eq(custom_filter.fetch_record_count_from_redis) - end end end @@ -66,16 +55,12 @@ RSpec.describe 'Custom Filters API', type: :request do context 'when it is an authenticated user' do it 'shows the custom filter' do - custom_filter.set_record_count_in_redis - get "/api/v1/accounts/#{account.id}/custom_filters/#{custom_filter.id}", headers: user.create_new_auth_token, as: :json expect(response).to have_http_status(:success) expect(response.body).to include(custom_filter.name) - json_response = response.parsed_body - expect(json_response['count']).to eq 1 end end end @@ -106,7 +91,6 @@ RSpec.describe 'Custom Filters API', type: :request do expect(response).to have_http_status(:success) json_response = response.parsed_body expect(json_response['name']).to eq 'vip-customers' - expect(json_response['count']).to be_zero end it 'gives the error for 51st record' do