Skip to content

Commit

Permalink
Implemented some job types (status and id) updated repository to save…
Browse files Browse the repository at this point in the history
… jobId
  • Loading branch information
FirestarJes committed Sep 8, 2024
1 parent 76d0a18 commit 7a96d05
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ limitations under the License.
<EmbeddedResource Include="Scripts\202405141445_Add_Period_To_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202405151335_Use_DateTime_In_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202406210900_Add_IsHiddenFromActor_to_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202409081607_Add_JobId_To_SettlementReport.sql" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE [settlementreports].[SettlementReport]
ADD [JobId] [BigInt] NULL;
GO
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public interface IRequestSettlemenReportJobHandler
/// <param name="userId"></param>
/// <param name="actorId"></param>
/// <param name="isFas"></param>
/// <returns>A long value representing the job id of the requested settlement report.</returns>
Task<long> HandleAsync(SettlementReportRequestDto command, Guid userId, Guid actorId, bool isFas);
/// <returns>A JobRunId value representing the run id of the requested settlement report.</returns>
Task<JobRunId> HandleAsync(SettlementReportRequestDto command, Guid userId, Guid actorId, bool isFas);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.SettlementReport.Application.Helpers;
using Energinet.DataHub.SettlementReport.Interfaces.Helpers;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;

Expand All @@ -31,18 +31,18 @@ public RequestSettlementReportHandler(
_settlementReportInitializeHandler = settlementReportInitializeHandler;
}

public async Task<long> HandleAsync(SettlementReportRequestDto request, Guid userId, Guid actorId, bool isFas)
public async Task<JobRunId> HandleAsync(SettlementReportRequestDto request, Guid userId, Guid actorId, bool isFas)
{
var jobId = await _jobHelper.RunSettlementReportsJobAsync(request).ConfigureAwait(false);
var runId = await _jobHelper.RunSettlementReportsJobAsync(request).ConfigureAwait(false);
await _settlementReportInitializeHandler
.InitializeFromJobAsync(
userId,
actorId,
isFas,
jobId,
runId,
request)
.ConfigureAwait(false);

return jobId;
return runId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ public async Task<IEnumerable<RequestedSettlementReportDto>> GetAsync(Guid actor
private static RequestedSettlementReportDto Map(SettlementReport report)
{
return new RequestedSettlementReportDto(
new SettlementReportRequestId(report.RequestId),
report.RequestId is not null ? new SettlementReportRequestId(report.RequestId) : null,
report.CalculationType,
report.PeriodStart.ToDateTimeOffset(),
report.PeriodEnd.ToDateTimeOffset(),
report.Status,
report.GridAreaCount,
0,
report.ActorId,
report.ContainsBasisData);
report.ContainsBasisData,
report.JobId is not null ? new JobRunId(report.JobId.Value) : null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public interface ISettlementReportFileRepository

Task DeleteAsync(SettlementReportRequestId reportRequestId, string fileName);

Task DeleteAsync(JobRunId reportRequestId, string fileName);

Task DownloadAsync(SettlementReportRequestId reportRequestId, string fileName, Stream downloadStream);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public sealed class SettlementReport
{
public int Id { get; init; }

public string RequestId { get; init; } = null!;
public string? RequestId { get; init; }

public Guid UserId { get; init; }

Expand All @@ -47,6 +47,8 @@ public sealed class SettlementReport

public string? BlobFileName { get; private set; }

public long? JobId { get; init; }

public SettlementReport(
IClock clock,
Guid userId,
Expand All @@ -68,6 +70,27 @@ public SettlementReport(
GridAreaCount = request.Filter.GridAreas.Count;
}

public SettlementReport(
IClock clock,
Guid userId,
Guid actorId,
bool hideReport,
JobRunId jobRunId,
SettlementReportRequestDto request)
{
JobId = jobRunId.Id;
UserId = userId;
ActorId = actorId;
IsHiddenFromActor = hideReport;
CreatedDateTime = clock.GetCurrentInstant();
Status = SettlementReportStatus.InProgress;
CalculationType = request.Filter.CalculationType;
ContainsBasisData = request.IncludeBasisData;
PeriodStart = request.Filter.PeriodStart.ToInstant();
PeriodEnd = request.Filter.PeriodEnd.ToInstant();
GridAreaCount = request.Filter.GridAreas.Count;
}

// EF Core Constructor.
// ReSharper disable once UnusedMember.Local
private SettlementReport()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Task InitializeAsync(
return _repository.AddOrUpdateAsync(settlementReport);
}

public Task InitializeFromJobAsync(Guid userId, Guid actorId, bool hideReport, long jobId, SettlementReportRequestDto request)
public Task InitializeFromJobAsync(Guid userId, Guid actorId, bool hideReport, JobRunId jobId, SettlementReportRequestDto request)
{
var settlementReport = new SettlementReport(SystemClock.Instance, userId, actorId, hideReport, new SettlementReportRequestId(jobId.ToString()), request);
return _repository.AddOrUpdateAsync(settlementReport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

using Energinet.DataHub.Core.Databricks.Jobs.Abstractions;
using Energinet.DataHub.SettlementReport.Application.Handlers;
using Energinet.DataHub.SettlementReport.Application.Helpers;
using Energinet.DataHub.SettlementReport.Infrastructure.SqlStatements.Mappers;
using Energinet.DataHub.SettlementReport.Interfaces.Helpers;
using Energinet.DataHub.SettlementReport.Interfaces.Models;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Microsoft.Azure.Databricks.Client.Models;
Expand All @@ -31,10 +31,16 @@ public DatabricksJobsHelper(IJobsApiClient jobsApiClient)
_jobsApiClient = jobsApiClient;
}

public async Task<long> RunSettlementReportsJobAsync(SettlementReportRequestDto request)
public async Task<JobRunId> RunSettlementReportsJobAsync(SettlementReportRequestDto request)
{
var job = await GetSettlementReportsJobAsync(GetJobName(request.Filter.CalculationType)).ConfigureAwait(false);
return await _jobsApiClient.Jobs.RunNow(job.JobId, CreateParameters(request)).ConfigureAwait(false);
return new JobRunId(await _jobsApiClient.Jobs.RunNow(job.JobId, CreateParameters(request)).ConfigureAwait(false));
}

public async Task<JobRunStatus> GetSettlementReportsJobStatusAsync(long runId)
{
var jobRun = await _jobsApiClient.Jobs.RunsGet(runId, false).ConfigureAwait(false);
return ConvertJobStatus(jobRun.Item1);
}

private string GetJobName(CalculationType calculationType)
Expand All @@ -53,12 +59,12 @@ private string GetJobName(CalculationType calculationType)

private async Task<Job> GetSettlementReportsJobAsync(string jobName)
{
var calculatorJob = await _jobsApiClient.Jobs
var settlementJob = await _jobsApiClient.Jobs
.ListPageable(name: jobName)
.SingleAsync()
.ConfigureAwait(false);

return await _jobsApiClient.Jobs.Get(calculatorJob.JobId).ConfigureAwait(false);
return await _jobsApiClient.Jobs.Get(settlementJob.JobId).ConfigureAwait(false);
}

private RunParameters CreateParameters(SettlementReportRequestDto request)
Expand All @@ -75,4 +81,26 @@ private RunParameters CreateParameters(SettlementReportRequestDto request)

return RunParameters.CreatePythonParams(jobParameters);
}

private static JobRunStatus ConvertJobStatus(Run jobRun)
{
if (jobRun.State == null)
{
return JobRunStatus.Queued;
}

if (jobRun.State.ResultState == RunResultState.SUCCESS || jobRun.IsCompleted)
{
return JobRunStatus.Completed;
}

return jobRun.State.LifeCycleState switch
{
RunLifeCycleState.RUNNING => JobRunStatus.Running,
RunLifeCycleState.QUEUED or RunLifeCycleState.PENDING => JobRunStatus.Queued,
RunLifeCycleState.TERMINATED => JobRunStatus.Canceled,
RunLifeCycleState.INTERNAL_ERROR => JobRunStatus.Failed,
_ => JobRunStatus.Queued,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,18 @@ public async Task RemoveExpiredAsync(IList<Application.SettlementReports_v2.Sett

if (settlementReport.BlobFileName != null)
{
await _settlementReportFileRepository
.DeleteAsync(new SettlementReportRequestId(settlementReport.RequestId), settlementReport.BlobFileName)
.ConfigureAwait(false);
if (settlementReport.RequestId is not null)
{
await _settlementReportFileRepository
.DeleteAsync(new SettlementReportRequestId(settlementReport.RequestId), settlementReport.BlobFileName)
.ConfigureAwait(false);
}
else if (settlementReport.JobId is not null)
{
await _settlementReportFileRepository
.DeleteAsync(new JobRunId(settlementReport.JobId.GetValueOrDefault()), settlementReport.BlobFileName)
.ConfigureAwait(false);
}
}

await _settlementReportRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public Task<Stream> OpenForReadingAsync(SettlementReportRequestId reportRequestI
return blobClient.OpenReadAsync();
}

// TODO: Impelement this method when we know the blob storage for job reports
public Task DeleteAsync(JobRunId reportRequestId, string fileName)
{
throw new NotImplementedException();
}

public async Task DownloadAsync(SettlementReportRequestId reportRequestId, string fileName, Stream downloadStream)
{
var blobName = GetBlobName(reportRequestId, fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ public async Task GetAsync_SingleUser_ReturnsOwnRows()
item =>
{
Assert.Equal(targetActorId, item.RequestedByActorId);
Assert.Equal(requestId2.ToString(), item.RequestId.Id);
Assert.Equal(requestId2.ToString(), item.RequestId!.Id);
},
item =>
{
Assert.Equal(targetActorId, item.RequestedByActorId);
Assert.Equal(requestId3.ToString(), item.RequestId.Id);
Assert.Equal(requestId3.ToString(), item.RequestId!.Id);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public async Task GetAsync_RequestExistsWithSuppliedRequestId_ReturnsRequest()
var repository = new SettlementReportRepository(context);

// act
var actual = await repository.GetAsync(expectedRequest.RequestId);
var actual = await repository.GetAsync(expectedRequest.RequestId!);

// assert
Assert.NotNull(actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;

namespace Energinet.DataHub.SettlementReport.Application.Helpers;
namespace Energinet.DataHub.SettlementReport.Interfaces.Helpers;

public interface IDatabricksJobsHelper
{
Task<long> RunSettlementReportsJobAsync(SettlementReportRequestDto request);
Task<JobRunId> RunSettlementReportsJobAsync(SettlementReportRequestDto request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ Task InitializeFromJobAsync(
Guid userId,
Guid actorId,
bool hideReport,
long jobId,
JobRunId jobId,
SettlementReportRequestDto request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

namespace Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;

public sealed record JobRunId(long Id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

namespace Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;

public enum JobRunStatus
{
Queued,
Running,
Completed,
Failed,
Canceled,
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
namespace Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;

public sealed record RequestedSettlementReportDto(
SettlementReportRequestId RequestId,
SettlementReportRequestId? RequestId,
CalculationType CalculationType,
DateTimeOffset PeriodStart,
DateTimeOffset PeriodEnd,
SettlementReportStatus Status,
int GridAreaCount,
double Progress,
Guid RequestedByActorId,
bool ContainsBasisData);
bool ContainsBasisData,
JobRunId? JobId);
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ public async Task<ActionResult> RequestSettlementReport([FromBody] SettlementRep
[HttpGet]
[Route("status/{jobId:long}")]
[Authorize]
public async Task<string> RequestSettlementReport(long jobId)
public async Task<JobRunStatus> RequestSettlementReport(long jobId)
{
return await Task.FromResult("running").ConfigureAwait(false);
var jobRunId = new JobRunId(jobId);
return await Task.FromResult(JobRunStatus.Running).ConfigureAwait(false);
}

private bool IsValid(SettlementReportRequestDto req)
Expand Down

0 comments on commit 7a96d05

Please sign in to comment.