Skip to content

Commit

Permalink
Merge pull request #807 from Project-MONAI/release/0.1.19
Browse files Browse the repository at this point in the history
Release/0.1.19
  • Loading branch information
woodheadio committed May 15, 2023
2 parents 5ffc9d6 + 4e15f84 commit adf72ae
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 6 deletions.
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
2 changes: 1 addition & 1 deletion src/WorkflowManager/Common/Interfaces/IWorkflowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface IWorkflowService : IPaginatedApi<WorkflowRevision>
/// </summary>
/// <param name="workflow">Workflow to Update.</param>
/// <param name="id">Id of the workflow to Update.</param>
Task<string?> UpdateAsync(Workflow workflow, string id);
Task<string?> UpdateAsync(Workflow workflow, string id, bool isUpdateToWorkflowName = false);

/// <summary>
/// Soft deletes a given workflow and all revisions
Expand Down
15 changes: 14 additions & 1 deletion src/WorkflowManager/Common/Services/WorkflowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,17 @@ public async Task<string> CreateAsync(Workflow workflow)
{
Guard.Against.Null(workflow);

foreach (var task in workflow.Tasks)
{
task.Args["workflow_name"] = workflow.Name;
}

var id = await _workflowRepository.CreateAsync(workflow);
_logger.WorkflowCreated(id, workflow.Name);
return id;
}

public async Task<string?> UpdateAsync(Workflow workflow, string id)
public async Task<string?> UpdateAsync(Workflow workflow, string id, bool isUpdateToWorkflowName = false)
{
Guard.Against.Null(workflow);
Guard.Against.NullOrWhiteSpace(id);
Expand All @@ -71,6 +76,14 @@ public async Task<string> CreateAsync(Workflow workflow)
return null;
}

if (isUpdateToWorkflowName)
{
foreach (var task in workflow.Tasks)
{
task.Args["workflow_name"] = workflow.Name;
}
}

var result = await _workflowRepository.UpdateAsync(workflow, existingWorkflow);
_logger.WorkflowUpdated(id, workflow.Name);
return result;
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 @@ -261,7 +261,7 @@ public async Task<IActionResult> UpdateAsync([FromBody] WorkflowUpdateRequest re

try
{
var workflowId = await _workflowService.UpdateAsync(workflow, id);
var workflowId = await _workflowService.UpdateAsync(workflow, id, workflow.Name != originalName);

if (workflowId == null)
{
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
14 changes: 13 additions & 1 deletion tests/UnitTests/Common.Tests/Services/WorkflowServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public async Task WorkflowService_CreateAsync_With_Empty()
await Assert.ThrowsAsync<ArgumentNullException>(() => WorkflowService.CreateAsync(null));
}

[Fact]
public async Task WorkflowService_CreateAsync_With_ValidReturn()
{
var expectedResult = "1";
_workflowRepository.Setup(w => w.CreateAsync(It.IsAny<Workflow>())).ReturnsAsync(expectedResult);
var tasks = new TaskObject[] { new TaskObject() };
var result = await WorkflowService.CreateAsync(new Workflow() { Name = "workflow1", Tasks = tasks });

Assert.Equal(expectedResult, result);
}


[Fact]
public async Task WorkflowService_WorkflowExists_ReturnsWorkflowId()
{
Expand Down Expand Up @@ -102,7 +114,7 @@ public async Task WorkflowService_WorkflowExists_ReturnsWorkflowId()
_workflowRepository.Setup(w => w.GetByWorkflowIdAsync(workflowRevision.WorkflowId)).ReturnsAsync(workflowRevision);
_workflowRepository.Setup(w => w.UpdateAsync(It.IsAny<Workflow>(), workflowRevision)).ReturnsAsync(workflowRevision.WorkflowId);

var result = await WorkflowService.UpdateAsync(new Workflow(), workflowRevision.WorkflowId);
var result = await WorkflowService.UpdateAsync(new Workflow(), workflowRevision.WorkflowId, true);

Assert.Equal(workflowRevision.WorkflowId, result);
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ public async Task UpdateAsync_WorkflowsExist_ReturnsWorkflowId()
request.Workflow = newWorkflow;
request.OriginalWorkflowName = newWorkflow.Name + "1";

_workflowService.Setup(w => w.UpdateAsync(newWorkflow, workflowRevision.WorkflowId)).ReturnsAsync(workflowRevision.WorkflowId);
_workflowService.Setup(w => w.UpdateAsync(newWorkflow, workflowRevision.WorkflowId, It.IsAny<bool>())).ReturnsAsync(workflowRevision.WorkflowId);

var result = await WorkflowsController.UpdateAsync(request, workflowRevision.WorkflowId);

Expand Down

0 comments on commit adf72ae

Please sign in to comment.