Analysis: one selected period, one set of numbers, on every page
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:
Florian Schmidt
2026-09-20 10:29:13 +02:00
parent c0f52dbb6f
commit 8940ef25c3
384 changed files with 82753 additions and 4518 deletions
+349
View File
@@ -0,0 +1,349 @@
using MeterVault.Core.Analysis.Quantities;
namespace MeterVault.Core.Tests.Analysis;
public sealed class UnitsTests
{
public static TheoryData<string, string, bool> AliasTableEntries
{
get
{
var data = new TheoryData<string, string, bool>();
foreach (var entry in Units.AliasTable)
{
data.Add(entry.Alias, entry.Info.Symbol, entry.CaseSensitive);
}
return data;
}
}
[Theory]
// Energy
[InlineData("kWh", "kWh")]
[InlineData("kwh", "kWh")]
[InlineData("KWH", "kWh")]
[InlineData("KWh", "kWh")]
[InlineData(" kWh ", "kWh")]
[InlineData("k Wh", "kWh")]
[InlineData("Kilowattstunden", "kWh")]
[InlineData("kilowatt-hour", "kWh")]
[InlineData("Kilowatt hours", "kWh")]
[InlineData("Wh", "Wh")]
[InlineData("wh", "Wh")]
[InlineData("Wattstunden", "Wh")]
[InlineData("MWh", "MWh")]
[InlineData("MWH", "MWh")]
[InlineData("Mwh", "MWh")]
[InlineData("Megawattstunde", "MWh")]
[InlineData("mWh", "mWh")]
[InlineData("GWh", "GWh")]
[InlineData("MJ", "MJ")]
[InlineData("gj", "GJ")]
// Power
[InlineData("W", "W")]
[InlineData("w", "W")]
[InlineData("Watt", "W")]
[InlineData("kW", "kW")]
[InlineData("kw", "kW")]
[InlineData("KW", "kW")]
[InlineData("Kilowatt", "kW")]
[InlineData("MW", "MW")]
[InlineData("Mw", "MW")]
[InlineData("mW", "mW")]
// Volume
[InlineData("m3", "m³")]
[InlineData("M3", "m³")]
[InlineData("m³", "m³")]
[InlineData("cbm", "m³")]
[InlineData("CBM", "m³")]
[InlineData("m^3", "m³")]
[InlineData("Kubikmeter", "m³")]
[InlineData("cubic metre", "m³")]
[InlineData("l", "L")]
[InlineData("L", "L")]
[InlineData("Liter", "L")]
[InlineData("liter", "L")]
[InlineData("Litre", "L")]
[InlineData("litres", "L")]
[InlineData("ltr", "L")]
[InlineData("dm3", "L")]
[InlineData("hl", "hL")]
[InlineData("Hektoliter", "hL")]
// Time
[InlineData("h", "h")]
[InlineData("H", "h")]
[InlineData("hour", "h")]
[InlineData("hours", "h")]
[InlineData("hrs", "h")]
[InlineData("Std", "h")]
[InlineData("Std.", "h")]
[InlineData("Stunden", "h")]
[InlineData("Betriebsstunden", "h")]
[InlineData("min", "min")]
[InlineData("Minuten", "min")]
[InlineData("s", "s")]
// Mass
[InlineData("kg", "kg")]
[InlineData("KG", "kg")]
[InlineData("t", "t")]
[InlineData("Tonne", "t")]
public void Every_alias_normalizes_to_its_canonical_symbol(string alias, string canonical)
{
Assert.Equal(canonical, Units.Normalize(alias));
}
[Theory]
[MemberData(nameof(AliasTableEntries))]
public void Every_entry_of_the_alias_table_names_its_own_unit_however_it_is_capitalised(
string alias, string symbol, bool caseSensitive)
{
var info = Units.Describe(symbol);
Assert.NotNull(info);
Assert.Equal(symbol, info.Symbol);
Assert.Equal(symbol, Units.Normalize(symbol));
Assert.Equal(symbol, Units.Normalize(alias));
Assert.Equal(info, Units.Describe(alias));
Assert.Equal(info, Units.Describe($" {alias} "));
if (!caseSensitive)
{
// Typing a unit in capitals never changes what it means ("KWH", "LITER", "M³").
Assert.Equal(info, Units.Describe(alias.ToUpperInvariant()));
}
}
[Fact]
public void The_alias_table_holds_every_canonical_symbol_once_per_spelling()
{
var spellings = Units.AliasTable.Select(a => a.Alias).ToList();
Assert.Equal(spellings.Count, spellings.Distinct(StringComparer.Ordinal).Count());
Assert.All(
new[] { "mWh", "Wh", "kWh", "MWh", "GWh", "MJ", "GJ", "mW", "W", "kW", "MW", "GW", "L", "hL", "m³", "g", "kg", "t", "h", "min", "s" },
symbol => Assert.Contains(symbol, spellings));
}
[Fact]
public void Milli_and_mega_are_told_apart_by_the_case_of_the_m()
{
// Home Assistant reports small sensors in mW/mWh; reading them as MW/MWh would be off by 10⁹.
Assert.Equal(new UnitInfo("mW", UnitDimension.Power, 0.000_001), Units.Describe("mW"));
Assert.Equal(new UnitInfo("MW", UnitDimension.Power, 1_000), Units.Describe("MW"));
Assert.Equal(new UnitInfo("mWh", UnitDimension.Energy, 0.000_001), Units.Describe("mWh"));
Assert.Equal(new UnitInfo("MWh", UnitDimension.Energy, 1_000), Units.Describe("MWh"));
Assert.Equal(0.000_001, Units.ConversionFactor("mWh", "kWh")!.Value, 12);
Assert.False(Units.AreSame("mW", "MW"));
}
[Theory]
[InlineData("mwh")]
[InlineData("mw")]
[InlineData("mj")]
[InlineData("mWH")]
public void A_lower_case_m_on_a_mega_symbol_is_not_guessed(string unit)
{
Assert.Null(Units.Describe(unit));
Assert.Null(Units.ConversionFactor(unit, "kWh"));
Assert.Null(Units.ConversionFactor(unit, "kW"));
}
[Theory]
[InlineData(" Stk ", "stk")]
[InlineData("STK", "stk")]
[InlineData("Pellets", "pellets")]
[InlineData("%", "%")]
[InlineData("100 L", "100 l")]
[InlineData("kWh (el)", "kwh (el)")]
[InlineData("Nm³", "nm³")]
public void An_unknown_unit_is_trimmed_and_folded_so_every_spelling_gives_one_key(string unit, string expected)
{
Assert.Equal(expected, Units.Normalize(unit));
Assert.Null(Units.Describe(unit));
}
[Fact]
public void Normalized_units_can_be_grouped_ordinally()
{
var meters = new[] { "Stk", "stk", "STK", "m3", "m³", "cbm", "kWh", "KWH" };
var groups = meters.GroupBy(Units.Normalize, StringComparer.Ordinal).Select(g => (g.Key, g.Count())).ToList();
Assert.Equal([("stk", 3), ("m³", 3), ("kWh", 2)], groups);
}
[Fact]
public void The_unit_comparer_treats_every_spelling_of_a_unit_as_one_key()
{
var comparer = Units.Comparer;
Assert.True(comparer.Equals("m3", "m³"));
Assert.True(comparer.Equals("Stk", "stk"));
Assert.True(comparer.Equals(null, " "));
Assert.False(comparer.Equals("kWh", "MWh"));
Assert.False(comparer.Equals("mW", "MW"));
Assert.Equal(comparer.GetHashCode("Stk"), comparer.GetHashCode("STK"));
Assert.Equal(comparer.GetHashCode("m3"), comparer.GetHashCode("cbm"));
Assert.Equal(3, new[] { "m3", "m³", "Stk", "stk", "kWh", "kwh" }.Distinct(comparer).Count());
var byUnit = new Dictionary<string, int>(comparer) { ["m3"] = 1 };
Assert.True(byUnit.ContainsKey("Kubikmeter"));
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void A_missing_unit_normalizes_to_empty(string? unit)
{
Assert.Equal(string.Empty, Units.Normalize(unit));
}
[Theory]
[InlineData("m3/h", "m³/h")]
[InlineData("l/h", "L/h")]
[InlineData("Liter / Std", "L/h")]
[InlineData("kWh / h", "kWh/h")]
[InlineData("Stk/h", "stk/h")]
public void A_rate_normalizes_both_sides(string unit, string expected)
{
Assert.Equal(expected, Units.Normalize(unit));
}
[Theory]
[InlineData("m3", "m³")]
[InlineData("cbm", "M3")]
[InlineData("Liter", "l")]
[InlineData("Stk", "stk")]
[InlineData("L/h", "l/Std")]
[InlineData("Kilowattstunden", "kWh")]
public void Different_spellings_of_one_unit_are_the_same(string a, string b)
{
Assert.True(Units.AreSame(a, b));
}
[Fact]
public void Units_that_only_convert_into_each_other_are_not_the_same()
{
Assert.False(Units.AreSame("kWh", "MWh"));
Assert.False(Units.AreSame("L", "m³"));
}
[Theory]
[InlineData("kWh", "MWh")]
[InlineData("Wh", "kWh")]
[InlineData("L", "m3")]
[InlineData("hl", "L")]
[InlineData("kW", "MW")]
[InlineData("h", "min")]
[InlineData("kg", "t")]
[InlineData("GJ", "kWh")]
[InlineData("Stk", "Stk")]
[InlineData("m3/h", "L/min")]
[InlineData("mWh", "kWh")]
public void Units_of_one_dimension_are_compatible(string a, string b)
{
Assert.True(Units.AreCompatible(a, b));
Assert.True(Units.AreCompatible(b, a));
}
[Theory]
[InlineData("kWh", "m³")]
[InlineData("kW", "kWh")]
[InlineData("h", "L")]
[InlineData("kg", "L")]
[InlineData("Stk", "kWh")]
[InlineData("Stk", "Pellets")]
[InlineData("", "kWh")]
[InlineData("", "")]
[InlineData("m³/h", "m³")]
public void Units_of_different_dimensions_are_not_compatible(string a, string b)
{
Assert.False(Units.AreCompatible(a, b));
Assert.Null(Units.ConversionFactor(a, b));
}
[Theory]
[InlineData("MWh", "kWh", 1000)]
[InlineData("kWh", "MWh", 0.001)]
[InlineData("Wh", "kWh", 0.001)]
[InlineData("kWh", "kWh", 1)]
[InlineData("m3", "L", 1000)]
[InlineData("L", "m³", 0.001)]
[InlineData("hL", "L", 100)]
[InlineData("L", "hL", 0.01)]
[InlineData("t", "kg", 1000)]
[InlineData("min", "h", 1 / 60d)]
[InlineData("GJ", "kWh", 1000 / 3.6)]
[InlineData("MJ", "kWh", 1 / 3.6)]
[InlineData("Stk", "stk", 1)]
[InlineData("m³/h", "L/min", 1000 / 60d)]
[InlineData("Kilowattstunden", "Wh", 1000)]
public void The_conversion_factor_expresses_an_amount_in_the_target_unit(string from, string to, double factor)
{
Assert.Equal(factor, Units.ConversionFactor(from, to)!.Value, 9);
}
[Theory]
[InlineData("W", "Wh")]
[InlineData("kW", "kWh")]
[InlineData("kw", "kWh")]
[InlineData("MW", "MWh")]
[InlineData("GW", "GWh")]
[InlineData("mW", "mWh")]
[InlineData("L/h", "L")]
[InlineData("m3/h", "m³")]
[InlineData("l/Std", "L")]
[InlineData("kWh/h", "kWh")]
[InlineData("W/m²", "Wh/m²")]
[InlineData("kW/m2", "kWh/m2")]
[InlineData("kWh", "kWh")]
[InlineData("L", "L")]
[InlineData("Stk", "stk")]
[InlineData("", "")]
public void A_rate_per_hour_integrates_over_hours_to_its_quantity_unit(string rateUnit, string integrated)
{
Assert.Equal(integrated, Units.IntegratedOverHours(rateUnit));
}
[Theory]
[InlineData("L/min", "L/min")]
[InlineData("l/Minute", "L/min")]
[InlineData("m³/s", "m³/s")]
[InlineData("L/Tag", "L/tag")]
public void A_rate_over_another_time_keeps_its_rate_unit_because_the_normalizer_does_not_rescale_it(
string rateUnit, string kept)
{
// The normalizer books value × elapsed hours: 10 L/min for an hour books 10, which is not 10 L.
Assert.Equal(kept, Units.IntegratedOverHours(rateUnit));
Assert.Null(Units.ConversionFactor(Units.IntegratedOverHours(rateUnit), "L"));
}
[Theory]
[InlineData("kW", true)]
[InlineData("W", true)]
[InlineData("mW", true)]
[InlineData("L/h", true)]
[InlineData("m3/Std", true)]
[InlineData("W/m²", true)]
[InlineData("L/min", false)]
[InlineData("m³/s", false)]
[InlineData("kWh", false)]
[InlineData("Stk", false)]
[InlineData("", false)]
public void Only_power_and_explicit_per_hour_units_are_stated_per_hour_rates(string rateUnit, bool perHour)
{
Assert.Equal(perHour, Units.IsPerHourRate(rateUnit));
}
[Fact]
public void Describe_reports_dimension_and_scale_against_the_reference_unit()
{
var mwh = Units.Describe("MWH")!;
var cubic = Units.Describe("cbm")!;
Assert.Equal(new UnitInfo("MWh", UnitDimension.Energy, 1000), mwh);
Assert.Equal(new UnitInfo("m³", UnitDimension.Volume, 1000), cubic);
Assert.Null(Units.Describe("L/h"));
}
}