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.
167 lines
7.0 KiB
C#
167 lines
7.0 KiB
C#
using MeterVault.Core.Analysis;
|
|
using static MeterVault.Core.Tests.Analysis.AnalysisClock;
|
|
|
|
namespace MeterVault.Core.Tests.Analysis;
|
|
|
|
/// <summary>
|
|
/// The legacy entry points' ranges (D-45): the REST API's exact instants and the older services' date ranges become
|
|
/// ordinary resolved periods, cut at now, that the bucket planner tiles exactly.
|
|
/// </summary>
|
|
public sealed class LegacyPeriodsTests
|
|
{
|
|
private static readonly DateTimeOffset Now = At(Berlin, 2026, 9, 19, 14, 37);
|
|
|
|
[Fact]
|
|
public void Utc_midnights_keep_their_exact_instants_in_a_zone_ahead_of_utc()
|
|
{
|
|
// The contract test's bounds: 2024-01-01T00:00Z is 01:00 in Berlin, and 2024-03-01T00:00Z is 01:00 on 1 March.
|
|
var period = LegacyPeriods.FromInstants(Utc(2024, 1, 1), Utc(2024, 3, 1), Now, Berlin);
|
|
|
|
Assert.Equal((Day(2024, 1, 1), Day(2024, 3, 1)), (period.FirstDay, period.LastDay));
|
|
Assert.Equal((Utc(2024, 1, 1), Utc(2024, 3, 1)), (period.From, period.To));
|
|
Assert.False(period.IsToDate);
|
|
Assert.False(period.HasNotStarted());
|
|
|
|
var plan = BucketPlanner.Plan(period, BucketSize.Month);
|
|
|
|
// January from 01:00, February whole, and the first hour of 1 March — the buckets tile [from, to) exactly.
|
|
Assert.Equal(3, plan.Buckets.Count);
|
|
Assert.Equal(Utc(2024, 1, 1), plan.Buckets[0].From);
|
|
Assert.Equal(At(Berlin, 2024, 2, 1), plan.Buckets[0].To);
|
|
Assert.Equal((At(Berlin, 2024, 3, 1), Utc(2024, 3, 1)), (plan.Buckets[2].From, plan.Buckets[2].To));
|
|
Assert.Equal([Day(2024, 1, 1), Day(2024, 2, 1), Day(2024, 3, 1)], plan.Buckets.Select(LegacyPeriods.KeyOf));
|
|
}
|
|
|
|
[Fact]
|
|
public void Local_midnights_give_whole_months()
|
|
{
|
|
var period = LegacyPeriods.FromInstants(At(Berlin, 2024, 1, 1), At(Berlin, 2024, 3, 1), Now, Berlin);
|
|
|
|
Assert.Equal((Day(2024, 1, 1), Day(2024, 2, 29)), (period.FirstDay, period.LastDay));
|
|
var plan = BucketPlanner.Plan(period, BucketSize.Month);
|
|
Assert.Equal(2, plan.Buckets.Count);
|
|
Assert.Equal(period, PeriodResolver.Resolve(PeriodPreset.Custom, Day(2024, 1, 1), Day(2024, 2, 29), Now, Berlin));
|
|
}
|
|
|
|
[Fact]
|
|
public void An_offset_is_read_as_the_instant_it_names()
|
|
{
|
|
var withOffset = LegacyPeriods.FromInstants(
|
|
new DateTimeOffset(2024, 1, 1, 0, 0, 0, TimeSpan.FromHours(1)), new DateTimeOffset(2024, 3, 1, 0, 0, 0, TimeSpan.FromHours(1)), Now, Berlin);
|
|
|
|
Assert.Equal(TimeSpan.Zero, withOffset.From.Offset);
|
|
Assert.Equal(At(Berlin, 2024, 1, 1), withOffset.From);
|
|
Assert.Equal(At(Berlin, 2024, 3, 1), withOffset.To);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_range_reaching_past_now_stops_at_now()
|
|
{
|
|
// D-04: a legacy caller asking for this month and the next still gets actuals up to now only.
|
|
var period = LegacyPeriods.FromInstants(At(Berlin, 2026, 9, 1), At(Berlin, 2026, 11, 1), Now, Berlin);
|
|
|
|
Assert.True(period.IsToDate);
|
|
Assert.True(period.ExtendsPastNow);
|
|
Assert.Equal(Now, period.To);
|
|
Assert.Equal(Day(2026, 10, 31), period.LastDay);
|
|
|
|
var plan = BucketPlanner.Plan(period, BucketSize.Month);
|
|
var bucket = Assert.Single(plan.Buckets);
|
|
Assert.Equal(Now, bucket.To);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_range_after_now_has_not_started_and_plans_nothing()
|
|
{
|
|
var period = LegacyPeriods.FromInstants(At(Berlin, 2026, 10, 1), At(Berlin, 2026, 11, 1), Now, Berlin);
|
|
|
|
Assert.True(period.HasNotStarted());
|
|
Assert.Equal(period.From, period.To);
|
|
Assert.Empty(BucketPlanner.Plan(period, BucketSize.Month).Buckets);
|
|
Assert.Empty(LegacyPeriods.WholePeriodPlan(period).Buckets);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(-1)]
|
|
public void An_empty_or_inverted_range_plans_nothing(int days)
|
|
{
|
|
var from = At(Berlin, 2024, 5, 1);
|
|
var period = LegacyPeriods.FromInstants(from, from.AddDays(days), Now, Berlin);
|
|
|
|
Assert.True(period.HasNoHistory());
|
|
Assert.Empty(BucketPlanner.Plan(period, BucketSize.Month).Buckets);
|
|
}
|
|
|
|
[Fact]
|
|
public void Instants_outside_the_supported_dates_are_clamped()
|
|
{
|
|
var period = LegacyPeriods.FromInstants(DateTimeOffset.MinValue, DateTimeOffset.MaxValue, Now, Berlin);
|
|
|
|
Assert.Equal(PeriodResolver.MinSupportedDate, period.FirstDay);
|
|
Assert.Equal(At(Berlin, 1900, 1, 1), period.From);
|
|
Assert.Equal(Now, period.To);
|
|
Assert.Equal(PeriodResolver.MaxSupportedDate, period.LastDay);
|
|
Assert.False(BucketPlanner.Plan(period, BucketSize.Year).Refused);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_date_range_ends_the_day_before_its_exclusive_end_and_is_cut_at_now()
|
|
{
|
|
// The dashboard's "this year": [1 January, 1 January next year).
|
|
var year = LegacyPeriods.FromDates(Day(2026, 1, 1), Day(2027, 1, 1), Now, Berlin);
|
|
|
|
Assert.Equal((Day(2026, 1, 1), Day(2026, 12, 31)), (year.FirstDay, year.LastDay));
|
|
Assert.Equal(At(Berlin, 2026, 1, 1), year.From);
|
|
Assert.Equal(Now, year.To);
|
|
Assert.Equal(BucketSize.Year, PeriodBucket.Of(year).Size);
|
|
|
|
var previous = LegacyPeriods.FromDates(Day(2025, 1, 1), Day(2026, 1, 1), Now, Berlin);
|
|
Assert.False(previous.IsToDate);
|
|
Assert.Equal(At(Berlin, 2026, 1, 1), previous.To);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_date_range_in_a_zone_behind_utc_starts_at_local_midnight()
|
|
{
|
|
var period = LegacyPeriods.FromDates(Day(2026, 7, 1), Day(2026, 8, 1), Now, NewYork);
|
|
|
|
Assert.Equal(Utc(2026, 7, 1, 4), period.From);
|
|
Assert.Equal(Utc(2026, 8, 1, 4), period.To);
|
|
}
|
|
|
|
[Fact]
|
|
public void An_empty_date_range_or_one_outside_the_supported_dates_is_handled()
|
|
{
|
|
Assert.True(LegacyPeriods.FromDates(Day(2026, 5, 1), Day(2026, 5, 1), Now, Berlin).HasNoHistory());
|
|
Assert.True(LegacyPeriods.FromDates(Day(2026, 5, 2), Day(2026, 5, 1), Now, Berlin).HasNoHistory());
|
|
|
|
var wide = LegacyPeriods.FromDates(new DateOnly(1, 1, 1), new DateOnly(9999, 1, 1), Now, Berlin);
|
|
Assert.Equal((PeriodResolver.MinSupportedDate, PeriodResolver.MaxSupportedDate), (wide.FirstDay, wide.LastDay));
|
|
}
|
|
|
|
[Fact]
|
|
public void The_whole_period_plan_is_the_period_as_one_bucket()
|
|
{
|
|
var period = LegacyPeriods.FromDates(Day(2025, 1, 1), Day(2026, 1, 1), Now, Berlin);
|
|
|
|
var plan = LegacyPeriods.WholePeriodPlan(period);
|
|
|
|
Assert.False(plan.Refused);
|
|
Assert.Equal(BucketSize.Year, plan.Size);
|
|
Assert.Equal(PeriodBucket.Of(period), Assert.Single(plan.Buckets));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(BucketSize.Day, "2026-09-17")]
|
|
[InlineData(BucketSize.Week, "2026-09-14")]
|
|
[InlineData(BucketSize.Month, "2026-09-01")]
|
|
[InlineData(BucketSize.Year, "2026-01-01")]
|
|
public void A_bucket_is_filed_under_the_start_of_its_calendar_unit(BucketSize size, string key)
|
|
{
|
|
var bucket = new AnalysisBucket(Day(2026, 9, 17), Day(2026, 9, 18), At(Berlin, 2026, 9, 17), At(Berlin, 2026, 9, 18), size);
|
|
|
|
Assert.Equal(Iso(key), LegacyPeriods.KeyOf(bucket));
|
|
}
|
|
}
|