From b1f8792e5f710fab4bc2317b55e60309a609580c Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Tue, 30 Jun 2026 16:32:33 +0530 Subject: [PATCH] fix: exclude stale reopens from captain overview reopen rate --- enterprise/app/builders/captain/assistant_stats_builder.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/enterprise/app/builders/captain/assistant_stats_builder.rb b/enterprise/app/builders/captain/assistant_stats_builder.rb index 12531dbb1..266022ed9 100644 --- a/enterprise/app/builders/captain/assistant_stats_builder.rb +++ b/enterprise/app/builders/captain/assistant_stats_builder.rb @@ -187,12 +187,15 @@ class Captain::AssistantStatsBuilder def reopen_rate(range) resolved_scope = account.reporting_events.where(name: RESOLVED_EVENT_NAMES, inbox_id: assistant_inbox_ids, created_at: range) - resolved = resolved_scope.distinct.count(:conversation_id) + # event_start_time on a reopen is the preceding resolve's timestamp, so requiring it + # within the window keeps only reopens that followed the in-window resolve, not an + # earlier resolve/reopen cycle on the same conversation. reopened = account.reporting_events .where(name: 'conversation_opened', conversation_id: resolved_scope.select(:conversation_id)) .where('reporting_events.value > 0') + .where('reporting_events.event_start_time >= ?', range.first) .distinct.count(:conversation_id) - rate(reopened, resolved) + rate(reopened, resolved_scope.distinct.count(:conversation_id)) end def assistant_inbox_ids