From 072c60706238b5b7be38809f5b2750558d20e76c Mon Sep 17 00:00:00 2001 From: Pranav Date: Wed, 15 May 2024 09:22:36 -0700 Subject: [PATCH] Update --- Gemfile.lock | 25 ++++++----- app/builders/v2/new_report_builder.rb | 41 +++++++++++++------ app/builders/v2/report_builder.rb | 7 ++-- .../live_reports/account_reports_builder.rb | 14 +++++++ .../live_reports/agent_reports_builder.rb | 29 +++++++++++++ .../timeseries/average_report_builder.rb | 6 ++- .../timeseries/metric_count_report_builder.rb | 6 ++- .../api/v2/accounts/reports_controller.rb | 30 +++++++++++--- app/helpers/api/v2/accounts/reports_helper.rb | 2 +- 9 files changed, 123 insertions(+), 37 deletions(-) create mode 100644 app/builders/v2/reports/live_reports/account_reports_builder.rb create mode 100644 app/builders/v2/reports/live_reports/agent_reports_builder.rb diff --git a/Gemfile.lock b/Gemfile.lock index 35e1e2a48..52a475fa7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -316,15 +316,15 @@ GEM google-cloud-translate-v3 (0.6.0) gapic-common (>= 0.17.1, < 2.a) google-cloud-errors (~> 1.0) - google-protobuf (3.25.2) - google-protobuf (3.25.2-arm64-darwin) - google-protobuf (3.25.2-x86_64-darwin) - google-protobuf (3.25.2-x86_64-linux) + google-protobuf (3.25.3) + google-protobuf (3.25.3-arm64-darwin) + google-protobuf (3.25.3-x86_64-darwin) + google-protobuf (3.25.3-x86_64-linux) googleapis-common-protos (1.4.0) google-protobuf (~> 3.14) googleapis-common-protos-types (~> 1.2) grpc (~> 1.27) - googleapis-common-protos-types (1.11.0) + googleapis-common-protos-types (1.14.0) google-protobuf (~> 3.18) googleauth (1.5.2) faraday (>= 0.17.3, < 3.a) @@ -335,14 +335,17 @@ GEM signet (>= 0.16, < 2.a) groupdate (6.2.1) activesupport (>= 5.2) - grpc (1.54.3) - google-protobuf (~> 3.21) + grpc (1.63.0) + google-protobuf (~> 3.25) googleapis-common-protos-types (~> 1.0) - grpc (1.54.3-x86_64-darwin) - google-protobuf (~> 3.21) + grpc (1.63.0-arm64-darwin) + google-protobuf (~> 3.25) googleapis-common-protos-types (~> 1.0) - grpc (1.54.3-x86_64-linux) - google-protobuf (~> 3.21) + grpc (1.63.0-x86_64-darwin) + google-protobuf (~> 3.25) + googleapis-common-protos-types (~> 1.0) + grpc (1.63.0-x86_64-linux) + google-protobuf (~> 3.25) googleapis-common-protos-types (~> 1.0) haikunator (1.1.1) hairtrigger (1.0.0) diff --git a/app/builders/v2/new_report_builder.rb b/app/builders/v2/new_report_builder.rb index fc04010f3..920d709a1 100644 --- a/app/builders/v2/new_report_builder.rb +++ b/app/builders/v2/new_report_builder.rb @@ -4,29 +4,44 @@ class V2::NewReportBuilder pattr_initialize :account, :params AVG_METRICS = %w[avg_first_response_time avg_resolution_time reply_time].freeze - COUNT_METRICS = %w[conversations_count incoming_messages_count outgoing_messages_count resolutions_count bot_resolutions_count - bot_handoffs_count].freeze + COUNT_METRICS = %w[ + conversations_count + incoming_messages_count + outgoing_messages_count + resolutions_count + bot_resolutions_count + bot_handoffs_count + ].freeze DEFAULT_GROUP_BY = 'day'.freeze AGENT_RESULTS_PER_PAGE = 25 - def build - builder_class = case params[:metric] - when *AVG_METRICS - V2::Reports::Timeseries::AverageReportBuilder - when *COUNT_METRICS - V2::Reports::Timeseries::MetricCountReportBuilder - else - log_invalid_metric - return {} - end + def timeseries + return builder_class.new(account, params).timeseries if builder_class.present? - builder_class.new(account, params).build + log_invalid_metric + end + + def aggregate_value + return builder_class.new(account, params).aggregate_value if builder_class.present? + + log_invalid_metric end private + def builder_class + case params[:metric] + when *AVG_METRICS + V2::Reports::Timeseries::AverageReportBuilder + when *COUNT_METRICS + V2::Reports::Timeseries::MetricCountReportBuilder + end + end + def log_invalid_metric Rails.logger.error "ReportBuilder: Invalid metric - #{params[:metric]}" + + {} end end diff --git a/app/builders/v2/report_builder.rb b/app/builders/v2/report_builder.rb index 7f0fcb7d9..3fd9e09d3 100644 --- a/app/builders/v2/report_builder.rb +++ b/app/builders/v2/report_builder.rb @@ -46,11 +46,10 @@ class V2::ReportBuilder } end - def short_summary + def bot_summary { - conversations_count: conversations.count, - avg_first_response_time: avg_first_response_time_summary, - avg_resolution_time: avg_resolution_time_summary + bot_resolutions_count: bot_resolutions.count, + bot_handoffs_count: bot_handoffs.count } end diff --git a/app/builders/v2/reports/live_reports/account_reports_builder.rb b/app/builders/v2/reports/live_reports/account_reports_builder.rb new file mode 100644 index 000000000..ca3d2ab85 --- /dev/null +++ b/app/builders/v2/reports/live_reports/account_reports_builder.rb @@ -0,0 +1,14 @@ +class V2::LiveReports::AccountReportsBuilder + pattr_initialize :account + + def build + open_conversations = account.conversations.open + metric = { + open: open_conversations.count, + unattended: open_conversations.unattended.count + } + metric[:unassigned] = open_conversations.unassigned.count + metric[:pending] = open_conversations.pending.count + metric + end +end diff --git a/app/builders/v2/reports/live_reports/agent_reports_builder.rb b/app/builders/v2/reports/live_reports/agent_reports_builder.rb new file mode 100644 index 000000000..854436365 --- /dev/null +++ b/app/builders/v2/reports/live_reports/agent_reports_builder.rb @@ -0,0 +1,29 @@ +class V2::LiveReports::AgentReportsBuilder + pattr_initialize :account, :params + AGENT_RESULTS_PER_PAGE = 25 + + def build + 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(user) + } + end + end + + private + + def live_conversations(user) + open_conversations = user.conversations.where(account_id: @account.id).open + { + open: open_conversations.count, + unattended: open_conversations.unattended.count + } + end +end diff --git a/app/builders/v2/reports/timeseries/average_report_builder.rb b/app/builders/v2/reports/timeseries/average_report_builder.rb index 8e81cc078..0f4f03932 100644 --- a/app/builders/v2/reports/timeseries/average_report_builder.rb +++ b/app/builders/v2/reports/timeseries/average_report_builder.rb @@ -1,5 +1,5 @@ class V2::Reports::Timeseries::AverageReportBuilder < V2::Reports::Timeseries::BaseTimeseriesBuilder - def build + def timeseries grouped_average_time = reporting_events.average(average_value_key) grouped_event_count = reporting_events.count grouped_average_time.each_with_object([]) do |element, arr| @@ -12,6 +12,10 @@ class V2::Reports::Timeseries::AverageReportBuilder < V2::Reports::Timeseries::B end end + def aggregate_value + object_scope.average(average_value_key) + end + private def event_name diff --git a/app/builders/v2/reports/timeseries/metric_count_report_builder.rb b/app/builders/v2/reports/timeseries/metric_count_report_builder.rb index b4bbb5ba5..82b194a7d 100644 --- a/app/builders/v2/reports/timeseries/metric_count_report_builder.rb +++ b/app/builders/v2/reports/timeseries/metric_count_report_builder.rb @@ -1,11 +1,15 @@ class V2::Reports::Timeseries::MetricCountReportBuilder < V2::Reports::Timeseries::BaseTimeseriesBuilder - def build + def timeseries grouped_count.each_with_object([]) do |element, arr| event_date, event_count = element arr << { value: event_count, timestamp: event_date.to_time.to_i } end end + def aggregate_value + object_scope.count + end + private def metric diff --git a/app/controllers/api/v2/accounts/reports_controller.rb b/app/controllers/api/v2/accounts/reports_controller.rb index ff85e8616..a366e17b5 100644 --- a/app/controllers/api/v2/accounts/reports_controller.rb +++ b/app/controllers/api/v2/accounts/reports_controller.rb @@ -6,7 +6,7 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController def index builder = V2::NewReportBuilder.new(Current.account, report_params) - data = builder.build + data = builder.timeseries render json: data end @@ -14,6 +14,12 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController render json: summary_metrics end + def bot_summary + summary = V2::NewReportBuilder.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') @@ -42,6 +48,22 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController generate_csv('conversation_traffic_reports', 'api/v2/accounts/reports/conversation_traffic') end + def conversations + case params[:type].to_sym + when :account + render json: V2::LiveReports::AccountReportsBuilder.new(Current.account).build + when :agent + render json: V2::LiveReports::AgentReportsBuilder.new(Current.account, conversation_params).build + else + head :unprocessable_entity + end + 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) @@ -89,11 +111,7 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController end def conversation_params - { - type: params[:type].to_sym, - user_id: params[:user_id], - page: params[:page].presence || 1 - } + { page: params[:page].presence || 1 } end def range diff --git a/app/helpers/api/v2/accounts/reports_helper.rb b/app/helpers/api/v2/accounts/reports_helper.rb index 74c7af43e..9f7655c23 100644 --- a/app/helpers/api/v2/accounts/reports_helper.rb +++ b/app/helpers/api/v2/accounts/reports_helper.rb @@ -37,7 +37,7 @@ module Api::V2::Accounts::ReportsHelper business_hours: ActiveModel::Type::Boolean.new.cast(params[:business_hours]) } ) - ).short_summary + ) end private