Skip to content

Commit

Permalink
Merge branch 'main' into xvibe/settlement-report-without-basis-data-r…
Browse files Browse the repository at this point in the history
…uns-separate
  • Loading branch information
vbelinschi authored Sep 18, 2024
2 parents 41b714f + 7c17ed0 commit cfab9da
Show file tree
Hide file tree
Showing 35 changed files with 309 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ limitations under the License.
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.17.1" />
<PackageReference Include="Energinet.DataHub.Core.Databricks.Jobs" Version="11.1.1" />
<PackageReference Include="Energinet.DataHub.Core.Databricks.SqlStatementExecution" Version="11.2.1" />
<PackageReference Include="Energinet.DataHub.Core.Databricks.SqlStatementExecution" Version="11.2.4" />
<PackageReference Include="Energinet.DataHub.Core.App.Common.Abstractions" Version="12.2.1" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Databricks.Client" Version="2.5.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

namespace Energinet.DataHub.SettlementReport.Common.Infrastructure.Telemetry;

public static class TelemetryConstants
public static class SubsystemInformation
{
public const string SubsystemName = "settlement-report";
public static readonly Guid Id = Guid.Parse("293d2893-fbf0-4f61-8595-6becb6e0206b");
public static readonly string SubsystemName = "settlement-report";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Runtime.InteropServices;
using Azure.Storage.Blobs;
using Energinet.DataHub.Core.Databricks.Jobs.Configuration;
Expand All @@ -28,6 +29,8 @@
using Energinet.DataHub.SettlementReport.Orchestration.SettlementReports.IntegrationTests.DurableTask;
using Energinet.DataHub.SettlementReport.Test.Core.Fixture.Database;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using Xunit.Abstractions;

Expand Down Expand Up @@ -93,6 +96,7 @@ public async Task InitializeAsync()
var appHostSettings = CreateAppHostSettings(ref port);

await EnsureSettlementReportStorageContainerExistsAsync();
EnsureRevisionLogRespondsWithSuccess();

// Create and start host
AppHostManager = new FunctionAppHostManager(appHostSettings, TestLogger);
Expand Down Expand Up @@ -124,6 +128,22 @@ public void EnsureAppHostUsesMockedDatabricksJobs()
});
}

public void EnsureRevisionLogRespondsWithSuccess()
{
var requestRevision = Request
.Create()
.WithPath("/revision-log")
.UsingPost();

var response = Response
.Create()
.WithStatusCode(HttpStatusCode.OK);

MockServer
.Given(requestRevision)
.RespondWith(response);
}

public BlobContainerClient CreateBlobContainerClient()
{
return new BlobContainerClient(AzuriteManager.FullConnectionString, "settlement-report-container");
Expand Down Expand Up @@ -206,6 +226,18 @@ private FunctionAppHostSettings CreateAppHostSettings(ref int port)
$"{SettlementReportStorageOptions.SectionName}__{nameof(SettlementReportStorageOptions.StorageAccountUri)}",
AzuriteManager.BlobStorageServiceUri + "/");

appHostSettings.ProcessEnvironmentVariables.Add(
$"{SettlementReportStorageOptions.SectionName}__{nameof(SettlementReportStorageOptions.StorageContainerForJobsName)}",
"settlement-report-container-jobs");
appHostSettings.ProcessEnvironmentVariables.Add(
$"{SettlementReportStorageOptions.SectionName}__{nameof(SettlementReportStorageOptions.StorageAccountForJobsUri)}",
AzuriteManager.BlobStorageServiceUri + "/");

// Revision log
appHostSettings.ProcessEnvironmentVariables.Add(
$"RevisionLogOptions:ApiAddress",
MockServer.Url! + "/revision-log");

return appHostSettings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public Task InitializeAsync()

// Clear mappings etc. before each test
Fixture.MockServer.Reset();
Fixture.EnsureRevisionLogRespondsWithSuccess();

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,31 @@
using System.Net;
using Azure;
using Energinet.DataHub.Core.App.Common.Abstractions.Users;
using Energinet.DataHub.RevisionLog.Integration;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Security;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Telemetry;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using NodaTime;

namespace Energinet.DataHub.SettlementReport.Orchestration.SettlementReports.Functions.SettlementReports;

internal sealed class SettlementReportDownloadTrigger
{
private readonly IUserContext<FrontendUser> _userContext;
private readonly ISettlementReportDownloadHandler _settlementReportDownloadHandler;
private readonly IRevisionLogClient _revisionLogClient;

public SettlementReportDownloadTrigger(IUserContext<FrontendUser> userContext, ISettlementReportDownloadHandler settlementReportDownloadHandler)
public SettlementReportDownloadTrigger(
IUserContext<FrontendUser> userContext,
ISettlementReportDownloadHandler settlementReportDownloadHandler,
IRevisionLogClient revisionLogClient)
{
_userContext = userContext;
_settlementReportDownloadHandler = settlementReportDownloadHandler;
_revisionLogClient = revisionLogClient;
}

[Function(nameof(SettlementReportDownload))]
Expand All @@ -43,6 +51,18 @@ public async Task SettlementReportDownload(
{
try
{
await _revisionLogClient.LogAsync(
new RevisionLogEntry(
actorId: _userContext.CurrentUser.Actor.ActorId,
userId: _userContext.CurrentUser.UserId,
logId: Guid.NewGuid(),
systemId: SubsystemInformation.Id,
occurredOn: SystemClock.Instance.GetCurrentInstant(),
activity: "SettlementReportDownload",
origin: nameof(SettlementReportDownloadTrigger),
payload: settlementReportRequestId.Id))
.ConfigureAwait(false);

await _settlementReportDownloadHandler
.DownloadReportAsync(
settlementReportRequestId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

using System.Net;
using Energinet.DataHub.Core.App.Common.Abstractions.Users;
using Energinet.DataHub.RevisionLog.Integration;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Security;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Telemetry;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.SettlementReport.Orchestration.SettlementReports.Functions.SettlementReports.Model;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.DurableTask.Client;
using NodaTime;

namespace Energinet.DataHub.SettlementReport.Orchestration.SettlementReports.Functions.SettlementReports;

Expand All @@ -29,15 +32,18 @@ internal sealed class SettlementReportListHttpTrigger
private readonly IUserContext<FrontendUser> _userContext;
private readonly IGetSettlementReportsHandler _getSettlementReportsHandler;
private readonly IUpdateFailedSettlementReportsHandler _updateFailedSettlementReportsHandler;
private readonly IRevisionLogClient _revisionLogClient;

public SettlementReportListHttpTrigger(
IUserContext<FrontendUser> userContext,
IGetSettlementReportsHandler getSettlementReportsHandler,
IUpdateFailedSettlementReportsHandler updateFailedSettlementReportsHandler)
IUpdateFailedSettlementReportsHandler updateFailedSettlementReportsHandler,
IRevisionLogClient revisionLogClient)
{
_userContext = userContext;
_getSettlementReportsHandler = getSettlementReportsHandler;
_updateFailedSettlementReportsHandler = updateFailedSettlementReportsHandler;
_revisionLogClient = revisionLogClient;
}

[Function(nameof(ListSettlementReports))]
Expand All @@ -47,6 +53,18 @@ public async Task<HttpResponseData> ListSettlementReports(
[DurableClient] DurableTaskClient client,
FunctionContext executionContext)
{
await _revisionLogClient.LogAsync(
new RevisionLogEntry(
actorId: _userContext.CurrentUser.Actor.ActorId,
userId: _userContext.CurrentUser.UserId,
logId: Guid.NewGuid(),
systemId: SubsystemInformation.Id,
occurredOn: SystemClock.Instance.GetCurrentInstant(),
activity: "ListSettlementReports",
origin: nameof(SettlementReportListHttpTrigger),
payload: string.Empty))
.ConfigureAwait(false);

var allowedSettlementReports = await GetAllowedSettlementReportsAsync()
.ConfigureAwait(false);

Expand Down Expand Up @@ -81,7 +99,7 @@ private async Task<IEnumerable<RequestedSettlementReportDto>> CheckStatusOfSettl
{
var updatedReport = settlementReport;

if (settlementReport.Status == SettlementReportStatus.InProgress && settlementReport.RequestId != null)
if (settlementReport.Status == SettlementReportStatus.InProgress && settlementReport.JobId == null)
{
var instanceInfo = await durableTaskClient
.GetInstanceAsync(settlementReport.RequestId.Id, getInputsAndOutputs: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,34 @@

using System.Net;
using Energinet.DataHub.Core.App.Common.Abstractions.Users;
using Energinet.DataHub.RevisionLog.Integration;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Security;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Telemetry;
using Energinet.DataHub.SettlementReport.Interfaces.Models;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.SettlementReport.Orchestration.SettlementReports.Functions.SettlementReports.Model;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.DurableTask.Client;
using NodaTime;

namespace Energinet.DataHub.SettlementReport.Orchestration.SettlementReports.Functions.SettlementReports;

internal sealed class SettlementReportRequestTrigger
{
private readonly IUserContext<FrontendUser> _userContext;
private readonly ISettlementReportInitializeHandler _settlementReportInitializeHandler;
private readonly IRevisionLogClient _revisionLogClient;

public SettlementReportRequestTrigger(
IUserContext<FrontendUser> userContext,
ISettlementReportInitializeHandler settlementReportInitializeHandler)
ISettlementReportInitializeHandler settlementReportInitializeHandler,
IRevisionLogClient revisionLogClient)
{
_userContext = userContext;
_settlementReportInitializeHandler = settlementReportInitializeHandler;
_revisionLogClient = revisionLogClient;
}

[Function(nameof(RequestSettlementReport))]
Expand All @@ -46,6 +52,18 @@ public async Task<HttpResponseData> RequestSettlementReport(
[DurableClient] DurableTaskClient client,
FunctionContext executionContext)
{
await _revisionLogClient.LogAsync(
new RevisionLogEntry(
actorId: _userContext.CurrentUser.Actor.ActorId,
userId: _userContext.CurrentUser.UserId,
logId: Guid.NewGuid(),
systemId: SubsystemInformation.Id,
occurredOn: SystemClock.Instance.GetCurrentInstant(),
activity: "RequestSettlementReport",
origin: nameof(SettlementReportRequestTrigger),
payload: System.Text.Json.JsonSerializer.Serialize(settlementReportRequest)))
.ConfigureAwait(false);

if (_userContext.CurrentUser.Actor.MarketRole == FrontendActorMarketRole.EnergySupplier && string.IsNullOrWhiteSpace(settlementReportRequest.Filter.EnergySupplier))
{
settlementReportRequest = settlementReportRequest with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Energinet.DataHub.RevisionLog.Integration" Version="1.1.3" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Energinet.DataHub.Core.App.FunctionApp" Version="12.2.1" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Energinet.DataHub.Core.App.Common.Extensions.DependencyInjection;
using Energinet.DataHub.Core.App.FunctionApp.Extensions.Builder;
using Energinet.DataHub.Core.App.FunctionApp.Extensions.DependencyInjection;
using Energinet.DataHub.RevisionLog.Integration.Extensions.DependencyInjection;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Security;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Telemetry;
using Energinet.DataHub.SettlementReport.Infrastructure.Extensions.DependencyInjection;
Expand All @@ -29,7 +30,7 @@
.ConfigureServices((context, services) =>
{
// Common
services.AddApplicationInsightsForIsolatedWorker(TelemetryConstants.SubsystemName);
services.AddApplicationInsightsForIsolatedWorker(SubsystemInformation.SubsystemName);
services.AddHealthChecksForIsolatedWorker();

// Http => Authentication
Expand All @@ -38,6 +39,9 @@
// Shared by modules
services.AddNodaTimeForApplication();

// revision log
services.AddRevisionLogIntegrationModule(context.Configuration);

// Modules
services.AddSettlementReportsV2Module(context.Configuration);
})
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.Handlers;

public interface ISettlementReportJobsDownloadHandler
{
Task DownloadReportAsync(SettlementReportRequestId requestId, Func<Stream> outputStreamProvider, Guid actorId, bool isMultitenancy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ private async Task<IEnumerable<RequestedSettlementReportDto>> GetSettlementRepor
{
var jobStatus = await _jobHelper.GetSettlementReportsJobStatusAsync(settlementReportDto.JobId!.Id)
.ConfigureAwait(false);
if (jobStatus == JobRunStatus.Completed)
switch (jobStatus)
{
await MarkAsCompletedAsync(settlementReportDto).ConfigureAwait(false);
case JobRunStatus.Completed:
await MarkAsCompletedAsync(settlementReportDto).ConfigureAwait(false);
break;
case JobRunStatus.Canceled or JobRunStatus.Failed:
await MarkAsFailedAsync(settlementReportDto).ConfigureAwait(false);
break;
}

results.Add(settlementReportDto with { Status = MapFromJobStatus(jobStatus) });
Expand All @@ -85,7 +90,23 @@ private async Task MarkAsCompletedAsync(RequestedSettlementReportDto settlementR
.GetAsync(settlementReportDto.JobId.Id)
.ConfigureAwait(false);

request.MarkAsCompleted(_clock, settlementReportDto.JobId);
request.MarkAsCompleted(_clock, settlementReportDto.RequestId);

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

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

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

request.MarkAsFailed();

await _repository
.AddOrUpdateAsync(request)
Expand Down
Loading

0 comments on commit cfab9da

Please sign in to comment.