From c25aac7a9997a13c77e79c5dd313e30f049f41f1 Mon Sep 17 00:00:00 2001 From: aakashb95 Date: Tue, 21 Jul 2026 18:07:02 +0530 Subject: [PATCH] fix(captain): scope overview suggestion counts --- .../app/builders/captain/assistant_stats_builder.rb | 12 ++++++++---- .../v1/accounts/captain/assistants_controller.rb | 13 +++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/enterprise/app/builders/captain/assistant_stats_builder.rb b/enterprise/app/builders/captain/assistant_stats_builder.rb index d06c80633..bfc4017aa 100644 --- a/enterprise/app/builders/captain/assistant_stats_builder.rb +++ b/enterprise/app/builders/captain/assistant_stats_builder.rb @@ -23,10 +23,11 @@ class Captain::AssistantStatsBuilder # ('this_month', 'last_month'). `timezone_offset` is the viewer's UTC offset in # hours (as the reports API sends it), so month/day boundaries anchor to the # viewer's day rather than UTC. Both windows are resolved by AssistantStatsWindow. - def initialize(assistant, range = Captain::AssistantStatsWindow::DEFAULT_RANGE, timezone_offset = nil) + def initialize(assistant, range = Captain::AssistantStatsWindow::DEFAULT_RANGE, timezone_offset = nil, suggestions_scope: nil) @assistant = assistant @account = assistant.account @window = Captain::AssistantStatsWindow.new(range, timezone_offset) + @suggestions_scope = suggestions_scope || assistant.faq_suggestions end def metrics @@ -39,7 +40,7 @@ class Captain::AssistantStatsBuilder private - attr_reader :window + attr_reader :window, :suggestions_scope def current_range window.current @@ -185,8 +186,7 @@ class Captain::AssistantStatsBuilder def knowledge approved, suggestions, documents = Captain::AssistantResponse.by_assistant(assistant.id).reorder(nil).pick( Arel.sql("COUNT(*) FILTER (WHERE status = #{Captain::AssistantResponse.statuses['approved']})"), - Arel.sql("(SELECT COUNT(*) FROM captain_faq_suggestions WHERE assistant_id = #{assistant.id.to_i} " \ - "AND status = #{Captain::FaqSuggestion.statuses['open']})"), + Arel.sql("(#{open_suggestion_count_sql})"), Arel.sql("(SELECT COUNT(*) FROM captain_documents WHERE assistant_id = #{assistant.id.to_i})") ) total = approved + suggestions @@ -199,6 +199,10 @@ class Captain::AssistantStatsBuilder } end + def open_suggestion_count_sql + suggestions_scope.where(assistant_id: assistant.id).open.reorder(nil).select('COUNT(*)').to_sql + end + def rate(numerator, denominator) return 0 if denominator.zero? diff --git a/enterprise/app/controllers/api/v1/accounts/captain/assistants_controller.rb b/enterprise/app/controllers/api/v1/accounts/captain/assistants_controller.rb index 4fbb93d20..69c865211 100644 --- a/enterprise/app/controllers/api/v1/accounts/captain/assistants_controller.rb +++ b/enterprise/app/controllers/api/v1/accounts/captain/assistants_controller.rb @@ -44,11 +44,11 @@ class Api::V1::Accounts::Captain::AssistantsController < Api::V1::Accounts::Base end def stats - render json: Captain::AssistantStatsBuilder.new(@assistant, params[:range], params[:timezone_offset]).metrics + render json: assistant_stats_builder.metrics end def summary - result = cached_or_generated_summary(Captain::AssistantStatsBuilder.new(@assistant, params[:range], params[:timezone_offset])) + result = cached_or_generated_summary(assistant_stats_builder) if result[:error] render json: { error: result[:error] }, status: :unprocessable_content @@ -65,6 +65,15 @@ class Api::V1::Accounts::Captain::AssistantsController < Api::V1::Accounts::Base private + def assistant_stats_builder + Captain::AssistantStatsBuilder.new( + @assistant, + params[:range], + params[:timezone_offset], + suggestions_scope: Captain::FaqSuggestionFinder.new(Current.user, Current.account).perform + ) + end + def drilldown_params params.permit(:metric, :range, :timezone_offset, :page, :per_page) end