Skip to content

Commit

Permalink
Add params, and updated supporting code
Browse files Browse the repository at this point in the history
  • Loading branch information
FirestarJes committed Sep 11, 2024
1 parent d395560 commit d26fa1d
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public sealed record RequestSettlementReportCommand(
Guid UserId,
Guid ActorId,
bool IsFas,
string? ChargeOwnerId);
string? ChargeOwnerId,
MarketRole MarketRole);
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public RequestSettlementReportHandler(

public async Task<JobRunId> HandleAsync(RequestSettlementReportCommand request)
{
var runId = await _jobHelper.RunSettlementReportsJobAsync(request.RequestDto).ConfigureAwait(false);
var reportId = new SettlementReportRequestId(Guid.NewGuid().ToString());
var runId = await _jobHelper.RunSettlementReportsJobAsync(request.RequestDto, request.MarketRole, reportId).ConfigureAwait(false);
await _settlementReportInitializeHandler
.InitializeFromJobAsync(
request.UserId,
request.ActorId,
request.IsFas,
runId,
reportId,
request.RequestDto)
.ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task<IEnumerable<RequestedSettlementReportDto>> GetForJobsAsync(Gui
private static RequestedSettlementReportDto Map(SettlementReport report)
{
return new RequestedSettlementReportDto(
report.RequestId is not null ? new SettlementReportRequestId(report.RequestId) : null,
new SettlementReportRequestId(report.RequestId),
report.CalculationType,
report.PeriodStart.ToDateTimeOffset(),
report.PeriodEnd.ToDateTimeOffset(),
Expand Down
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; }
public string RequestId { get; init; } = null!;

public Guid UserId { get; init; }

Expand Down Expand Up @@ -78,8 +78,10 @@ public SettlementReport(
Guid actorId,
bool hideReport,
JobRunId jobRunId,
SettlementReportRequestId requestId,
SettlementReportRequestDto request)
{
RequestId = requestId.Id;
JobId = jobRunId.Id;
UserId = userId;
ActorId = actorId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ public Task InitializeAsync(
return _repository.AddOrUpdateAsync(settlementReport);
}

public Task InitializeFromJobAsync(Guid userId, Guid actorId, bool hideReport, JobRunId jobId, SettlementReportRequestDto request)
public Task InitializeFromJobAsync(
Guid userId,
Guid actorId,
bool hideReport,
JobRunId jobId,
SettlementReportRequestId requestId,
SettlementReportRequestDto request)
{
var settlementReport = new SettlementReport(SystemClock.Instance, userId, actorId, hideReport, new SettlementReportRequestId(jobId.ToString()), request);
var settlementReport = new SettlementReport(SystemClock.Instance, userId, actorId, hideReport, jobId, requestId, request);
return _repository.AddOrUpdateAsync(settlementReport);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,4 @@ public static IServiceCollection AddSettlementReportBlobStorage(this IServiceCol

return services;
}

public static IServiceCollection AddSettlementReportBlobStorageForJobs(this IServiceCollection services)
{
services
.AddOptions<SettlementReportStorageOptions>()
.BindConfiguration(SettlementReportStorageOptions.SectionName)
.ValidateDataAnnotations();

services.AddScoped<ISettlementReportJobsFileRepository, SettlementReportJobsFileBlobStorage>(serviceProvider =>
{
var blobSettings = serviceProvider.GetRequiredService<IOptions<SettlementReportStorageOptions>>().Value;

var blobContainerUri = new Uri(blobSettings.StorageAccountUri, blobSettings.StorageContainerName);
var blobContainerClient = new BlobContainerClient(blobContainerUri, new DefaultAzureCredential());

return new SettlementReportJobsFileBlobStorage(blobContainerClient);
});

return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public DatabricksJobsHelper(IJobsApiClient jobsApiClient)
_jobsApiClient = jobsApiClient;
}

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

public async Task<JobRunStatus> GetSettlementReportsJobStatusAsync(long runId)
Expand Down Expand Up @@ -67,21 +70,49 @@ private async Task<Job> GetSettlementReportsJobAsync(string jobName)
return await _jobsApiClient.Jobs.Get(settlementJob.JobId).ConfigureAwait(false);
}

private RunParameters CreateParameters(SettlementReportRequestDto request)
private RunParameters CreateParameters(SettlementReportRequestDto request, MarketRole marketRole, SettlementReportRequestId reportId)
{
var gridAreas = string.Join(", ", request.Filter.GridAreas.Select(c => c.Key));
var gridAreas = $"{{{string.Join(", ", request.Filter.GridAreas.Select(c => $"\"{c.Key}\", \"{c.Value}\""))}}}";

var jobParameters = new List<string>
{
$"--grid-areas=[{gridAreas}]",
$"--period-start-datetime={request.Filter.PeriodStart}",
$"--period-end-datetime={request.Filter.PeriodEnd}",
$"--report-id={reportId}",
$"--calculation-type={CalculationTypeMapper.ToDeltaTableValue(request.Filter.CalculationType)}",
$"--calculation-id-by-grid-area={gridAreas}",
$"--period-start={request.Filter.PeriodStart}",
$"--period-end={request.Filter.PeriodEnd}",
$"--market-role={MapMarketRole(marketRole)}",
};
if (request.Filter.EnergySupplier != null)
{
jobParameters.Add($"--energy-supplier-id={request.Filter.EnergySupplier}");
}

if (request.SplitReportPerGridArea)
{
jobParameters.Add("--split-report-by-grid-area");
}

if (request.PreventLargeTextFiles)
{
jobParameters.Add("--prevent-large-text-files");
}

return RunParameters.CreatePythonParams(jobParameters);
}

private static string MapMarketRole(MarketRole marketRole)
{
return marketRole switch
{
MarketRole.EnergySupplier => "energy_supplier",
MarketRole.DataHubAdministrator => "datahub_administrator",
MarketRole.GridAccessProvider => "grid_access_provider",
MarketRole.SystemOperator => "system_operator",
_ => throw new ArgumentOutOfRangeException(nameof(marketRole), marketRole, $"Market role \"{marketRole}\" not supported in report generation"),
};
}

private static JobRunStatus ConvertJobStatus(Run jobRun)
{
if (jobRun.State == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ public async Task RemoveExpiredAsync(IList<Application.SettlementReports_v2.Sett

if (settlementReport.BlobFileName != null)
{
if (settlementReport.RequestId is not null)
if (settlementReport.JobId is not null)
{
await _settlementReportFileRepository
.DeleteAsync(new SettlementReportRequestId(settlementReport.RequestId), settlementReport.BlobFileName)
await _settlementReportJobFileRepository
.DeleteAsync(
new JobRunId(settlementReport.JobId.GetValueOrDefault()),
settlementReport.BlobFileName)
.ConfigureAwait(false);
}
else if (settlementReport.JobId is not null)
else
{
await _settlementReportJobFileRepository
.DeleteAsync(new JobRunId(settlementReport.JobId.GetValueOrDefault()), settlementReport.BlobFileName)
await _settlementReportFileRepository
.DeleteAsync(
new SettlementReportRequestId(settlementReport.RequestId),
settlementReport.BlobFileName)
.ConfigureAwait(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,15 @@ public async Task GetAsync_HiddenReport_ReturnsRequestsForJobs()
expectedRequest.ActorId,
true,
new JobRunId(Random.Shared.NextInt64()),
new SettlementReportRequestId(Guid.NewGuid().ToString()),
new SettlementReportRequestDto(false, false, false, false, requestFilterDto)));
await PrepareNewRequestAsync(requestFilterDto => new SettlementReport.Application.SettlementReports_v2.SettlementReport(
SystemClock.Instance,
Guid.NewGuid(),
expectedRequest.ActorId,
true,
new JobRunId(Random.Shared.NextInt64()),
new SettlementReportRequestId(Guid.NewGuid().ToString()),
new SettlementReportRequestDto(false, false, false, false, requestFilterDto)));

await using var context = _databaseManager.CreateDbContext();
Expand Down Expand Up @@ -348,6 +350,7 @@ public async Task GetAsync_HiddenReport_ReturnsRequestsForJobs()
Guid.NewGuid(),
false,
new JobRunId(Random.Shared.NextInt64()),
new SettlementReportRequestId(Guid.NewGuid().ToString()),
new SettlementReportRequestDto(false, false, false, false, requestFilterDto));

if (createReport != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ namespace Energinet.DataHub.SettlementReport.Interfaces.Helpers;

public interface IDatabricksJobsHelper
{
Task<JobRunId> RunSettlementReportsJobAsync(SettlementReportRequestDto request);
Task<JobRunId> RunSettlementReportsJobAsync(
SettlementReportRequestDto request,
MarketRole marketRole,
SettlementReportRequestId reportId);

Task<JobRunStatus> GetSettlementReportsJobStatusAsync(long runId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common.Infrastructure\Common.Infrastructure.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ Task InitializeFromJobAsync(
Guid actorId,
bool hideReport,
JobRunId jobId,
SettlementReportRequestId requestId,
SettlementReportRequestDto request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public async Task Handle_ValidRequest_ReturnsJobId()
var jobHelperMock = new Mock<IDatabricksJobsHelper>();
var jobRunId = new JobRunId(Random.Shared.NextInt64());
jobHelperMock
.Setup(x => x.RunSettlementReportsJobAsync(It.IsAny<SettlementReportRequestDto>()))
.Setup(x => x.RunSettlementReportsJobAsync(It.IsAny<SettlementReportRequestDto>(), It.IsAny<MarketRole>(), It.IsAny<SettlementReportRequestId>()))
.ReturnsAsync(jobRunId);

var command = new RequestSettlementReportCommand(request, Guid.NewGuid(), Guid.NewGuid(), true, null);
var command = new RequestSettlementReportCommand(request, Guid.NewGuid(), Guid.NewGuid(), true, null, MarketRole.EnergySupplier);
var handler = new RequestSettlementReportHandler(jobHelperMock.Object, initializerMock.Object);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public async Task<ActionResult<long>> RequestSettlementReport([FromBody] Settlem
_userContext.CurrentUser.UserId,
_userContext.CurrentUser.Actor.ActorId,
_userContext.CurrentUser.MultiTenancy,
chargeOwnerId);
chargeOwnerId,
marketRole);

var result = await _requestSettlementReportJobHandler.HandleAsync(requestCommand).ConfigureAwait(false);

Expand Down

0 comments on commit d26fa1d

Please sign in to comment.