Skip to content

Commit

Permalink
Merge pull request #581 from Laixer/develop
Browse files Browse the repository at this point in the history
v3.3.6
  • Loading branch information
yorickdewid authored Jul 7, 2021
2 parents 53d7b97 + 587a427 commit 2410dd2
Show file tree
Hide file tree
Showing 27 changed files with 466 additions and 37 deletions.
6 changes: 6 additions & 0 deletions scripts/dumpdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

set -e

if [ ! -d "./scripts" ]
then
echo "Run script from the project directory"
exit 1
fi

pg_dump -h localhost -U postgres -d fundermaps -s -N public -f database/fundermaps_base.sql
pg_dump -h localhost -U postgres -d fundermaps -a -n application -f database/data/seed_application.sql
pg_dump -h localhost -U postgres -d fundermaps -a -n geocoder -f database/data/seed_geocoder.sql
Expand Down
6 changes: 6 additions & 0 deletions scripts/loaddb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

set -e

if [ ! -d "./scripts" ]
then
echo "Run script from the project directory"
exit 1
fi

createuser -h localhost -U postgres -L fundermaps
createuser -h localhost -U postgres -I fundermaps_batch
createuser -h localhost -U postgres -I fundermaps_portal
Expand Down
6 changes: 6 additions & 0 deletions scripts/migrate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@

set -e

if [ ! -d "./scripts" ]
then
echo "Run script from the project directory"
exit 1
fi

migra --with-privileges --exclude public --unsafe postgresql://user:password@host/fundermaps postgresql://postgres@localhost/fundermaps > diff.sql
6 changes: 6 additions & 0 deletions scripts/setupdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@

set -e

if [ ! -d "./scripts" ]
then
echo "Run script from the project directory"
exit 1
fi

docker run --rm --name fm_local -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 -d postgis/postgis:12-master
30 changes: 28 additions & 2 deletions src/FunderMaps.Core/Components/GeocoderParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ public virtual GeocoderDatasource FromIdentifier(string input)
{
string when input.StartsWith("gfm-", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.FunderMaps,
string when input.StartsWith("NL.IMBAG.PAND", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.NlBagBuilding,
string when input.StartsWith("NL.IMBAG.LIGPLAATS", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.NlBagBuilding,
string when input.StartsWith("NL.IMBAG.STANDPLAATS", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.NlBagBuilding,
string when input.StartsWith("NL.IMBAG.LIGPLAATS", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.NlBagBerth,
string when input.StartsWith("NL.IMBAG.STANDPLAATS", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.NlBagPosting,
string when input.StartsWith("NL.IMBAG.NUMMERAANDUIDING", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.NlBagAddress,
string when input.Length == 10 && input.StartsWith("BU", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.NlCbsNeighborhood,
string when input.Length == 8 && input.StartsWith("WK", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.NlCbsDistrict,
string when input.Length == 6 && input.StartsWith("GM", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.NlCbsMunicipality,
string when input.Length == 4 && input.StartsWith("PV", StringComparison.InvariantCultureIgnoreCase) => GeocoderDatasource.NlCbsState,
string when input.Length == 16 && input.Substring(4, 2) == "10" => GeocoderDatasource.NlBagLegacyBuilding,
string when input.Length == 16 && input.Substring(4, 2) == "20" => GeocoderDatasource.NlBagLegacyAddress,
string when input.Length == 16 && input.Substring(4, 2) == "02" => GeocoderDatasource.NlBagLegacyBerth,
string when input.Length == 16 && input.Substring(4, 2) == "03" => GeocoderDatasource.NlBagLegacyPosting,
string when input.Length == 15 && input.Substring(3, 2) == "10" => GeocoderDatasource.NlBagLegacyBuilding,
string when input.Length == 15 && input.Substring(3, 2) == "20" => GeocoderDatasource.NlBagLegacyAddress,
string when input.Length == 15 && input.Substring(3, 2) == "02" => GeocoderDatasource.NlBagLegacyBerth,
string when input.Length == 15 && input.Substring(3, 2) == "03" => GeocoderDatasource.NlBagLegacyPosting,
_ => GeocoderDatasource.Unknown,
};

Expand Down Expand Up @@ -71,6 +75,28 @@ public virtual GeocoderDatasource FromIdentifier(string input, out string output
output = $"NL.IMBAG.NUMMERAANDUIDING.0{output}";
}
return GeocoderDatasource.NlBagAddress;

case GeocoderDatasource.NlBagLegacyBerth:
if (input.Length == 16)
{
output = $"NL.IMBAG.LIGPLAATS.{output}";
}
else if (input.Length == 15)
{
output = $"NL.IMBAG.LIGPLAATS.0{output}";
}
return GeocoderDatasource.NlBagBerth;

case GeocoderDatasource.NlBagLegacyPosting:
if (input.Length == 16)
{
output = $"NL.IMBAG.STANDPLAATS.{output}";
}
else if (input.Length == 15)
{
output = $"NL.IMBAG.STANDPLAATS.0{output}";
}
return GeocoderDatasource.NlBagPosting;
}

return source;
Expand Down
21 changes: 21 additions & 0 deletions src/FunderMaps.Core/Entities/ProductTelemetry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;

namespace FunderMaps.Core.Entities
{
/// <summary>
/// Product telemetry.
/// </summary>
public sealed class ProductTelemetry
{
/// <summary>
/// Product name.
/// </summary>
[Required]
public string Product { get; set; }

/// <summary>
/// Product hit count.
/// </summary>
public int Count { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using FunderMaps.Core.Entities;

namespace FunderMaps.Core.Interfaces.Repositories
{
Expand All @@ -13,5 +15,10 @@ public interface ITelemetryRepository
/// <param name="productName">Product name.</param>
/// <param name="hitCount">Number of hits to log.</param>
Task ProductHitAsync(string productName, int hitCount = 1);

/// <summary>
/// Retrieve all product telemetrics.
/// </summary>
IAsyncEnumerable<ProductTelemetry> ListAllUsageAsync();
}
}
4 changes: 4 additions & 0 deletions src/FunderMaps.Core/Services/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public virtual async IAsyncEnumerable<AnalysisProduct> GetAnalysisAsync(Analysis
{
GeocoderDatasource.FunderMaps => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetByIdAsync(id)),
GeocoderDatasource.NlBagBuilding => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetByExternalIdAsync(id)),
GeocoderDatasource.NlBagBerth => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetByExternalIdAsync(id)),
GeocoderDatasource.NlBagPosting => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetByExternalIdAsync(id)),
GeocoderDatasource.NlBagAddress => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetByAddressExternalIdAsync(id)),
_ => throw new InvalidIdentifierException(),
})
Expand Down Expand Up @@ -165,6 +167,8 @@ public virtual async IAsyncEnumerable<AnalysisProduct2> GetAnalysis2Async(string
{
GeocoderDatasource.FunderMaps => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetById2Async(id)),
GeocoderDatasource.NlBagBuilding => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetByExternalId2Async(id)),
GeocoderDatasource.NlBagBerth => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetByExternalId2Async(id)),
GeocoderDatasource.NlBagPosting => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetByExternalId2Async(id)),
GeocoderDatasource.NlBagAddress => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetByAddressExternalId2Async(id)),
_ => throw new InvalidIdentifierException(),
})
Expand Down
3 changes: 2 additions & 1 deletion src/FunderMaps.Core/Services/ProductTrackingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace FunderMaps.Core.Services
public class ProductTrackingService : ProductService
{
private const string statisticsProductName = "statistics";
private const string analysisProductName = "analysis2";

private readonly ITelemetryRepository _trackingRepository;

Expand Down Expand Up @@ -67,7 +68,7 @@ public override async IAsyncEnumerable<AnalysisProduct2> GetAnalysis2Async(strin
}
finally
{
await _trackingRepository.ProductHitAsync("analysis2", itemCount);
await _trackingRepository.ProductHitAsync(analysisProductName, itemCount);
}
}

Expand Down
28 changes: 24 additions & 4 deletions src/FunderMaps.Core/Types/GeocoderDatasource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,45 @@ public enum GeocoderDatasource
FunderMaps = 0,

/// <summary>
/// Basis Registratie Gebouwen (BAG) building identifier.
/// Basisregistratie Adressen en Gebouwen (BAG) building identifier.
/// </summary>
NlBagBuilding = 1,

/// <summary>
/// Basis Registratie Gebouwen (BAG) address identifier.
/// Basisregistratie Adressen en Gebouwen (BAG) address identifier.
/// </summary>
NlBagAddress = 2,

/// <summary>
/// Basis Registratie Gebouwen (BAG) legacy building identifier.
/// Basisregistratie Adressen en Gebouwen (BAG) posting identifier.
/// </summary>
NlBagPosting = 3,

/// <summary>
/// Basisregistratie Adressen en Gebouwen (BAG) berth identifier.
/// </summary>
NlBagBerth = 4,

/// <summary>
/// Basisregistratie Adressen en Gebouwen (BAG) legacy building identifier.
/// </summary>
NlBagLegacyBuilding = 8,

/// <summary>
/// Basis Registratie Gebouwen (BAG) legacy address identifier.
/// Basisregistratie Adressen en Gebouwen (BAG) legacy address identifier.
/// </summary>
NlBagLegacyAddress = 9,

/// <summary>
/// Basisregistratie Adressen en Gebouwen (BAG) legacy posting identifier.
/// </summary>
NlBagLegacyPosting = 10,

/// <summary>
/// Basisregistratie Adressen en Gebouwen (BAG) legacy berth identifier.
/// </summary>
NlBagLegacyBerth = 11,

/// <summary>
/// Centraal Bureau Statistiek (CBS) neighborhoor identifier.
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions src/FunderMaps.Core/Types/Products/AnalysisProduct2.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FunderMaps.Core.DataAnnotations;
using System;

namespace FunderMaps.Core.Types.Products
{
Expand Down Expand Up @@ -44,7 +43,7 @@ public sealed record AnalysisProduct2
/// <summary>
/// Foundation recovery type.
/// </summary>
public RecoveryDocumentType? RecoveryType { get; init; }
public RecoveryType? RecoveryType { get; init; }

/// <summary>
/// Represents the estimated restoration costs for this building.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FunderMaps.Core.Components;
using FunderMaps.Core.Interfaces.Repositories;
using FunderMaps.Data;
using FunderMaps.Data.Abstractions;
using FunderMaps.Data.Providers;
using FunderMaps.Data.Repositories;
Expand Down
2 changes: 1 addition & 1 deletion src/FunderMaps.Data/Repositories/AnalysisRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public static AnalysisProduct2 MapFromReader2(DbDataReader reader, int offset =
DewateringDepthRisk = reader.GetFieldValue<FoundationRisk?>(offset++),
DewateringDepthReliability = reader.GetFieldValue<Reliability>(offset++),
UnclassifiedRisk = reader.GetFieldValue<FoundationRisk?>(offset++),
RecoveryType = reader.GetFieldValue<RecoveryDocumentType?>(offset++),
RecoveryType = reader.GetFieldValue<RecoveryType?>(offset++),
};
}
}
Expand Down
35 changes: 34 additions & 1 deletion src/FunderMaps.Data/Repositories/TelemetryRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using FunderMaps.Core.Interfaces.Repositories;
using FunderMaps.Core.Entities;
using FunderMaps.Core.Interfaces.Repositories;
using FunderMaps.Data.Abstractions;
using FunderMaps.Data.Extensions;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Threading.Tasks;

namespace FunderMaps.Data.Repositories
Expand Down Expand Up @@ -56,5 +60,34 @@ DO UPDATE SET

await context.NonQueryAsync();
}

/// <summary>
/// Retrieve all product telemetrics.
/// </summary>
public async IAsyncEnumerable<ProductTelemetry> ListAllUsageAsync()
{
var sql = @"
SELECT -- ProductTelemetry
pt.product,
pt.count
FROM application.product_telemetry AS pt
WHERE pt.organization_id = @tenant";

await using var context = await DbContextFactory.CreateAsync(sql);

context.AddParameterWithValue("tenant", AppContext.TenantId);

await foreach (var reader in context.EnumerableReaderAsync())
{
yield return MapFromReader(reader);
}
}

public static ProductTelemetry MapFromReader(DbDataReader reader, int offset = 0)
=> new()
{
Product = reader.GetString(offset++),
Count = reader.GetInt(offset++),
};
}
}
4 changes: 2 additions & 2 deletions src/FunderMaps.Data/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ INSERT INTO application.user(
@email,
@avatar,
NULLIF(trim(@job_title), ''),
@phone_number,
REGEXP_REPLACE(@phone_number,'\D','','g'),
@role)
RETURNING id";

Expand Down Expand Up @@ -250,7 +250,7 @@ UPDATE application.user
last_name = @last_name,
avatar = @avatar,
job_title = NULLIF(trim(@job_title), ''),
phone_number = @phone_number,
phone_number = REGEXP_REPLACE(@phone_number,'\D','','g'),
role = @role
WHERE id = @id";

Expand Down
2 changes: 1 addition & 1 deletion src/FunderMaps.Webservice/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task<ActionResult<ResponseWrapper<AnalysisDto>>> GetProductAnalysis
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<ResponseWrapper<StatisticsDto>>> GetProducitStatisticsAsync([FromQuery][Required] string id)
public async Task<ActionResult<ResponseWrapper<StatisticsDto>>> GetProductStatisticsAsync([FromQuery][Required] string id)
{
// Assign.
IAsyncEnumerable<StatisticsProduct> productList = _productService.GetStatisticsAsync(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<ActionResult<List<AnalysisV2Dto>>> GetProductAnalysisAsync([Fr
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<ResponseWrapper<StatisticsDto>>> GetProducitStatisticsAsync([FromQuery][Required] string id)
public async Task<ActionResult<ResponseWrapper<StatisticsDto>>> GetProductStatisticsAsync([FromQuery][Required] string id)
{
// Assign.
IAsyncEnumerable<StatisticsProduct> productList = _productService.GetStatisticsAsync(id);
Expand Down
50 changes: 50 additions & 0 deletions src/FunderMaps.Webservice/Controllers/QuotaController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using AutoMapper;
using FunderMaps.Core.Entities;
using FunderMaps.Core.Interfaces.Repositories;
using FunderMaps.Webservice.DataTransferObjects;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace FunderMaps.Webservice.Controllers
{
/// <summary>
/// Controller for all product quotas.
/// </summary>
[Route("quota")]
public sealed class QuotaController : ControllerBase
{
private readonly IMapper _mapper;
private readonly ITelemetryRepository _telemetryRepository;

/// <summary>
/// Create new instance.
/// </summary>
public QuotaController(IMapper mapper, ITelemetryRepository telemetryRepository)
{
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
_telemetryRepository = telemetryRepository ?? throw new ArgumentNullException(nameof(telemetryRepository));
}

// GET: api/quota/usage
/// <summary>
/// Request product quota usage.
/// </summary>
[HttpGet("usage")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<IList<ProductTelemetryDto>>> GetQuotaUsageAsync()
{
// Assign.
IAsyncEnumerable<ProductTelemetry> productUsageList = _telemetryRepository.ListAllUsageAsync();

// Map.
var result = await _mapper.MapAsync<IList<ProductTelemetryDto>, ProductTelemetry>(productUsageList);

// Return.
return Ok(productUsageList);
}
}
}
Loading

0 comments on commit 2410dd2

Please sign in to comment.