Skip to content

Commit

Permalink
feat: added duration filter (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrasseur-aneo authored Oct 10, 2023
2 parents 89f7bbd + 364f5ce commit f02b8a3
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Base/src/ArmoniK.Core.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Core" Version="3.14.0-edge.3.9fd5060" />
<PackageReference Include="ArmoniK.Api.Core" Version="3.14.0-edge.11.ca5b236" />
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.10" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion Common/src/ArmoniK.Core.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


<ItemGroup>
<PackageReference Include="ArmoniK.Api.Core" Version="3.14.0-edge.3.9fd5060" />
<PackageReference Include="ArmoniK.Api.Core" Version="3.14.0-edge.11.ca5b236" />
<PackageReference Include="Calzolari.Grpc.AspNetCore.Validation" Version="6.3.0" />
<PackageReference Include="Grpc.HealthCheck" Version="2.55.0" />
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0" />
Expand Down
36 changes: 36 additions & 0 deletions Common/src/gRPC/FilterRangeExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,40 @@ public static Expression<Func<T, bool>> ToFilter<T>(this FilterBooleanOperator
ExpressionType.Equal),
_ => throw new ArgumentOutOfRangeException(nameof(filterOperator)),
};

/// <summary>
/// Generate a filter <see cref="Expression" /> for operations on durations
/// </summary>
/// <typeparam name="T">Type of the value and field on which the operation is applied</typeparam>
/// <param name="filterOperator">The gRPC enum that selects the operation</param>
/// <param name="field">The <see cref="Expression" /> to select the field on which to apply the operation</param>
/// <param name="value">Value for the operation</param>
/// <returns>
/// The <see cref="Expression" /> that represents the operation on the field with the given value
/// </returns>
public static Expression<Func<T, bool>> ToFilter<T>(this FilterDurationOperator filterOperator,
Expression<Func<T, object?>> field,
TimeSpan? value)
=> filterOperator switch
{
FilterDurationOperator.ShorterThan => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.LessThan),
FilterDurationOperator.ShorterThanOrEqual => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.LessThanOrEqual),
FilterDurationOperator.Equal => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.Equal),
FilterDurationOperator.NotEqual => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.NotEqual),
FilterDurationOperator.LongerThanOrEqual => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.GreaterThanOrEqual),
FilterDurationOperator.LongerThan => ExpressionBuilders.MakeBinary(field,
value,
ExpressionType.GreaterThan),
_ => throw new ArgumentOutOfRangeException(nameof(filterOperator)),
};
}
4 changes: 4 additions & 0 deletions Common/src/gRPC/ListSessionsRequestExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public static Expression<Func<SessionData, bool>> ToSessionDataFilter(this Filte
exprAnd = exprAnd.ExpressionAnd(filterField.FilterArray.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterArray.Value));
break;
case FilterField.ValueConditionOneofCase.FilterDuration:
exprAnd = exprAnd.ExpressionAnd(filterField.FilterDuration.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterDuration.Value?.ToTimeSpan()));
break;
case FilterField.ValueConditionOneofCase.None:
default:
throw new ArgumentOutOfRangeException(nameof(filters));
Expand Down
4 changes: 4 additions & 0 deletions Common/src/gRPC/ListTasksRequestExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public static Expression<Func<TaskData, bool>> ToTaskDataFilter(this Filters fil
exprAnd = exprAnd.ExpressionAnd(filterField.FilterArray.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterArray.Value));
break;
case FilterField.ValueConditionOneofCase.FilterDuration:
exprAnd = exprAnd.ExpressionAnd(filterField.FilterDuration.Operator.ToFilter(filterField.Field.ToField(),
filterField.FilterDuration.Value?.ToTimeSpan()));
break;
case FilterField.ValueConditionOneofCase.None:
default:
throw new ArgumentOutOfRangeException(nameof(filters));
Expand Down
29 changes: 28 additions & 1 deletion Common/tests/ListSessionsRequestExt/ToSessionDataFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
using ArmoniK.Core.Common.gRPC;
using ArmoniK.Core.Common.Storage;

using Google.Protobuf.WellKnownTypes;

using NUnit.Framework;

using static Google.Protobuf.WellKnownTypes.Timestamp;
Expand All @@ -37,7 +39,7 @@ namespace ArmoniK.Core.Common.Tests.ListSessionsRequestExt;
public class ToSessionDataFilterTest
{
private static readonly TaskOptions Options = new(new Dictionary<string, string>(),
TimeSpan.MaxValue,
TimeSpan.FromMinutes(5),
5,
1,
"part1",
Expand Down Expand Up @@ -210,6 +212,25 @@ public static FilterField CreateListSessionsFilterDate(SessionRawEnumField field
},
};

public static FilterField CreateListSessionsFilterDuration(TaskOptionEnumField field,
FilterDurationOperator op,
TimeSpan value)
=> new()
{
Field = new SessionField
{
TaskOptionField = new TaskOptionField
{
Field = field,
},
},
FilterDuration = new FilterDuration
{
Operator = op,
Value = Duration.FromTimeSpan(value),
},
};

[Test]
[TestCaseSource(nameof(TestCasesFilter))]
public void Filter(IEnumerable<FilterField> filterFields,
Expand Down Expand Up @@ -279,5 +300,11 @@ TestCaseData CaseFalse(FilterField filterField)
yield return CaseFalse(CreateListSessionsFilterDate(SessionRawEnumField.CancelledAt,
FilterDateOperator.Before,
DateTime.UtcNow));
yield return CaseTrue(CreateListSessionsFilterDuration(TaskOptionEnumField.MaxDuration,
FilterDurationOperator.ShorterThanOrEqual,
TimeSpan.FromMinutes(5)));
yield return CaseFalse(CreateListSessionsFilterDuration(TaskOptionEnumField.MaxDuration,
FilterDurationOperator.NotEqual,
TimeSpan.FromMinutes(5)));
}
}
22 changes: 22 additions & 0 deletions Common/tests/ListTasksRequestExt/ListTasksHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using ArmoniK.Api.gRPC.V1;
using ArmoniK.Api.gRPC.V1.Tasks;

using static Google.Protobuf.WellKnownTypes.Duration;
using static Google.Protobuf.WellKnownTypes.Timestamp;

namespace ArmoniK.Core.Common.Tests.ListTasksRequestExt;
Expand Down Expand Up @@ -159,4 +160,25 @@ public static FilterField CreateListTasksFilterDate(TaskSummaryEnumField field,
: FromDateTime(value.Value),
},
};

public static FilterField CreateListTasksFilterDuration(TaskSummaryEnumField field,
FilterDurationOperator op,
TimeSpan? value)
=> new()
{
Field = new TaskField
{
TaskSummaryField = new TaskSummaryField
{
Field = field,
},
},
FilterDuration = new FilterDuration
{
Operator = op,
Value = value is null
? null
: FromTimeSpan(value.Value),
},
};
}
14 changes: 14 additions & 0 deletions Common/tests/ListTasksRequestExt/ToTaskDataFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public class ToTaskDataFilterTest
3,
15).ToUniversalTime();

private static readonly TimeSpan TimeSpanToCompare = TimeSpan.FromDays(1.5);

private static readonly ListTasksRequest.Types.Sort Sort = new()
{
Direction = SortDirection.Asc,
Expand Down Expand Up @@ -222,5 +224,17 @@ TestCaseData CaseFalse(FilterField filterField)
yield return CaseTrue(ListTasksHelper.CreateListTasksFilterString("key1",
FilterStringOperator.EndsWith,
"val1"));
yield return CaseTrue(ListTasksHelper.CreateListTasksFilterDuration(TaskSummaryEnumField.ProcessingToEndDuration,
FilterDurationOperator.ShorterThanOrEqual,
TimeSpanToCompare));
yield return CaseFalse(ListTasksHelper.CreateListTasksFilterDuration(TaskSummaryEnumField.CreationToEndDuration,
FilterDurationOperator.ShorterThanOrEqual,
TimeSpanToCompare));
yield return CaseFalse(ListTasksHelper.CreateListTasksFilterDuration(TaskSummaryEnumField.ProcessingToEndDuration,
FilterDurationOperator.LongerThan,
TimeSpanToCompare));
yield return CaseTrue(ListTasksHelper.CreateListTasksFilterDuration(TaskSummaryEnumField.CreationToEndDuration,
FilterDurationOperator.LongerThan,
TimeSpanToCompare));
}
}
2 changes: 1 addition & 1 deletion Tests/Bench/Server/src/ArmoniK.Samples.Bench.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Worker" Version="3.14.0-edge.3.9fd5060" />
<PackageReference Include="ArmoniK.Api.Worker" Version="3.14.0-edge.11.ca5b236" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Client" Version="3.14.0-edge.3.9fd5060" />
<PackageReference Include="ArmoniK.Api.Client" Version="3.14.0-edge.11.ca5b236" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Worker" Version="3.14.0-edge.3.9fd5060" />
<PackageReference Include="ArmoniK.Api.Worker" Version="3.14.0-edge.11.ca5b236" />
<PackageReference Include="Htc.Mock" Version="3.0.0-alpha11" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Client" Version="3.14.0-edge.3.9fd5060" />
<PackageReference Include="ArmoniK.Api.Client" Version="3.14.0-edge.11.ca5b236" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArmoniK.Api.Worker" Version="3.14.0-edge.3.9fd5060" />
<PackageReference Include="ArmoniK.Api.Worker" Version="3.14.0-edge.11.ca5b236" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit f02b8a3

Please sign in to comment.