diff --git a/app/builders/v2/reports/bot_metrics_builder.rb b/app/builders/v2/reports/bot_metrics_builder.rb index c46daf978..d96ab2ccb 100644 --- a/app/builders/v2/reports/bot_metrics_builder.rb +++ b/app/builders/v2/reports/bot_metrics_builder.rb @@ -18,8 +18,15 @@ class V2::Reports::BotMetricsBuilder private + def filter_inbox_id + @filter_inbox_id ||= params[:inbox_id].presence&.to_i + end + def bot_activated_inbox_ids - @bot_activated_inbox_ids ||= account.inboxes.filter(&:active_bot?).map(&:id) + @bot_activated_inbox_ids ||= begin + ids = account.inboxes.filter(&:active_bot?).map(&:id) + filter_inbox_id ? ids & [filter_inbox_id] : ids + end end def bot_conversations @@ -30,14 +37,18 @@ class V2::Reports::BotMetricsBuilder @bot_messages ||= account.messages.outgoing.where(conversation_id: bot_conversations.ids).where(created_at: range) end + def base_reporting_events + scope = account.reporting_events.where(account_id: account.id, created_at: range) + scope = scope.where(inbox_id: filter_inbox_id) if filter_inbox_id + scope + 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 + base_reporting_events.joins(:conversation).select(:conversation_id).where(name: :conversation_bot_resolved).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 + base_reporting_events.joins(:conversation).select(:conversation_id).where(name: :conversation_bot_handoff).distinct.count end def bot_resolution_rate diff --git a/app/controllers/api/v2/accounts/reports_controller.rb b/app/controllers/api/v2/accounts/reports_controller.rb index 4fe1656e0..d16451973 100644 --- a/app/controllers/api/v2/accounts/reports_controller.rb +++ b/app/controllers/api/v2/accounts/reports_controller.rb @@ -1,3 +1,4 @@ +# rubocop:disable Metrics/ClassLength class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController include Api::V2::Accounts::ReportsHelper include Api::V2::Accounts::HeatmapHelper @@ -58,7 +59,7 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController end def bot_metrics - bot_metrics = V2::Reports::BotMetricsBuilder.new(Current.account, params).metrics + bot_metrics = V2::Reports::BotMetricsBuilder.new(Current.account, bot_metrics_params).metrics render json: bot_metrics end @@ -196,6 +197,14 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController } end + def bot_metrics_params + { + inbox_id: params[:inbox_id], + since: params[:since], + until: params[:until] + } + end + def inbox_leads_summary_params { inbox_id: params[:inbox_id], @@ -205,3 +214,4 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController } end end +# rubocop:enable Metrics/ClassLength diff --git a/app/javascript/dashboard/api/reports.js b/app/javascript/dashboard/api/reports.js index 55cf25bc8..ade6a030c 100644 --- a/app/javascript/dashboard/api/reports.js +++ b/app/javascript/dashboard/api/reports.js @@ -91,18 +91,19 @@ class ReportsAPI extends ApiClient { }); } - getBotMetrics({ from, to } = {}) { + getBotMetrics({ from, to, inboxId } = {}) { return axios.get(`${this.url}/bot_metrics`, { - params: { since: from, until: to }, + params: { since: from, until: to, inbox_id: inboxId }, }); } - getBotSummary({ from, to, groupBy, businessHours } = {}) { + getBotSummary({ from, to, groupBy, businessHours, type, id } = {}) { return axios.get(`${this.url}/bot_summary`, { params: { since: from, until: to, - type: 'account', + type: type || 'account', + id, group_by: groupBy, business_hours: businessHours, }, diff --git a/app/javascript/dashboard/routes/dashboard/settings/reports/BotReports.vue b/app/javascript/dashboard/routes/dashboard/settings/reports/BotReports.vue index 03b7290d6..1f3b186e9 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/reports/BotReports.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/reports/BotReports.vue @@ -20,6 +20,7 @@ export default { from: 0, to: 0, groupBy: GROUP_BY_FILTER[1], + inboxId: null, reportKeys: { BOT_RESOLUTION_COUNT: 'bot_resolutions_count', BOT_HANDOFF_COUNT: 'bot_handoffs_count', @@ -32,6 +33,7 @@ export default { return { from: this.from, to: this.to, + inboxId: this.inboxId, }; }, }, @@ -60,24 +62,35 @@ export default { }); }, getRequestPayload() { - const { from, to, groupBy, businessHours } = this; - - return { + const { from, to, groupBy, businessHours, inboxId } = this; + const payload = { from, to, groupBy: groupBy?.period, businessHours, }; + if (inboxId) { + payload.type = 'inbox'; + payload.id = inboxId; + } + return payload; }, - onFilterChange({ from, to, groupBy, businessHours }) { + onFilterChange({ from, to, groupBy, businessHours, inboxes }) { this.from = from; this.to = to; this.groupBy = groupBy; this.businessHours = businessHours; + this.inboxId = inboxes?.id || null; this.fetchAllData(); useTrack(REPORTS_EVENTS.FILTER_REPORT, { - filterValue: { from, to, groupBy, businessHours }, + filterValue: { + from, + to, + groupBy, + businessHours, + inboxId: this.inboxId, + }, reportType: 'bots', }); }, @@ -89,7 +102,7 @@ export default {
{ commit(types.default.SET_BOT_SUMMARY, botSummary.data); diff --git a/app/models/inbox.rb b/app/models/inbox.rb index ba5474ecd..49e884d9b 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -77,6 +77,7 @@ class Inbox < ApplicationRecord has_many :conversations, dependent: :destroy_async has_many :messages, dependent: :destroy_async has_many :scheduled_messages, dependent: :destroy_async + has_many :reporting_events, dependent: :nullify has_one :inbox_assignment_policy, dependent: :destroy has_one :assignment_policy, through: :inbox_assignment_policy diff --git a/lib/tasks/rebuild_bot_resolved.rake b/lib/tasks/rebuild_bot_resolved.rake index 1ccf745e8..c1cd41469 100644 --- a/lib/tasks/rebuild_bot_resolved.rake +++ b/lib/tasks/rebuild_bot_resolved.rake @@ -38,6 +38,7 @@ namespace :reports do base_scope = base_scope.where(account_id: account_id) if account_id affected_conversation_ids = Message + .unscope(:order) .where(message_type: :outgoing, sender_id: nil) .distinct .pluck(:conversation_id)