Analysis: one selected period, one set of numbers, on every page
ci / build-test (push) Successful in 2m31s
ci / build-test (push) Successful in 2m31s
The dashboards told several stories at once. Overview asked for full calendar years, meter detail for a fixed 12-month window that was really 13, Trends for 24 months with an Apply button, and the energy pages for 60. Each page derived "today" from UTC, so the first hours of a local day belonged to yesterday. A missing tariff, a month nobody measured and a genuine zero all rendered as 0. And a virtual meter -- the one thing the spreadsheet leans on hardest -- was excluded from analysis outright: MeterPeriodService returned null for it and the page offered a flow diagram instead. docs/DASHBOARD_ANALYSIS_CHANGE_BRIEF.md is the work order. Every choice it left open is settled in docs/ANALYSIS_IMPLEMENTATION_NOTE.md as D-01..D-58 plus amendments A-01..A-30; code, tests and release notes cite those ids. The analysis layer Core/Analysis holds the pure rules: period presets resolved once in the instance zone into a local date range and a half-open UTC range, bucket plans, calendar-unit comparisons, coverage runs with a resolution class, normalized quantities and units, the totals policy, the virtual formula parser/validator/evaluator, and the cost calculator. "Now" comes from TimeProvider; services never read the clock. Normalization now writes, in the same transaction as consumption and by diff, per-meter rollups by local day and month plus coverage runs and a rollup state (AnalysisDataWriter). AnalysisReader answers a request from those tables -- month rollups for month and year buckets, day rollups otherwise, at most two partial edge days from consumption -- and CostReader prices the result month by month. Pages, /api/v1 and the CSV export read nothing else. The unused continuous aggregates are dropped. The reader's statement count per request is constant whether it covers one meter or a thousand. On a synthetic 1,000-meter, ten-year instance the brief's target request (100 meters, ten years, monthly) takes 374 ms against a two-second target, and the Overview went from 48,244 SQL statements per load to 205. Missing is not zero Every bucket carries a status -- available, partial, missing, unresolved, invalid, pending -- derived from coverage, never from the amount, with provenance and a reason code beside it. A true zero is a number and a bar on the baseline; an unknown bucket is a gap that says why; a month whose data only exists monthly says so instead of inventing daily detail; a scope with no tariff says "not priced" instead of 0. Rows whose interval closes after now are reported separately rather than counted. Virtual meters are analysis subjects A virtual meter stores a canonical definition -- expression over m<id> references, result kind, unit and cost rule -- validated on save and on read for syntax, unknown or self references, loops and unit/kind rules. It is evaluated on read from its sources' rollups over their joint coverage: a missing source makes the bucket missing, an observed zero is a valid input, a non-finite result is invalid with its dependency path, and the page lists each source's contribution. Topology links are topology only and never rewrite a saved calculation; expression-less meters from older installs are converted once at startup. The editor has Sum, Difference and Advanced modes with a live preview. Totals and the bill Per energy type the totals policy separates use, grid import, export, generation and runtime, marks breakdown meters as breakdowns and virtual meters as views, and never adds across units. The bill follows it: grid import where there is one, separately priced subsections at their own price, feed-in only on export meters, standing charges once per scope per local day, manual costs once on their start day, categories as non-overlapping covers whose composition reconciles to the bill. The seeded demo's yearly totals now match the spreadsheet. Pages and navigation The period lives in the URL and every page reads the same contract, so a link, a reload and the browser's Back button keep it. Shared components carry it: page header with breadcrumbs, period toolbar, theme-aware chart with an accessible table beside it, metric cards, comparison and availability states, attention items that each link to the one action that fixes them. Meter detail leads with an Analysis tab and resolves its tabs by key; the energy page has Overview, History, Flow and Meters; the old cost-only Trends page is a general Analysis page over portfolio, type, category, meter or a meter comparison. Records tabs are paged server-side instead of showing the latest 200. Everything is English and German, light and dark, down to 360px. Some figures change on purpose; docs/RELEASE_NOTES.md lists each one and what the first start after the update does (it rebuilds all analysis data before the web server listens). docs/SDD.md and CLAUDE.md describe the system as it now is. Tests: 1,733 Core and 746 integration, all green, plus an opt-in performance suite with a synthetic 1,000-meter generator.
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
using MeterVault.Core.Analysis;
|
||||
using MeterVault.Core.Analysis.Coverage;
|
||||
using MeterVault.Core.Analysis.Totals;
|
||||
using static MeterVault.Core.Tests.Analysis.CoverageTestData;
|
||||
|
||||
namespace MeterVault.Core.Tests.Analysis;
|
||||
|
||||
/// <summary>
|
||||
/// The pure pieces the Infrastructure reader rests on: a period as one bucket, lifecycle zeros (D-24), available
|
||||
/// ranges (D-19) and the sum of a measure's members (D-22).
|
||||
/// </summary>
|
||||
public sealed class ReaderSupportTests
|
||||
{
|
||||
private static readonly DateTimeOffset Now = At(2026, 9, 19, 14, 37);
|
||||
|
||||
[Theory]
|
||||
[InlineData(2025, 1, 1, 2025, 12, 31, BucketSize.Year)]
|
||||
[InlineData(2024, 1, 1, 2025, 12, 31, BucketSize.Year)]
|
||||
[InlineData(2026, 2, 1, 2026, 2, 28, BucketSize.Month)]
|
||||
[InlineData(2025, 10, 1, 2026, 9, 30, BucketSize.Month)]
|
||||
[InlineData(2026, 9, 14, 2026, 9, 20, BucketSize.Week)]
|
||||
[InlineData(2026, 9, 15, 2026, 9, 21, BucketSize.Day)]
|
||||
[InlineData(2026, 1, 15, 2026, 3, 31, BucketSize.Day)]
|
||||
public void A_period_is_the_coarsest_unit_it_is_made_of(int y1, int m1, int d1, int y2, int m2, int d2, BucketSize expected) =>
|
||||
Assert.Equal(expected, PeriodBucket.AlignedSize(new DateOnly(y1, m1, d1), new DateOnly(y2, m2, d2)));
|
||||
|
||||
[Fact]
|
||||
public void A_year_to_date_is_a_year_cut_at_now()
|
||||
{
|
||||
var period = PeriodResolver.Resolve(PeriodPreset.YearToDate, null, null, Now, Berlin);
|
||||
|
||||
var bucket = PeriodBucket.Of(period);
|
||||
|
||||
Assert.Equal(BucketSize.Year, bucket.Size);
|
||||
Assert.Equal(new DateOnly(2026, 1, 1), bucket.FirstDay);
|
||||
Assert.Equal(new DateOnly(2026, 9, 20), bucket.EndDay);
|
||||
Assert.Equal(period.From, bucket.From);
|
||||
Assert.Equal(Now, bucket.To);
|
||||
Assert.Equal(new DateOnly(2027, 1, 1), bucket.NominalEndDay);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_monthly_source_resolves_the_month_as_a_period_although_not_its_days()
|
||||
{
|
||||
// The reason totals get their own status: last month by day is unresolved day by day, resolved as a month.
|
||||
var period = PeriodResolver.Resolve(PeriodPreset.LastMonth, null, null, Now, Berlin);
|
||||
var run = new CoverageRun(Midnight(new DateOnly(2026, 8, 1)), Midnight(new DateOnly(2026, 9, 1)), ResolutionClass.Month, DividedAtMonths: true);
|
||||
|
||||
var total = CoverageEvaluator.Evaluate(PeriodBucket.Of(period), [run], Berlin, openingBalanceInBucket: false, Now);
|
||||
var day = CoverageEvaluator.Evaluate(Day(2026, 8, 10), [run], Berlin, openingBalanceInBucket: false, Now);
|
||||
|
||||
Assert.Equal(BucketStatus.Available, total.Status);
|
||||
Assert.Equal(BucketStatus.Unresolved, day.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_period_that_has_not_started_is_an_empty_bucket()
|
||||
{
|
||||
var period = PeriodResolver.Resolve(PeriodPreset.Custom, new DateOnly(2026, 11, 1), new DateOnly(2026, 11, 30), Now, Berlin);
|
||||
|
||||
var bucket = PeriodBucket.Of(period);
|
||||
|
||||
Assert.Equal(bucket.From, bucket.To);
|
||||
Assert.Equal(bucket.FirstDay, bucket.EndDay);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Time_outside_the_service_period_is_known_zero_coverage()
|
||||
{
|
||||
var runs = LifecycleCoverage.OutsideService(new DateOnly(2026, 2, 10), new DateOnly(2026, 6, 30), Berlin);
|
||||
|
||||
Assert.Equal(2, runs.Count);
|
||||
Assert.Equal(Midnight(new DateOnly(2026, 2, 10)), runs[0].To);
|
||||
Assert.Equal(Midnight(new DateOnly(2026, 7, 1)), runs[1].From);
|
||||
Assert.All(runs, r => Assert.False(r.IsGap));
|
||||
Assert.All(runs, r => Assert.Equal(ResolutionClass.Hour, r.Resolution));
|
||||
Assert.Empty(LifecycleCoverage.OutsideService(null, null, Berlin));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_meter_installed_mid_month_contributes_a_whole_month()
|
||||
{
|
||||
// Installed 10 February with daily data from then: as its own series February is partial, as a member of a
|
||||
// total the days before installation are a known zero and February is complete (D-24).
|
||||
var data = new CoverageRun(Midnight(new DateOnly(2026, 2, 10)), Midnight(new DateOnly(2026, 3, 1)), ResolutionClass.Day, DividedAtMonths: true);
|
||||
var february = Month(2026, 2);
|
||||
|
||||
var own = CoverageEvaluator.Evaluate(february, [data], Berlin, false, Now);
|
||||
var contributing = CoverageEvaluator.Evaluate(
|
||||
february, LifecycleCoverage.WithService([data], new DateOnly(2026, 2, 10), null, Berlin), Berlin, false, Now);
|
||||
|
||||
Assert.Equal(BucketStatus.Partial, own.Status);
|
||||
Assert.Equal(BucketStatus.Available, contributing.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_retired_meters_zero_is_known_only_up_to_now()
|
||||
{
|
||||
var runs = LifecycleCoverage.OutsideService(null, new DateOnly(2026, 6, 30), Berlin);
|
||||
|
||||
var capped = CoverageRuns.CapAt(runs, Now, Berlin);
|
||||
|
||||
Assert.Equal(Now, Assert.Single(capped).To);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Available_range_is_capped_at_now_and_names_its_latest_month()
|
||||
{
|
||||
// A current-month label row covers September but closes after now: availability ends where it starts.
|
||||
var runs = new[]
|
||||
{
|
||||
new CoverageRun(Midnight(new DateOnly(2025, 11, 1)), Midnight(new DateOnly(2026, 10, 1)), ResolutionClass.Month, true,
|
||||
LastIntervalStart: Midnight(new DateOnly(2026, 9, 1))),
|
||||
};
|
||||
|
||||
var range = AvailableRange.OfRuns(runs, Now, Berlin);
|
||||
|
||||
Assert.NotNull(range);
|
||||
Assert.Equal(new DateOnly(2025, 11, 1), range.FirstDay);
|
||||
Assert.Equal(new DateOnly(2026, 8, 31), range.LastDay);
|
||||
Assert.Equal(new DateOnly(2026, 8, 1), range.LatestMonth);
|
||||
Assert.Null(AvailableRange.OfRuns([], Now, Berlin));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Available_ranges_unite_to_their_outer_bounds()
|
||||
{
|
||||
var early = AvailableRange.Of(Midnight(new DateOnly(2022, 11, 1)), Midnight(new DateOnly(2023, 1, 1)), Berlin);
|
||||
var late = AvailableRange.Of(Midnight(new DateOnly(2026, 5, 1)), Midnight(new DateOnly(2026, 6, 1)), Berlin);
|
||||
|
||||
var union = AvailableRange.Union([early, null, late], Berlin);
|
||||
|
||||
Assert.NotNull(union);
|
||||
Assert.Equal(new DateOnly(2022, 11, 1), union.FirstDay);
|
||||
Assert.Equal(new DateOnly(2026, 5, 31), union.LastDay);
|
||||
Assert.Equal(new DateOnly(2026, 5, 1), union.LatestMonth);
|
||||
Assert.Null(AvailableRange.Union([null], Berlin));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Measure_members_add_up_when_all_are_available()
|
||||
{
|
||||
var sum = MeasureValues.Sum([(1, BucketValue.Available(100, Provenance.Measured)), (2, BucketValue.Available(150, Provenance.Imported))]);
|
||||
|
||||
Assert.Equal(BucketStatus.Available, sum.Status);
|
||||
Assert.Equal(250, sum.Value);
|
||||
Assert.Equal(Provenance.Measured | Provenance.Imported, sum.Provenance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_missing_member_makes_the_total_partial_and_names_it()
|
||||
{
|
||||
var sum = MeasureValues.Sum([(1, BucketValue.Available(100, Provenance.Measured)), (2, BucketValue.Missing())]);
|
||||
|
||||
Assert.Equal(BucketStatus.Partial, sum.Status);
|
||||
Assert.Equal(100, sum.Value);
|
||||
Assert.Equal(ValueIssue.MissingSource, sum.Issue);
|
||||
Assert.Equal([2], sum.DependencyPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Without_any_member_data_the_total_is_missing()
|
||||
{
|
||||
var sum = MeasureValues.Sum([(1, BucketValue.Missing()), (2, BucketValue.Missing())]);
|
||||
|
||||
Assert.Equal(BucketStatus.Missing, sum.Status);
|
||||
Assert.Null(sum.Value);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(BucketStatus.Pending, ValueIssue.AnalysisPending)]
|
||||
[InlineData(BucketStatus.Invalid, ValueIssue.NonFinite)]
|
||||
[InlineData(BucketStatus.Unresolved, ValueIssue.CoarseResolution)]
|
||||
public void A_spoiling_member_decides_the_total(BucketStatus status, ValueIssue issue)
|
||||
{
|
||||
var spoiling = new BucketValue(null, status, Provenance.None, issue, null, [7, 3]);
|
||||
|
||||
var sum = MeasureValues.Sum([(1, BucketValue.Available(100, Provenance.Measured)), (7, spoiling), (2, BucketValue.Missing())]);
|
||||
|
||||
Assert.Equal(status, sum.Status);
|
||||
Assert.Null(sum.Value);
|
||||
Assert.Equal(issue, sum.Issue);
|
||||
Assert.Equal([7, 3], sum.DependencyPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_partial_member_keeps_its_issue_and_signed_values_stay_signed()
|
||||
{
|
||||
var partial = new BucketValue(-40, BucketStatus.Partial, Provenance.Derived, ValueIssue.OpeningBalance);
|
||||
|
||||
var sum = MeasureValues.Sum([(1, BucketValue.Available(-10, Provenance.Measured)), (5, partial)]);
|
||||
|
||||
Assert.Equal(BucketStatus.Partial, sum.Status);
|
||||
Assert.Equal(-50, sum.Value);
|
||||
Assert.Equal(ValueIssue.OpeningBalance, sum.Issue);
|
||||
Assert.Equal([5], sum.DependencyPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_series_of_members_is_summed_bucket_by_bucket()
|
||||
{
|
||||
IReadOnlyList<BucketValue> a = [BucketValue.Available(100, Provenance.Measured), BucketValue.Available(80, Provenance.Measured)];
|
||||
IReadOnlyList<BucketValue> b = [BucketValue.Available(150, Provenance.Measured), BucketValue.Missing()];
|
||||
|
||||
var series = MeasureValues.SumSeries([(1, a), (2, b)], 2);
|
||||
|
||||
Assert.Equal(250, series[0].Value);
|
||||
Assert.Equal(BucketStatus.Available, series[0].Status);
|
||||
Assert.Equal(80, series[1].Value);
|
||||
Assert.Equal(BucketStatus.Partial, series[1].Status);
|
||||
Assert.Throws<ArgumentException>(() => MeasureValues.SumSeries([(1, a)], 3));
|
||||
}
|
||||
|
||||
private static AnalysisBucket Month(int year, int month)
|
||||
{
|
||||
var first = new DateOnly(year, month, 1);
|
||||
return new AnalysisBucket(first, first.AddMonths(1), Midnight(first), Midnight(first.AddMonths(1)), BucketSize.Month);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user