From 63590613c5f2bff5db6232a0639879d3ff8537d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gurhem?= Date: Thu, 27 Jul 2023 10:01:03 +0200 Subject: [PATCH] refactor: update API --- Common/src/ArmoniK.Core.Common.csproj | 2 +- Common/src/Injection/ServiceCollectionExt.cs | 1 - Common/src/gRPC/FilterRangeExt.cs | 28 ++++++++--- Common/src/gRPC/ListPartitionsRequestExt.cs | 5 +- Common/src/gRPC/ListSessionsRequestExt.cs | 5 +- Common/src/gRPC/ListTasksRequestExt.cs | 5 +- .../src/gRPC/Services/GrpcSessionsService.cs | 41 --------------- ...sionsCountTasksByStatusRequestValidator.cs | 37 -------------- ...sCountTasksByStatusRequestValidatorTest.cs | 50 ------------------- .../src/ArmoniK.Samples.Bench.Server.csproj | 2 +- .../ArmoniK.Core.Common.Tests.Client.csproj | 2 +- .../src/ArmoniK.Samples.HtcMock.Server.csproj | 2 +- ...s.Common.StreamWrapper.Tests.Client.csproj | 2 +- ...s.Common.StreamWrapper.Tests.Server.csproj | 2 +- 14 files changed, 34 insertions(+), 150 deletions(-) delete mode 100644 Common/src/gRPC/Validators/SessionsCountTasksByStatusRequestValidator.cs delete mode 100644 Common/tests/Validators/SessionsCountTasksByStatusRequestValidatorTest.cs diff --git a/Common/src/ArmoniK.Core.Common.csproj b/Common/src/ArmoniK.Core.Common.csproj index 04617df18..1a13f30ca 100644 --- a/Common/src/ArmoniK.Core.Common.csproj +++ b/Common/src/ArmoniK.Core.Common.csproj @@ -26,7 +26,7 @@ - + diff --git a/Common/src/Injection/ServiceCollectionExt.cs b/Common/src/Injection/ServiceCollectionExt.cs index ce50f25c4..9d576a0ae 100644 --- a/Common/src/Injection/ServiceCollectionExt.cs +++ b/Common/src/Injection/ServiceCollectionExt.cs @@ -156,6 +156,5 @@ public static IServiceCollection ValidateGrpcRequests(this IServiceCollection se .AddValidator() .AddValidator() .AddValidator() - .AddValidator() .AddGrpcValidation(); } diff --git a/Common/src/gRPC/FilterRangeExt.cs b/Common/src/gRPC/FilterRangeExt.cs index 1f7619251..f02cd304a 100644 --- a/Common/src/gRPC/FilterRangeExt.cs +++ b/Common/src/gRPC/FilterRangeExt.cs @@ -45,7 +45,6 @@ public static Expression> ToFilter(this FilterStatusOp TStatus value) => filterOperator switch { - FilterStatusOperator.Unspecified => throw new ArgumentOutOfRangeException(), FilterStatusOperator.Equal => ExpressionBuilders.MakeBinary(field, value, ExpressionType.Equal), @@ -70,7 +69,6 @@ public static Expression> ToFilter(this FilterDateOperator DateTime? value) => filterOperator switch { - FilterDateOperator.Unspecified => throw new ArgumentOutOfRangeException(), FilterDateOperator.Before => ExpressionBuilders.MakeBinary(field, value, ExpressionType.LessThan), @@ -108,7 +106,6 @@ public static Expression> ToFilter(this FilterNumberOperator long value) => filterOperator switch { - FilterNumberOperator.Unspecified => throw new ArgumentOutOfRangeException(), FilterNumberOperator.LessThan => ExpressionBuilders.MakeBinary(field, value, ExpressionType.LessThan), @@ -165,8 +162,7 @@ public static Expression> ToFilter(this FilterStringOperator FilterStringOperator.EndsWith => ExpressionBuilders.MakeCallString(field, value, nameof(string.EndsWith)), - FilterStringOperator.Unspecified => throw new ArgumentOutOfRangeException(), - _ => throw new ArgumentOutOfRangeException(), + _ => throw new ArgumentOutOfRangeException(), }; /// @@ -184,7 +180,6 @@ public static Expression> ToFilter(this FilterArrayOperator string value) => filterOperator switch { - FilterArrayOperator.Unspecified => throw new ArgumentOutOfRangeException(), FilterArrayOperator.Contains => ExpressionBuilders.MakeCall(field, value, typeof(Enumerable), @@ -196,4 +191,25 @@ public static Expression> ToFilter(this FilterArrayOperator true), _ => throw new ArgumentOutOfRangeException(), }; + + /// + /// Generate a filter for operations on arrays + /// + /// Type of the value and field on which the operation is applied + /// The gRPC enum that selects the operation + /// The to select the field on which to apply the operation + /// Value for the operation + /// + /// The that represents the operation on the field with the given value + /// + public static Expression> ToFilter(this FilterBooleanOperator filterOperator, + Expression> field, + bool value) + => filterOperator switch + { + FilterBooleanOperator.Is => ExpressionBuilders.MakeBinary(field, + value, + ExpressionType.Equal), + _ => throw new ArgumentOutOfRangeException(), + }; } diff --git a/Common/src/gRPC/ListPartitionsRequestExt.cs b/Common/src/gRPC/ListPartitionsRequestExt.cs index 0ac6e6631..7c7aab544 100644 --- a/Common/src/gRPC/ListPartitionsRequestExt.cs +++ b/Common/src/gRPC/ListPartitionsRequestExt.cs @@ -91,9 +91,8 @@ public static Expression> ToPartitionFilter(this Filte filterField.FilterNumber.Value)); break; case FilterField.ValueConditionOneofCase.FilterBoolean: - predicateAnd = predicateAnd.And(ExpressionBuilders.MakeBinary(filterField.Field.ToField(), - filterField.FilterBoolean.Value, - ExpressionType.Equal)); + predicateAnd = predicateAnd.And(filterField.FilterBoolean.Operator.ToFilter(filterField.Field.ToField(), + filterField.FilterBoolean.Value))); break; case FilterField.ValueConditionOneofCase.FilterArray: predicateAnd = predicateAnd.And(filterField.FilterArray.Operator.ToFilter(filterField.Field.ToField(), diff --git a/Common/src/gRPC/ListSessionsRequestExt.cs b/Common/src/gRPC/ListSessionsRequestExt.cs index 109a17a1e..8d5797456 100644 --- a/Common/src/gRPC/ListSessionsRequestExt.cs +++ b/Common/src/gRPC/ListSessionsRequestExt.cs @@ -95,9 +95,8 @@ public static Expression> ToSessionDataFilter(this Filte filterField.FilterNumber.Value)); break; case FilterField.ValueConditionOneofCase.FilterBoolean: - predicateAnd = predicateAnd.And(ExpressionBuilders.MakeBinary(filterField.Field.ToField(), - filterField.FilterBoolean.Value, - ExpressionType.Equal)); + predicateAnd = predicateAnd.And(filterField.FilterBoolean.Operator.ToFilter(filterField.Field.ToField(), + filterField.FilterBoolean.Value))); break; case FilterField.ValueConditionOneofCase.FilterStatus: predicateAnd = predicateAnd.And(filterField.FilterStatus.Operator.ToFilter(filterField.Field.ToField(), diff --git a/Common/src/gRPC/ListTasksRequestExt.cs b/Common/src/gRPC/ListTasksRequestExt.cs index 85ed95442..f34637531 100644 --- a/Common/src/gRPC/ListTasksRequestExt.cs +++ b/Common/src/gRPC/ListTasksRequestExt.cs @@ -97,9 +97,8 @@ public static Expression> ToTaskDataFilter(this Filters fil filterField.FilterNumber.Value)); break; case FilterField.ValueConditionOneofCase.FilterBoolean: - predicateAnd = predicateAnd.And(ExpressionBuilders.MakeBinary(filterField.Field.ToField(), - filterField.FilterBoolean.Value, - ExpressionType.Equal)); + predicateAnd = predicateAnd.And(filterField.FilterBoolean.Operator.ToFilter(filterField.Field.ToField(), + filterField.FilterBoolean.Value))); break; case FilterField.ValueConditionOneofCase.FilterStatus: predicateAnd = predicateAnd.And(filterField.FilterStatus.Operator.ToFilter(filterField.Field.ToField(), diff --git a/Common/src/gRPC/Services/GrpcSessionsService.cs b/Common/src/gRPC/Services/GrpcSessionsService.cs index 17bb77729..75548c2ca 100644 --- a/Common/src/gRPC/Services/GrpcSessionsService.cs +++ b/Common/src/gRPC/Services/GrpcSessionsService.cs @@ -19,7 +19,6 @@ using System.Linq; using System.Threading.Tasks; -using ArmoniK.Api.gRPC.V1; using ArmoniK.Api.gRPC.V1.Sessions; using Armonik.Api.Grpc.V1.SortDirection; @@ -41,15 +40,12 @@ public class GrpcSessionsService : Sessions.SessionsBase { private readonly ILogger logger_; private readonly ISessionTable sessionTable_; - private readonly ITaskTable taskTable_; public GrpcSessionsService(ISessionTable sessionTable, - ITaskTable taskTable, ILogger logger) { logger_ = logger; sessionTable_ = sessionTable; - taskTable_ = taskTable; } [RequiresPermission(typeof(GrpcSessionsService), @@ -171,41 +167,4 @@ request.Sort is null "Unknown Exception, see application logs")); } } - - [RequiresPermission(typeof(GrpcSessionsService), - nameof(CountTasksByStatus))] - public override async Task CountTasksByStatus(CountTasksByStatusRequest request, - ServerCallContext context) - { - try - { - return new CountTasksByStatusResponse - { - Status = - { - (await taskTable_.CountTasksAsync(data => data.SessionId == request.SessionId, - context.CancellationToken) - .ConfigureAwait(false)).Select(count => new StatusCount - { - Status = count.Status, - Count = count.Count, - }), - }, - }; - } - catch (ArmoniKException e) - { - logger_.LogWarning(e, - "Error while counting tasks in session"); - throw new RpcException(new Status(StatusCode.Internal, - "Internal Armonik Exception, see application logs")); - } - catch (Exception e) - { - logger_.LogWarning(e, - "Error while counting tasks in session"); - throw new RpcException(new Status(StatusCode.Unknown, - "Unknown Exception, see application logs")); - } - } } diff --git a/Common/src/gRPC/Validators/SessionsCountTasksByStatusRequestValidator.cs b/Common/src/gRPC/Validators/SessionsCountTasksByStatusRequestValidator.cs deleted file mode 100644 index 20352bee9..000000000 --- a/Common/src/gRPC/Validators/SessionsCountTasksByStatusRequestValidator.cs +++ /dev/null @@ -1,37 +0,0 @@ -// This file is part of the ArmoniK project -// -// Copyright (C) ANEO, 2021-2023. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY, without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -using ArmoniK.Api.gRPC.V1.Sessions; - -using FluentValidation; - -namespace ArmoniK.Core.Common.gRPC.Validators; - -/// -/// Validator for -/// -public class SessionsCountTasksByStatusRequestValidator : AbstractValidator -{ - /// - /// Initializes a validator for - /// - public SessionsCountTasksByStatusRequestValidator() - => RuleFor(o => o.SessionId) - .NotEmpty() - .NotNull() - .WithName(nameof(CountTasksByStatusRequest.SessionId)); -} diff --git a/Common/tests/Validators/SessionsCountTasksByStatusRequestValidatorTest.cs b/Common/tests/Validators/SessionsCountTasksByStatusRequestValidatorTest.cs deleted file mode 100644 index 88d57af06..000000000 --- a/Common/tests/Validators/SessionsCountTasksByStatusRequestValidatorTest.cs +++ /dev/null @@ -1,50 +0,0 @@ -// This file is part of the ArmoniK project -// -// Copyright (C) ANEO, 2021-2023. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY, without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -using ArmoniK.Api.gRPC.V1.Sessions; -using ArmoniK.Core.Common.gRPC.Validators; - -using NUnit.Framework; - -namespace ArmoniK.Core.Common.Tests.Validators; - -[TestFixture(TestOf = typeof(SessionsCountTasksByStatusRequestValidator))] -public class SessionsCountTasksByStatusRequestValidatorTest -{ - private readonly SessionsCountTasksByStatusRequestValidator validator_ = new(); - - [Test] - public void DefaultShouldFail() - => Assert.IsFalse(validator_.Validate(new CountTasksByStatusRequest()) - .IsValid); - - [Test] - public void EmptySessionIdShouldFail() - => Assert.IsFalse(validator_.Validate(new CountTasksByStatusRequest - { - SessionId = "", - }) - .IsValid); - - [Test] - public void ShouldSucceed() - => Assert.IsTrue(validator_.Validate(new CountTasksByStatusRequest - { - SessionId = "SessionId", - }) - .IsValid); -} diff --git a/Tests/Bench/Server/src/ArmoniK.Samples.Bench.Server.csproj b/Tests/Bench/Server/src/ArmoniK.Samples.Bench.Server.csproj index 9f8a7692e..591fc8612 100644 --- a/Tests/Bench/Server/src/ArmoniK.Samples.Bench.Server.csproj +++ b/Tests/Bench/Server/src/ArmoniK.Samples.Bench.Server.csproj @@ -25,7 +25,7 @@ - + diff --git a/Tests/Common/Client/src/ArmoniK.Core.Common.Tests.Client.csproj b/Tests/Common/Client/src/ArmoniK.Core.Common.Tests.Client.csproj index 67a9f50c6..3388f6f9a 100644 --- a/Tests/Common/Client/src/ArmoniK.Core.Common.Tests.Client.csproj +++ b/Tests/Common/Client/src/ArmoniK.Core.Common.Tests.Client.csproj @@ -25,7 +25,7 @@ - + diff --git a/Tests/HtcMock/Server/src/ArmoniK.Samples.HtcMock.Server.csproj b/Tests/HtcMock/Server/src/ArmoniK.Samples.HtcMock.Server.csproj index 2a4ccee23..fdbee5fc5 100644 --- a/Tests/HtcMock/Server/src/ArmoniK.Samples.HtcMock.Server.csproj +++ b/Tests/HtcMock/Server/src/ArmoniK.Samples.HtcMock.Server.csproj @@ -25,7 +25,7 @@ - + diff --git a/Tests/Stream/Client/ArmoniK.Extensions.Common.StreamWrapper.Tests.Client.csproj b/Tests/Stream/Client/ArmoniK.Extensions.Common.StreamWrapper.Tests.Client.csproj index 66fb49b81..86ffee7b7 100644 --- a/Tests/Stream/Client/ArmoniK.Extensions.Common.StreamWrapper.Tests.Client.csproj +++ b/Tests/Stream/Client/ArmoniK.Extensions.Common.StreamWrapper.Tests.Client.csproj @@ -25,7 +25,7 @@ - + diff --git a/Tests/Stream/Server/ArmoniK.Extensions.Common.StreamWrapper.Tests.Server.csproj b/Tests/Stream/Server/ArmoniK.Extensions.Common.StreamWrapper.Tests.Server.csproj index 2f3570d60..a34bc3a59 100644 --- a/Tests/Stream/Server/ArmoniK.Extensions.Common.StreamWrapper.Tests.Server.csproj +++ b/Tests/Stream/Server/ArmoniK.Extensions.Common.StreamWrapper.Tests.Server.csproj @@ -25,7 +25,7 @@ - +