-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathAnalysisService.cs
More file actions
357 lines (309 loc) · 13.7 KB
/
Copy pathAnalysisService.cs
File metadata and controls
357 lines (309 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using PerformanceMonitor.Analysis;
using PerformanceMonitorDashboard.Helpers;
namespace PerformanceMonitorDashboard.Analysis;
/// <summary>
/// Orchestrates the full analysis pipeline: collect → score → traverse → persist.
/// Can be run on-demand or on a timer. Each run analyzes a single server's data
/// for a given time window and persists the findings.
/// Port of Lite's AnalysisService — uses SQL Server instead of DuckDB.
/// </summary>
public class AnalysisService
{
private readonly string _connectionString;
private readonly SqlServerFindingStore _findingStore;
private readonly SqlServerFactCollector _collector;
private readonly FactScorer _scorer;
private readonly RelationshipGraph _graph;
private readonly InferenceEngine _engine;
private readonly SqlServerDrillDownCollector _drillDown;
private readonly SqlServerAnomalyDetector _anomalyDetector;
private readonly SqlServerBaselineProvider _baselineProvider;
/// <summary>
/// Minimum hours of collected data required before analysis will run.
/// Short collection windows distort fraction-of-period calculations —
/// 5 seconds of THREADPOOL looks alarming in a 16-minute window.
/// 24 hours has been validated empirically as sufficient.
/// </summary>
internal double MinimumDataHours { get; set; } = 24;
/// <summary>
/// Raised after each analysis run completes, providing the findings for UI display.
/// </summary>
public event EventHandler<AnalysisCompletedEventArgs>? AnalysisCompleted;
/// <summary>
/// Whether an analysis is currently running.
/// </summary>
public bool IsAnalyzing { get; private set; }
/// <summary>
/// Time of the last completed analysis run.
/// </summary>
public DateTime? LastAnalysisTime { get; private set; }
/// <summary>
/// Set after AnalyzeAsync if insufficient data was found. Null if enough data exists.
/// </summary>
public string? InsufficientDataMessage { get; private set; }
public AnalysisService(string connectionString, IPlanFetcher? planFetcher = null)
{
_connectionString = connectionString;
_findingStore = new SqlServerFindingStore(connectionString);
_collector = new SqlServerFactCollector(connectionString);
_scorer = new FactScorer();
_graph = new RelationshipGraph();
_engine = new InferenceEngine(_graph);
_drillDown = new SqlServerDrillDownCollector(connectionString, planFetcher);
_baselineProvider = new SqlServerBaselineProvider(connectionString);
_anomalyDetector = new SqlServerAnomalyDetector(connectionString, _baselineProvider);
}
/// <summary>
/// Runs the full analysis pipeline for a server.
/// Default time range is the last 4 hours.
/// </summary>
public async Task<List<AnalysisFinding>> AnalyzeAsync(int serverId, string serverName, int hoursBack = 4)
{
var timeRangeEnd = DateTime.UtcNow;
var timeRangeStart = timeRangeEnd.AddHours(-hoursBack);
var context = new AnalysisContext
{
ServerId = serverId,
ServerName = serverName,
TimeRangeStart = timeRangeStart,
TimeRangeEnd = timeRangeEnd
};
return await AnalyzeAsync(context);
}
/// <summary>
/// Runs the full analysis pipeline with a specific context.
/// </summary>
public async Task<List<AnalysisFinding>> AnalyzeAsync(AnalysisContext context)
{
if (IsAnalyzing)
return [];
IsAnalyzing = true;
InsufficientDataMessage = null;
try
{
// 0. Check minimum data span — total history, not the analysis window.
// A server with 100h of total history can be analyzed over a 4h window.
var dataSpanHours = await GetTotalDataSpanHoursAsync();
if (dataSpanHours < MinimumDataHours)
{
var needed = MinimumDataHours >= 24
? $"{MinimumDataHours / 24:F1} days"
: $"{MinimumDataHours:F0} hours";
var have = dataSpanHours >= 24
? $"{dataSpanHours / 24:F1} days"
: $"{dataSpanHours:F1} hours";
InsufficientDataMessage =
$"Not enough data for reliable analysis. Need {needed} of collected data, " +
$"have {have}. Keep the collector running and try again later.";
Logger.Info(
$"[AnalysisService] Skipping analysis for {context.ServerName}: {dataSpanHours:F1}h data, need {MinimumDataHours}h");
LastAnalysisTime = DateTime.UtcNow;
return [];
}
// 1. Collect facts from SQL Server
var facts = await _collector.CollectFactsAsync(context);
if (facts.Count == 0)
{
LastAnalysisTime = DateTime.UtcNow;
return [];
}
// 1.5. Detect anomalies (compare analysis window against baseline)
var anomalies = await _anomalyDetector.DetectAnomaliesAsync(context);
facts.AddRange(anomalies);
// 2. Score facts (base severity + amplifiers)
_scorer.ScoreAll(facts);
// 3. Build stories via graph traversal
var stories = _engine.BuildStories(facts);
// 4. Mute-filter the stories into the surviving findings (P2 reorder) — WITHOUT
// inserting yet, so enrichment + action-build happen on the survivors first
// and the BUILT RemediationAction is persisted on each row (D2). Muted/
// absolution findings are dropped here and never enriched (no enrich-then-
// discard; round-3 MODERATE-2).
var findings = await _findingStore.FilterMutedFindingsAsync(stories, context);
// 5. Enrich the survivors with drill-down data (ephemeral, not persisted). The
// cheap config drill-down runs regardless of severity (D7), so config/RCSI/
// db-config actions can build at their true 0.3 severity; the expensive
// plan-fetch enrichment stays behind the 0.5 gate inside the collector.
await _drillDown.EnrichFindingsAsync(findings, context);
// 6. Build + attach each finding's RemediationAction from the now drill-down-
// populated finding (D2). The builders REQUIRE finding.DrillDown, which the
// store read-back does not return — so the BUILT action is persisted, exactly
// the artifact the alert path serializes into ContextJson. Try the always-safe/
// db-config force action first, then the two destructive entry points (each
// gates internally on RootFactKey + drill-down and returns null when N/A);
// attach the first non-null.
foreach (var finding in findings)
{
finding.Remediation =
FactRemediation.BuildAction(finding)
?? FactRemediation.BuildRcsiAction(finding)
?? FactRemediation.BuildClearPlanAction(finding)
?? FactRemediation.BuildFileAutogrowthAction(finding); // WS3: advisory only (no handler -> no Apply); carried for the read-time copy-paste
}
// 7. Insert the survivors in one batched pass, persisting remediation_action_json
// (D2). Reuses PR-1's single-connection + single-schema-check discipline.
await _findingStore.InsertFindingsAsync(findings, context);
LastAnalysisTime = DateTime.UtcNow;
// 8. Notify listeners — the returned/enriched findings (now action-bearing) flow
// to the AnalysisCompleted event and, via the scheduler, to NotifyAsync, which
// builds its own context from the drill-down. The reorder did not change which
// list notify receives.
AnalysisCompleted?.Invoke(this, new AnalysisCompletedEventArgs
{
ServerId = context.ServerId,
ServerName = context.ServerName,
Findings = findings,
AnalysisTime = LastAnalysisTime.Value
});
Logger.Info(
$"[AnalysisService] Analysis complete for {context.ServerName}: {findings.Count} finding(s), " +
$"highest severity {(findings.Count > 0 ? findings.Max(f => f.Severity) : 0):F2}");
return findings;
}
catch (Exception ex)
{
Logger.Error($"[AnalysisService] Analysis failed for {context.ServerName}: {ex.Message}");
return [];
}
finally
{
IsAnalyzing = false;
}
}
/// <summary>
/// Runs the collect + score pipeline without graph traversal.
/// Returns raw scored facts with amplifier details for direct inspection.
/// </summary>
public async Task<List<Fact>> CollectAndScoreFactsAsync(int serverId, string serverName, int hoursBack = 4)
{
var timeRangeEnd = DateTime.UtcNow;
var timeRangeStart = timeRangeEnd.AddHours(-hoursBack);
var context = new AnalysisContext
{
ServerId = serverId,
ServerName = serverName,
TimeRangeStart = timeRangeStart,
TimeRangeEnd = timeRangeEnd
};
try
{
var facts = await _collector.CollectFactsAsync(context);
if (facts.Count == 0) return facts;
_scorer.ScoreAll(facts);
return facts;
}
catch (Exception ex)
{
Logger.Error($"[AnalysisService] Fact collection failed for {serverName}: {ex.Message}");
return [];
}
}
/// <summary>
/// Compares analysis of two time periods, returning facts from both for comparison.
/// </summary>
public async Task<(List<Fact> BaselineFacts, List<Fact> ComparisonFacts)> ComparePeriodsAsync(
int serverId, string serverName,
DateTime baselineStart, DateTime baselineEnd,
DateTime comparisonStart, DateTime comparisonEnd)
{
var baselineContext = new AnalysisContext
{
ServerId = serverId,
ServerName = serverName,
TimeRangeStart = baselineStart,
TimeRangeEnd = baselineEnd
};
var comparisonContext = new AnalysisContext
{
ServerId = serverId,
ServerName = serverName,
TimeRangeStart = comparisonStart,
TimeRangeEnd = comparisonEnd
};
try
{
var baselineFacts = await _collector.CollectFactsAsync(baselineContext);
var comparisonFacts = await _collector.CollectFactsAsync(comparisonContext);
_scorer.ScoreAll(baselineFacts);
_scorer.ScoreAll(comparisonFacts);
return (baselineFacts, comparisonFacts);
}
catch (Exception ex)
{
Logger.Error($"[AnalysisService] Period comparison failed for {serverName}: {ex.Message}");
return ([], []);
}
}
/// <summary>
/// Gets the latest findings for a server without running a new analysis.
/// </summary>
public async Task<List<AnalysisFinding>> GetLatestFindingsAsync(int serverId)
{
return await _findingStore.GetLatestFindingsAsync(serverId);
}
/// <summary>
/// Gets recent findings for a server within the given time range.
/// </summary>
public async Task<List<AnalysisFinding>> GetRecentFindingsAsync(int serverId, int hoursBack = 24)
{
return await _findingStore.GetRecentFindingsAsync(serverId, hoursBack);
}
/// <summary>
/// Mutes a finding pattern so it won't appear in future runs.
/// </summary>
public async Task MuteFindingAsync(AnalysisFinding finding, string? reason = null)
{
await _findingStore.MuteStoryAsync(
finding.ServerId, finding.StoryPathHash, finding.StoryPath, reason);
}
/// <summary>
/// Cleans up old findings beyond the retention period.
/// </summary>
public async Task CleanupAsync(int retentionDays = 30)
{
await _findingStore.CleanupOldFindingsAsync(retentionDays);
}
/// <summary>
/// Returns the total span of collected data (no time range filter).
/// This answers "has this server been monitored long enough?" — separate from
/// the analysis window. A server with 100 hours of total history can safely
/// be analyzed over a 4-hour window without dilution.
/// Dashboard monitors one server per database, so no server_id filtering.
/// </summary>
private async Task<double> GetTotalDataSpanHoursAsync()
{
try
{
using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync();
using var cmd = connection.CreateCommand();
cmd.CommandText = @"
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT DATEDIFF(SECOND, MIN(collection_time), MAX(collection_time)) / 3600.0
FROM collect.wait_stats;";
var result = await cmd.ExecuteScalarAsync();
if (result == null || result is DBNull)
return 0;
return Convert.ToDouble(result);
}
catch
{
return 0;
}
}
}
/// <summary>
/// Event args for when an analysis run completes.
/// </summary>
public class AnalysisCompletedEventArgs : EventArgs
{
public int ServerId { get; set; }
public string ServerName { get; set; } = string.Empty;
public List<AnalysisFinding> Findings { get; set; } = [];
public DateTime AnalysisTime { get; set; }
}