fix(captain): scope overview suggestion counts

This commit is contained in:
aakashb95
2026-07-21 18:07:02 +05:30
parent 4c947f9260
commit c25aac7a99
2 changed files with 19 additions and 6 deletions
@@ -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?
@@ -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