From 360a21775f155148df240fc67c0e3f5e4a2fed5e Mon Sep 17 00:00:00 2001 From: Jesper Justesen <1972142+FirestarJes@users.noreply.github.com> Date: Fri, 6 Sep 2024 21:25:51 +0200 Subject: [PATCH] First implementation of Job API --- .../actions/dotnet-prepare-outputs/action.yml | 2 +- SettlementReport.sln | 2 +- .../Common.Infrastructure.csproj | 3 +- .../RequestSettlementReportJobCommand.cs | 12 +--- .../IRequestSettlemenReportJobHandler.cs | 4 +- .../RequestSettlementReportHandler.cs | 62 ++++++++++++++++++ .../RequestSettlementReporthHandler.cs | 25 -------- .../SettlementReportsController.cs | 2 +- .../SettlementReportModuleExtensions.cs | 63 +++++++++++++++++++ .../Program.cs | 5 ++ .../Properties/launchSettings.json | 0 .../SettlementReports.WebAPI.csproj} | 2 + 12 files changed, 141 insertions(+), 41 deletions(-) create mode 100644 source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReportHandler.cs delete mode 100644 source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReporthHandler.cs rename source/settlement-report/{SettlmentReports.WebAPI => SettlementReports.WebAPI}/Controllers/SettlementReportsController.cs (98%) create mode 100644 source/settlement-report/SettlementReports.WebAPI/Extensions/DependencyInjection/SettlementReportModuleExtensions.cs rename source/settlement-report/{SettlmentReports.WebAPI => SettlementReports.WebAPI}/Program.cs (86%) rename source/settlement-report/{SettlmentReports.WebAPI => SettlementReports.WebAPI}/Properties/launchSettings.json (100%) rename source/settlement-report/{SettlmentReports.WebAPI/SettlmentReports.WebAPI.csproj => SettlementReports.WebAPI/SettlementReports.WebAPI.csproj} (87%) diff --git a/.github/actions/dotnet-prepare-outputs/action.yml b/.github/actions/dotnet-prepare-outputs/action.yml index 50497ef..1247191 100644 --- a/.github/actions/dotnet-prepare-outputs/action.yml +++ b/.github/actions/dotnet-prepare-outputs/action.yml @@ -46,7 +46,7 @@ runs: shell: bash run: | dotnet publish \ - '.\source\settlement-report\SettlmentReports.WebAPI\SettlmentReports.WebAPI.csproj' \ + '.\source\settlement-report\SettlementReports.WebAPI\SettlmentReports.WebAPI.csproj' \ --no-build \ --no-restore \ --configuration Release \ diff --git a/SettlementReport.sln b/SettlementReport.sln index 11bdb65..1ef1e0f 100644 --- a/SettlementReport.sln +++ b/SettlementReport.sln @@ -17,7 +17,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Core", "source\settlem EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchestration.SettlementReports.IntegrationTests", "source\settlement-report\Orchestration.SettlementReports.IntegrationTests\Orchestration.SettlementReports.IntegrationTests.csproj", "{63FCEA68-F792-4860-88AD-7EF163AD667F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SettlmentReports.WebAPI", "source\settlement-report\SettlmentReports.WebAPI\SettlmentReports.WebAPI.csproj", "{016FF5AF-759C-4A11-ABA1-723C6F925953}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SettlementReports.WebAPI", "source\settlement-report\SettlementReports.WebAPI\SettlementReports.WebAPI.csproj", "{016FF5AF-759C-4A11-ABA1-723C6F925953}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SettlementReports.Application", "source\settlement-report\SettlementReports.Application\SettlementReports.Application.csproj", "{90B61921-AF88-4010-AFB8-08FAA3EBF173}" EndProject diff --git a/source/settlement-report/Common.Infrastructure/Common.Infrastructure.csproj b/source/settlement-report/Common.Infrastructure/Common.Infrastructure.csproj index 17dc9a8..ace3f1c 100644 --- a/source/settlement-report/Common.Infrastructure/Common.Infrastructure.csproj +++ b/source/settlement-report/Common.Infrastructure/Common.Infrastructure.csproj @@ -20,12 +20,13 @@ limitations under the License. - + + diff --git a/source/settlement-report/SettlementReports.Infrastructure/Commands/RequestSettlementReportJobCommand.cs b/source/settlement-report/SettlementReports.Infrastructure/Commands/RequestSettlementReportJobCommand.cs index 35f2ec5..e40807b 100644 --- a/source/settlement-report/SettlementReports.Infrastructure/Commands/RequestSettlementReportJobCommand.cs +++ b/source/settlement-report/SettlementReports.Infrastructure/Commands/RequestSettlementReportJobCommand.cs @@ -1,4 +1,4 @@ -// Copyright 2020 Energinet DataHub A/S +// Copyright 2020 Energinet DataHub A/S // // Licensed under the Apache License, Version 2.0 (the "License2"); // you may not use this file except in compliance with the License. @@ -16,12 +16,4 @@ namespace Energinet.DataHub.SettlementReport.Infrastructure.Commands; -public class RequestSettlementReportJobCommand -{ - private readonly SettlementReportRequestDto _request; - - public RequestSettlementReportJobCommand(SettlementReportRequestDto request) - { - _request = request; - } -} +public record RequestSettlementReportJobCommand(SettlementReportRequestDto Request); diff --git a/source/settlement-report/SettlementReports.Infrastructure/Handlers/IRequestSettlemenReportJobHandler.cs b/source/settlement-report/SettlementReports.Infrastructure/Handlers/IRequestSettlemenReportJobHandler.cs index a9038df..6f116c6 100644 --- a/source/settlement-report/SettlementReports.Infrastructure/Handlers/IRequestSettlemenReportJobHandler.cs +++ b/source/settlement-report/SettlementReports.Infrastructure/Handlers/IRequestSettlemenReportJobHandler.cs @@ -22,6 +22,6 @@ public interface IRequestSettlemenReportJobHandler /// Request a settlement report job /// /// - /// A representing the job id of the requested settlement report. - Task HandleAsync(RequestSettlementReportJobCommand command); + /// A long value representing the job id of the requested settlement report. + Task HandleAsync(RequestSettlementReportJobCommand command); } diff --git a/source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReportHandler.cs b/source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReportHandler.cs new file mode 100644 index 0000000..3f63c08 --- /dev/null +++ b/source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReportHandler.cs @@ -0,0 +1,62 @@ +// Copyright 2020 Energinet DataHub A/S +// +// Licensed under the Apache License, Version 2.0 (the "License2"); +// 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 Energinet.DataHub.Core.Databricks.Jobs.Abstractions; +using Energinet.DataHub.SettlementReport.Infrastructure.Commands; +using Energinet.DataHub.SettlementReport.Infrastructure.SqlStatements.Mappers; +using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models; +using Microsoft.Azure.Databricks.Client.Models; + +namespace Energinet.DataHub.SettlementReport.Infrastructure.Handlers; + +public sealed class RequestSettlementReportHandler : IRequestSettlemenReportJobHandler +{ + private readonly IJobsApiClient _jobsApiClient; + + public RequestSettlementReportHandler(IJobsApiClient jobsApiClient) + { + _jobsApiClient = jobsApiClient; + } + + public async Task HandleAsync(RequestSettlementReportJobCommand command) + { + var parameters = CreateParameters(command.Request); + return await _jobsApiClient.Jobs.RunNow(1, parameters).ConfigureAwait(false); + } + + private RunParameters CreateParameters(SettlementReportRequestDto request) + { + var gridAreas = string.Join(", ", request.Filter.GridAreas.Select(c => c.Key)); + + var jobParameters = new List + { + $"--grid-areas=[{gridAreas}]", + $"--period-start-datetime={request.Filter.PeriodStart}", + $"--period-end-datetime={request.Filter.PeriodEnd}", + $"--calculation-type={CalculationTypeMapper.ToDeltaTableValue(request.Filter.CalculationType)}", + }; + + return RunParameters.CreatePythonParams(jobParameters); + } + + private async Task GetSettlementReportsJobAsync() + { + var calculatorJob = await _jobsApiClient.Jobs + .ListPageable(name: "CalculatorJob") + .SingleAsync() + .ConfigureAwait(false); + + return await _jobsApiClient.Jobs.Get(calculatorJob.JobId).ConfigureAwait(false); + } +} diff --git a/source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReporthHandler.cs b/source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReporthHandler.cs deleted file mode 100644 index 0352ced..0000000 --- a/source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReporthHandler.cs +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2020 Energinet DataHub A/S -// -// Licensed under the Apache License, Version 2.0 (the "License2"); -// 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 Energinet.DataHub.SettlementReport.Infrastructure.Commands; - -namespace Energinet.DataHub.SettlementReport.Infrastructure.Handlers; - -public class RequestSettlementReporthHandler() : IRequestSettlemenReportJobHandler -{ - public Task HandleAsync(RequestSettlementReportJobCommand command) - { - throw new NotImplementedException(); - } -} diff --git a/source/settlement-report/SettlmentReports.WebAPI/Controllers/SettlementReportsController.cs b/source/settlement-report/SettlementReports.WebAPI/Controllers/SettlementReportsController.cs similarity index 98% rename from source/settlement-report/SettlmentReports.WebAPI/Controllers/SettlementReportsController.cs rename to source/settlement-report/SettlementReports.WebAPI/Controllers/SettlementReportsController.cs index 20288e7..5077fc5 100644 --- a/source/settlement-report/SettlmentReports.WebAPI/Controllers/SettlementReportsController.cs +++ b/source/settlement-report/SettlementReports.WebAPI/Controllers/SettlementReportsController.cs @@ -19,7 +19,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -namespace Energinet.DataHub.ImbalancePrices.Hosts.WebApi.Controllers; +namespace SettlementReports.WebAPI.Controllers; [ApiController] [Route("settlement-reports")] diff --git a/source/settlement-report/SettlementReports.WebAPI/Extensions/DependencyInjection/SettlementReportModuleExtensions.cs b/source/settlement-report/SettlementReports.WebAPI/Extensions/DependencyInjection/SettlementReportModuleExtensions.cs new file mode 100644 index 0000000..398d37d --- /dev/null +++ b/source/settlement-report/SettlementReports.WebAPI/Extensions/DependencyInjection/SettlementReportModuleExtensions.cs @@ -0,0 +1,63 @@ +// Copyright 2020 Energinet DataHub A/S +// +// Licensed under the Apache License, Version 2.0 (the "License2"); +// 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 Energinet.DataHub.Core.App.Common.Extensions.DependencyInjection; +using Energinet.DataHub.SettlementReport.Common.Infrastructure.Extensions.Options; +using Energinet.DataHub.SettlementReport.Common.Infrastructure.HealthChecks; +using Energinet.DataHub.SettlementReport.Infrastructure.Handlers; +using Energinet.DataHub.SettlementReport.Infrastructure.Persistence; +using Microsoft.EntityFrameworkCore; + +namespace SettlementReports.WebAPI.Extensions.DependencyInjection; + +public static class SettlementReportModuleExtensions +{ + public static IServiceCollection AddSettlementReportApiModule(this IServiceCollection services, IConfiguration configuration) + { + ArgumentNullException.ThrowIfNull(configuration); + + // handlers + services.AddScoped(); + + services.AddScoped(); + services.AddDbContext( + options => options.UseSqlServer( + configuration + .GetSection(ConnectionStringsOptions.ConnectionStrings) + .Get()!.DB_CONNECTION_STRING, + o => + { + o.UseNodaTime(); + o.EnableRetryOnFailure(); + })); + + // Database Health check + services.TryAddHealthChecks( + registrationKey: HealthCheckNames.SettlementReportDatabase, + (key, builder) => + { + builder.AddDbContextCheck(name: key); + }); + + AddHealthChecks(services); + + return services; + } + + private static void AddHealthChecks(IServiceCollection services) + { + services + .AddHealthChecks(); + } +} diff --git a/source/settlement-report/SettlmentReports.WebAPI/Program.cs b/source/settlement-report/SettlementReports.WebAPI/Program.cs similarity index 86% rename from source/settlement-report/SettlmentReports.WebAPI/Program.cs rename to source/settlement-report/SettlementReports.WebAPI/Program.cs index 2c55085..b628d37 100644 --- a/source/settlement-report/SettlmentReports.WebAPI/Program.cs +++ b/source/settlement-report/SettlementReports.WebAPI/Program.cs @@ -17,9 +17,12 @@ using Asp.Versioning; using Energinet.DataHub.Core.App.WebApp.Extensions.Builder; using Energinet.DataHub.Core.App.WebApp.Extensions.DependencyInjection; +using Energinet.DataHub.Core.Databricks.Jobs.Extensions.DependencyInjection; using Energinet.DataHub.Core.Logging.LoggingMiddleware; using Energinet.DataHub.SettlementReport.Common.Infrastructure.Security; using Energinet.DataHub.SettlementReport.Common.Infrastructure.Telemetry; +using Energinet.DataHub.SettlementReport.Infrastructure.Extensions.DependencyInjection; +using SettlementReports.WebAPI.Extensions.DependencyInjection; const string subsystemName = TelemetryConstants.SubsystemName; @@ -38,6 +41,8 @@ .AddSwaggerForWebApp(Assembly.GetExecutingAssembly(), subsystemName) .AddJwtBearerAuthenticationForWebApp(builder.Configuration) .AddUserAuthenticationForWebApp() + .AddDatabricksJobs(builder.Configuration) + .AddSettlementReportApiModule(builder.Configuration) .AddPermissionAuthorizationForWebApp(); var app = builder.Build(); diff --git a/source/settlement-report/SettlmentReports.WebAPI/Properties/launchSettings.json b/source/settlement-report/SettlementReports.WebAPI/Properties/launchSettings.json similarity index 100% rename from source/settlement-report/SettlmentReports.WebAPI/Properties/launchSettings.json rename to source/settlement-report/SettlementReports.WebAPI/Properties/launchSettings.json diff --git a/source/settlement-report/SettlmentReports.WebAPI/SettlmentReports.WebAPI.csproj b/source/settlement-report/SettlementReports.WebAPI/SettlementReports.WebAPI.csproj similarity index 87% rename from source/settlement-report/SettlmentReports.WebAPI/SettlmentReports.WebAPI.csproj rename to source/settlement-report/SettlementReports.WebAPI/SettlementReports.WebAPI.csproj index 8bc78a8..af807c1 100644 --- a/source/settlement-report/SettlmentReports.WebAPI/SettlmentReports.WebAPI.csproj +++ b/source/settlement-report/SettlementReports.WebAPI/SettlementReports.WebAPI.csproj @@ -4,6 +4,7 @@ net8.0 enable enable + SettlementReports.WebAPI @@ -20,6 +21,7 @@ +