From 768e983d241afd59293055e41779f977e07ceead Mon Sep 17 00:00:00 2001 From: Jesper Justesen <1972142+FirestarJes@users.noreply.github.com> Date: Sat, 7 Sep 2024 10:57:45 +0200 Subject: [PATCH] Implemented helper and start job --- .../SettlementReportInitializeHandler.cs | 6 ++ .../IRequestSettlemenReportJobHandler.cs | 7 +- .../RequestSettlementReportHandler.cs | 58 +++++--------- .../Helpers/DatabricksJobsHelper.cs | 77 +++++++++++++++++++ .../IDatabricksJobsHelper.cs} | 7 +- .../ISettlementReportInitializeHandler.cs | 7 ++ 6 files changed, 119 insertions(+), 43 deletions(-) create mode 100644 source/settlement-report/SettlementReports.Infrastructure/Helpers/DatabricksJobsHelper.cs rename source/settlement-report/SettlementReports.Infrastructure/{Commands/RequestSettlementReportJobCommand.cs => Helpers/IDatabricksJobsHelper.cs} (78%) diff --git a/source/settlement-report/SettlementReports.Application/SettlementReports_v2/SettlementReportInitializeHandler.cs b/source/settlement-report/SettlementReports.Application/SettlementReports_v2/SettlementReportInitializeHandler.cs index 07ea2c8..b423b0a 100644 --- a/source/settlement-report/SettlementReports.Application/SettlementReports_v2/SettlementReportInitializeHandler.cs +++ b/source/settlement-report/SettlementReports.Application/SettlementReports_v2/SettlementReportInitializeHandler.cs @@ -37,4 +37,10 @@ public Task InitializeAsync( var settlementReport = new SettlementReport(SystemClock.Instance, userId, actorId, hideReport, requestId, request); return _repository.AddOrUpdateAsync(settlementReport); } + + public Task InitializeFromJobAsync(Guid userId, Guid actorId, bool hideReport, long jobId, SettlementReportRequestDto request) + { + var settlementReport = new SettlementReport(SystemClock.Instance, userId, actorId, hideReport, new SettlementReportRequestId(jobId.ToString()), request); + return _repository.AddOrUpdateAsync(settlementReport); + } } diff --git a/source/settlement-report/SettlementReports.Infrastructure/Handlers/IRequestSettlemenReportJobHandler.cs b/source/settlement-report/SettlementReports.Infrastructure/Handlers/IRequestSettlemenReportJobHandler.cs index 6f116c6..f5cc9f2 100644 --- a/source/settlement-report/SettlementReports.Infrastructure/Handlers/IRequestSettlemenReportJobHandler.cs +++ b/source/settlement-report/SettlementReports.Infrastructure/Handlers/IRequestSettlemenReportJobHandler.cs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -using Energinet.DataHub.SettlementReport.Infrastructure.Commands; +using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models; namespace Energinet.DataHub.SettlementReport.Infrastructure.Handlers; @@ -22,6 +22,9 @@ public interface IRequestSettlemenReportJobHandler /// Request a settlement report job /// /// + /// + /// + /// /// A long value representing the job id of the requested settlement report. - Task HandleAsync(RequestSettlementReportJobCommand command); + Task HandleAsync(SettlementReportRequestDto command, Guid userId, Guid actorId, bool isFas); } diff --git a/source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReportHandler.cs b/source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReportHandler.cs index c9f59d0..86b76b2 100644 --- a/source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReportHandler.cs +++ b/source/settlement-report/SettlementReports.Infrastructure/Handlers/RequestSettlementReportHandler.cs @@ -12,57 +12,37 @@ // 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.Models; +using Energinet.DataHub.SettlementReport.Infrastructure.Helpers; +using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2; 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; + private readonly IDatabricksJobsHelper _jobHelper; + private readonly ISettlementReportInitializeHandler _settlementReportInitializeHandler; - public RequestSettlementReportHandler(IJobsApiClient jobsApiClient) + public RequestSettlementReportHandler( + IDatabricksJobsHelper jobHelper, + ISettlementReportInitializeHandler settlementReportInitializeHandler) { - _jobsApiClient = jobsApiClient; + _jobHelper = jobHelper; + _settlementReportInitializeHandler = settlementReportInitializeHandler; } - public async Task HandleAsync(RequestSettlementReportJobCommand command) + public async Task HandleAsync(SettlementReportRequestDto request, Guid userId, Guid actorId, bool isFas) { - var job = await GetSettlementReportsJobAsync(command.Request.Filter.CalculationType == CalculationType.BalanceFixing - ? DatabricksJobNames.BalanceFixing - : DatabricksJobNames.Wholesale) + var jobId = await _jobHelper.RunSettlementReportsJobAsync(request).ConfigureAwait(false); + await _settlementReportInitializeHandler + .InitializeFromJobAsync( + userId, + actorId, + isFas, + jobId, + request) .ConfigureAwait(false); - var parameters = CreateParameters(command.Request); - return await _jobsApiClient.Jobs.RunNow(job.JobId, 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(string jobName) - { - var calculatorJob = await _jobsApiClient.Jobs - .ListPageable(name: jobName) - .SingleAsync() - .ConfigureAwait(false); - - return await _jobsApiClient.Jobs.Get(calculatorJob.JobId).ConfigureAwait(false); + return jobId; } } diff --git a/source/settlement-report/SettlementReports.Infrastructure/Helpers/DatabricksJobsHelper.cs b/source/settlement-report/SettlementReports.Infrastructure/Helpers/DatabricksJobsHelper.cs new file mode 100644 index 0000000..b129397 --- /dev/null +++ b/source/settlement-report/SettlementReports.Infrastructure/Helpers/DatabricksJobsHelper.cs @@ -0,0 +1,77 @@ +// 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.Handlers; +using Energinet.DataHub.SettlementReport.Infrastructure.SqlStatements.Mappers; +using Energinet.DataHub.SettlementReport.Interfaces.Models; +using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models; +using Microsoft.Azure.Databricks.Client.Models; + +namespace Energinet.DataHub.SettlementReport.Infrastructure.Helpers; + +public class DatabricksJobsHelper : IDatabricksJobsHelper +{ + private readonly IJobsApiClient _jobsApiClient; + + public DatabricksJobsHelper(IJobsApiClient jobsApiClient) + { + _jobsApiClient = jobsApiClient; + } + + public async Task RunSettlementReportsJobAsync(SettlementReportRequestDto request) + { + var job = await GetSettlementReportsJobAsync(GetJobName(request.Filter.CalculationType)).ConfigureAwait(false); + return await _jobsApiClient.Jobs.RunNow(job.JobId, CreateParameters(request)).ConfigureAwait(false); + } + + private string GetJobName(CalculationType calculationType) + { + return calculationType switch + { + CalculationType.BalanceFixing => DatabricksJobNames.BalanceFixing, + CalculationType.WholesaleFixing => DatabricksJobNames.Wholesale, + CalculationType.FirstCorrectionSettlement => DatabricksJobNames.Wholesale, + CalculationType.SecondCorrectionSettlement => DatabricksJobNames.Wholesale, + CalculationType.ThirdCorrectionSettlement => DatabricksJobNames.Wholesale, + CalculationType.Aggregation => DatabricksJobNames.Wholesale, + _ => throw new ArgumentOutOfRangeException(nameof(calculationType), calculationType, null), + }; + } + + private async Task GetSettlementReportsJobAsync(string jobName) + { + var calculatorJob = await _jobsApiClient.Jobs + .ListPageable(name: jobName) + .SingleAsync() + .ConfigureAwait(false); + + return await _jobsApiClient.Jobs.Get(calculatorJob.JobId).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); + } +} diff --git a/source/settlement-report/SettlementReports.Infrastructure/Commands/RequestSettlementReportJobCommand.cs b/source/settlement-report/SettlementReports.Infrastructure/Helpers/IDatabricksJobsHelper.cs similarity index 78% rename from source/settlement-report/SettlementReports.Infrastructure/Commands/RequestSettlementReportJobCommand.cs rename to source/settlement-report/SettlementReports.Infrastructure/Helpers/IDatabricksJobsHelper.cs index e40807b..3dc229e 100644 --- a/source/settlement-report/SettlementReports.Infrastructure/Commands/RequestSettlementReportJobCommand.cs +++ b/source/settlement-report/SettlementReports.Infrastructure/Helpers/IDatabricksJobsHelper.cs @@ -14,6 +14,9 @@ using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models; -namespace Energinet.DataHub.SettlementReport.Infrastructure.Commands; +namespace Energinet.DataHub.SettlementReport.Infrastructure.Helpers; -public record RequestSettlementReportJobCommand(SettlementReportRequestDto Request); +public interface IDatabricksJobsHelper +{ + Task RunSettlementReportsJobAsync(SettlementReportRequestDto request); +} diff --git a/source/settlement-report/SettlementReports.Interfaces/SettlementReports_v2/ISettlementReportInitializeHandler.cs b/source/settlement-report/SettlementReports.Interfaces/SettlementReports_v2/ISettlementReportInitializeHandler.cs index 7b58221..a0a9832 100644 --- a/source/settlement-report/SettlementReports.Interfaces/SettlementReports_v2/ISettlementReportInitializeHandler.cs +++ b/source/settlement-report/SettlementReports.Interfaces/SettlementReports_v2/ISettlementReportInitializeHandler.cs @@ -24,4 +24,11 @@ Task InitializeAsync( bool hideReport, SettlementReportRequestId requestId, SettlementReportRequestDto request); + + Task InitializeFromJobAsync( + Guid userId, + Guid actorId, + bool hideReport, + long jobId, + SettlementReportRequestDto request); }