Fix caching and loop query on the get current user
This commit is contained in:
@@ -20,7 +20,7 @@ public class SqlLoopDetectionService
|
||||
{
|
||||
_logger = logger;
|
||||
_queryTrackers = new ConcurrentDictionary<string, QueryExecutionTracker>();
|
||||
|
||||
|
||||
// Setup cleanup timer to remove old tracking data
|
||||
_cleanupTimer = new Timer(CleanupOldTrackers, null, _cleanupInterval, _cleanupInterval);
|
||||
}
|
||||
@@ -33,12 +33,13 @@ public class SqlLoopDetectionService
|
||||
/// <param name="queryPattern">Pattern or hash of the query being executed</param>
|
||||
/// <param name="executionTime">Time taken to execute the query</param>
|
||||
/// <returns>True if a potential loop is detected</returns>
|
||||
public bool TrackQueryExecution(string repositoryName, string methodName, string queryPattern, TimeSpan executionTime)
|
||||
public bool TrackQueryExecution(string repositoryName, string methodName, string queryPattern,
|
||||
TimeSpan executionTime)
|
||||
{
|
||||
var key = $"{repositoryName}.{methodName}.{queryPattern}";
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
var tracker = _queryTrackers.AddOrUpdate(key,
|
||||
|
||||
var tracker = _queryTrackers.AddOrUpdate(key,
|
||||
new QueryExecutionTracker
|
||||
{
|
||||
RepositoryName = repositoryName,
|
||||
@@ -56,8 +57,12 @@ public class SqlLoopDetectionService
|
||||
existing.LastExecution = now;
|
||||
existing.ExecutionCount++;
|
||||
existing.TotalExecutionTime += executionTime;
|
||||
existing.MaxExecutionTime = existing.MaxExecutionTime > executionTime ? existing.MaxExecutionTime : executionTime;
|
||||
existing.MinExecutionTime = existing.MinExecutionTime < executionTime ? existing.MinExecutionTime : executionTime;
|
||||
existing.MaxExecutionTime = existing.MaxExecutionTime > executionTime
|
||||
? existing.MaxExecutionTime
|
||||
: executionTime;
|
||||
existing.MinExecutionTime = existing.MinExecutionTime < executionTime
|
||||
? existing.MinExecutionTime
|
||||
: executionTime;
|
||||
return existing;
|
||||
});
|
||||
|
||||
@@ -86,7 +91,8 @@ public class SqlLoopDetectionService
|
||||
if (tracker.ExecutionCount > 5 && timeSinceFirst.TotalSeconds < 10)
|
||||
{
|
||||
isLoopDetected = true;
|
||||
reasons.Add($"Rapid execution: {tracker.ExecutionCount} executions in {timeSinceFirst.TotalSeconds:F1} seconds");
|
||||
reasons.Add(
|
||||
$"Rapid execution: {tracker.ExecutionCount} executions in {timeSinceFirst.TotalSeconds:F1} seconds");
|
||||
}
|
||||
|
||||
// Check for consistently slow queries
|
||||
@@ -100,13 +106,13 @@ public class SqlLoopDetectionService
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"[SQL-LOOP-DETECTED] {Repository}.{Method} | Pattern: {Pattern} | Count: {Count} | Reasons: {Reasons} | Avg Time: {AvgTime}ms",
|
||||
repositoryName, methodName, queryPattern, tracker.ExecutionCount,
|
||||
repositoryName, methodName, queryPattern, tracker.ExecutionCount,
|
||||
string.Join(", ", reasons), tracker.AverageExecutionTime.TotalMilliseconds);
|
||||
|
||||
// Log detailed execution history
|
||||
_logger.LogWarning(
|
||||
"[SQL-LOOP-DETAILS] {Repository}.{Method} | First: {First} | Last: {Last} | Min: {Min}ms | Max: {Max}ms | Total: {Total}ms",
|
||||
repositoryName, methodName, tracker.FirstExecution.ToString("HH:mm:ss.fff"),
|
||||
repositoryName, methodName, tracker.FirstExecution.ToString("HH:mm:ss.fff"),
|
||||
tracker.LastExecution.ToString("HH:mm:ss.fff"), tracker.MinExecutionTime.TotalMilliseconds,
|
||||
tracker.MaxExecutionTime.TotalMilliseconds, tracker.TotalExecutionTime.TotalMilliseconds);
|
||||
}
|
||||
@@ -126,7 +132,7 @@ public class SqlLoopDetectionService
|
||||
{
|
||||
var tracker = kvp.Value;
|
||||
var timeSinceFirst = now - tracker.FirstExecution;
|
||||
|
||||
|
||||
stats[kvp.Key] = new QueryExecutionStats
|
||||
{
|
||||
RepositoryName = tracker.RepositoryName,
|
||||
@@ -197,7 +203,7 @@ public class SqlLoopDetectionService
|
||||
public TimeSpan MaxExecutionTime { get; set; }
|
||||
public TimeSpan MinExecutionTime { get; set; }
|
||||
|
||||
public TimeSpan AverageExecutionTime =>
|
||||
public TimeSpan AverageExecutionTime =>
|
||||
ExecutionCount > 0 ? TimeSpan.FromTicks(TotalExecutionTime.Ticks / ExecutionCount) : TimeSpan.Zero;
|
||||
}
|
||||
}
|
||||
@@ -218,4 +224,4 @@ public class QueryExecutionStats
|
||||
public TimeSpan MaxExecutionTime { get; set; }
|
||||
public double ExecutionsPerMinute { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user