feat: Add backend APIs for the bot metrics (#9031)

Co-authored-by: Pranav <pranav@chatwoot.com>
This commit is contained in:
Sojan Jose
2024-03-01 21:50:20 +05:30
committed by GitHub
parent 9581264286
commit 881d4bf644
11 changed files with 291 additions and 4 deletions

View File

@@ -54,6 +54,13 @@ class V2::ReportBuilder
}
end
def bot_summary
{
bot_resolutions_count: bot_resolutions.count,
bot_handoffs_count: bot_handoffs.count
}
end
def conversation_metrics
if params[:type].equal?(:account)
live_conversations
@@ -71,6 +78,8 @@ class V2::ReportBuilder
avg_first_response_time
avg_resolution_time reply_time
resolutions_count
bot_resolutions_count
bot_handoffs_count
reply_time].include?(params[:metric])
end
@@ -123,6 +132,7 @@ class V2::ReportBuilder
unattended: @open_conversations.unattended.count
}
metric[:unassigned] = @open_conversations.unassigned.count if params[:type].equal?(:account)
metric[:pending] = @open_conversations.pending.count if params[:type].equal?(:account)
metric
end
end

View File

@@ -0,0 +1,54 @@
class V2::Reports::BotMetricsBuilder
include DateRangeHelper
attr_reader :account, :params
def initialize(account, params)
@account = account
@params = params
end
def metrics
{
conversation_count: bot_conversations.count,
message_count: bot_messages.count,
resolution_rate: bot_resolution_rate.to_i,
handoff_rate: bot_handoff_rate.to_i
}
end
private
def bot_activated_inbox_ids
@bot_activated_inbox_ids ||= account.inboxes.filter(&:active_bot?).map(&:id)
end
def bot_conversations
@bot_conversations ||= account.conversations.where(inbox_id: bot_activated_inbox_ids).where(created_at: range)
end
def bot_messages
@bot_messages ||= account.messages.outgoing.where(conversation_id: bot_conversations.ids).where(created_at: range)
end
def bot_resolutions_count
account.reporting_events.joins(:conversation).select(:conversation_id).where(account_id: account.id, name: :conversation_bot_resolved,
created_at: range).distinct.count
end
def bot_handoffs_count
account.reporting_events.joins(:conversation).select(:conversation_id).where(account_id: account.id, name: :conversation_bot_handoff,
created_at: range).distinct.count
end
def bot_resolution_rate
return 0 if bot_conversations.count.zero?
bot_resolutions_count.to_f / bot_conversations.count * 100
end
def bot_handoff_rate
return 0 if bot_conversations.count.zero?
bot_handoffs_count.to_f / bot_conversations.count * 100
end
end

View File

@@ -14,6 +14,12 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController
render json: summary_metrics
end
def bot_summary
summary = V2::ReportBuilder.new(Current.account, current_summary_params).bot_summary
summary[:previous] = V2::ReportBuilder.new(Current.account, previous_summary_params).bot_summary
render json: summary
end
def agents
@report_data = generate_agents_report
generate_csv('agents_report', 'api/v2/accounts/reports/agents')
@@ -48,6 +54,11 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController
render json: conversation_metrics
end
def bot_metrics
bot_metrics = V2::Reports::BotMetricsBuilder.new(Current.account, params).metrics
render json: bot_metrics
end
private
def generate_csv(filename, template)

View File

@@ -32,6 +32,14 @@ module ReportHelper
(get_grouped_values resolutions).count
end
def bot_resolutions_count
(get_grouped_values bot_resolutions).count
end
def bot_handoffs_count
(get_grouped_values bot_handoffs).count
end
def conversations
scope.conversations.where(account_id: account.id, created_at: range)
end
@@ -49,6 +57,16 @@ module ReportHelper
conversations: { status: :resolved }, created_at: range).distinct
end
def bot_resolutions
scope.reporting_events.joins(:conversation).select(:conversation_id).where(account_id: account.id, name: :conversation_bot_resolved,
conversations: { status: :resolved }, created_at: range).distinct
end
def bot_handoffs
scope.reporting_events.joins(:conversation).select(:conversation_id).where(account_id: account.id, name: :conversation_bot_handoff,
created_at: range).distinct
end
def avg_first_response_time
grouped_reporting_events = (get_grouped_values scope.reporting_events.where(name: 'first_response', account_id: account.id))
return grouped_reporting_events.average(:value_in_business_hours) if params[:business_hours]

View File

@@ -412,7 +412,8 @@
"LOADING_MESSAGE": "Loading conversation metrics...",
"OPEN": "Open",
"UNATTENDED": "Unattended",
"UNASSIGNED": "Unassigned"
"UNASSIGNED": "Unassigned",
"PENDING": "Pending"
},
"CONVERSATION_HEATMAP": {
"HEADER": "Conversation Traffic",

View File

@@ -200,6 +200,7 @@ export const OVERVIEW_METRICS = {
open: 'OPEN',
unattended: 'UNATTENDED',
unassigned: 'UNASSIGNED',
pending: 'PENDING',
online: 'ONLINE',
busy: 'BUSY',
offline: 'OFFLINE',