From 399df7304da69b0f4840b39bdbc6a418062c9c6d Mon Sep 17 00:00:00 2001 From: Pranav Date: Mon, 28 Oct 2024 23:24:02 -0700 Subject: [PATCH] feat: Cache contacts count on account --- .../api/v1/accounts/contacts_controller.rb | 2 +- .../update_contactable_contacts_count_job.rb | 15 +++++++++++ app/models/account.rb | 25 ++++++++++--------- ...029060838_add_counter_cache_to_accounts.rb | 16 ++++++++++++ db/schema.rb | 3 ++- 5 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 app/jobs/update_contactable_contacts_count_job.rb create mode 100644 db/migrate/20241029060838_add_counter_cache_to_accounts.rb diff --git a/app/controllers/api/v1/accounts/contacts_controller.rb b/app/controllers/api/v1/accounts/contacts_controller.rb index 250c64a86..ae23d4f6f 100644 --- a/app/controllers/api/v1/accounts/contacts_controller.rb +++ b/app/controllers/api/v1/accounts/contacts_controller.rb @@ -17,7 +17,7 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController before_action :set_include_contact_inboxes, only: [:index, :search, :filter] def index - @contacts_count = resolved_contacts.count + @contacts_count = Current.account.contactable_contacts_count @contacts = fetch_contacts(resolved_contacts) end diff --git a/app/jobs/update_contactable_contacts_count_job.rb b/app/jobs/update_contactable_contacts_count_job.rb new file mode 100644 index 000000000..aa5967905 --- /dev/null +++ b/app/jobs/update_contactable_contacts_count_job.rb @@ -0,0 +1,15 @@ +class UpdateContactableContactsCountJob < ApplicationJob + queue_as :default + + def perform(account_id) + account = Account.find_by(id: account_id) + return unless account + + # rubocop:disable Rails/SkipsModelValidations + account.update_column( + :contactable_contacts_count, + account.contacts.contactable.count + ) + # rubocop:enable Rails/SkipsModelValidations + end +end diff --git a/app/models/account.rb b/app/models/account.rb index 419ccc471..4d4032543 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -2,18 +2,19 @@ # # Table name: accounts # -# id :integer not null, primary key -# auto_resolve_duration :integer -# custom_attributes :jsonb -# domain :string(100) -# feature_flags :bigint default(0), not null -# limits :jsonb -# locale :integer default("en") -# name :string not null -# status :integer default("active") -# support_email :string(100) -# created_at :datetime not null -# updated_at :datetime not null +# id :integer not null, primary key +# auto_resolve_duration :integer +# contactable_contacts_count :integer default(0) +# custom_attributes :jsonb +# domain :string(100) +# feature_flags :bigint default(0), not null +# limits :jsonb +# locale :integer default("en") +# name :string not null +# status :integer default("active") +# support_email :string(100) +# created_at :datetime not null +# updated_at :datetime not null # # Indexes # diff --git a/db/migrate/20241029060838_add_counter_cache_to_accounts.rb b/db/migrate/20241029060838_add_counter_cache_to_accounts.rb new file mode 100644 index 000000000..df59ffd79 --- /dev/null +++ b/db/migrate/20241029060838_add_counter_cache_to_accounts.rb @@ -0,0 +1,16 @@ +class AddCounterCacheToAccounts < ActiveRecord::Migration[7.0] + def up + add_column :accounts, :contactable_contacts_count, :integer, default: 0 + + # Initialize the counter for existing records in batches + Account.in_batches(of: 100) do |batch| + batch.ids.each do |account_id| + UpdateContactableContactsCountJob.perform_later(account_id) + end + end + end + + def down + remove_column :accounts, :contactable_contacts_count + end +end diff --git a/db/schema.rb b/db/schema.rb index abb14d1e9..b4c21c8a2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_09_23_215335) do +ActiveRecord::Schema[7.0].define(version: 2024_10_29_060838) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "pg_trgm" @@ -56,6 +56,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_09_23_215335) do t.jsonb "limits", default: {} t.jsonb "custom_attributes", default: {} t.integer "status", default: 0 + t.integer "contactable_contacts_count", default: 0 t.index ["status"], name: "index_accounts_on_status" end