feat(perf): Cache labels on the conversation model (#8527)

This commit is contained in:
Pranav Raj S
2023-12-11 18:27:55 -08:00
committed by GitHub
parent 79412ba2c6
commit 890515edfd
9 changed files with 48 additions and 53 deletions

View File

@@ -14,7 +14,8 @@ class Api::V1::Accounts::Conversations::AssignmentsController < Api::V1::Account
def set_agent
@agent = Current.account.users.find_by(id: params[:assignee_id])
@conversation.update_assignee(@agent)
@conversation.assignee = @agent
@conversation.save!
render_agent
end

View File

@@ -110,8 +110,8 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro
end
def assign_conversation
@agent = Current.account.users.find(current_user.id)
@conversation.update_assignee(@agent)
@conversation.assignee = current_user
@conversation.save!
end
def conversation

View File

@@ -0,0 +1,16 @@
class Migration::ConversationCacheLabelJob < ApplicationJob
queue_as :async_database_migration
# To cache the label, we simply access it from the object and save it. Anytime the object is
# saved in the future, ActsAsTaggable will automatically recompute it. This process is done
# initially when the user has not performed any action.
# Reference: https://github.com/mbleigh/acts-as-taggable-on/wiki/Caching
def perform(account)
account.conversations.find_in_batches do |conversation_batch|
conversation_batch.each do |conversation|
conversation.label_list
conversation.save!
end
end
end
end

View File

@@ -6,6 +6,7 @@
# additional_attributes :jsonb
# agent_last_seen_at :datetime
# assignee_last_seen_at :datetime
# cached_label_list :string
# contact_last_seen_at :datetime
# custom_attributes :jsonb
# first_reply_created_at :datetime
@@ -144,10 +145,6 @@ class Conversation < ApplicationRecord
end
end
def update_assignee(agent = nil)
update!(assignee: agent)
end
def toggle_status
# FIXME: implement state machine with aasm
self.status = open? ? :resolved : :open
@@ -185,6 +182,10 @@ class Conversation < ApplicationRecord
Conversations::EventDataPresenter.new(self).push_data
end
def cached_label_list_array
(cached_label_list || '').split(',')
end
def notifiable_assignee_change?
return false unless saved_change_to_assignee_id?
return false if assignee_id.blank?

View File

@@ -1,5 +1,5 @@
json.meta do
json.labels @conversation.label_list
json.labels @conversation.cached_label_list_array
json.additional_attributes @conversation.additional_attributes
json.contact @conversation.contact.push_event_data
json.assignee @conversation.assignee.push_event_data if @conversation.assignee.present?

View File

@@ -35,7 +35,7 @@ json.can_reply conversation.can_reply?
json.contact_last_seen_at conversation.contact_last_seen_at.to_i
json.custom_attributes conversation.custom_attributes
json.inbox_id conversation.inbox_id
json.labels conversation.label_list
json.labels conversation.cached_label_list_array
json.muted conversation.muted?
json.snoozed_until conversation.snoozed_until
json.status conversation.status