fix: migration script to run on all reporting events (#6590)

* fix: migration script to run on all reporting events

* fix: don't update user_id if it is already present

* refactor: create a new migration

* feat: update schema

* feat: ignore events with bot handoff

* feat: prefetch conversations with handoff events

* Revert "feat: update schema"

This reverts commit 25ed2856e62655f5f1db14fd0cffad3a69d0b1fb.

* feat: update schema

* refactor: separate method get_conversations_with_bot_handoffs

* refactor: cognitive complexity

* refactor: early return if last_bot_reply is blank

* feat: add async_database_migration queue

* feat: update queue priority
This commit is contained in:
Shivam Mishra
2023-03-02 19:03:31 +05:30
committed by GitHub
parent b185059681
commit a6405ea339
5 changed files with 40 additions and 26 deletions

View File

@@ -2,20 +2,30 @@
class Migration::UpdateFirstResponseTimeInReportingEventsJob < ApplicationJob
include ReportingEventHelper
queue_as :scheduled_jobs
queue_as :async_database_migration
def perform(account)
account.reporting_events.where(name: 'first_response', user_id: nil).each do |event|
get_conversations_with_bot_handoffs(account)
account.reporting_events.where(name: 'first_response').each do |event|
conversation = event.conversation
next if conversation.nil?
# if the conversation has a bot handoff event, we don't need to update the response_time
next if conversation.nil? || @conversations_with_handoffs.include?(conversation.id)
update_event_data(event, conversation)
end
end
def get_conversations_with_bot_handoffs(account)
@conversations_with_handoffs = account.reporting_events.where(name: 'conversation_bot_handoff').pluck(:conversation_id)
end
def update_event_data(event, conversation)
last_bot_reply = conversation.messages.where(sender_type: 'AgentBot').order(created_at: :asc).last
return if last_bot_reply.blank?
first_human_reply = conversation.messages.where(sender_type: 'User').order(created_at: :asc).first
return if first_human_reply.blank?
# accomodate for campaign if required
# new_value = difference between the first_human_reply and the first_bot_reply if it exists or first_human_reply and created at
@@ -32,8 +42,6 @@ class Migration::UpdateFirstResponseTimeInReportingEventsJob < ApplicationJob
#
# bot handoff happens at the last_bot_reply created time
# the response time is the time between last bot reply created and the first human reply created
return if last_bot_reply.blank? || first_human_reply.blank?
return if last_bot_reply.created_at.to_i >= first_human_reply.created_at.to_i
# this means a bot replied existed, so we need to update the event_start_time
@@ -47,7 +55,7 @@ class Migration::UpdateFirstResponseTimeInReportingEventsJob < ApplicationJob
value: calculate_event_value(last_bot_reply, first_human_reply),
value_in_business_hours: calculate_event_value_in_business_hours(inbox, last_bot_reply,
first_human_reply),
user_id: first_human_reply.sender_id)
user_id: event.user_id || first_human_reply.sender_id)
# rubocop:enable Rails/SkipsModelValidations
end