-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryAnalysisExtensions.cs
More file actions
328 lines (293 loc) · 14 KB
/
Copy pathQueryAnalysisExtensions.cs
File metadata and controls
328 lines (293 loc) · 14 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
#nullable enable
// =============================================================================
// Author: Vladyslav Zaiets | https://sarmkadan.com
// CTO & Software Architect
// =============================================================================
using System;
using SqlQueryAnalyzer.Models;
namespace SqlQueryAnalyzer.Extensions;
/// <summary>
/// Extension methods for query analysis results.
/// Provides convenient operations on analysis data.
/// </summary>
public static class QueryAnalysisExtensions
{
/// <summary>
/// Gets all issues of a specific severity level.
/// </summary>
/// <param name="severity">The severity level to filter by.</param>
/// <returns>List of performance issues matching the specified severity.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
public static List<PerformanceIssue> GetIssuesBySeverity(
this QueryAnalysisResult result,
Constants.IssueSeverity severity)
{
ArgumentNullException.ThrowIfNull(result);
return result.Issues.Where(i => i.Severity == severity).ToList();
}
/// <summary>
/// Gets all issues of a specific type.
/// </summary>
/// <param name="issueType">The issue type to filter by.</param>
/// <returns>List of performance issues matching the specified type.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
public static List<PerformanceIssue> GetIssuesByType(
this QueryAnalysisResult result,
Constants.IssueType issueType)
{
ArgumentNullException.ThrowIfNull(result);
return result.Issues.Where(i => i.IssueType == issueType).ToList();
}
/// <summary>
/// Gets top N issues by performance impact.
/// </summary>
/// <param name="count">Maximum number of issues to return. Default is 5.</param>
/// <returns>List of top performance issues ordered by impact.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than 1.</exception>
public static List<PerformanceIssue> GetTopIssuesByImpact(
this QueryAnalysisResult result,
int count = 5)
{
ArgumentNullException.ThrowIfNull(result);
ArgumentOutOfRangeException.ThrowIfLessThan(count, 1);
return result.Issues
.OrderByDescending(i => i.EstimatedPerformanceImpact)
.Take(count)
.ToList();
}
/// <summary>
/// Gets top N index suggestions by performance gain.
/// </summary>
/// <param name="count">Maximum number of suggestions to return. Default is 3.</param>
/// <returns>List of top index suggestions ordered by estimated performance gain.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than 1.</exception>
public static List<IndexSuggestion> GetTopSuggestions(
this QueryAnalysisResult result,
int count = 3)
{
ArgumentNullException.ThrowIfNull(result);
ArgumentOutOfRangeException.ThrowIfLessThan(count, 1);
return result.IndexSuggestions
.OrderByDescending(s => s.EstimatedPerformanceGain)
.Take(count)
.ToList();
}
/// <summary>
/// Checks if result has any issues of critical severity.
/// </summary>
/// <returns><see langword="true"/> if critical issues exist; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
public static bool HasCriticalProblems(this QueryAnalysisResult result)
{
ArgumentNullException.ThrowIfNull(result);
return result.HasCriticalIssues;
}
/// <summary>
/// Checks if result meets minimum performance threshold.
/// </summary>
/// <param name="threshold">Minimum acceptable performance score (0-100). Default is 70.0.</param>
/// <returns><see langword="true"/> if performance meets or exceeds threshold; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
public static bool MeetsPerformanceThreshold(this QueryAnalysisResult result, double threshold = 70.0)
{
ArgumentNullException.ThrowIfNull(result);
return result.PerformanceScore >= threshold;
}
/// <summary>
/// Gets issue summary grouped by type.
/// </summary>
/// <returns>Dictionary mapping issue types to their occurrence counts.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
public static Dictionary<Constants.IssueType, int> GetIssueSummary(this QueryAnalysisResult result)
{
ArgumentNullException.ThrowIfNull(result);
return result.Issues
.GroupBy(i => i.IssueType)
.ToDictionary(g => g.Key, g => g.Count());
}
/// <summary>
/// Calculates percentage improvement if all suggestions are implemented.
/// </summary>
/// <returns>Potential performance score improvement as a percentage point value.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
public static double GetPotentialImprovement(this QueryAnalysisResult result)
{
ArgumentNullException.ThrowIfNull(result);
// Score increases from total optimization potential
var newScore = Math.Min(100, result.PerformanceScore + result.TotalOptimizationPotential);
return newScore - result.PerformanceScore;
}
/// <summary>
/// Gets criticality level (0-10 scale).
/// 0 = no issues, 10 = critical problems.
/// </summary>
/// <returns>Criticality level from 0 to 10.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
public static int GetCriticalityLevel(this QueryAnalysisResult result)
{
ArgumentNullException.ThrowIfNull(result);
var criticalCount = result.Issues.Count(i => i.Severity == Constants.IssueSeverity.Critical);
var warningCount = result.Issues.Count(i => i.Severity == Constants.IssueSeverity.Warning);
var score = criticalCount * 3 + warningCount * 1;
return Math.Min(10, score);
}
/// <summary>
/// Gets a human-readable recommendation based on analysis.
/// </summary>
/// <returns>Recommendation string based on performance score and issues.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
public static string GetRecommendation(this QueryAnalysisResult result)
{
ArgumentNullException.ThrowIfNull(result);
if (result.PerformanceScore >= 90)
return "Query is well-optimized. No action required.";
if (result.PerformanceScore >= 70)
return "Query has minor optimization opportunities. Consider implementing index suggestions.";
var topIssue = result.GetTopIssuesByImpact(1).FirstOrDefault();
if (topIssue != null)
return $"Query has performance issues. Priority: Address {topIssue.IssueType}.";
return "Query analysis complete. Review results for optimization opportunities.";
}
/// <summary>
/// Merges multiple analysis results.
/// Useful for batch analysis summaries.
/// </summary>
/// <param name="results">Collection of analysis results to merge.</param>
/// <returns>Merged analysis result with averaged scores and combined issues/suggestions.</returns>
/// <exception cref="ArgumentNullException"><paramref name="results"/> is <see langword="null"/>.</exception>
public static QueryAnalysisResult Merge(
this IEnumerable<QueryAnalysisResult> results)
{
ArgumentNullException.ThrowIfNull(results);
var resultList = results.ToList();
if (!resultList.Any())
{
return new QueryAnalysisResult();
}
var merged = new QueryAnalysisResult
{
Query = "BATCH ANALYSIS",
PerformanceScore = resultList.Average(r => r.PerformanceScore),
Complexity = resultList.Max(r => r.Complexity),
Issues = resultList.SelectMany(r => r.Issues).ToList(),
IndexSuggestions = resultList.SelectMany(r => r.IndexSuggestions).Distinct().ToList()
};
return merged;
}
/// <summary>
/// Exports analysis result to dictionary for serialization.
/// </summary>
/// <returns>Dictionary containing key-value pairs for serialization.</returns>
/// <exception cref="ArgumentNullException"><paramref name="result"/> is <see langword="null"/>.</exception>
public static Dictionary<string, object> ExportAsJson(this QueryAnalysisResult result)
{
ArgumentNullException.ThrowIfNull(result);
return new Dictionary<string, object>
{
{ "id", result.QueryId },
{ "score", result.PerformanceScore },
{ "complexity", result.Complexity.ToString() },
{ "issues", result.Issues.Count },
{ "criticalIssues", result.Issues.Count(i => i.Severity == Constants.IssueSeverity.Critical) },
{ "suggestions", result.IndexSuggestions.Count },
{ "potential", result.TotalOptimizationPotential }
};
}
}
/// <summary>
/// Extension methods for collections of analysis results.
/// </summary>
public static class AnalysisResultCollectionExtensions
{
/// <summary>
/// Filters results by complexity level.
/// </summary>
/// <param name="complexity">The complexity level to filter by.</param>
/// <returns>List of results matching the specified complexity.</returns>
/// <exception cref="ArgumentNullException"><paramref name="results"/> is <see langword="null"/>.</exception>
public static List<QueryAnalysisResult> FilterByComplexity(
this IEnumerable<QueryAnalysisResult> results,
Constants.QueryComplexity complexity)
{
ArgumentNullException.ThrowIfNull(results);
return results.Where(r => r.Complexity == complexity).ToList();
}
/// <summary>
/// Filters results by performance score threshold.
/// </summary>
/// <param name="minScore">Minimum performance score (inclusive).</param>
/// <param name="maxScore">Maximum performance score (inclusive). Default is 100.</param>
/// <returns>List of results within the specified score range.</returns>
/// <exception cref="ArgumentNullException"><paramref name="results"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="minScore"/> is greater than <paramref name="maxScore"/>.</exception>
public static List<QueryAnalysisResult> FilterByScore(
this IEnumerable<QueryAnalysisResult> results,
double minScore,
double maxScore = 100)
{
ArgumentNullException.ThrowIfNull(results);
ArgumentOutOfRangeException.ThrowIfGreaterThan(minScore, maxScore);
return results
.Where(r => r.PerformanceScore >= minScore && r.PerformanceScore <= maxScore)
.ToList();
}
/// <summary>
/// Filters results that have critical issues.
/// </summary>
/// <returns>List of results containing critical issues.</returns>
/// <exception cref="ArgumentNullException"><paramref name="results"/> is <see langword="null"/>.</exception>
public static List<QueryAnalysisResult> WithCriticalIssues(
this IEnumerable<QueryAnalysisResult> results)
{
ArgumentNullException.ThrowIfNull(results);
return results.Where(r => r.HasCriticalIssues).ToList();
}
/// <summary>
/// Orders results by performance score (worst first).
/// </summary>
/// <returns>Ordered sequence of results by ascending performance score.</returns>
/// <exception cref="ArgumentNullException"><paramref name="results"/> is <see langword="null"/>.</exception>
public static IOrderedEnumerable<QueryAnalysisResult> OrderByPerformance(
this IEnumerable<QueryAnalysisResult> results)
{
ArgumentNullException.ThrowIfNull(results);
return results.OrderBy(r => r.PerformanceScore);
}
/// <summary>
/// Gets overall statistics for a batch of results.
/// </summary>
/// <param name="results">Collection of analysis results.</param>
/// <returns><see cref="BatchStatistics"/> object containing batch metrics.</returns>
/// <exception cref="ArgumentNullException"><paramref name="results"/> is <see langword="null"/>.</exception>
public static BatchStatistics GetBatchStatistics(this IEnumerable<QueryAnalysisResult> results)
{
ArgumentNullException.ThrowIfNull(results);
var resultList = results.ToList();
return new BatchStatistics
{
TotalQueries = resultList.Count,
AverageScore = resultList.Average(r => r.PerformanceScore),
WorstScore = resultList.Min(r => r.PerformanceScore),
BestScore = resultList.Max(r => r.PerformanceScore),
TotalIssuesFound = resultList.Sum(r => r.Issues.Count),
QueriesWithIssues = resultList.Count(r => r.Issues.Count > 0)
};
}
}
/// <summary>
/// Statistics for a batch of analysis results.
/// </summary>
public sealed class BatchStatistics
{
public int TotalQueries { get; set; }
public double AverageScore { get; set; }
public double WorstScore { get; set; }
public double BestScore { get; set; }
public int TotalIssuesFound { get; set; }
public int QueriesWithIssues { get; set; }
public override string ToString() =>
$"Batch Stats: {TotalQueries} queries, avg={AverageScore:F1}, " +
$"range={WorstScore:F0}-{BestScore:F0}, {TotalIssuesFound} issues in {QueriesWithIssues} queries";
}