fix: make Analyze Now button actually re-run all rules

Previously, AnalyzeSessionAsync always loaded existing results and
skipped rules already evaluated (deduplication check). The reanalyze
flag from the API endpoint was never passed through, so clicking
"Analyze Now" only returned cached results.

Fix:
- Add `reanalyze` parameter to `AnalyzeSessionAsync`; when true, skip
  deduplication so all active rules evaluate against the full event stream
- In GetRuleResultsFunction, pass reanalyze=true and delete stale stored
  results before writing the fresh evaluation output

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Oliver Kieselbach
2026-02-24 14:17:07 +01:00
parent d9cab13299
commit 8f9aec3e05
2 changed files with 20 additions and 10 deletions
@@ -49,17 +49,17 @@ namespace AutopilotMonitor.Functions.Functions
try
{
var ruleEngine = new RuleEngine(_analyzeRuleService, _storageService, _logger);
var newResults = await ruleEngine.AnalyzeSessionAsync(tenantId, sessionId);
var freshResults = await ruleEngine.AnalyzeSessionAsync(tenantId, sessionId, reanalyze: true);
foreach (var result in newResults)
// Delete existing results so stale entries don't persist after re-analysis
await _storageService.DeleteSessionRuleResultsAsync(tenantId, sessionId);
foreach (var result in freshResults)
{
await _storageService.StoreRuleResultAsync(result);
}
if (newResults.Count > 0)
{
_logger.LogInformation($"On-demand analysis for session {sessionId}: {newResults.Count} new issue(s) detected");
}
_logger.LogInformation($"On-demand re-analysis for session {sessionId}: {freshResults.Count} issue(s) detected");
}
catch (Exception ex)
{
@@ -25,8 +25,9 @@ namespace AutopilotMonitor.Functions.Services
/// <summary>
/// Evaluates ALL active analyze rules against the full session event stream.
/// Called once at enrollment end or on-demand. Fetches events internally.
/// When reanalyze=true, all rules are re-evaluated regardless of existing results.
/// </summary>
public async Task<List<RuleResult>> AnalyzeSessionAsync(string tenantId, string sessionId)
public async Task<List<RuleResult>> AnalyzeSessionAsync(string tenantId, string sessionId, bool reanalyze = false)
{
var results = new List<RuleResult>();
@@ -41,9 +42,18 @@ namespace AutopilotMonitor.Functions.Services
return results;
}
// Get existing rule results to avoid duplicates
var existingResults = await _storageService.GetRuleResultsAsync(tenantId, sessionId);
var existingRuleIds = new HashSet<string>(existingResults.Select(r => r.RuleId));
// On reanalyze: skip deduplication so all rules are re-evaluated from scratch
// On normal run: skip rules already evaluated to avoid duplicate storage
HashSet<string> existingRuleIds;
if (reanalyze)
{
existingRuleIds = new HashSet<string>();
}
else
{
var existingResults = await _storageService.GetRuleResultsAsync(tenantId, sessionId);
existingRuleIds = new HashSet<string>(existingResults.Select(r => r.RuleId));
}
_logger.LogInformation($"Analyzing session {sessionId}: {allEvents.Count} events, {activeRules.Count} rules ({existingRuleIds.Count} already evaluated)");