mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 03:57:52 +00:00
perf: Contact optimisation fixes (#12016)
- Avoids the duplicate count queries for contact end point Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
committed by
GitHub
parent
cd2c58726f
commit
59f7c8aa55
@@ -17,8 +17,8 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
|
|||||||
before_action :set_include_contact_inboxes, only: [:index, :active, :search, :filter, :show, :update]
|
before_action :set_include_contact_inboxes, only: [:index, :active, :search, :filter, :show, :update]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@contacts_count = resolved_contacts.count
|
|
||||||
@contacts = fetch_contacts(resolved_contacts)
|
@contacts = fetch_contacts(resolved_contacts)
|
||||||
|
@contacts_count = @contacts.total_count
|
||||||
end
|
end
|
||||||
|
|
||||||
def search
|
def search
|
||||||
@@ -29,8 +29,8 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
|
|||||||
OR contacts.additional_attributes->>\'company_name\' ILIKE :search',
|
OR contacts.additional_attributes->>\'company_name\' ILIKE :search',
|
||||||
search: "%#{params[:q].strip}%"
|
search: "%#{params[:q].strip}%"
|
||||||
)
|
)
|
||||||
@contacts_count = contacts.count
|
|
||||||
@contacts = fetch_contacts(contacts)
|
@contacts = fetch_contacts(contacts)
|
||||||
|
@contacts_count = @contacts.total_count
|
||||||
end
|
end
|
||||||
|
|
||||||
def import
|
def import
|
||||||
@@ -55,8 +55,8 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
|
|||||||
def active
|
def active
|
||||||
contacts = Current.account.contacts.where(id: ::OnlineStatusTracker
|
contacts = Current.account.contacts.where(id: ::OnlineStatusTracker
|
||||||
.get_available_contact_ids(Current.account.id))
|
.get_available_contact_ids(Current.account.id))
|
||||||
@contacts_count = contacts.count
|
|
||||||
@contacts = fetch_contacts(contacts)
|
@contacts = fetch_contacts(contacts)
|
||||||
|
@contacts_count = @contacts.total_count
|
||||||
end
|
end
|
||||||
|
|
||||||
def show; end
|
def show; end
|
||||||
@@ -133,13 +133,14 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def fetch_contacts(contacts)
|
def fetch_contacts(contacts)
|
||||||
contacts_with_avatar = filtrate(contacts)
|
# Build includes hash to avoid separate query when contact_inboxes are needed
|
||||||
.includes([{ avatar_attachment: [:blob] }])
|
includes_hash = { avatar_attachment: [:blob] }
|
||||||
.page(@current_page).per(RESULTS_PER_PAGE)
|
includes_hash[:contact_inboxes] = { inbox: :channel } if @include_contact_inboxes
|
||||||
|
|
||||||
return contacts_with_avatar.includes([{ contact_inboxes: [:inbox] }]) if @include_contact_inboxes
|
filtrate(contacts)
|
||||||
|
.includes(includes_hash)
|
||||||
contacts_with_avatar
|
.page(@current_page)
|
||||||
|
.per(RESULTS_PER_PAGE)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_contact_inbox
|
def build_contact_inbox
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ end
|
|||||||
|
|
||||||
json.payload do
|
json.payload do
|
||||||
json.array! @contacts do |contact|
|
json.array! @contacts do |contact|
|
||||||
json.partial! 'api/v1/models/contact', formats: [:json], resource: contact, with_contact_inboxes: true
|
json.partial! 'api/v1/models/contact', formats: [:json], resource: contact, with_contact_inboxes: @include_contact_inboxes
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user