From f96c3609a43ac4500c754b3a1b0bd0211c188e8d Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Tue, 30 Jun 2026 16:19:17 +0530 Subject: [PATCH] refactor: constrain captain overview stats range to known windows --- .../app/builders/captain/assistant_stats_builder.rb | 6 +++--- .../api/v1/accounts/captain/assistants_controller.rb | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/enterprise/app/builders/captain/assistant_stats_builder.rb b/enterprise/app/builders/captain/assistant_stats_builder.rb index 138c40cf5..6e8f0befc 100644 --- a/enterprise/app/builders/captain/assistant_stats_builder.rb +++ b/enterprise/app/builders/captain/assistant_stats_builder.rb @@ -9,6 +9,7 @@ class Captain::AssistantStatsBuilder RESOLVED_EVENT_NAMES = %w[conversation_captain_inference_resolved conversation_bot_resolved].freeze HANDOFF_EVENT_NAMES = %w[conversation_captain_inference_handoff conversation_bot_handoff].freeze DEFAULT_RANGE = '30'.freeze + ALLOWED_RANGES = %w[7 30 90 this_month last_month].freeze attr_reader :assistant, :account, :range @@ -18,7 +19,7 @@ class Captain::AssistantStatsBuilder def initialize(assistant, range = DEFAULT_RANGE) @assistant = assistant @account = assistant.account - @range = range.presence || DEFAULT_RANGE + @range = ALLOWED_RANGES.include?(range.to_s) ? range.to_s : DEFAULT_RANGE end def metrics @@ -96,8 +97,7 @@ class Captain::AssistantStatsBuilder range.to_i.positive? ? range.to_i : 30 end - # Combines the per-window message counts and reply time (batched, passed in) - # with the reporting-event metrics (resolution and reopen) for one window. + # Combines the per-window message counts and reply time with the reporting-event metrics for one window. def window_metrics(range, message_counts, avg_reply) handled = message_counts[:handled] public_count = message_counts[:public_count] 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 db64aa7d4..89498c7af 100644 --- a/enterprise/app/controllers/api/v1/accounts/captain/assistants_controller.rb +++ b/enterprise/app/controllers/api/v1/accounts/captain/assistants_controller.rb @@ -48,8 +48,8 @@ class Api::V1::Accounts::Captain::AssistantsController < Api::V1::Accounts::Base end def summary - result = Rails.cache.fetch(summary_cache_key, expires_in: 1.hour) do - builder = Captain::AssistantStatsBuilder.new(@assistant, params[:range]) + builder = Captain::AssistantStatsBuilder.new(@assistant, params[:range]) + result = Rails.cache.fetch(summary_cache_key(builder.range), expires_in: 1.hour) do Captain::OverviewSummaryService.new( account: Current.account, assistant: @assistant, @@ -68,8 +68,8 @@ class Api::V1::Accounts::Captain::AssistantsController < Api::V1::Accounts::Base private - def summary_cache_key - "captain_overview_summary/#{@assistant.id}/#{params[:range]}/#{Date.current}" + def summary_cache_key(range) + "captain_overview_summary/#{@assistant.id}/#{range}/#{Date.current}" end def set_assistant