Compose coherent Overview snapshots and preserve access ordering

This commit is contained in:
Steffen committed 2026-10-14 12:06:52 +02:00
1 parent cab10c5306
commit cc7f87105d
16 files changed
+38314 -5

No files matched your search

@@ -0,0 +1,6 @@
namespace HealthApp.Contracts;
public sealed record HealthOverviewDto(PatientDto Patient,ObservationDto? LatestObservation,HealthInsight? Insight,DateTime PeriodStart,DateTime PeriodEnd,ContextSelection InsightSelection,int InsightContributingCount,string LatestObservationBasis,string InsightExplanation);
public interface OverviewQueries
{
Task<HealthOverviewDto?> GetAsync(AuthorizedPatientContext scope,DateTime from,DateTime to,ContextSelection selection,CancellationToken ct);
}
@@ -0,0 +1,17 @@
using HealthApp.Contracts;
using HealthApp.Infrastructure;
using HealthApp.Security;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace HealthApp.Controllers;
[ApiController]
public sealed class HealthOverviewController(OverviewQueries overview,AccessPolicy access,RequestTransaction tx) : ControllerBase
{
[HttpGet("api/patients/{patientId:guid}/overview")]
public async Task<IActionResult> Get(Guid patientId,[FromQuery,BindRequired]DateTime from,[FromQuery,BindRequired]DateTime to,[FromQuery]ContextSelection selection,CancellationToken ct)
{
var scope=await access.AuthorizeAsync(User,patientId,false,ct);if(scope is null){Response.StatusCode=404;return new EmptyResult();}
try{var o=await overview.GetAsync(scope,from,to,selection,ct);await tx.FinishAsync(ct);return o is null?Problem(statusCode:404,title:"Patient does not exist."):Ok(o);}
catch(ArgumentException){ModelState.AddModelError(nameof(to),"The end time must not be earlier than the start time.");return ValidationProblem(ModelState);}
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup><TargetFramework>net10.0</TargetFramework><Nullable>enable</Nullable><ImplicitUsings>enable</ImplicitUsings></PropertyGroup>
<ItemGroup><ProjectReference Include="../HealthApp.Contracts/HealthApp.Contracts.csproj" /><ProjectReference Include="../Modules/Guidance/Guidance.csproj" /><ProjectReference Include="../HealthApp.Infrastructure/HealthApp.Infrastructure.csproj"/><ProjectReference Include="../Modules/Profile/Profile.csproj"/><ProjectReference Include="../Modules/Observations/Observations.csproj"/><ProjectReference Include="../Modules/Analytics/Analytics.csproj"/></ItemGroup>
<ItemGroup><ProjectReference Include="../HealthApp.Contracts/HealthApp.Contracts.csproj" /><ProjectReference Include="../Modules/Guidance/Guidance.csproj" /><ProjectReference Include="../HealthApp.Infrastructure/HealthApp.Infrastructure.csproj"/><ProjectReference Include="../Modules/Profile/Profile.csproj"/><ProjectReference Include="../Modules/Observations/Observations.csproj"/><ProjectReference Include="../Modules/Analytics/Analytics.csproj"/><ProjectReference Include="../Modules/Overview/Overview.csproj"/></ItemGroup>
</Project>
+15 -2
View File
@@ -3,6 +3,7 @@ using HealthApp.Infrastructure;
using HealthApp.Profile;
using HealthApp.Observations;
using HealthApp.Analytics;
using HealthApp.Overview;
using HealthApp.Contracts;
using Npgsql;
using HealthApp.Security;
@@ -19,7 +20,7 @@ public class Program
builder.Services.AddProblemDetails();
builder.Services.AddSingleton<PrototypeAccounts>();
builder.Services.AddSingleton<ProfessionalAssignments>();
builder.Services.AddSingleton<GuidanceQueries>();builder.Services.AddSingleton<ReferenceCatalog,ReviewedReferenceCatalog>();builder.Services.AddAnalytics();
builder.Services.AddSingleton<GuidanceQueries>();builder.Services.AddSingleton<ReferenceCatalog,ReviewedReferenceCatalog>();builder.Services.AddAnalytics();builder.Services.AddOverview();
builder.Services.AddSingleton(sp=> {
var c=sp.GetRequiredService<IConfiguration>();
var text=c.GetConnectionString("HealthDatabase");
@@ -57,7 +58,19 @@ public class Program
if(ctx.User.Identity?.IsAuthenticated!=true){await next(ctx);return;}
if(ctx.User.IsInRole("HealthcareProfessional") && ctx.Request.Method!="GET"){ctx.Response.StatusCode=403;return;}
var tx=ctx.RequestServices.GetRequiredService<RequestTransaction>();
try{await tx.BeginAsync(ctx.Request.Method!="GET",ctx.RequestAborted);await next(ctx);}
try{
var write=ctx.Request.Method!="GET";
await tx.BeginAsync(write,ctx.RequestAborted);
Guid? actual=null;
if(Guid.TryParse(ctx.Request.RouteValues["patientId"]?.ToString(),out var patientId))actual=patientId;
else if(Guid.TryParse(ctx.Request.RouteValues["observationId"]?.ToString(),out var observationId))actual=await ctx.RequestServices.GetRequiredService<ObservationQueries>().ResolveOwnerAsync(observationId,ctx.RequestAborted);
if(ctx.Request.RouteValues.ContainsKey("patientId") || ctx.Request.RouteValues.ContainsKey("observationId"))
{
var approved=actual is null?null:await ctx.RequestServices.GetRequiredService<AccessPolicy>().AuthorizeAsync(ctx.User,actual.Value,write,ctx.RequestAborted);
if(approved is null){ctx.Response.StatusCode=404;return;}
}
await next(ctx);
}
catch(NpgsqlException){if(ctx.Response.HasStarted)ctx.Abort();else {ctx.Response.Clear();await Results.Problem(statusCode:503,title:"Health data dependency is unavailable.").ExecuteAsync(ctx);}}
});app.MapControllers();
await app.RunAsync();
+4
View File
@@ -0,0 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup><TargetFramework>net10.0</TargetFramework><Nullable>enable</Nullable><ImplicitUsings>enable</ImplicitUsings></PropertyGroup>
<ItemGroup><FrameworkReference Include="Microsoft.AspNetCore.App" /><ProjectReference Include="../../HealthApp.Contracts/HealthApp.Contracts.csproj" /></ItemGroup>
</Project>
+24
View File
@@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;
using HealthApp.Contracts;
using Microsoft.Extensions.DependencyInjection;
namespace HealthApp.Overview;
internal sealed class OverviewFacade(ProfileQueries patients,ObservationQueries observations,AnalyticsQueries analytics) : OverviewQueries
{
public async Task<HealthOverviewDto?> GetAsync(AuthorizedPatientContext scope,DateTime from,DateTime to,ContextSelection selection,CancellationToken ct)
{
from=MeasurementPeriod.Normalize(from);to=MeasurementPeriod.Normalize(to);MeasurementPeriod.Validate(from,to);Validator.ValidateObject(selection,new ValidationContext(selection),true);
var patient=await patients.GetAsync(scope,ct);if(patient is null)return null;
var latest=await observations.LatestAsync(scope,ct);
// Analytics captures the selected series once and closes the shared snapshot before calculation.
var insight=await analytics.InsightAsync(scope,from,to,selection,ct);
return new(patient,latest,insight,from,to,selection,insight?.ContributingCount??0,
"Latest overall observation for the patient, independent of the insight period and context selections. "+
"Its recorded context applies only to that observation, not to the insight's contributing observations.",
insight is null?"No calculated insight is available: no observations match the requested period and insight selection.":
"The insight uses the requested period and context selection only. "+insight.ContextExplanation);
}
}
public static class OverviewModule
{
public static IServiceCollection AddOverview(this IServiceCollection services)=>services.AddScoped<OverviewQueries,OverviewFacade>();
}