Skip to content

Commit

Permalink
Merge branch 'main' into xchrh/settlement_report_job_python
Browse files Browse the repository at this point in the history
  • Loading branch information
cenh-halfspace authored Jan 2, 2025
2 parents 5799a66 + 0dc25bc commit 12407e9
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ limitations under the License.
<EmbeddedResource Include="Scripts\202409081607_Add_JobId_To_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202410261405_Add_IsNotificationSent_to_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202412021100_Add_GridAreaOwner.sql" />
<EmbeddedResource Include="Scripts\202412131100_Update_GridAreaOwner_ValidFrom.sql" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
if not exists(select 1 from sys.tables where name = 'GridAreaOwnerBak')
begin
select *
into settlementreports.GridAreaOwnerBak
from settlementreports.gridareaowner
end

update settlementreports.gridareaowner
set validfrom = '2020-01-01'
where id in (select g.id
from settlementreports.gridareaowner g
join (select code, min(validfrom) as validfrom
from settlementreports.gridareaowner
group by code) temp
on g.code = temp.code and g.validfrom = temp.validfrom)
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,79 @@
// limitations under the License.

using Energinet.DataHub.SettlementReport.Application.Commands;
using Energinet.DataHub.SettlementReport.Application.Model;
using Energinet.DataHub.SettlementReport.Application.Services;
using Energinet.DataHub.SettlementReport.Interfaces.Helpers;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using NodaTime.Extensions;

namespace Energinet.DataHub.SettlementReport.Application.Handlers;

public sealed class RequestSettlementReportHandler : IRequestSettlementReportJobHandler
{
private readonly IDatabricksJobsHelper _jobHelper;
private readonly ISettlementReportInitializeHandler _settlementReportInitializeHandler;
private readonly IGridAreaOwnerRepository _gridAreaOwnerRepository;

public RequestSettlementReportHandler(
IDatabricksJobsHelper jobHelper,
ISettlementReportInitializeHandler settlementReportInitializeHandler)
ISettlementReportInitializeHandler settlementReportInitializeHandler,
IGridAreaOwnerRepository gridAreaOwnerRepository)
{
_jobHelper = jobHelper;
_settlementReportInitializeHandler = settlementReportInitializeHandler;
_gridAreaOwnerRepository = gridAreaOwnerRepository;
}

public async Task<JobRunId> HandleAsync(RequestSettlementReportCommand request)
{
if (request.MarketRole == MarketRole.GridAccessProvider && request.RequestDto.Filter.GridAreas.Count > 0)
{
JobRunId? firstRunId = null;

var gridAreas = request.RequestDto.Filter.GridAreas.Select(x => x.Key);

var distinctOwners = await GetGridAreaOwnersAsync(gridAreas, request.RequestDto.Filter).ConfigureAwait(false);

foreach (var owner in distinctOwners)
{
var id = await StartReportAsync(request, owner.Value).ConfigureAwait(false);
firstRunId ??= id;
}

return firstRunId!;
}

return await StartReportAsync(request, request.ActorGln).ConfigureAwait(false);
}

private async Task<IEnumerable<ActorNumber>> GetGridAreaOwnersAsync(IEnumerable<string> gridAreaCodes, SettlementReportRequestFilterDto filter)
{
var gridAreaOwners = new HashSet<ActorNumber>();

foreach (var grid in gridAreaCodes)
{
var owners = await _gridAreaOwnerRepository.GetGridAreaOwnersAsync(
new GridAreaCode(grid),
filter.PeriodStart.ToInstant(),
filter.PeriodEnd.ToInstant()).ConfigureAwait(false);

foreach (var owner in owners)
{
gridAreaOwners.Add(owner.ActorNumber);
}
}

return gridAreaOwners;
}

private async Task<JobRunId> StartReportAsync(RequestSettlementReportCommand request, string requestActorGln)
{
var reportId = new SettlementReportRequestId(Guid.NewGuid().ToString());
var runId = await _jobHelper.RunSettlementReportsJobAsync(request.RequestDto, request.MarketRole, reportId, request.ActorGln).ConfigureAwait(false);

var runId = await _jobHelper.RunSettlementReportsJobAsync(request.RequestDto, request.MarketRole, reportId, requestActorGln).ConfigureAwait(false);

await _settlementReportInitializeHandler
.InitializeFromJobAsync(
request.UserId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Energinet.DataHub.SettlementReport.Infrastructure.Model;
namespace Energinet.DataHub.SettlementReport.Application.Model;

public sealed record ActorNumber(string Value);
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Energinet.DataHub.SettlementReport.Infrastructure.Model;
namespace Energinet.DataHub.SettlementReport.Application.Model;

public sealed record GridAreaCode(string Value);
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

using NodaTime;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Model;
namespace Energinet.DataHub.SettlementReport.Application.Model;

public sealed record GridAreaOwner(GridAreaCode Code, ActorNumber ActorNumber, Instant ValidFrom);
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.SettlementReport.Infrastructure.Model;
using Energinet.DataHub.SettlementReport.Application.Model;
using NodaTime;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Services;
namespace Energinet.DataHub.SettlementReport.Application.Services;

public interface IGridAreaOwnerRepository
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.SettlementReport.Application.Model;
using Energinet.DataHub.SettlementReport.Application.Services;
using Energinet.DataHub.SettlementReport.Infrastructure.Contracts;
using Energinet.DataHub.SettlementReport.Infrastructure.Model;
using Energinet.DataHub.SettlementReport.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using NodaTime;
Expand All @@ -33,20 +34,35 @@ public async Task<IEnumerable<GridAreaOwner>> GetGridAreaOwnersAsync(GridAreaCod
{
ArgumentNullException.ThrowIfNull(gridAreaCode);

var from = periodFrom.ToDateTimeOffset();
var to = periodTo.ToDateTimeOffset();
var f = periodFrom.ToDateTimeOffset();
var t = periodTo.ToDateTimeOffset();

var entities = await _dbContext
.GridAreaOwners
.OrderByDescending(gridAreaOwnerEntity => gridAreaOwnerEntity.ValidFrom)
.ThenByDescending(gridAreaOwnerEntity => gridAreaOwnerEntity.SequenceNumber)
.Where(gridAreaOwnerEntity =>
gridAreaOwnerEntity.ValidFrom >= from && gridAreaOwnerEntity.ValidFrom < to &&
gridAreaOwnerEntity.Code == gridAreaCode.Value)
.ToListAsync()
.ConfigureAwait(false);
var inner = from ga in _dbContext.GridAreaOwners
select new
{
ga.Code,
ga.ActorNumber,
ga.ValidFrom,
ValidTo = _dbContext.GridAreaOwners
.Where(x => x.Code == ga.Code && x.ValidFrom > ga.ValidFrom)
.Select(x => (DateTimeOffset?)x.ValidFrom)
.OrderBy(x => x).FirstOrDefault(),
ga.SequenceNumber,
};

return entities
var query = from ga in inner
where
ga.Code == gridAreaCode.Value &&
(ga.ValidTo == null || f <= ga.ValidTo) && ga.ValidFrom < t
orderby ga.ValidFrom descending, ga.SequenceNumber descending
select new
{
ga.Code,
ga.ActorNumber,
ga.ValidFrom,
};

return (await query.ToListAsync().ConfigureAwait(false))
.Select(x => new GridAreaOwner(new GridAreaCode(x.Code), new ActorNumber(x.ActorNumber), Instant.FromDateTimeOffset(x.ValidFrom)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.SettlementReport.Application.Model;
using Energinet.DataHub.SettlementReport.Infrastructure.Contracts;
using Energinet.DataHub.SettlementReport.Infrastructure.Model;
using Energinet.DataHub.SettlementReport.Infrastructure.Persistence;
using Energinet.DataHub.SettlementReport.Infrastructure.Services;
using Energinet.DataHub.SettlementReport.Test.Core.Fixture.Database;
Expand All @@ -32,50 +32,56 @@ public GridAreaOwnerRepositoryTests(WholesaleDatabaseFixture<SettlementReportDat
_databaseManager = fixture.DatabaseManager;
}

[Fact]
public async Task Get_MultipleGridOwnersInPeriod_ReturnsAll()
[Theory]
// fixed owners for grid area 1:2024-01-01, 2:2024-01-15
// sliding window + grid area code + expected
[InlineData("2024-02-01", "2024-03-01", "100", "2")]
[InlineData("2024-01-14", "2024-03-01", "101", "1,2")]
[InlineData("2024-01-01", "2024-01-14", "102", "1")]
[InlineData("2024-01-01", "2024-01-15", "103", "1")]
[InlineData("2024-01-01", "2024-01-16", "104", "1,2")]
[InlineData("2023-12-30", "2023-12-31", "105", "")]
[InlineData("2023-12-30", "2024-01-01", "106", "")]
[InlineData("2023-12-30", "2024-01-02", "107", "1")]
[InlineData("2023-12-30", "2024-04-02", "108", "1,2")]
public async Task Get_GridOwnerInPeriod_ReturnsOwner(string from, string to, string gridAreaCode, string expectedOwners)
{
// Arrange
var gridAreaCode = new GridAreaCode("111");
var actorNumber = "7025915927252";
var jan = new GridAreaOwnershipAssigned
// arrange
await using var dbContext = _databaseManager.CreateDbContext();

var target = new GridAreaOwnerRepository(dbContext);

await target.AddAsync(new GridAreaOwnershipAssigned
{
GridAreaCode = gridAreaCode.Value,
ActorNumber = actorNumber,
GridAreaCode = new GridAreaCode(gridAreaCode).Value,
ActorNumber = "1",
ValidFrom = DateTimeOffset.Parse("2024-01-01").ToTimestamp(),
SequenceNumber = 1,
};
var feb = new GridAreaOwnershipAssigned
{
GridAreaCode = gridAreaCode.Value,
ActorNumber = actorNumber,
ValidFrom = DateTimeOffset.Parse("2024-02-01").ToTimestamp(),
SequenceNumber = 1,
};
var mar = new GridAreaOwnershipAssigned
});
await target.AddAsync(new GridAreaOwnershipAssigned
{
GridAreaCode = gridAreaCode.Value,
ActorNumber = actorNumber,
ValidFrom = DateTimeOffset.Parse("2024-03-01").ToTimestamp(),
GridAreaCode = new GridAreaCode(gridAreaCode).Value,
ActorNumber = "2",
ValidFrom = DateTimeOffset.Parse("2024-01-15").ToTimestamp(),
SequenceNumber = 1,
};
});

await using var dbContext = _databaseManager.CreateDbContext();
var target = new GridAreaOwnerRepository(dbContext);
await target.AddAsync(jan);
await target.AddAsync(feb);
await target.AddAsync(mar);

// Act
// act
var owners = (await target.GetGridAreaOwnersAsync(
new GridAreaCode(jan.GridAreaCode),
DateTimeOffset.Parse("2024-01-01").ToInstant(),
DateTimeOffset.Parse("2024-03-01").ToInstant()))
.OrderBy(x => x.ValidFrom).ToList();
new GridAreaCode(gridAreaCode),
DateTimeOffset.Parse(from).ToInstant(),
DateTimeOffset.Parse(to).ToInstant()))
.Select(x => x.ActorNumber)
.ToList();

// Assert
Assert.Equal(2, owners.Count);
Assert.Equal(DateTimeOffset.Parse("2024-01-01"), owners[0].ValidFrom.ToDateTimeOffset());
Assert.Equal(DateTimeOffset.Parse("2024-02-01"), owners[1].ValidFrom.ToDateTimeOffset());
// assert
var expected = expectedOwners.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x => new ActorNumber(x)).ToList();

Assert.Equal(expected.Count, owners.Count);

foreach (var expectedOwner in expected)
{
Assert.Contains(expectedOwner, owners);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Collections.ObjectModel;
using Energinet.DataHub.SettlementReport.Application.Commands;
using Energinet.DataHub.SettlementReport.Application.Handlers;
using Energinet.DataHub.SettlementReport.Application.Services;
using Energinet.DataHub.SettlementReport.Interfaces.Helpers;
using Energinet.DataHub.SettlementReport.Interfaces.Models;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2;
Expand Down Expand Up @@ -55,7 +56,7 @@ public async Task Handle_ValidRequest_ReturnsJobId()
.ReturnsAsync(jobRunId);

var command = new RequestSettlementReportCommand(request, Guid.NewGuid(), Guid.NewGuid(), true, "1233", MarketRole.EnergySupplier);
var handler = new RequestSettlementReportHandler(jobHelperMock.Object, initializerMock.Object);
var handler = new RequestSettlementReportHandler(jobHelperMock.Object, initializerMock.Object, new Mock<IGridAreaOwnerRepository>().Object);

// Act
var result = await handler.HandleAsync(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using Energinet.DataHub.Core.App.Common.Extensions.DependencyInjection;
using Energinet.DataHub.SettlementReport.Application.Handlers;
using Energinet.DataHub.SettlementReport.Application.Services;
using Energinet.DataHub.SettlementReport.Application.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Extensions.Options;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.HealthChecks;
Expand Down

0 comments on commit 12407e9

Please sign in to comment.