Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: calculate for which buildings to invalidate the cache upon parc… #1267

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace BuildingRegistry.Api.Oslo.Building.Count
using Be.Vlaanderen.Basisregisters.Api.Search.Pagination;
using Be.Vlaanderen.Basisregisters.GrAr.Legacy;
using Consumer.Read.Parcel;
using Infrastructure.ParcelMatching;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Projections.Legacy;
Expand All @@ -17,22 +16,22 @@ public class BuildingCountHandlerV2 : IRequestHandler<BuildingCountRequest, Tota
{
private readonly LegacyContext _legacyContext;
private readonly ConsumerParcelContext _consumerParcelContext;
private readonly IParcelMatching _parcelMatching;
private readonly IBuildingMatching _buildingMatching;

public BuildingCountHandlerV2(
LegacyContext legacyContext, ConsumerParcelContext consumerParcelContext, IParcelMatching parcelMatching)
LegacyContext legacyContext, ConsumerParcelContext consumerParcelContext, IBuildingMatching buildingMatching)
{
_legacyContext = legacyContext;
_consumerParcelContext = consumerParcelContext;
_parcelMatching = parcelMatching;
_buildingMatching = buildingMatching;
}

public async Task<TotaalAantalResponse> Handle(BuildingCountRequest request, CancellationToken cancellationToken)
{
return new TotaalAantalResponse
{
Aantal = request.FilteringHeader.ShouldFilter
? await new BuildingListOsloQueryV2(_legacyContext, _consumerParcelContext, _parcelMatching)
? await new BuildingListOsloQueryV2(_legacyContext, _consumerParcelContext, _buildingMatching)
.Fetch(request.FilteringHeader, request.SortingHeader, new NoPaginationRequest())
.Items
.CountAsync(cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace BuildingRegistry.Api.Oslo.Building.Detail
using Consumer.Read.Parcel;
using Converters;
using Infrastructure.Options;
using Infrastructure.ParcelMatching;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace BuildingRegistry.Api.Oslo.Building.List
using Converters;
using Infrastructure;
using Infrastructure.Options;
using Infrastructure.ParcelMatching;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
Expand All @@ -19,24 +18,24 @@ public class BuildingListHandlerV2 : IRequestHandler<BuildingListRequest, Buildi
{
private readonly LegacyContext _legacyContext;
private readonly ConsumerParcelContext _consumerParcelContext;
private readonly IParcelMatching _parcelMatching;
private readonly IBuildingMatching _buildingMatching;
private readonly IOptions<ResponseOptions> _responseOptions;

public BuildingListHandlerV2(
LegacyContext legacyContext,
IOptions<ResponseOptions> responseOptions,
ConsumerParcelContext consumerParcelContext,
IParcelMatching parcelMatching)
IBuildingMatching buildingMatching)
{
_legacyContext = legacyContext;
_responseOptions = responseOptions;
_consumerParcelContext = consumerParcelContext;
_parcelMatching = parcelMatching;
_buildingMatching = buildingMatching;
}

public async Task<BuildingListOsloResponse> Handle(BuildingListRequest request, CancellationToken cancellationToken)
{
var pagedBuildings = new BuildingListOsloQueryV2(_legacyContext, _consumerParcelContext, _parcelMatching)
var pagedBuildings = new BuildingListOsloQueryV2(_legacyContext, _consumerParcelContext, _buildingMatching)
.Fetch(request.FilteringHeader, request.SortingHeader, request.PaginationRequest);

var buildings = await pagedBuildings.Items.ToListAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace BuildingRegistry.Api.Oslo.Building.Query
using Consumer.Read.Parcel;
using Consumer.Read.Parcel.ParcelWithCount;
using Converters;
using Infrastructure.ParcelMatching;
using Microsoft.EntityFrameworkCore;
using Projections.Legacy;
using Projections.Legacy.BuildingDetailV2;
Expand All @@ -19,14 +18,17 @@ public class BuildingListOsloQueryV2 : Query<BuildingQueryItem, BuildingFilter>
{
private readonly LegacyContext _context;
private readonly ConsumerParcelContext _consumerParcelContext;
private readonly IParcelMatching _parcelMatching;
private readonly IBuildingMatching _buildingMatching;
protected override ISorting Sorting => new BuildingSorting();

public BuildingListOsloQueryV2(LegacyContext context, ConsumerParcelContext consumerParcelContext, IParcelMatching parcelMatching)
public BuildingListOsloQueryV2(
LegacyContext context,
ConsumerParcelContext consumerParcelContext,
IBuildingMatching buildingMatching)
{
_context = context;
_consumerParcelContext = consumerParcelContext;
_parcelMatching = parcelMatching;
_buildingMatching = buildingMatching;
}

protected override IQueryable<BuildingQueryItem> Filter(FilteringHeader<BuildingFilter> filtering)
Expand Down Expand Up @@ -63,7 +65,7 @@ protected override IQueryable<BuildingQueryItem> Filter(FilteringHeader<Building
var parcel = _consumerParcelContext.ParcelConsumerItemsWithCount.FirstOrDefault(x => x.CaPaKey == filtering.Filter.CaPaKey);
if (parcel is not null && parcel.Status == ParcelStatus.Realized)
{
var underlyingBuildings = _parcelMatching.GetUnderlyingBuildings(parcel.Geometry);
var underlyingBuildings = _buildingMatching.GetUnderlyingBuildings(parcel.Geometry);
buildings = buildings.Where(x => underlyingBuildings.Contains(x.PersistentLocalId));
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override void Load(ContainerBuilder builder)
{
builder
.RegisterModule(new MediatRModule())
.RegisterModule(new ParcelMatchingModule())
.RegisterModule(new ParcelBuildingMatchingModule())
.RegisterModule(new LegacyModule(_configuration, _services, _loggerFactory))
.RegisterModule(new ConsumerParcelModule(_configuration, _services, _loggerFactory));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
namespace BuildingRegistry.Api.Oslo.Infrastructure.Modules
{
using Autofac;
using ParcelMatching;
using Consumer.Read.Parcel;
using Projections.Legacy;

public class ParcelMatchingModule : Module
public class ParcelBuildingMatchingModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder
.RegisterType<ParcelMatching>()
.AsImplementedInterfaces();

builder
.RegisterType<BuildingMatching>()
.AsImplementedInterfaces();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<ItemGroup>
<ProjectReference Include="..\BuildingRegistry.Infrastructure\BuildingRegistry.Infrastructure.csproj" />
<ProjectReference Include="..\BuildingRegistry.Projections.Legacy\BuildingRegistry.Projections.Legacy.csproj" />
</ItemGroup>

<Import Project="..\..\.paket\Paket.Restore.targets" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace BuildingRegistry.Consumer.Read.Parcel
{
using BuildingRegistry.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class BuildingToInvalidate
{
public int Id { get; set; }
public int BuildingPersistentLocalId { get; set; }
}

public class BuildingToInvalidateConfiguration : IEntityTypeConfiguration<BuildingToInvalidate>
{
public const string TableName = "BuildingsToInvalidate";

public void Configure(EntityTypeBuilder<BuildingToInvalidate> builder)
{
builder.ToTable(TableName, Schema.ConsumerReadParcel)
.HasKey(x => x.Id)
.IsClustered();

builder.Property(x => x.Id).UseIdentityColumn(1).ValueGeneratedOnAdd();

builder.Property(x => x.BuildingPersistentLocalId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ namespace BuildingRegistry.Consumer.Read.Parcel

public class ConsumerParcelContext : RunnerDbContext<ConsumerParcelContext>, IParcels
{
public DbSet<ParcelConsumerItem> ParcelConsumerItemsWithCount { get; set; }
public DbSet<ParcelAddressItem> ParcelAddressItemsWithCount { get; set; }
public DbSet<ParcelConsumerItem> ParcelConsumerItemsWithCount => Set<ParcelConsumerItem>();
public DbSet<ParcelAddressItem> ParcelAddressItemsWithCount => Set<ParcelAddressItem>();

public DbSet<BuildingToInvalidate> BuildingsToInvalidate => Set<BuildingToInvalidate>();

// This needs to be here to please EF
public ConsumerParcelContext()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BuildingRegistry.Consumer.Read.Parcel
{
using System.Collections.Generic;

public interface IParcelMatching
{
IEnumerable<string> GetUnderlyingParcels(byte[] buildingGeometryBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace BuildingRegistry.Consumer.Read.Parcel.Infrastructure
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ParcelWithCount;
using Projections.Legacy;
using Serilog;
using Serilog.Debugging;
using Serilog.Extensions.Logging;
Expand Down Expand Up @@ -80,6 +81,16 @@ public static async Task Main(string[] args)
sqlServerOptions.MigrationsHistoryTable(MigrationTables.ConsumerReadParcel, Schema.ConsumerReadParcel);
sqlServerOptions.UseNetTopologySuite();
}));

services
.AddDbContext<LegacyContext>((_, options) => options
.UseLoggerFactory(loggerFactory)
.UseSqlServer(hostContext.Configuration.GetConnectionString("LegacyProjections"), sqlServerOptions =>
{
sqlServerOptions.EnableRetryOnFailure();
sqlServerOptions.MigrationsHistoryTable(MigrationTables.Legacy, Schema.Legacy);
sqlServerOptions.UseNetTopologySuite();
}));
})
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
Expand Down Expand Up @@ -127,6 +138,11 @@ public static async Task Main(string[] args)
.As<IConsumer>()
.SingleInstance();

builder
.RegisterType<BuildingMatching>()
.AsImplementedInterfaces()
.InstancePerLifetimeScope();

builder
.RegisterType<ConsumerParcel>()
.As<IHostedService>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
namespace BuildingRegistry.Api.Oslo.Infrastructure.ParcelMatching
namespace BuildingRegistry.Consumer.Read.Parcel
{
using System;
using System.Collections.Generic;
using System.Linq;
using Consumer.Read.Parcel;
using Consumer.Read.Parcel.ParcelWithCount;
using NetTopologySuite.Geometries;
using Projections.Legacy;
using ParcelWithCount;

public class ParcelMatching : IParcelMatching
{
private readonly ConsumerParcelContext _consumerParcelContext;
private readonly LegacyContext _legacyContext;

public ParcelMatching(ConsumerParcelContext consumerParcelContext, LegacyContext legacyContext)
public ParcelMatching(ConsumerParcelContext consumerParcelContext)
{
_consumerParcelContext = consumerParcelContext;
_legacyContext = legacyContext;
}

public IEnumerable<string> GetUnderlyingParcels(byte[] buildingGeometryBytes)
Expand All @@ -41,27 +37,6 @@ public IEnumerable<string> GetUnderlyingParcels(byte[] buildingGeometryBytes)
.Select(parcel => parcel.CaPaKey);
}

public IEnumerable<int> GetUnderlyingBuildings(Geometry parcelGeometry)
{
var boundingBox = parcelGeometry.Factory.ToGeometry(parcelGeometry.EnvelopeInternal);

var underlyingBuildings = _legacyContext
.BuildingDetailsV2
.Where(building => boundingBox.Intersects(building.SysGeometry))
.ToList()
.Where(building => parcelGeometry.Intersects(building.SysGeometry))
.Select(building =>
new {
building.PersistentLocalId,
Overlap = CalculateOverlap(building.SysGeometry, parcelGeometry)
})
.ToList();

return underlyingBuildings
.Where(building => building.Overlap >= 0.8 / underlyingBuildings.Count)
.Select(building => building.PersistentLocalId);
}

private static double CalculateOverlap(Geometry? buildingGeometry, Geometry parcel)
{
if (buildingGeometry is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace BuildingRegistry.Consumer.Read.Parcel.ParcelWithCount
using System;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Be.Vlaanderen.Basisregisters.MessageHandling.Kafka.Consumer;
using Be.Vlaanderen.Basisregisters.ProjectionHandling.Connector;
using Microsoft.EntityFrameworkCore;
Expand All @@ -12,17 +13,20 @@ namespace BuildingRegistry.Consumer.Read.Parcel.ParcelWithCount
public class ConsumerParcel : BackgroundService
{
private readonly IHostApplicationLifetime _hostApplicationLifetime;
private readonly ILifetimeScope _lifetimeScope;
private readonly IDbContextFactory<ConsumerParcelContext> _consumerParcelDbContextFactory;
private readonly IConsumer _consumer;
private readonly ILogger<ConsumerParcel> _logger;

public ConsumerParcel(
IHostApplicationLifetime hostApplicationLifetime,
ILifetimeScope lifetimeScope,
IDbContextFactory<ConsumerParcelContext> consumerParcelDbContextFactory,
IConsumer consumer,
ILoggerFactory loggerFactory)
{
_hostApplicationLifetime = hostApplicationLifetime;
_lifetimeScope = lifetimeScope;
_consumerParcelDbContextFactory = consumerParcelDbContextFactory;
_consumer = consumer;
_logger = loggerFactory.CreateLogger<ConsumerParcel>();
Expand All @@ -32,7 +36,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var projector =
new ConnectedProjector<ConsumerParcelContext>(
Resolve.WhenEqualToHandlerMessageType(new ParcelKafkaProjection().Handlers));
Resolve.WhenEqualToHandlerMessageType(new ParcelKafkaProjection(_lifetimeScope).Handlers));

try
{
Expand Down
Loading
Loading