Chore: clean up Reporting Events (#4044)
Tech debt clean up Fixes #4057 Co-authored-by: Aswin Dev P S <aswin@chatwoot.com>
This commit is contained in:
@@ -95,15 +95,15 @@ class V2::ReportBuilder
|
||||
end
|
||||
|
||||
def avg_first_response_time
|
||||
(get_grouped_values scope.events.where(name: 'first_response')).average(:value)
|
||||
(get_grouped_values scope.reporting_events.where(name: 'first_response')).average(:value)
|
||||
end
|
||||
|
||||
def avg_resolution_time
|
||||
(get_grouped_values scope.events.where(name: 'conversation_resolved')).average(:value)
|
||||
(get_grouped_values scope.reporting_events.where(name: 'conversation_resolved')).average(:value)
|
||||
end
|
||||
|
||||
def avg_resolution_time_summary
|
||||
avg_rt = scope.events
|
||||
avg_rt = scope.reporting_events
|
||||
.where(name: 'conversation_resolved', created_at: range)
|
||||
.average(:value)
|
||||
|
||||
@@ -113,7 +113,7 @@ class V2::ReportBuilder
|
||||
end
|
||||
|
||||
def avg_first_response_time_summary
|
||||
avg_frt = scope.events
|
||||
avg_frt = scope.reporting_events
|
||||
.where(name: 'first_response', created_at: range)
|
||||
.average(:value)
|
||||
|
||||
|
||||
@@ -12,10 +12,10 @@ class AsyncDispatcher < BaseDispatcher
|
||||
[
|
||||
CampaignListener.instance,
|
||||
CsatSurveyListener.instance,
|
||||
EventListener.instance,
|
||||
HookListener.instance,
|
||||
InstallationWebhookListener.instance,
|
||||
NotificationListener.instance,
|
||||
ReportingEventListener.instance,
|
||||
WebhookListener.instance,
|
||||
AutomationRuleListener.instance
|
||||
]
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
module Api::V1::ReportsHelper
|
||||
end
|
||||
@@ -1,9 +1,9 @@
|
||||
class EventListener < BaseListener
|
||||
class ReportingEventListener < BaseListener
|
||||
def conversation_resolved(event)
|
||||
conversation = extract_conversation_and_account(event)[0]
|
||||
time_to_resolve = conversation.updated_at.to_i - conversation.created_at.to_i
|
||||
|
||||
event = Event.new(
|
||||
reporting_event = ReportingEvent.new(
|
||||
name: 'conversation_resolved',
|
||||
value: time_to_resolve,
|
||||
account_id: conversation.account_id,
|
||||
@@ -11,7 +11,7 @@ class EventListener < BaseListener
|
||||
user_id: conversation.assignee_id,
|
||||
conversation_id: conversation.id
|
||||
)
|
||||
event.save
|
||||
reporting_event.save
|
||||
end
|
||||
|
||||
def first_reply_created(event)
|
||||
@@ -19,7 +19,7 @@ class EventListener < BaseListener
|
||||
conversation = message.conversation
|
||||
first_response_time = message.created_at.to_i - conversation.created_at.to_i
|
||||
|
||||
event = Event.new(
|
||||
reporting_event = ReportingEvent.new(
|
||||
name: 'first_response',
|
||||
value: first_response_time,
|
||||
account_id: conversation.account_id,
|
||||
@@ -27,6 +27,6 @@ class EventListener < BaseListener
|
||||
user_id: conversation.assignee_id,
|
||||
conversation_id: conversation.id
|
||||
)
|
||||
event.save
|
||||
reporting_event.save
|
||||
end
|
||||
end
|
||||
@@ -1,45 +0,0 @@
|
||||
class ReportingListener < BaseListener
|
||||
def conversation_created(event)
|
||||
conversation, account = extract_conversation_and_account(event)
|
||||
timestamp = event.timestamp
|
||||
|
||||
::Reports::UpdateAccountIdentity.new(account, timestamp).incr_conversations_count
|
||||
end
|
||||
|
||||
def conversation_resolved(event)
|
||||
conversation, account = extract_conversation_and_account(event)
|
||||
timestamp = event.timestamp
|
||||
|
||||
time_to_resolve = conversation.updated_at.to_i - conversation.created_at.to_i
|
||||
|
||||
if conversation.assignee.present?
|
||||
agent = conversation.assignee
|
||||
::Reports::UpdateAgentIdentity.new(account, agent, timestamp).update_avg_resolution_time(time_to_resolve)
|
||||
::Reports::UpdateAgentIdentity.new(account, agent, timestamp).incr_resolutions_count
|
||||
end
|
||||
|
||||
::Reports::UpdateAccountIdentity.new(account, timestamp).update_avg_resolution_time(time_to_resolve)
|
||||
::Reports::UpdateAccountIdentity.new(account, timestamp).incr_resolutions_count
|
||||
end
|
||||
|
||||
def first_reply_created(event)
|
||||
message, account = extract_message_and_account(event)
|
||||
timestamp = event.timestamp
|
||||
|
||||
conversation = message.conversation
|
||||
agent = conversation.assignee
|
||||
first_response_time = message.created_at.to_i - conversation.created_at.to_i
|
||||
::Reports::UpdateAgentIdentity.new(account, agent, timestamp).update_avg_first_response_time(first_response_time) if agent.present?
|
||||
::Reports::UpdateAccountIdentity.new(account, timestamp).update_avg_first_response_time(first_response_time)
|
||||
end
|
||||
|
||||
def message_created(event)
|
||||
message, account = extract_message_and_account(event)
|
||||
timestamp = event.timestamp
|
||||
|
||||
return unless message.reportable?
|
||||
|
||||
identity = ::Reports::UpdateAccountIdentity.new(account, timestamp)
|
||||
message.outgoing? ? identity.incr_outgoing_messages_count : identity.incr_incoming_messages_count
|
||||
end
|
||||
end
|
||||
@@ -4,6 +4,6 @@ module Reportable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :events, dependent: :destroy_async
|
||||
has_many :reporting_events, dependent: :destroy
|
||||
end
|
||||
end
|
||||
|
||||
@@ -39,8 +39,8 @@ class Label < ApplicationRecord
|
||||
account.messages.where(conversation_id: conversations.pluck(:id))
|
||||
end
|
||||
|
||||
def events
|
||||
account.events.where(conversation_id: conversations.pluck(:id))
|
||||
def reporting_events
|
||||
account.reporting_events.where(conversation_id: conversations.pluck(:id))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: events
|
||||
# Table name: reporting_events
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# name :string
|
||||
@@ -14,14 +14,14 @@
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_events_on_account_id (account_id)
|
||||
# index_events_on_created_at (created_at)
|
||||
# index_events_on_inbox_id (inbox_id)
|
||||
# index_events_on_name (name)
|
||||
# index_events_on_user_id (user_id)
|
||||
# index_reporting_events_on_account_id (account_id)
|
||||
# index_reporting_events_on_created_at (created_at)
|
||||
# index_reporting_events_on_inbox_id (inbox_id)
|
||||
# index_reporting_events_on_name (name)
|
||||
# index_reporting_events_on_user_id (user_id)
|
||||
#
|
||||
|
||||
class Event < ApplicationRecord
|
||||
class ReportingEvent < ApplicationRecord
|
||||
validates :account_id, presence: true
|
||||
validates :name, presence: true
|
||||
validates :value, presence: true
|
||||
@@ -45,7 +45,7 @@ class Team < ApplicationRecord
|
||||
account.messages.where(conversation_id: conversations.pluck(:id))
|
||||
end
|
||||
|
||||
def events
|
||||
account.events.where(conversation_id: conversations.pluck(:id))
|
||||
def reporting_events
|
||||
account.reporting_events.where(conversation_id: conversations.pluck(:id))
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user