Skip to content

Commit

Permalink
First implementation of Job API
Browse files Browse the repository at this point in the history
  • Loading branch information
FirestarJes committed Sep 6, 2024
1 parent 21e21a4 commit 360a217
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/actions/dotnet-prepare-outputs/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ runs:
shell: bash
run: |
dotnet publish \
'.\source\settlement-report\SettlmentReports.WebAPI\SettlmentReports.WebAPI.csproj' \
'.\source\settlement-report\SettlementReports.WebAPI\SettlmentReports.WebAPI.csproj' \
--no-build \
--no-restore \
--configuration Release \
Expand Down
2 changes: 1 addition & 1 deletion SettlementReport.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.Core", "source\settlem
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchestration.SettlementReports.IntegrationTests", "source\settlement-report\Orchestration.SettlementReports.IntegrationTests\Orchestration.SettlementReports.IntegrationTests.csproj", "{63FCEA68-F792-4860-88AD-7EF163AD667F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SettlmentReports.WebAPI", "source\settlement-report\SettlmentReports.WebAPI\SettlmentReports.WebAPI.csproj", "{016FF5AF-759C-4A11-ABA1-723C6F925953}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SettlementReports.WebAPI", "source\settlement-report\SettlementReports.WebAPI\SettlementReports.WebAPI.csproj", "{016FF5AF-759C-4A11-ABA1-723C6F925953}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SettlementReports.Application", "source\settlement-report\SettlementReports.Application\SettlementReports.Application.csproj", "{90B61921-AF88-4010-AFB8-08FAA3EBF173}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ limitations under the License.
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="8.0.1" />
<PackageReference Include="Azure.Identity" Version="1.11.4" />
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.17.1" />
<PackageReference Include="Energinet.DataHub.Core.Databricks.SqlStatementExecution" Version="11.0.0" />
<PackageReference Include="Energinet.DataHub.Core.Databricks.Jobs" Version="11.0.0" />
<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" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Energinet DataHub A/S
// 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.
Expand All @@ -16,12 +16,4 @@

namespace Energinet.DataHub.SettlementReport.Infrastructure.Commands;

public class RequestSettlementReportJobCommand
{
private readonly SettlementReportRequestDto _request;

public RequestSettlementReportJobCommand(SettlementReportRequestDto request)
{
_request = request;
}
}
public record RequestSettlementReportJobCommand(SettlementReportRequestDto Request);
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public interface IRequestSettlemenReportJobHandler
/// Request a settlement report job
/// </summary>
/// <param name="command"></param>
/// <returns>A <see cref="Task{Guid}"/> representing the job id of the requested settlement report.</returns>
Task<Guid> HandleAsync(RequestSettlementReportJobCommand command);
/// <returns>A long value representing the job id of the requested settlement report.</returns>
Task<long> HandleAsync(RequestSettlementReportJobCommand command);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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.Commands;
using Energinet.DataHub.SettlementReport.Infrastructure.SqlStatements.Mappers;
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;

public RequestSettlementReportHandler(IJobsApiClient jobsApiClient)
{
_jobsApiClient = jobsApiClient;
}

public async Task<long> HandleAsync(RequestSettlementReportJobCommand command)
{
var parameters = CreateParameters(command.Request);
return await _jobsApiClient.Jobs.RunNow(1, parameters).ConfigureAwait(false);
}

private RunParameters CreateParameters(SettlementReportRequestDto request)
{
var gridAreas = string.Join(", ", request.Filter.GridAreas.Select(c => c.Key));

var jobParameters = new List<string>
{
$"--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<Job> GetSettlementReportsJobAsync()
{
var calculatorJob = await _jobsApiClient.Jobs
.ListPageable(name: "CalculatorJob")
.SingleAsync()
.ConfigureAwait(false);

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Energinet.DataHub.ImbalancePrices.Hosts.WebApi.Controllers;
namespace SettlementReports.WebAPI.Controllers;

[ApiController]
[Route("settlement-reports")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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.App.Common.Extensions.DependencyInjection;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Extensions.Options;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.HealthChecks;
using Energinet.DataHub.SettlementReport.Infrastructure.Handlers;
using Energinet.DataHub.SettlementReport.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;

namespace SettlementReports.WebAPI.Extensions.DependencyInjection;

public static class SettlementReportModuleExtensions
{
public static IServiceCollection AddSettlementReportApiModule(this IServiceCollection services, IConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(configuration);

// handlers
services.AddScoped<IRequestSettlemenReportJobHandler, RequestSettlementReportHandler>();

services.AddScoped<ISettlementReportDatabaseContext, SettlementReportDatabaseContext>();
services.AddDbContext<SettlementReportDatabaseContext>(
options => options.UseSqlServer(
configuration
.GetSection(ConnectionStringsOptions.ConnectionStrings)
.Get<ConnectionStringsOptions>()!.DB_CONNECTION_STRING,
o =>
{
o.UseNodaTime();
o.EnableRetryOnFailure();
}));

// Database Health check
services.TryAddHealthChecks(
registrationKey: HealthCheckNames.SettlementReportDatabase,
(key, builder) =>
{
builder.AddDbContextCheck<SettlementReportDatabaseContext>(name: key);
});

AddHealthChecks(services);

return services;
}

private static void AddHealthChecks(IServiceCollection services)
{
services
.AddHealthChecks();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
using Asp.Versioning;
using Energinet.DataHub.Core.App.WebApp.Extensions.Builder;
using Energinet.DataHub.Core.App.WebApp.Extensions.DependencyInjection;
using Energinet.DataHub.Core.Databricks.Jobs.Extensions.DependencyInjection;
using Energinet.DataHub.Core.Logging.LoggingMiddleware;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Security;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Telemetry;
using Energinet.DataHub.SettlementReport.Infrastructure.Extensions.DependencyInjection;
using SettlementReports.WebAPI.Extensions.DependencyInjection;

const string subsystemName = TelemetryConstants.SubsystemName;

Expand All @@ -38,6 +41,8 @@
.AddSwaggerForWebApp(Assembly.GetExecutingAssembly(), subsystemName)
.AddJwtBearerAuthenticationForWebApp(builder.Configuration)
.AddUserAuthenticationForWebApp<FrontendUser, FrontendUserProvider>()
.AddDatabricksJobs(builder.Configuration)
.AddSettlementReportApiModule(builder.Configuration)
.AddPermissionAuthorizationForWebApp();

var app = builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>SettlementReports.WebAPI</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand All @@ -20,6 +21,7 @@

<ItemGroup>
<ProjectReference Include="..\Common.Infrastructure\Common.Infrastructure.csproj" />
<ProjectReference Include="..\SettlementReports.Infrastructure\SettlementReports.Infrastructure.csproj" />
<ProjectReference Include="..\SettlementReports.Interfaces\SettlementReports.Interfaces.csproj" />
</ItemGroup>

Expand Down

0 comments on commit 360a217

Please sign in to comment.