fix: bots included in time to response metrics (#6409)

* feat: ignore bots in avg_first_response_time

* feat: ignore bots in avg_first_response count

* feat: add bot handoff event

* feat: add handoff event listener and reporting event

* fix: ignore agent bot in first response

* refactor: calculate first_response with last handoff

* refactor: method defn order

* test: new reporting events

* feat: Revert "feat: ignore bots in avg_first_response count"

This reverts commit de1977c219a2e7a9180dd02272244fe3b3f7ce89.

* feat: Revert "feat: ignore bots in avg_first_response_time"

This reverts commit bb9171945d5e3b2f6015f4f96dd1b76b3efb6987.

* fix: business hour calculation for first_reply

* fix: event_start_time for first_response

* feat: add migration to recompute first_responses

* refactor: separate mute helpers for conversation

* refactor: rename migration

* refactor: migration script

* fix: migration typo

* fix: typo in query

* feat: update schema.rb

* Revert "feat: update schema.rb"

This reverts commit 353ef355f2d956dd219907bb66982dc90ca5d896.

* feat: update schema

* refactor: update events as a batch job

* fix: ignore the event if value is negative

* feat: don't create a new hand-off if it's already present

* refactor: break the action into smaller chunks

* refactor: update reporting listener spec

Handle the case to ensure extra bot handoffs are not created for a give conversation

* fix: import error

---------

Co-authored-by: Vishnu Narayanan <vishnu@chatwoot.com>
This commit is contained in:
Shivam Mishra
2023-02-25 09:48:48 +05:30
committed by GitHub
parent 92d0398744
commit 06ffaa90fc
11 changed files with 200 additions and 28 deletions

View File

@@ -0,0 +1,28 @@
module ConversationMuteHelpers
extend ActiveSupport::Concern
def mute!
resolved!
Redis::Alfred.setex(mute_key, 1, mute_period)
create_muted_message
end
def unmute!
Redis::Alfred.delete(mute_key)
create_unmuted_message
end
def muted?
Redis::Alfred.get(mute_key).present?
end
private
def mute_key
format(Redis::RedisKeys::CONVERSATION_MUTE_KEY, id: id)
end
def mute_period
6.hours
end
end

View File

@@ -48,6 +48,7 @@ class Conversation < ApplicationRecord
include ActivityMessageHandler
include UrlHelper
include SortHandler
include ConversationMuteHelpers
validates :account_id, presence: true
validates :inbox_id, presence: true
@@ -142,19 +143,9 @@ class Conversation < ApplicationRecord
save
end
def mute!
resolved!
Redis::Alfred.setex(mute_key, 1, mute_period)
create_muted_message
end
def unmute!
Redis::Alfred.delete(mute_key)
create_unmuted_message
end
def muted?
Redis::Alfred.get(mute_key).present?
def bot_handoff!
open!
dispatcher_dispatch(CONVERSATION_BOT_HANDOFF)
end
def unread_messages
@@ -269,14 +260,6 @@ class Conversation < ApplicationRecord
create_label_removed(user_name, previous_labels - current_labels)
end
def mute_key
format(Redis::RedisKeys::CONVERSATION_MUTE_KEY, id: id)
end
def mute_period
6.hours
end
def validate_referer_url
return unless additional_attributes['referer']

View File

@@ -188,10 +188,15 @@ class Message < ApplicationRecord
sender.update(last_activity_at: DateTime.now) if sender.is_a?(Contact)
end
def first_human_response?
conversation.messages.outgoing
.where.not(sender_type: 'AgentBot')
.where("(additional_attributes->'campaign_id') is null").count == 1
end
def dispatch_create_events
Rails.configuration.dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by)
if outgoing? && conversation.messages.outgoing.where("(additional_attributes->'campaign_id') is null").count == 1
if outgoing? && first_human_response?
Rails.configuration.dispatcher.dispatch(FIRST_REPLY_CREATED, Time.zone.now, message: self, performed_by: Current.executed_by)
end
end