Skip to content

Commit

Permalink
merge in, plus regex timeout
Browse files Browse the repository at this point in the history
Signed-off-by: Neil South <[email protected]>
  • Loading branch information
neildsouth committed Jun 7, 2023
2 parents ea60232 + 4db093d commit dc10561
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Shared/Shared/Utilities/DicomTagUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class DicomTagUtilities
{
public static DicomTag GetDicomTagByName(string tag)
{
return DicomDictionary.Default[tag] ?? DicomDictionary.Default[Regex.Replace(tag, @"\s+", "")];
return DicomDictionary.Default[tag] ?? DicomDictionary.Default[Regex.Replace(tag, @"\s+", "", RegexOptions.None, TimeSpan.FromSeconds(1))];
}

public static (bool valid, IList<string> invalidTags) DicomTagsValid(IEnumerable<string> dicomTags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,11 @@ await _taskExecutionStatsCollection.UpdateOneAsync(o =>

public async Task<IEnumerable<ExecutionStats>> GetStatsAsync(DateTime startTime, DateTime endTime, int PageSize = 10, int PageNumber = 1, string workflowId = "", string taskId = "")
{
startTime = startTime.ToUniversalTime();
CreateFilter(startTime, endTime, workflowId, taskId, out var builder, out var filter);

var workflowNull = string.IsNullOrWhiteSpace(workflowId);
var taskIdNull = string.IsNullOrWhiteSpace(taskId);
filter &= builder.Where(GetExecutedTasksFilter());

var result = await _taskExecutionStatsCollection.Find(T =>
T.StartedUTC >= startTime &&
T.StartedUTC <= endTime.ToUniversalTime() &&
(workflowNull || T.WorkflowId == workflowId) &&
(taskIdNull || T.TaskId == taskId))
var result = await _taskExecutionStatsCollection.Find(filter)
.Limit(PageSize)
.Skip((PageNumber - 1) * PageSize)
.ToListAsync();
Expand Down Expand Up @@ -222,33 +217,52 @@ public async Task<long> GetStatsStatusCountAsync(DateTime startTime, DateTime en
}

public async Task<long> GetStatsCountAsync(DateTime startTime, DateTime endTime, Expression<Func<ExecutionStats, bool>>? statusFilter = null, string workflowId = "", string taskId = "")
{
CreateFilter(startTime, endTime, workflowId, taskId, out var builder, out var filter);

if (statusFilter is not null)
{
filter &= builder.Where(statusFilter);
}

return await _taskExecutionStatsCollection.CountDocumentsAsync(filter);
}

private static void CreateFilter(DateTime startTime, DateTime endTime, string workflowId, string taskId, out FilterDefinitionBuilder<ExecutionStats> builder, out FilterDefinition<ExecutionStats> filter)
{
var workflowNull = string.IsNullOrWhiteSpace(workflowId);
var taskIdNull = string.IsNullOrWhiteSpace(taskId);

var builder = Builders<ExecutionStats>.Filter;
var filter = builder.Empty;

builder = Builders<ExecutionStats>.Filter;
filter = builder.Empty;
filter &= builder.Where(t => t.StartedUTC >= startTime.ToUniversalTime());
filter &= builder.Where(t => t.StartedUTC <= endTime.ToUniversalTime());
filter &= builder.Where(t => workflowNull || t.WorkflowId == workflowId);
filter &= builder.Where(t => taskIdNull || t.TaskId == taskId);
if (statusFilter is not null)
{
filter &= builder.Where(statusFilter);
}
}

return await _taskExecutionStatsCollection.CountDocumentsAsync(filter);
/// <summary>
/// Gets filter for tasks that have ran to completion.
/// </summary>
/// <returns></returns>
public static Expression<Func<ExecutionStats, bool>> GetExecutedTasksFilter()
{
var dispatched = TaskExecutionStatus.Dispatched.ToString();
var created = TaskExecutionStatus.Created.ToString();
var accepted = TaskExecutionStatus.Accepted.ToString();

return t => t.Status != dispatched && t.Status != created && t.Status != accepted;
}


public async Task<long> GetStatsTotalCompleteExecutionsCountAsync(DateTime startTime, DateTime endTime, string workflowId = "", string taskId = "")
{
var dispatched = TaskExecutionStatus.Dispatched.ToString();
var created = TaskExecutionStatus.Created.ToString();
var accepted = TaskExecutionStatus.Accepted.ToString();
Expression<Func<ExecutionStats, bool>> statusFilter = t => t.Status != dispatched && t.Status != created && t.Status != accepted;

return await GetStatsCountAsync(startTime, endTime, statusFilter, workflowId, taskId);
return await GetStatsCountAsync(startTime, endTime, GetExecutedTasksFilter(), workflowId, taskId);
}

public async Task<long> GetStatsStatusSucceededCountAsync(DateTime startTime, DateTime endTime, string workflowId = "", string taskId = "")
Expand All @@ -265,16 +279,11 @@ public async Task<long> GetStatsStatusFailedCountAsync(DateTime startTime, DateT

public async Task<(double avgTotalExecution, double avgArgoExecution)> GetAverageStats(DateTime startTime, DateTime endTime, string workflowId = "", string taskId = "")
{
var workflowNull = string.IsNullOrWhiteSpace(workflowId);
var taskIdNull = string.IsNullOrWhiteSpace(taskId);
CreateFilter(startTime, endTime, workflowId, taskId, out var builder, out var filter);
filter &= builder.Where(t => t.Status == TaskExecutionStatus.Succeeded.ToString());

var test = await _taskExecutionStatsCollection.Aggregate()
.Match(T =>
T.StartedUTC >= startTime.ToUniversalTime() &&
T.StartedUTC <= endTime.ToUniversalTime() &&
(workflowNull || T.WorkflowId == workflowId) &&
(taskIdNull || T.TaskId == taskId) &&
T.Status == TaskExecutionStatus.Succeeded.ToString())
.Match(filter)
.Group(g => new { g.Version }, r => new
{
avgTotalExecution = r.Average(x => (x.DurationSeconds)),
Expand Down

0 comments on commit dc10561

Please sign in to comment.