Skip to content

Commit

Permalink
Merge pull request #583 from Laixer/develop
Browse files Browse the repository at this point in the history
v3.3.7
  • Loading branch information
yorickdewid authored Jul 15, 2021
2 parents 2410dd2 + b6c8427 commit ce0f58a
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 11 deletions.
2 changes: 1 addition & 1 deletion contrib/Laixer.TdmClient/Laixer.TdmClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="OAuth.DotNetCore" Version="3.0.1" />
<PackageReference Include="RestSharp" Version="106.11.5" />
<PackageReference Include="RestSharp" Version="106.11.8-alpha.0.13" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/FunderMaps.AspNetCore/DataTransferObjects/RiskIndexDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace FunderMaps.AspNetCore.DataTransferObjects
{
/// <summary>
/// Represents a response model for the risk index endpoint.
/// </summary>
public record RiskIndexDto
{
/// <summary>
/// Whether or not the object has an increased risk.
/// </summary>
public bool IncreasedRisk { get; init; }
}
}
2 changes: 1 addition & 1 deletion src/FunderMaps.AspNetCore/FunderMaps.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.8" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/FunderMaps.AspNetCore/FunderMapsStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ private static void ConfigureMapper(IMapperConfigurationExpression mapper)
.ForMember(dest => dest.CenterX, o => o.MapFrom(src => src.Center.CenterX))
.ForMember(dest => dest.CenterY, o => o.MapFrom(src => src.Center.CenterY))
.ReverseMap();
mapper.CreateMap<StatisticsProduct, StatisticsDto>();
mapper.CreateMap<bool, RiskIndexDto>()
.ForMember(dest => dest.IncreasedRisk, o => o.MapFrom(src => src));
mapper.CreateMap<TokenContext, SignInSecurityTokenDto>()
.ForMember(dest => dest.Id, o => o.MapFrom(src => src.Token.Id))
.ForMember(dest => dest.Issuer, o => o.MapFrom(src => src.Token.Issuer))
.ForMember(dest => dest.Token, o => o.MapFrom(src => src.TokenString))
.ForMember(dest => dest.ValidFrom, o => o.MapFrom(src => src.Token.ValidFrom))
.ForMember(dest => dest.ValidTo, o => o.MapFrom(src => src.Token.ValidTo));
mapper.CreateMap<StatisticsProduct, StatisticsDto>();
mapper.CreateMap<User, UserDto>().ReverseMap();
mapper.CreateMap<User, OrganizationUserDto>().ReverseMap();

Expand Down
2 changes: 1 addition & 1 deletion src/FunderMaps.Core/FunderMaps.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
Expand Down
10 changes: 9 additions & 1 deletion src/FunderMaps.Core/Interfaces/IProductService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FunderMaps.Core.Types.Products;
using System;
using System.Collections.Generic;

namespace FunderMaps.Core.Interfaces
Expand All @@ -13,10 +14,11 @@ public interface IProductService
/// </summary>
/// <param name="productType">Product type.</param>
/// <param name="input">Input query.</param>
[Obsolete("GetAnalysisAsync is deprecated, please use GetAnalysis2Async instead.")]
IAsyncEnumerable<AnalysisProduct> GetAnalysisAsync(AnalysisProductType productType, string input);

/// <summary>
/// Get an analysis product v2.
/// Get the analysis product.
/// </summary>
/// <param name="input">Input query.</param>
IAsyncEnumerable<AnalysisProduct2> GetAnalysis2Async(string input);
Expand All @@ -26,5 +28,11 @@ public interface IProductService
/// </summary>
/// <param name="input">Input query.</param>
IAsyncEnumerable<StatisticsProduct> GetStatisticsAsync(string input);

/// <summary>
/// Get risk index on id.
/// </summary>
/// <param name="input">Input query.</param>
IAsyncEnumerable<bool> GetRiskIndexAsync(string input);
}
}
18 changes: 18 additions & 0 deletions src/FunderMaps.Core/Interfaces/Repositories/IAnalysisRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,23 @@ public interface IAnalysisRepository
/// </summary>
/// <param name="id">External address id.</param>
Task<AnalysisProduct2> GetByAddressExternalId2Async(string id);

/// <summary>
/// Gets the risk index by its internal building id.
/// </summary>
/// <param name="id">Internal building id.</param>
Task<bool> GetRiskIndexByIdAsync(string id);

/// <summary>
/// Gets the risk index by its external building id and source.
/// </summary>
/// <param name="id">Internal building id.</param>
Task<bool> GetRiskIndexByExternalIdAsync(string id);

/// <summary>
/// Gets the risk index by its external building id and source.
/// </summary>
/// <param name="id">Internal building id.</param>
Task<bool> GetRiskIndexByAddressExternalIdAsync(string id);
}
}
20 changes: 20 additions & 0 deletions src/FunderMaps.Core/Services/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ public virtual async IAsyncEnumerable<AnalysisProduct2> GetAnalysis2Async(string
}
}

/// <summary>
/// Get risk index on id.
/// </summary>
/// <param name="input">Input query.</param>
public virtual async IAsyncEnumerable<bool> GetRiskIndexAsync(string input)
{
await foreach (var product in _geocoderParser.FromIdentifier(input, out string id) switch
{
GeocoderDatasource.FunderMaps => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetRiskIndexByIdAsync(id)),
GeocoderDatasource.NlBagBuilding => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetRiskIndexByExternalIdAsync(id)),
GeocoderDatasource.NlBagBerth => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetRiskIndexByExternalIdAsync(id)),
GeocoderDatasource.NlBagPosting => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetRiskIndexByExternalIdAsync(id)),
GeocoderDatasource.NlBagAddress => AsyncEnumerableHelper.AsEnumerable(await _analysisRepository.GetRiskIndexByAddressExternalIdAsync(id)),
_ => throw new InvalidIdentifierException(),
})
{
yield return product;
}
}

/// <summary>
/// Get statistics per region.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions src/FunderMaps.Core/Services/ProductTrackingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ProductTrackingService : ProductService
{
private const string statisticsProductName = "statistics";
private const string analysisProductName = "analysis2";
private const string RiskIndexProductName = "riskindex";

private readonly ITelemetryRepository _trackingRepository;

Expand Down Expand Up @@ -93,5 +94,27 @@ public override async IAsyncEnumerable<StatisticsProduct> GetStatisticsAsync(str
await _trackingRepository.ProductHitAsync(statisticsProductName);
}
}

/// <summary>
/// Get risk index on id and log product hit.
/// </summary>
/// <param name="input">Input query.</param>
public override async IAsyncEnumerable<bool> GetRiskIndexAsync(string input)
{
int itemCount = 0;

try
{
await foreach (var product in base.GetRiskIndexAsync(input))
{
++itemCount;
yield return product;
}
}
finally
{
await _trackingRepository.ProductHitAsync(RiskIndexProductName);
}
}
}
}
108 changes: 108 additions & 0 deletions src/FunderMaps.Data/Repositories/AnalysisRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,114 @@ FROM data.analysis_complete ac
return MapFromReader2(reader);
}

/// <summary>
/// Gets the risk index by its internal building id.
/// </summary>
/// <param name="id">Internal building id.</param>
public async Task<bool> GetRiskIndexByIdAsync(string id)
{
var sql = @"
SELECT -- AnalysisComplete
'a'::data.foundation_risk_indication <> ANY (ARRAY[
CASE
WHEN ac.drystand_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.drystand_risk
END,
CASE
WHEN ac.bio_infection_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.bio_infection_risk
END,
CASE
WHEN ac.dewatering_depth_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.dewatering_depth_risk
END,
CASE
WHEN ac.unclassified_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.unclassified_risk
END]) AS has_risk
FROM data.analysis_complete ac
WHERE ac.building_id = @id
LIMIT 1";

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

context.AddParameterWithValue("id", id);

return await context.ScalarAsync<bool>();
}

/// <summary>
/// Gets the risk index by its external building id and source.
/// </summary>
/// <param name="id">Internal building id.</param>
public async Task<bool> GetRiskIndexByExternalIdAsync(string id)
{
var sql = @"
SELECT -- AnalysisComplete
'a'::data.foundation_risk_indication <> ANY (ARRAY[
CASE
WHEN ac.drystand_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.drystand_risk
END,
CASE
WHEN ac.bio_infection_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.bio_infection_risk
END,
CASE
WHEN ac.dewatering_depth_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.dewatering_depth_risk
END,
CASE
WHEN ac.unclassified_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.unclassified_risk
END]) AS has_risk
FROM data.analysis_complete ac
WHERE ac.external_building_id = upper(@external_id)
LIMIT 1";

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

context.AddParameterWithValue("external_id", id);

return await context.ScalarAsync<bool>();
}

/// <summary>
/// Gets the risk index by its external address id and source.
/// </summary>
/// <param name="id">Internal building id.</param>
public async Task<bool> GetRiskIndexByAddressExternalIdAsync(string id)
{
var sql = @"
SELECT -- AnalysisComplete
'a'::data.foundation_risk_indication <> ANY (ARRAY[
CASE
WHEN ac.drystand_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.drystand_risk
END,
CASE
WHEN ac.bio_infection_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.bio_infection_risk
END,
CASE
WHEN ac.dewatering_depth_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.dewatering_depth_risk
END,
CASE
WHEN ac.unclassified_risk IS NULL THEN 'a'::data.foundation_risk_indication
ELSE ac.unclassified_risk
END]) AS has_risk
FROM data.analysis_complete ac
WHERE ac.address_external_id = upper(@external_id)
LIMIT 1";

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

context.AddParameterWithValue("external_id", id);

return await context.ScalarAsync<bool>();
}

/// <summary>
/// Maps a reader to an <see cref="AnalysisProduct"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
<PackageReference Include="AWSSDK.S3" Version="3.7.1.8" />
<PackageReference Include="AWSSDK.S3" Version="3.7.1.14" />
<PackageReference Include="MailKit" Version="2.13.0" />
</ItemGroup>

Expand Down
31 changes: 27 additions & 4 deletions src/FunderMaps.Webservice/Controllers/ProductV2Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace FunderMaps.Webservice.Controllers
{
// FUTURE: Split logic into analysis, risk index, statistics controller.
// FUTURE: Drop the 'product' uri prefix.
// FUTURE: Drop the 'v' uri version prefix.
/// <summary>
/// Controller for all product endpoints.
/// </summary>
Expand Down Expand Up @@ -40,13 +43,13 @@ private async Task<ResponseWrapper<TDto>> AsResponseWrapperAsync<TDto, TSource>(

// GET: api/v2/product/analysis
/// <summary>
/// Request an analysis product.
/// Request the analysis product.
/// </summary>
[HttpGet("analysis")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<List<AnalysisV2Dto>>> GetProductAnalysisAsync([FromQuery] string id)
public async Task<ActionResult<List<AnalysisV2Dto>>> GetAnalysisAsync([FromQuery] string id)
{
// Assign.
IAsyncEnumerable<AnalysisProduct2> productList = _productService.GetAnalysis2Async(id);
Expand All @@ -58,15 +61,35 @@ public async Task<ActionResult<List<AnalysisV2Dto>>> GetProductAnalysisAsync([Fr
return Ok(result);
}

// GET: api/v2/product/risk_index
/// <summary>
/// Request the risk index per id.
/// </summary>
[HttpGet("risk_index")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<List<RiskIndexDto>>> GetRiskIndexAsync([FromQuery] string id)
{
// Assign.
IAsyncEnumerable<bool> productList = _productService.GetRiskIndexAsync(id);

// Map.
var result = await _mapper.MapAsync<IList<RiskIndexDto>, bool>(productList);

// Return.
return Ok(result);
}

// GET: api/v2/product/statistics
/// <summary>
/// Request statistics product.
/// Request the statistics product.
/// </summary>
[HttpGet("statistics")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<ResponseWrapper<StatisticsDto>>> GetProductStatisticsAsync([FromQuery][Required] string id)
public async Task<ActionResult<ResponseWrapper<StatisticsDto>>> GetStatisticsAsync([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 @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Bogus" Version="33.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="System.Collections" Version="4.3.0" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
Expand Down
Loading

0 comments on commit ce0f58a

Please sign in to comment.