Skip to content

Commit

Permalink
Added MarkAsCompleted
Browse files Browse the repository at this point in the history
  • Loading branch information
FirestarJes committed Sep 11, 2024
1 parent 1859e38 commit d76026d
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,71 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

namespace Energinet.DataHub.SettlementReport.Application.Handlers;

public sealed class ListSettlementReportJobsHandler : IListSettlementReportJobsHandler
{
private readonly IDatabricksJobsHelper _jobHelper;
private readonly IGetSettlementReportsHandler _getSettlementReportsHandler;
private readonly ISettlementReportRepository _repository;
private readonly IClock _clock;

public ListSettlementReportJobsHandler(
IDatabricksJobsHelper jobHelper,
IGetSettlementReportsHandler getSettlementReportsHandler)
IGetSettlementReportsHandler getSettlementReportsHandler,
ISettlementReportRepository repository,
IClock clock)
{
_jobHelper = jobHelper;
_getSettlementReportsHandler = getSettlementReportsHandler;
_repository = repository;
_clock = clock;
}

public async Task<IEnumerable<RequestedSettlementReportDto>> HandleAsync()
{
var settlementReports = (await _getSettlementReportsHandler
.GetAsync()
.ConfigureAwait(false))
.Where(x => x.JobId is not null).ToList();
.Where(x => x.JobId is not null && x.Status != SettlementReportStatus.Completed).ToList();

var results = new List<RequestedSettlementReportDto>();
foreach (var settlementReportDto in settlementReports)
{
var jobStatus = await _jobHelper.GetSettlementReportsJobStatusAsync(settlementReportDto.JobId!.Id).ConfigureAwait(false);
if (jobStatus == JobRunStatus.Completed)
{
await MarkAsCompletedAsync(settlementReportDto).ConfigureAwait(false);
}

results.Add(settlementReportDto with { Status = MapFromJobStatus(jobStatus) });
}

return results;
}

private async Task MarkAsCompletedAsync(RequestedSettlementReportDto settlementReportDto)
{
ArgumentNullException.ThrowIfNull(settlementReportDto);
ArgumentNullException.ThrowIfNull(settlementReportDto.JobId);

var request = await _repository
.GetAsync(settlementReportDto.JobId.Id)
.ConfigureAwait(false);

request.MarkAsCompleted(_clock, settlementReportDto.JobId);

await _repository
.AddOrUpdateAsync(request)
.ConfigureAwait(false);
}

private SettlementReportStatus MapFromJobStatus(JobRunStatus status)
{
return status switch
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.Interfaces.SettlementReports_v2.Models;

namespace Energinet.DataHub.SettlementReport.Application.SettlementReports_v2;

public interface ISettlementReportJobsFileRepository
{
Task DownloadAsync(JobRunId jobRunId, string fileName, Stream downloadStream);
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ public void MarkAsCompleted(IClock clock, GeneratedSettlementReportDto generated
EndedDateTime = clock.GetCurrentInstant();
}

public void MarkAsCompleted(IClock clock, JobRunId jobRunId)
{
Status = SettlementReportStatus.Completed;
BlobFileName = jobRunId.Id.ToString();
EndedDateTime = clock.GetCurrentInstant();
}

public void MarkAsFailed()
{
Status = SettlementReportStatus.Failed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,43 @@ public static IServiceCollection AddSettlementReportBlobStorage(this IServiceCol
{
var blobSettings = serviceProvider.GetRequiredService<IOptions<SettlementReportStorageOptions>>().Value;
options.ContainerName = blobSettings.StorageContainerName;
},
"SettlementReportBlobStorage");

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.StorageAccountJobsUri, blobSettings.StorageContainerJobsName);
var blobContainerClient = new BlobContainerClient(blobContainerUri, new DefaultAzureCredential());

return new SettlementReportJobsFileBlobStorage(blobContainerClient);
});

// Health checks
services.AddHealthChecks().AddAzureBlobStorage(
serviceProvider =>
{
var blobSettings = serviceProvider.GetRequiredService<IOptions<SettlementReportStorageOptions>>().Value;
return new BlobServiceClient(blobSettings.StorageAccountUri, new DefaultAzureCredential());
},
(serviceProvider, options) =>
{
var blobSettings = serviceProvider.GetRequiredService<IOptions<SettlementReportStorageOptions>>().Value;
options.ContainerName = blobSettings.StorageContainerName;
},
"SettlementReportBlobStorage - Jobs");

return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public class SettlementReportStorageOptions

[Required]
public string StorageContainerName { get; set; } = string.Empty;

[Required]
public Uri StorageAccountJobsUri { get; set; } = null!;

[Required]
public string StorageContainerJobsName { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 Azure.Storage.Blobs;
using Energinet.DataHub.SettlementReport.Application.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;

namespace Energinet.DataHub.SettlementReport.Infrastructure.SettlementReports_v2;

public sealed class SettlementReportJobsFileBlobStorage : ISettlementReportJobsFileRepository
{
private readonly BlobContainerClient _blobContainerClient;

public SettlementReportJobsFileBlobStorage(BlobContainerClient blobContainerClient)
{
_blobContainerClient = blobContainerClient;
}

public async Task DownloadAsync(JobRunId jobRunId, string fileName, Stream downloadStream)
{
var blobName = GetBlobName(jobRunId, fileName);
var blobClient = _blobContainerClient.GetBlobClient(blobName);
await blobClient.DownloadToAsync(downloadStream).ConfigureAwait(false);
}

private static string GetBlobName(JobRunId jobRunId, string fileName)
{
return string.Join('/', "settlement-reports", jobRunId.Id, fileName);
}
}

0 comments on commit d76026d

Please sign in to comment.