37 lines
1.8 KiB
C#
37 lines
1.8 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using InsightsService.Clients;
|
|
using InsightsService.Domain;
|
|
|
|
namespace InsightsService.Application;
|
|
|
|
public sealed class InsightsApplicationService(ObservationServiceClient observations, InsightCalculator calculator)
|
|
{
|
|
public async Task<HealthInsight?> GetSummaryAsync(Guid patientId, DateTime from, DateTime to,
|
|
ContextSelection selection, CancellationToken cancellationToken)
|
|
{
|
|
from = NormalizeTimestamp(from);
|
|
to = NormalizeTimestamp(to);
|
|
if (to < from)
|
|
throw new ArgumentException("The end time must not be earlier than the start time.", nameof(to));
|
|
Validator.ValidateObject(selection, new ValidationContext(selection), validateAllProperties: true);
|
|
var history = await observations.GetByPeriodAsync(patientId, from, to, selection, cancellationToken);
|
|
return calculator.CalculateSummary(patientId, from, to, history, selection);
|
|
}
|
|
|
|
public async Task<TrendAssessment> GetTrendAsync(Guid patientId, DateTime from, DateTime to,
|
|
ContextSelection selection, CancellationToken cancellationToken)
|
|
{
|
|
from = NormalizeTimestamp(from);
|
|
to = NormalizeTimestamp(to);
|
|
if (to < from)
|
|
throw new ArgumentException("The end time must not be earlier than the start time.", nameof(to));
|
|
Validator.ValidateObject(selection, new ValidationContext(selection), validateAllProperties: true);
|
|
var history = await observations.GetByPeriodAsync(patientId, from, to, selection, cancellationToken);
|
|
return calculator.CalculateTrend(patientId, from, to, history, selection);
|
|
}
|
|
|
|
private static DateTime NormalizeTimestamp(DateTime value) =>
|
|
DateTime.SpecifyKind(value.Kind == DateTimeKind.Unspecified ? value : value.ToUniversalTime(),
|
|
DateTimeKind.Unspecified);
|
|
}
|