refactor: constrain captain overview stats range to known windows

This commit is contained in:
Shivam Mishra
2026-06-30 16:21:48 +05:30
parent cd1472b0ac
commit f96c3609a4
2 changed files with 7 additions and 7 deletions
@@ -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]
@@ -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