Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding failure reason to executionStats #984

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

namespace Monai.Deploy.WorkflowManager.Common.Contracts.Migrations
{
public class M001_ExecutionStats_addVersion : DocumentMigration<ExecutionStats>

Check warning on line 23 in src/WorkflowManager/Contracts/Migrations/M001_ExecutionStats_addVersion.cs

View workflow job for this annotation

GitHub Actions / sonarscanner

Rename class 'M001_ExecutionStats_addVersion' to match pascal case naming rules, consider using 'M001ExecutionStatsaddVersion'. (https://rules.sonarsource.com/csharp/RSPEC-101)
{
public M001_ExecutionStats_addVersion() : base("1.0.0") { }
public M001_ExecutionStats_addVersion() : base("1.0.1") { }

public override void Up(BsonDocument document)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

namespace Monai.Deploy.WorkflowManager.Common.Contracts.Migrations
{
public class M002_ExecutionStats_addWorkflowId : DocumentMigration<ExecutionStats>

Check warning on line 23 in src/WorkflowManager/Contracts/Migrations/M002_ExecutionStats_addWorkflowId.cs

View workflow job for this annotation

GitHub Actions / sonarscanner

Rename class 'M002_ExecutionStats_addWorkflowId' to match pascal case naming rules, consider using 'M002ExecutionStatsaddWorkflowId'. (https://rules.sonarsource.com/csharp/RSPEC-101)
{
public M002_ExecutionStats_addWorkflowId() : base("1.0.1") { }
public M002_ExecutionStats_addWorkflowId() : base("1.0.2") { }

public override void Up(BsonDocument document)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 MONAI Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Monai.Deploy.WorkflowManager.Common.Contracts.Models;
using Mongo.Migration.Migrations.Document;
using MongoDB.Bson;

namespace Monai.Deploy.WorkflowManager.Common.Contracts.Migrations
{
public class M003_ExecutionStats_addFailureReason : DocumentMigration<ExecutionStats>

Check warning on line 23 in src/WorkflowManager/Contracts/Migrations/M003_ExecutionStats_addFailureReason.cs

View workflow job for this annotation

GitHub Actions / sonarscanner

Rename class 'M003_ExecutionStats_addFailureReason' to match pascal case naming rules, consider using 'M003ExecutionStatsaddFailureReason'. (https://rules.sonarsource.com/csharp/RSPEC-101)
{
public M003_ExecutionStats_addFailureReason() : base("1.0.3") { }

public override void Up(BsonDocument document)
{
// empty, but this will make all objects re-saved with a reason
}
public override void Down(BsonDocument document)
{
try
{
document.Remove("Reason");
}
catch
{ // can ignore we dont want failures stopping startup !
}
}
}
}
14 changes: 11 additions & 3 deletions src/WorkflowManager/Contracts/Models/ExecutionStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using Monai.Deploy.WorkflowManager.Common.Contracts.Migrations;
using Ardalis.GuardClauses;
using Monai.Deploy.Messaging.Events;
using Mongo.Migration.Documents;
using Mongo.Migration.Documents.Attributes;
Expand All @@ -26,7 +25,7 @@

namespace Monai.Deploy.WorkflowManager.Common.Contracts.Models
{
[CollectionLocation("ExecutionStats"), RuntimeVersion("1.0.1")]
[CollectionLocation("ExecutionStats"), RuntimeVersion("1.0.3")]
public class ExecutionStats : IDocument
{
/// <summary>
Expand All @@ -40,7 +39,7 @@
/// Gets or sets Db version.
/// </summary>
[JsonConverter(typeof(DocumentVersionConvert)), BsonSerializer(typeof(DocumentVersionConverBson))]
public DocumentVersion Version { get; set; } = new DocumentVersion(1, 0, 1);
public DocumentVersion Version { get; set; } = new DocumentVersion(1, 0, 2);

/// <summary>
/// the correlationId of the event
Expand Down Expand Up @@ -110,6 +109,12 @@
[JsonProperty(PropertyName = "status")]
public string Status { get; set; } = TaskExecutionStatus.Created.ToString();

/// <summary>
/// Gets or sets the failure reason.
/// </summary>
[JsonProperty(PropertyName = "reason")]
public FailureReason Reason { get; set; }

/// <summary>
/// Gets or sets the duration, difference between startedAt and CompletedAt time.
/// </summary>
Expand All @@ -126,7 +131,7 @@

public ExecutionStats(TaskExecution execution, string workflowId, string correlationId)
{
ArgumentNullException.ThrowIfNull(execution, "dispatchInfo");

Check warning on line 134 in src/WorkflowManager/Contracts/Models/ExecutionStats.cs

View workflow job for this annotation

GitHub Actions / sonarscanner

Remove this argument from the method call; it hides the caller information. (https://rules.sonarsource.com/csharp/RSPEC-3236)
CorrelationId = correlationId;
WorkflowInstanceId = execution.WorkflowInstanceId;
ExecutionId = execution.ExecutionId;
Expand All @@ -134,28 +139,31 @@
StartedUTC = execution.TaskStartTime.ToUniversalTime();
Status = execution.Status.ToString();
WorkflowId = workflowId;
Reason = execution.Reason;
}

public ExecutionStats(TaskUpdateEvent taskUpdateEvent, string workflowId)
{
ArgumentNullException.ThrowIfNull(taskUpdateEvent, "taskUpdateEvent");

Check warning on line 147 in src/WorkflowManager/Contracts/Models/ExecutionStats.cs

View workflow job for this annotation

GitHub Actions / sonarscanner

Remove this argument from the method call; it hides the caller information. (https://rules.sonarsource.com/csharp/RSPEC-3236)
CorrelationId = taskUpdateEvent.CorrelationId;
WorkflowInstanceId = taskUpdateEvent.WorkflowInstanceId;
ExecutionId = taskUpdateEvent.ExecutionId;
TaskId = taskUpdateEvent.TaskId;
Status = taskUpdateEvent.Status.ToString();
WorkflowId = workflowId;
Reason = taskUpdateEvent.Reason;
}

public ExecutionStats(TaskCancellationEvent taskCanceledEvent, string workflowId, string correlationId)
{
ArgumentNullException.ThrowIfNull(taskCanceledEvent, "taskCanceledEvent");

Check warning on line 159 in src/WorkflowManager/Contracts/Models/ExecutionStats.cs

View workflow job for this annotation

GitHub Actions / sonarscanner

Remove this argument from the method call; it hides the caller information. (https://rules.sonarsource.com/csharp/RSPEC-3236)
CorrelationId = correlationId;
WorkflowInstanceId = taskCanceledEvent.WorkflowInstanceId;
ExecutionId = taskCanceledEvent.ExecutionId;
TaskId = taskCanceledEvent.TaskId;
Status = TaskExecutionStatus.Failed.ToString();
WorkflowId = workflowId;
Reason = taskCanceledEvent.Reason;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@

private static async Task EnsureIndex(IMongoCollection<ExecutionStats> taskExecutionStatsCollection)
{
ArgumentNullException.ThrowIfNull(taskExecutionStatsCollection, "TaskExecutionStatsCollection");

Check warning on line 51 in src/WorkflowManager/Database/Repositories/TaskExecutionStatsRepository.cs

View workflow job for this annotation

GitHub Actions / sonarscanner

Remove this argument from the method call; it hides the caller information. (https://rules.sonarsource.com/csharp/RSPEC-3236)

var asyncCursor = await taskExecutionStatsCollection.Indexes.ListAsync();
var bsonDocuments = await asyncCursor.ToListAsync();
var indexes = bsonDocuments.Select(_ => _.GetElement("name").Value.ToString()).ToList();

// If index not present create it else skip.
if (!indexes.Any(i => i is not null && i.Equals("ExecutionStatsIndex")))

Check warning on line 58 in src/WorkflowManager/Database/Repositories/TaskExecutionStatsRepository.cs

View workflow job for this annotation

GitHub Actions / sonarscanner

Collection-specific "Exists" method should be used instead of the "Any" extension. (https://rules.sonarsource.com/csharp/RSPEC-6605)
{
// Create Index here

Expand All @@ -74,7 +74,7 @@

public async Task CreateAsync(TaskExecution taskExecutionInfo, string workflowId, string correlationId)
{
ArgumentNullException.ThrowIfNull(taskExecutionInfo, "taskDispatchEventInfo");

Check warning on line 77 in src/WorkflowManager/Database/Repositories/TaskExecutionStatsRepository.cs

View workflow job for this annotation

GitHub Actions / sonarscanner

Remove this argument from the method call; it hides the caller information. (https://rules.sonarsource.com/csharp/RSPEC-3236)

try
{
Expand All @@ -94,7 +94,7 @@

public async Task UpdateExecutionStatsAsync(TaskExecution taskUpdateEvent, string workflowId, TaskExecutionStatus? status = null)
{
ArgumentNullException.ThrowIfNull(taskUpdateEvent, "taskUpdateEvent");

Check warning on line 97 in src/WorkflowManager/Database/Repositories/TaskExecutionStatsRepository.cs

View workflow job for this annotation

GitHub Actions / sonarscanner

Remove this argument from the method call; it hides the caller information. (https://rules.sonarsource.com/csharp/RSPEC-3236)
var currentStatus = status ?? taskUpdateEvent.Status;

try
Expand All @@ -110,6 +110,7 @@
.Set(w => w.CompletedAtUTC, updateMe.CompletedAtUTC)
.Set(w => w.ExecutionTimeSeconds, updateMe.ExecutionTimeSeconds)
.Set(w => w.DurationSeconds, duration)
.Set(w => w.Reason, taskUpdateEvent.Reason)

, new UpdateOptions { IsUpsert = true }).ConfigureAwait(false);
}
Expand All @@ -132,6 +133,7 @@
o.ExecutionId == updateMe.ExecutionId,
Builders<ExecutionStats>.Update
.Set(w => w.Status, updateMe.Status)
.Set(w => w.Reason, taskCanceledEvent.Reason)
.Set(w => w.LastUpdatedUTC, DateTime.UtcNow)
.Set(w => w.CompletedAtUTC, updateMe.CompletedAtUTC)
.Set(w => w.DurationSeconds, duration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,15 @@
{
Date = DateOnly.FromDateTime(g.Key.Date),
TotalExecutions = g.Count(),
TotalFailures = g.Count(i => string.Compare(i.Status, "Failed", true) == 0),
TotalApprovals = g.Count(i => string.Compare(i.Status, ApplicationReviewStatus.Approved.ToString(), true) == 0),
TotalRejections = g.Count(i => string.Compare(i.Status, ApplicationReviewStatus.Rejected.ToString(), true) == 0),
TotalCancelled = g.Count(i => string.Compare(i.Status, ApplicationReviewStatus.Cancelled.ToString(), true) == 0),
TotalFailures = g.Count(i => string.Compare(i.Status, "Failed", true) == 0 && i.Reason != FailureReason.TimedOut && i.Reason != FailureReason.Rejected),
TotalApprovals = g.Count(i => string.Compare(i.Status, "Succeeded", true) == 0 && i.Reason == FailureReason.None),
TotalRejections = g.Count(i => string.Compare(i.Status, "Failed", true) == 0 && i.Reason == FailureReason.Rejected),
TotalCancelled = g.Count(i => string.Compare(i.Status, "Failed", true) == 0 && i.Reason == FailureReason.TimedOut),
TotalAwaitingReview = g.Count(i => string.Compare(i.Status, ApplicationReviewStatus.AwaitingReview.ToString(), true) == 0),
});

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / task-manager-integration-tests

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / task-manager-integration-tests

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / unit-tests-and-codecov

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / unit-tests-and-codecov

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / unit-tests-and-codecov

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / scan

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / scan

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / workflow-executor-integration-tests

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / workflow-executor-integration-tests

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / workflow-executor-integration-tests

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / docs

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / docs

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / docs

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / docs

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / analyze

Check warning on line 145 in src/WorkflowManager/WorkflowManager/Controllers/TaskStatsController.cs

View workflow job for this annotation

GitHub Actions / analyze



var pagedStats = statsDto.Skip((filter.PageNumber - 1) * pageSize).Take(pageSize);

var res = CreateStatsPagedResponse(pagedStats, validFilter, statsDto.Count(), _uriService, route);
Expand All @@ -152,7 +154,7 @@
res.PeriodEnd = filter.EndTime;
res.TotalExecutions = allStats.Count();
res.TotalSucceeded = statsDto.Sum(s => s.TotalApprovals);
res.TotalFailures = statsDto.Sum(s => s.TotalFailures);
res.TotalFailures = statsDto.Sum(s => s.TotalFailures + s.TotalCancelled + s.TotalRejections);
res.TotalInprogress = statsDto.Sum(s => s.TotalAwaitingReview);
res.AverageTotalExecutionSeconds = Math.Round(avgTotalExecution, 2);
res.AverageArgoExecutionSeconds = Math.Round(avgArgoExecution, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,102 @@ public async Task GetAllStatsAsync_Pass_All_Arguments_To_GetStatsAsync_In_Repo()
It.Is<string>(s => s.Equals("")))
);
}

[Fact]
public async Task GetAllStatsAsync_Get_Correct_Reject_Count()
{
var startTime = new DateTime(2023, 4, 4);
var endTime = new DateTime(2023, 4, 5);
const int pageNumber = 1;
const int pageSize = 10;

var executionStats = new ExecutionStats[]
{
new ExecutionStats
{
ExecutionId = Guid.NewGuid().ToString(),
StartedUTC = _startTime,
WorkflowInstanceId= "workflow",
TaskId = "task",
Status = "Failed",
Reason = Messaging.Events.FailureReason.Rejected,
},
};

_repo.Setup(w => w.GetAllStatsAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(executionStats);

var result = await StatsController.GetDailyStatsAsync(new TimeFilter { StartTime = startTime, EndTime = endTime, PageNumber = pageNumber, PageSize = pageSize }, "workflow");

var resultCollection = result.As<OkObjectResult>().Value.As<StatsPagedResponse<IEnumerable<ExecutionStatDayOverview>>>().Data;

Assert.Equal(1, resultCollection.First().TotalExecutions);
Assert.Equal(1, resultCollection.First().TotalRejections);
Assert.Equal(0, resultCollection.First().TotalFailures);
}

[Fact]
public async Task GetAllStatsAsync_Get_Correct_Canceled_Count()
{
var startTime = new DateTime(2023, 4, 4);
var endTime = new DateTime(2023, 4, 5);
const int pageNumber = 1;
const int pageSize = 10;

var executionStats = new ExecutionStats[]
{
new ExecutionStats
{
ExecutionId = Guid.NewGuid().ToString(),
StartedUTC = _startTime,
WorkflowInstanceId= "workflow",
TaskId = "task",
Status = "Failed",
Reason = Messaging.Events.FailureReason.TimedOut,
},
};

_repo.Setup(w => w.GetAllStatsAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(executionStats);

var result = await StatsController.GetDailyStatsAsync(new TimeFilter { StartTime = startTime, EndTime = endTime, PageNumber = pageNumber, PageSize = pageSize }, "workflow");

var resultCollection = result.As<OkObjectResult>().Value.As<StatsPagedResponse<IEnumerable<ExecutionStatDayOverview>>>().Data;

Assert.Equal(1, resultCollection.First().TotalExecutions);
Assert.Equal(1, resultCollection.First().TotalCancelled);
Assert.Equal(0, resultCollection.First().TotalFailures);
}

[Fact]
public async Task GetAllStatsAsync_Get_Correct_Accepted_Count()
{
var startTime = new DateTime(2023, 4, 4);
var endTime = new DateTime(2023, 4, 5);
const int pageNumber = 1;
const int pageSize = 10;

var executionStats = new ExecutionStats[]
{
new ExecutionStats
{
ExecutionId = Guid.NewGuid().ToString(),
StartedUTC = _startTime,
WorkflowInstanceId= "workflow",
TaskId = "task",
Status = "Succeeded",
Reason = Messaging.Events.FailureReason.None,
},
};

_repo.Setup(w => w.GetAllStatsAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(executionStats);

var result = await StatsController.GetDailyStatsAsync(new TimeFilter { StartTime = startTime, EndTime = endTime, PageNumber = pageNumber, PageSize = pageSize }, "workflow");

var resultCollection = result.As<OkObjectResult>().Value.As<StatsPagedResponse<IEnumerable<ExecutionStatDayOverview>>>().Data;

Assert.Equal(1, resultCollection.First().TotalExecutions);
Assert.Equal(1, resultCollection.First().TotalApprovals);
Assert.Equal(0, resultCollection.First().TotalFailures);
}
}
#pragma warning restore CS8604 // Possible null reference argument.
#pragma warning restore CS8602 // Dereference of a possibly null reference.
Expand Down
Loading