Skip to content

Commit

Permalink
Merge pull request #802 from Project-MONAI/AC-2220
Browse files Browse the repository at this point in the history
Return 'totalSucceeded' stat in TaskExecutionStats
  • Loading branch information
mehranrashid authored May 15, 2023
2 parents fa43b84 + 8960674 commit 2a0c339
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Shared/Shared/Wrappers/StatsPagedResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class StatsPagedResponse<T> : PagedResponse<T>
public DateTime PeriodStart { get; set; }
public DateTime PeriodEnd { get; set; }
public long TotalExecutions { get; set; }
public long TotalSucceeded { get; set; }
public long TotalFailures { get; set; }
public long TotalInprogress { get; set; }
public double AverageTotalExecutionSeconds { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public interface ITaskExecutionStatsRepository
/// <returns>The count of all records in range</returns>
Task<long> GetStatsStatusCountAsync(DateTime start, DateTime endTime, string status = "", string workflowId = "", string taskId = "");

/// <summary>
/// Returns all stats in Succeeded status.
/// </summary>
/// <param name="startTime">start of the range.</param>
/// <param name="endTime">end of the range.</param>
/// <returns>All stats that succeeded</returns>
Task<long> GetStatsStatusSucceededCountAsync(DateTime startTime, DateTime endTime, string workflowId = "", string taskId = "");

/// <summary>
/// Returns all stats in Failed or PartialFail status.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@ public async Task<long> GetStatsStatusCountAsync(DateTime start, DateTime endTim
(statusNull || T.Status == status));
}

public async Task<long> GetStatsStatusSucceededCountAsync(DateTime startTime, DateTime endTime, string workflowId = "", string taskId = "")
{
var workflowNull = string.IsNullOrWhiteSpace(workflowId);
var taskIdNull = string.IsNullOrWhiteSpace(taskId);

return await _taskExecutionStatsCollection.CountDocumentsAsync(T =>
T.StartedUTC >= startTime.ToUniversalTime() &&
T.StartedUTC <= endTime.ToUniversalTime() &&
(workflowNull || T.WorkflowId == workflowId) &&
(taskIdNull || T.TaskId == taskId) &&
T.Status == TaskExecutionStatus.Succeeded.ToString());
}

public async Task<long> GetStatsStatusFailedCountAsync(DateTime startTime, DateTime endTime, string workflowId = "", string taskId = "")
{
var workflowNull = string.IsNullOrWhiteSpace(workflowId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public async Task<IActionResult> GetOverviewAsync([FromQuery] DateTime startTime

try
{
var successes = _repository.GetStatsStatusSucceededCountAsync(startTime, endTime);
var fails = _repository.GetStatsStatusFailedCountAsync(startTime, endTime);
var running = _repository.GetStatsStatusCountAsync(startTime, endTime, TaskExecutionStatus.Accepted.ToString());
var rangeCount = _repository.GetStatsStatusCountAsync(startTime, endTime);
Expand All @@ -98,6 +99,7 @@ public async Task<IActionResult> GetOverviewAsync([FromQuery] DateTime startTime
PeriodStart = startTime,
PeriodEnd = endTime,
TotalExecutions = (int)rangeCount.Result,
TotalSucceeded = (int)successes.Result,
TotalFailures = (int)fails.Result,
TotalInprogress = running.Result,
AverageTotalExecutionSeconds = Math.Round(stats.Result.avgTotalExecution, 2),
Expand Down Expand Up @@ -149,6 +151,7 @@ public async Task<IActionResult> GetStatsAsync([FromQuery] TimeFilter filter, st
try
{
var allStats = _repository.GetStatsAsync(filter.StartTime, filter.EndTime, pageSize, filter.PageNumber, workflowId ?? string.Empty, taskId ?? string.Empty);
var successes = _repository.GetStatsStatusSucceededCountAsync(filter.StartTime, filter.EndTime, workflowId ?? string.Empty, taskId ?? string.Empty);
var fails = _repository.GetStatsStatusFailedCountAsync(filter.StartTime, filter.EndTime, workflowId ?? string.Empty, taskId ?? string.Empty);
var rangeCount = _repository.GetStatsStatusCountAsync(filter.StartTime, filter.EndTime, string.Empty, workflowId ?? string.Empty, taskId ?? string.Empty);
var stats = _repository.GetAverageStats(filter.StartTime, filter.EndTime, workflowId ?? string.Empty, taskId ?? string.Empty);
Expand All @@ -168,6 +171,7 @@ public async Task<IActionResult> GetStatsAsync([FromQuery] TimeFilter filter, st
res.PeriodStart = filter.StartTime;
res.PeriodEnd = filter.EndTime;
res.TotalExecutions = rangeCount.Result;
res.TotalSucceeded = successes.Result;
res.TotalFailures = fails.Result;
res.TotalInprogress = running.Result;
res.AverageTotalExecutionSeconds = Math.Round(stats.Result.avgTotalExecution, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"periodStart": "2023-01-01T00:00:00",
"periodEnd": "2023-04-26T15:01:01.2301205+01:00",
"totalExecutions": 1,
"totalSucceeded": 1,
"totalFailures": 0,
"totalInprogress": 1,
"averageTotalExecutionSeconds": 30.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"periodStart": "2023-01-01T00:00:00",
"periodEnd": "2023-04-26T15:01:01.0902241+01:00",
"totalExecutions": 5,
"totalSucceeded": 3,
"totalFailures": 1,
"totalInprogress": 1,
"averageTotalExecutionSeconds": 30.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task GetListAsync_PayloadsExist_ReturnsList()
[Fact]
public async Task GetStatsOverviewAsync_ServiceException_ReturnProblem()
{
_repo.Setup(w => w.GetStatsStatusFailedCountAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>())).ThrowsAsync(new Exception());
_repo.Setup(w => w.GetStatsStatusSucceededCountAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>())).ThrowsAsync(new Exception());

var result = await StatsController.GetOverviewAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>());

Expand Down Expand Up @@ -138,6 +138,22 @@ public async Task GetStatsAsync_Pass_All_Arguments_To_GetStatsAsync_In_Repo()
);
}

[Fact]
public async Task GetStatsAsync_Pass_All_Arguments_To_GetStatsStatusSucceededCountAsync_in_Repo()
{
var startTime = new DateTime(2023, 4, 4);
var endTime = new DateTime(2023, 4, 5);

var result = await StatsController.GetStatsAsync(new TimeFilter { StartTime = startTime, EndTime = endTime }, "workflow", "task");

_repo.Verify(v => v.GetStatsStatusSucceededCountAsync(
It.Is<DateTime>(d => d.Equals(startTime)),
It.Is<DateTime>(d => d.Equals(endTime)),
It.Is<string>(s => s.Equals("workflow")),
It.Is<string>(s => s.Equals("task"))));
}


[Fact]
public async Task GetStatsAsync_Pass_All_Arguments_To_GetStatsStatusFailedCountAsync_in_Repo()
{
Expand Down Expand Up @@ -200,6 +216,21 @@ public async Task GetOverviewAsync_Pass_All_Arguments_To_GetStatsCountAsync_in_R
It.Is<string>(s => s.Equals(""))));
}

[Fact]
public async Task GetOverviewAsync_Pass_All_Arguments_To_GetStatsStatusSucceededCountAsync_in_Repo()
{
var startTime = new DateTime(2023, 4, 4);
var endTime = new DateTime(2023, 4, 5);

var result = await StatsController.GetOverviewAsync(startTime, endTime);

_repo.Verify(v => v.GetStatsStatusSucceededCountAsync(
It.Is<DateTime>(d => d.Equals(startTime)),
It.Is<DateTime>(d => d.Equals(endTime)),
It.Is<string>(s => s.Equals("")),
It.Is<string>(s => s.Equals(""))));
}

[Fact]
public async Task GetOverviewAsync_Pass_All_Arguments_To_GetStatsStatusFailedCountAsync_in_Repo()
{
Expand Down

0 comments on commit 2a0c339

Please sign in to comment.