Chore: Fix N+1 queries in dashboard side (#1254)

* Chore: Fix N+1 queries in dashboard side

Fixed a couple of N+1 queries fired on the dashboard side of
the app to improve performance.
This commit is contained in:
Sony Mathew 2020-09-19 12:46:34 +05:30 committed by GitHub
parent 74d07c876e
commit fc7b84d612
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 6 additions and 6 deletions

View file

@ -1,7 +1,7 @@
class Api::V1::Accounts::Contacts::ConversationsController < Api::V1::Accounts::BaseController class Api::V1::Accounts::Contacts::ConversationsController < Api::V1::Accounts::BaseController
def index def index
@conversations = Current.account.conversations.includes( @conversations = Current.account.conversations.includes(
:assignee, :contact, :inbox :assignee, :contact, :inbox, :taggings
).where(inbox_id: inbox_ids, contact_id: permitted_params[:contact_id]) ).where(inbox_id: inbox_ids, contact_id: permitted_params[:contact_id])
end end

View file

@ -63,6 +63,6 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
end end
def fetch_contact def fetch_contact
@contact = Current.account.contacts.find(params[:id]) @contact = Current.account.contacts.includes(contact_inboxes: [:inbox]).find(params[:id])
end end
end end

View file

@ -62,7 +62,7 @@ class ConversationFinder
def find_all_conversations def find_all_conversations
@conversations = current_account.conversations.includes( @conversations = current_account.conversations.includes(
:assignee, :inbox, contact: [:avatar_attachment] :assignee, :inbox, :taggings, contact: [:avatar_attachment]
).where(inbox_id: @inbox_ids) ).where(inbox_id: @inbox_ids)
end end

View file

@ -11,7 +11,7 @@ class MessageFinder
private private
def conversation_messages def conversation_messages
@conversation.messages.includes(:attachments, user: { avatar_attachment: :blob }) @conversation.messages.includes(:attachments, :sender)
end end
def messages def messages

View file

@ -12,9 +12,9 @@ end
json.id conversation.display_id json.id conversation.display_id
if conversation.unread_incoming_messages.count.zero? if conversation.unread_incoming_messages.count.zero?
json.messages [conversation.messages.last.try(:push_event_data)] json.messages [conversation.messages.includes([{ attachments: [{ file_attachment: [:blob] }] }]).last.try(:push_event_data)]
else else
json.messages conversation.unread_messages.includes([:user, :attachments]).map(&:push_event_data) json.messages conversation.unread_messages.includes([:user, { attachments: [{ file_attachment: [:blob] }] }]).map(&:push_event_data)
end end
json.inbox_id conversation.inbox_id json.inbox_id conversation.inbox_id