Skip to content

Commit

Permalink
Update CommandView to keep a rolling + summary
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Apr 6, 2024
1 parent 4e4a466 commit 0e0e2e5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
17 changes: 12 additions & 5 deletions MyApp.ServiceInterface/BackgroundMqServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task ExecuteAsync<T>(IExecuteCommandAsync<T> command, T request) wh
await command.ExecuteAsync(request);
log.LogDebug("{Command} took {ElapsedMilliseconds}ms to execute", commandName, sw.ElapsedMilliseconds);
#if DEBUG
appConfig.CommandResults.Add(new() {
appConfig.AddCommandResult(new() {
Name = commandName,
Ms = sw.ElapsedMilliseconds,
});
Expand All @@ -63,7 +63,7 @@ public async Task ExecuteAsync<T>(IExecuteCommandAsync<T> command, T request) wh
{
log.LogError(e, "{Command}({Request}) failed: {Message}", commandName, request.ToJsv(), e.Message);
#if DEBUG
appConfig.CommandResults.Add(new() {
appConfig.AddCommandResult(new() {
Name = commandName,
Ms = sw.ElapsedMilliseconds,
Request = request,
Expand Down Expand Up @@ -148,11 +148,18 @@ public async Task Any(AnalyticsTasks request)

public object Any(ViewCommands request)
{
var results = appConfig.CommandResults;
var to = new ViewCommandsResponse
{
LatestResults = new(appConfig.CommandResults),
LatestFailed = new(appConfig.CommandFailures),
Totals = new(appConfig.CommandTotals.Values)
};
if (request.Clear == true)
{
appConfig.CommandResults = [];
appConfig.CommandResults.Clear();
appConfig.CommandFailures.Clear();
appConfig.CommandTotals.Clear();
}
return results;
return to;
}
}
60 changes: 58 additions & 2 deletions MyApp.ServiceInterface/Data/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,53 @@ public bool HasUnreadAchievements(string? userName)
return userName != null && UsersUnreadAchievements.TryGetValue(userName, out var count) && count > 0;
}

public List<CommandResult> CommandResults { get; set; } = [];

public bool IsHuman(string? userName) => userName != null && GetModelUser(userName) == null;

public const int DefaultCapacity = 500;
public ConcurrentQueue<CommandResult> CommandResults { get; set; } = [];
public ConcurrentQueue<CommandResult> CommandFailures { get; set; } = new();

public ConcurrentDictionary<string, CommandSummary> CommandTotals { get; set; } = new();

public void AddCommandResult(CommandResult result)
{
var ms = result.Ms ?? 0;
if (result.Error == null)
{
CommandResults.Enqueue(result);
while (CommandResults.Count > DefaultCapacity)
CommandResults.TryDequeue(out _);

CommandTotals.AddOrUpdate(result.Name,
_ => new CommandSummary { Name = result.Name, Count = 1, TotalMs = ms, MinMs = ms > 0 ? ms : int.MinValue },
(_, summary) =>
{
summary.Count++;
summary.TotalMs += ms;
summary.MaxMs = Math.Max(summary.MaxMs, ms);
if (ms > 0)
{
summary.MinMs = Math.Min(summary.MinMs, ms);
}
return summary;
});
}
else
{
CommandFailures.Enqueue(result);
while (CommandFailures.Count > DefaultCapacity)
CommandFailures.TryDequeue(out _);

CommandTotals.AddOrUpdate(result.Name,
_ => new CommandSummary { Name = result.Name, Failed = 1, Count = 0, TotalMs = 0, MinMs = int.MinValue, LastError = result.Error },
(_, summary) =>
{
summary.Failed++;
summary.LastError = result.Error;
return summary;
});
}
}
}

public class CommandResult
Expand All @@ -223,3 +267,15 @@ public class CommandResult
public object Request { get; set; }
public string? Error { get; set; }
}

public class CommandSummary
{
public string Name { get; set; }
public long Count { get; set; }
public long Failed { get; set; }
public long TotalMs { get; set; }
public long MinMs { get; set; }
public long MaxMs { get; set; }
public int AverageMs => (int) Math.Floor(TotalMs / (double)Count);
public string? LastError { get; set; }
}
11 changes: 9 additions & 2 deletions MyApp.ServiceInterface/Data/Tasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,15 @@ public class SearchTasks

[Tag(Tag.Tasks)]
[ExcludeMetadata]
[Restrict(InternalOnly = true)]
public class ViewCommands : IGet, IReturn<CommandResult[]>
[ValidateIsAdmin]
public class ViewCommands : IGet, IReturn<ViewCommandsResponse>
{
public bool? Clear { get; set; }
}
public class ViewCommandsResponse
{
public List<CommandResult> LatestResults { get; set; }
public List<CommandResult> LatestFailed { get; set; }
public List<CommandSummary> Totals { get; set; }
public ResponseStatus? ResponseStatus { get; set; }
}

0 comments on commit 0e0e2e5

Please sign in to comment.