From 375d216d12c1e3ff13902063f257e12b39ac663f Mon Sep 17 00:00:00 2001 From: Pranav Date: Sat, 10 Feb 2024 18:41:11 -0800 Subject: [PATCH] Re-write live-conversations API --- app/builders/v2/report_builder.rb | 33 ----------- .../v2/accounts/live_reports_controller.rb | 57 +++++++++++++++++++ .../api/v2/accounts/reports_controller.rb | 10 ---- app/javascript/dashboard/api/liveReports.js | 20 +++++++ .../settings/reports/LiveReports.vue | 20 +------ .../components/overview/AgentTable.vue | 36 +++++------- .../dashboard/store/modules/reports.js | 11 ++-- config/routes.rb | 6 ++ 8 files changed, 107 insertions(+), 86 deletions(-) create mode 100644 app/controllers/api/v2/accounts/live_reports_controller.rb create mode 100644 app/javascript/dashboard/api/liveReports.js diff --git a/app/builders/v2/report_builder.rb b/app/builders/v2/report_builder.rb index 2725c55a3..7f0fcb7d9 100644 --- a/app/builders/v2/report_builder.rb +++ b/app/builders/v2/report_builder.rb @@ -54,14 +54,6 @@ class V2::ReportBuilder } end - def conversation_metrics - if params[:type].equal?(:account) - live_conversations - else - agent_metrics.sort_by { |hash| hash[:metric][:open] }.reverse - end - end - private def metric_valid? @@ -100,29 +92,4 @@ class V2::ReportBuilder time_zone: @timezone ) end - - def agent_metrics - account_users = @account.account_users.page(params[:page]).per(AGENT_RESULTS_PER_PAGE) - account_users.each_with_object([]) do |account_user, arr| - @user = account_user.user - arr << { - id: @user.id, - name: @user.name, - email: @user.email, - thumbnail: @user.avatar_url, - availability: account_user.availability_status, - metric: live_conversations - } - end - end - - def live_conversations - @open_conversations = scope.conversations.where(account_id: @account.id).open - metric = { - open: @open_conversations.count, - unattended: @open_conversations.unattended.count - } - metric[:unassigned] = @open_conversations.unassigned.count if params[:type].equal?(:account) - metric - end end diff --git a/app/controllers/api/v2/accounts/live_reports_controller.rb b/app/controllers/api/v2/accounts/live_reports_controller.rb new file mode 100644 index 000000000..59804c988 --- /dev/null +++ b/app/controllers/api/v2/accounts/live_reports_controller.rb @@ -0,0 +1,57 @@ +class Api::V2::Accounts::LiveReportsController < Api::V1::Accounts::BaseController + before_action :load_conversations, only: [:conversation_metrics, :grouped_conversation_metrics] + before_action :set_group_scope, only: [:grouped_conversation_metrics] + + def conversation_metrics + render json: { + open: @conversations.open.count, + unattended: @conversations.open.unattended.count, + unassigned: @conversations.open.unassigned.count + } + end + + def grouped_conversation_metrics + count_by_group = @conversations.open.group(@group_scope).count + unattended_by_group = @conversations.open.unattended.group(@group_scope).count + unassigned_by_group = @conversations.open.unassigned.group(@group_scope).count + + group_metrics = count_by_group.map do |group_id, count| + metric = { + open: count, + unattended: unattended_by_group[group_id] || 0, + unassigned: unassigned_by_group[group_id] || 0 + } + metric[@group_scope] = group_id + metric + end + + render json: group_metrics + end + + private + + def set_group_scope + render json: { error: 'invalid group_by' }, status: :unprocessable_entity and return unless %w[ + team_id + assignee_id + ].include?(permitted_params[:group_by]) + + @group_scope = permitted_params[:group_by] + end + + def team + return unless permitted_params[:team_id] + + @team ||= Current.account.teams.find_by(permitted_params[:team_id]) + end + + def load_conversations + scope = Current.account.conversations + scope = scope.where(team_id: team.id) if team.present? + @conversations = scope + end + + def permitted_params + params.permit(:team_id, :group_by) + end +end diff --git a/app/controllers/api/v2/accounts/reports_controller.rb b/app/controllers/api/v2/accounts/reports_controller.rb index 260400feb..38be4cdbf 100644 --- a/app/controllers/api/v2/accounts/reports_controller.rb +++ b/app/controllers/api/v2/accounts/reports_controller.rb @@ -42,12 +42,6 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController generate_csv('conversation_traffic_reports', 'api/v2/accounts/reports/conversation_traffic') end - def conversations - return head :unprocessable_entity if params[:type].blank? - - render json: conversation_metrics - end - private def generate_csv(filename, template) @@ -120,8 +114,4 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController summary[:previous] = V2::ReportBuilder.new(Current.account, previous_summary_params).summary summary end - - def conversation_metrics - V2::ReportBuilder.new(Current.account, conversation_params).conversation_metrics - end end diff --git a/app/javascript/dashboard/api/liveReports.js b/app/javascript/dashboard/api/liveReports.js new file mode 100644 index 000000000..759ebc2a9 --- /dev/null +++ b/app/javascript/dashboard/api/liveReports.js @@ -0,0 +1,20 @@ +/* global axios */ +import ApiClient from './ApiClient'; + +class LiveReportsAPI extends ApiClient { + constructor() { + super('live_reports', { accountScoped: true, apiVersion: 'v2' }); + } + + getConversationMetric() { + return axios.get(`${this.url}/conversation_metrics`); + } + + getGroupedConversations({ groupBy } = { groupBy: 'assignee_id' }) { + return axios.get(`${this.url}/grouped_conversation_metrics`, { + params: { group_by: groupBy }, + }); + } +} + +export default new LiveReportsAPI(); diff --git a/app/javascript/dashboard/routes/dashboard/settings/reports/LiveReports.vue b/app/javascript/dashboard/routes/dashboard/settings/reports/LiveReports.vue index b36b160b2..8a5a001bb 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/reports/LiveReports.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/reports/LiveReports.vue @@ -62,7 +62,6 @@ :agent-metrics="agentConversationMetric" :page-index="pageIndex" :is-loading="uiFlags.isFetchingAgentConversationMetric" - @page-change="onPageNumberChange" /> @@ -132,8 +131,8 @@ export default { }, methods: { fetchAllData() { - this.fetchAccountConversationMetric(); - this.fetchAgentConversationMetric(); + this.$store.dispatch('fetchLiveConversationMetric'); + this.$store.dispatch('fetchAgentConversationMetric'); this.fetchHeatmapData(); }, downloadHeatmapData() { @@ -170,21 +169,6 @@ export default { businessHours: false, }); }, - fetchAccountConversationMetric() { - this.$store.dispatch('fetchAccountConversationMetric', { - type: 'account', - }); - }, - fetchAgentConversationMetric() { - this.$store.dispatch('fetchAgentConversationMetric', { - type: 'agent', - page: this.pageIndex, - }); - }, - onPageNumberChange(pageIndex) { - this.pageIndex = pageIndex; - this.fetchAgentConversationMetric(); - }, }, }; diff --git a/app/javascript/dashboard/routes/dashboard/settings/reports/components/overview/AgentTable.vue b/app/javascript/dashboard/routes/dashboard/settings/reports/components/overview/AgentTable.vue index 387e329b1..38500ecba 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/reports/components/overview/AgentTable.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/reports/components/overview/AgentTable.vue @@ -12,17 +12,12 @@ $t('OVERVIEW_REPORTS.AGENT_CONVERSATIONS.LOADING_MESSAGE') }} - -
+
@@ -31,14 +26,12 @@