< Summary

Information
Class: Elsa.Workflows.Management.Filters.WorkflowInstanceFilter
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Filters/WorkflowInstanceFilter.cs
Line coverage
61%
Covered lines: 47
Uncovered lines: 30
Coverable lines: 77
Total lines: 197
Line coverage: 61%
Branch coverage
46%
Covered branches: 31
Total branches: 67
Branch coverage: 46.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Id()100%11100%
get_Ids()100%11100%
get_SearchTerm()100%11100%
get_Name()100%11100%
get_DefinitionId()100%11100%
get_DefinitionVersionId()100%11100%
get_DefinitionIds()100%11100%
get_DefinitionVersionIds()100%11100%
get_Version()100%11100%
get_ParentWorkflowInstanceIds()100%11100%
get_CorrelationId()100%11100%
get_CorrelationIds()100%11100%
get_WorkflowStatus()100%11100%
get_WorkflowStatuses()100%11100%
get_WorkflowSubStatus()100%11100%
get_WorkflowSubStatuses()100%11100%
get_IsExecuting()100%11100%
get_HasIncidents()100%11100%
get_IsSystem()100%11100%
get_BeforeLastUpdated()100%11100%
get_TimestampFilters()100%11100%
get_Names()100%11100%
Apply(...)46.26%7966745.45%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Filters/WorkflowInstanceFilter.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using Elsa.Workflows.Management.Entities;
 3using Elsa.Workflows.Management.Enums;
 4using Elsa.Workflows.Management.Models;
 5using System.Linq.Dynamic.Core;
 6
 7namespace Elsa.Workflows.Management.Filters;
 8
 9/// <summary>
 10/// A filter for querying workflow instances.
 11/// </summary>
 12public class WorkflowInstanceFilter
 13{
 14    /// <summary>
 15    /// Filter workflow instances by ID.
 16    /// </summary>
 36017    public string? Id { get; set; }
 18
 19    /// <summary>
 20    /// Filter workflow instances by IDs.
 21    /// </summary>
 16122    public ICollection<string>? Ids { get; set; }
 23
 24    /// <summary>
 25    /// Filter workflow instances that match the specified search term.
 26    /// </summary>
 14127    public string? SearchTerm { get; set; }
 28
 29    /// <summary>
 30    /// Filter workflow instances that match the specified name.
 31    /// </summary>
 14132    public string? Name { get; set; }
 33
 34    /// <summary>
 35    /// Filter workflow instances by definition ID.
 36    /// </summary>
 17337    public string? DefinitionId { get; set; }
 38
 39    /// <summary>
 40    /// Filter workflow instances by definition version ID.
 41    /// </summary>
 14142    public string? DefinitionVersionId { get; set; }
 43
 44    /// <summary>
 45    /// Filter workflow instances by definition IDs.
 46    /// </summary>
 15347    public ICollection<string>? DefinitionIds { get; set; }
 48
 49    /// <summary>
 50    /// Filter workflow instances by definition version IDs.
 51    /// </summary>
 14152    public ICollection<string>? DefinitionVersionIds { get; set; }
 53
 54    /// <summary>
 55    /// Filter workflow instances by version.
 56    /// </summary>
 14157    public int? Version { get; set; }
 58
 59    /// <summary>
 60    /// Filter workflow instances by their parent instance IDs.
 61    /// </summary>
 14162    public ICollection<string>? ParentWorkflowInstanceIds { get; set; }
 63
 64    /// <summary>
 65    /// Filter workflow instances by correlation ID.
 66    /// </summary>
 14767    public string? CorrelationId { get; set; }
 68
 69    /// <summary>
 70    /// Filter workflow instances by correlation IDs.
 71    /// </summary>
 14172    public ICollection<string>? CorrelationIds { get; set; }
 73
 74    /// <summary>
 75    /// Filter workflow instances by status.
 76    /// </summary>
 15577    public WorkflowStatus? WorkflowStatus { get; set; }
 78
 79    /// <summary>
 80    /// Filter workflow instances by a set of statuses.
 81    /// </summary>
 14182    public ICollection<WorkflowStatus>? WorkflowStatuses { get; set; }
 83
 84    /// <summary>
 85    /// Filter workflow instances by sub-status.
 86    /// </summary>
 14387    public WorkflowSubStatus? WorkflowSubStatus { get; set; }
 88
 89    /// <summary>
 90    /// Filter workflow instances by a set of sub-status.
 91    /// </summary>
 14192    public ICollection<WorkflowSubStatus>? WorkflowSubStatuses { get; set; }
 93
 94    /// <summary>
 95    /// Filter workflow instances by whether they are executing.
 96    /// </summary>
 14797    public bool? IsExecuting { get; set; }
 98
 99    /// <summary>
 100    /// Filter workflow instances by whether they have incidents.
 101    /// </summary>
 141102    public bool? HasIncidents { get; set; }
 103
 104    /// <summary>
 105    /// Filter on workflows that are system workflows.
 106    /// </summary>
 141107    public bool? IsSystem { get; set; }
 108
 109    /// <summary>
 110    /// Filter workflow instances that are older than the specified timestamp.
 111    /// </summary>
 147112    public DateTimeOffset? BeforeLastUpdated { get; set; }
 113
 114    /// <summary>
 115    /// Filter workflow instances by timestamp.
 116    /// </summary>
 141117    public ICollection<TimestampFilter>? TimestampFilters { get; set; }
 118
 119    /// <summary>
 120    /// Filter workflow instances by name.
 121    /// </summary>
 141122    public List<string>? Names { get; set; }
 123
 124    /// <summary>
 125    /// Applies the filter to the specified query.
 126    /// </summary>
 127    [RequiresUnreferencedCode("The method uses reflection to create an expression tree.")]
 128    public IQueryable<WorkflowInstance> Apply(IQueryable<WorkflowInstance> query)
 129    {
 141130        var filter = this;
 131
 254132        if (!string.IsNullOrWhiteSpace(filter.Id)) query = query.Where(x => x.Id == filter.Id);
 151133        if (filter.Ids != null) query = query.Where(x => filter.Ids.Contains(x.Id));
 156134        if (!string.IsNullOrWhiteSpace(filter.DefinitionId)) query = query.Where(x => x.DefinitionId == filter.Definitio
 141135        if (!string.IsNullOrWhiteSpace(filter.DefinitionVersionId)) query = query.Where(x => x.DefinitionVersionId == fi
 142136        if (filter.DefinitionIds != null) query = query.Where(x => filter.DefinitionIds.Contains(x.DefinitionId));
 141137        if (filter.DefinitionVersionIds != null) query = query.Where(x => filter.DefinitionVersionIds.Contains(x.Definit
 141138        if (filter.Version != null) query = query.Where(x => x.Version == filter.Version);
 141139        if (filter.ParentWorkflowInstanceIds != null) query = query.Where(x => x.ParentWorkflowInstanceId != null && fil
 144140        if (!string.IsNullOrWhiteSpace(filter.CorrelationId)) query = query.Where(x => x.CorrelationId == filter.Correla
 141141        if (filter.CorrelationIds != null) query = query.Where(x => filter.CorrelationIds.Contains(x.CorrelationId!));
 141142        if (filter.Names != null) query = query.Where(x => filter.Names.Contains(x.Name!));
 151143        if (filter.WorkflowStatus != null) query = query.Where(x => x.Status == filter.WorkflowStatus);
 142144        if (filter.WorkflowSubStatus != null) query = query.Where(x => x.SubStatus == filter.WorkflowSubStatus);
 141145        if (filter.WorkflowStatuses != null) query = query.Where(x => filter.WorkflowStatuses.Contains(x.Status));
 141146        if (filter.WorkflowSubStatuses != null) query = query.Where(x => filter.WorkflowSubStatuses.Contains(x.SubStatus
 143147        if (filter.IsExecuting != null) query = query.Where(x => x.IsExecuting == filter.IsExecuting);
 141148        if (filter.HasIncidents != null) query = filter.HasIncidents == true ? query.Where(x => x.IncidentCount > 0) : q
 141149        if (filter.IsSystem != null) query = query.Where(x => x.IsSystem == filter.IsSystem);
 141150        if (filter.Name != null) query = query.Where(x => x.Name!.ToLower().Contains(filter.Name.ToLower()));
 143151        if (filter.BeforeLastUpdated != null) query = query.Where(x => x.UpdatedAt < filter.BeforeLastUpdated);
 152
 141153        if (TimestampFilters != null)
 154        {
 0155            foreach (var timestampFilter in TimestampFilters)
 156            {
 0157                var column = timestampFilter.Column;
 0158                var timestamp = timestampFilter.Timestamp;
 0159                var isZeroTime = timestamp.TimeOfDay == TimeSpan.Zero;
 0160                var startDay = new DateTimeOffset(timestamp.Date);
 0161                var endDay = startDay.AddDays(1);
 162
 0163                query = timestampFilter.Operator switch
 0164                {
 0165                    TimestampFilterOperator.Is when isZeroTime => query.Where($"{column} >= @0 && {column} < @1", startD
 0166                    TimestampFilterOperator.Is => query.Where($"{column} == @0", timestamp),
 0167                    TimestampFilterOperator.IsNot when isZeroTime => query.Where($"{column} < @0 || {column} >= @1", sta
 0168                    TimestampFilterOperator.IsNot => query.Where($"{column} != @0", timestamp),
 0169                    TimestampFilterOperator.GreaterThan when isZeroTime => query.Where($"{column} > @0", endDay),
 0170                    TimestampFilterOperator.GreaterThan => query.Where($"{column} > @0", timestamp),
 0171                    TimestampFilterOperator.GreaterThanOrEqual when isZeroTime => query.Where($"{column} >= @0", startDa
 0172                    TimestampFilterOperator.GreaterThanOrEqual => query.Where($"{column} >= @0", timestamp),
 0173                    TimestampFilterOperator.LessThan when isZeroTime => query.Where($"{column} < @0", startDay),
 0174                    TimestampFilterOperator.LessThan => query.Where($"{column} < @0", timestamp),
 0175                    TimestampFilterOperator.LessThanOrEqual when isZeroTime => query.Where($"{column} <= @0", endDay),
 0176                    TimestampFilterOperator.LessThanOrEqual => query.Where($"{column} <= @0", timestamp),
 0177                    _ => query
 0178                };
 179            }
 180        }
 181
 141182        var searchTerm = filter.SearchTerm;
 141183        if (!string.IsNullOrWhiteSpace(searchTerm))
 184        {
 0185            query =
 0186                from instance in query
 0187                where instance.Name!.ToLower().Contains(searchTerm.ToLower())
 0188                      || instance.DefinitionVersionId.Contains(searchTerm)
 0189                      || instance.DefinitionId.Contains(searchTerm)
 0190                      || instance.Id.Contains(searchTerm)
 0191                      || instance.CorrelationId!.Contains(searchTerm)
 0192                select instance;
 193        }
 194
 141195        return query;
 196    }
 197}