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.
168 lines
7.4 KiB
C#
168 lines
7.4 KiB
C#
using MeterVault.Core.Analysis;
|
|
using MeterVault.Core.Analysis.Costing;
|
|
using MeterVault.Core.Analysis.Totals;
|
|
using MeterVault.Core.Domain;
|
|
using static MeterVault.Core.Tests.Analysis.Costing.CostingTestData;
|
|
|
|
namespace MeterVault.Core.Tests.Analysis.Costing;
|
|
|
|
/// <summary>
|
|
/// Cost figures fold exactly: the sum of the parts is the same figure — value, status and missing prices — however it
|
|
/// is grouped. And the calculator refuses input it cannot price honestly.
|
|
/// </summary>
|
|
public sealed class CostAmountTests
|
|
{
|
|
[Fact]
|
|
public void The_empty_figure_is_priced_with_no_value()
|
|
{
|
|
Assert.Equal(CostStatus.Priced, CostAmount.Empty.Status);
|
|
Assert.Null(CostAmount.Empty.Cost);
|
|
Assert.Equal(BucketStatus.Available, CostAmount.Empty.Availability);
|
|
Assert.Empty(CostAmount.Empty.MissingPrices);
|
|
|
|
var sum = CostAmount.Sum([CostAmount.Empty, CostAmount.Empty]);
|
|
Assert.Equal((CostStatus.Priced, (double?)null, false), (sum.Status, sum.Cost, sum.IncludesNotPriced));
|
|
}
|
|
|
|
[Fact]
|
|
public void Summing_is_associative_over_lines_and_buckets()
|
|
{
|
|
// A priced grid line, a water line with a gap, an unpriced tank, a standing charge and a manual cost: the bill
|
|
// total must be the same whether the buckets or the lines are added first.
|
|
var book = Book(
|
|
TypePrice(1, Electricity, 0.30, "EUR/kWh", D(2025, 1, 1)),
|
|
TypePrice(2, Water, 5, "EUR/m3", D(2025, 3, 1)),
|
|
BasePrice(3, TariffScope.EnergyType, Electricity, 12, "EUR/Monat", D(2025, 1, 1)));
|
|
var buckets = Buckets(BucketSize.Month, D(2025, 1, 1), D(2025, 6, 30));
|
|
|
|
var result = Price(
|
|
buckets,
|
|
book,
|
|
[Line(Grid, Electricity, Each(buckets, 100)), Line(WaterMeter, Water, Each(buckets, 10), "m³"), Line(OilTank, Oil, Each(buckets, 50), "L")],
|
|
standing: [StandingChargeScope.ForEnergyType(Electricity, new ServicePeriod(D(2020, 1, 1)))],
|
|
manual: [Manual(1, D(2025, 4, 2), 99)]);
|
|
|
|
var byLine = CostAmount.Sum(result.Lines.Select(l => l.Total).Concat(result.StandingCharges.Select(r => r.Total)).Append(result.ManualCosts.Total));
|
|
var byBucket = CostAmount.Sum(result.Totals);
|
|
|
|
foreach (var figure in new[] { byLine, byBucket })
|
|
{
|
|
Assert.Equal(result.Total.Cost!.Value, figure.Cost!.Value, 9);
|
|
Assert.Equal(result.Total.Status, figure.Status);
|
|
Assert.Equal(result.Total.IncludesNotPriced, figure.IncludesNotPriced);
|
|
Assert.Equal(result.Total.MissingPrices, figure.MissingPrices);
|
|
}
|
|
|
|
Assert.Equal(CostStatus.Partial, result.Total.Status);
|
|
Assert.Equal((6 * 30) + (4 * 50) + (6 * 12) + 99, result.Total.Cost!.Value, 9);
|
|
}
|
|
|
|
[Fact]
|
|
public void Missing_prices_merge_into_one_entry_with_their_first_and_last_month()
|
|
{
|
|
var book = Book(TypePrice(1, Water, 5, "EUR/m3", D(2025, 6, 1)));
|
|
var buckets = Buckets(BucketSize.Day, D(2025, 3, 30), D(2025, 5, 2));
|
|
|
|
var result = Price(buckets, book, [Line(WaterMeter, Water, Each(buckets, 1), "m³")]);
|
|
|
|
var missing = Assert.Single(result.MissingPrices);
|
|
Assert.Equal((D(2025, 3, 1), D(2025, 5, 1)), (missing.FirstMonth, missing.LastMonth));
|
|
|
|
// Each day bucket names its own month.
|
|
Assert.Equal(D(2025, 4, 1), Assert.Single(result.Totals.At(buckets, D(2025, 4, 17)).MissingPrices).FirstMonth);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_unit_mismatch_outranks_a_gap_when_nothing_is_priced()
|
|
{
|
|
var book = Book(Tariff(1, TariffScope.EnergyType, Water, TariffComponent.UnitPrice, 0.3, "EUR/kWh", D(2025, 2, 1)));
|
|
var buckets = Buckets(BucketSize.Year, D(2025, 1, 1), D(2025, 2, 28));
|
|
|
|
var result = Price(buckets, book, [Line(WaterMeter, Water, Each(buckets, 10), "m³")]);
|
|
|
|
Assert.Equal(CostStatus.UnitMismatch, result.Total.Status);
|
|
Assert.Equal([CostStatus.PriceGap, CostStatus.UnitMismatch], result.MissingPrices.Select(m => m.Reason));
|
|
Assert.Null(result.Total.Cost);
|
|
}
|
|
|
|
[Fact]
|
|
public void Availability_reports_the_most_telling_reason_when_nothing_is_known()
|
|
{
|
|
var book = Book(TypePrice(1, Electricity, 0.30, "EUR/kWh", D(2025, 1, 1)));
|
|
var buckets = Buckets(BucketSize.Year, D(2025, 1, 1), D(2025, 3, 31));
|
|
var parts = CostCalculator.Parts(buckets);
|
|
|
|
var result = Price(
|
|
buckets,
|
|
book,
|
|
[Line(Grid, Electricity, [
|
|
CostQuantity.Unknown(parts[0]),
|
|
CostQuantity.Unknown(parts[1], BucketStatus.Unresolved),
|
|
CostQuantity.Unknown(parts[2], BucketStatus.Pending)])]);
|
|
|
|
Assert.Equal(BucketStatus.Pending, result.Total.Availability);
|
|
Assert.Null(result.Total.Cost);
|
|
}
|
|
|
|
// ---- refusing malformed input ---------------------------------------------------------------------
|
|
|
|
[Fact]
|
|
public void A_quantity_that_is_not_a_part_of_the_request_is_refused()
|
|
{
|
|
var buckets = Buckets(BucketSize.Week, D(2025, 1, 27), D(2025, 2, 2));
|
|
|
|
// The whole week in one quantity would be priced at one month's price: the caller must split it.
|
|
var wholeWeek = new CostQuantity(D(2025, 1, 27), D(2025, 2, 3), 70, BucketStatus.Available);
|
|
|
|
Assert.Throws<ArgumentException>(() => Price(buckets, Book(), [Line(Grid, Electricity, [wholeWeek])]));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_quantity_given_twice_or_not_finite_is_refused()
|
|
{
|
|
var buckets = Buckets(BucketSize.Month, D(2025, 1, 1), D(2025, 1, 31));
|
|
var part = CostCalculator.Parts(buckets).Single();
|
|
|
|
Assert.Throws<ArgumentException>(() => Price(buckets, Book(), [Line(Grid, Electricity, [CostQuantity.Known(part, 1), CostQuantity.Known(part, 2)])]));
|
|
Assert.Throws<ArgumentException>(() => Price(buckets, Book(), [Line(Grid, Electricity, [CostQuantity.Known(part, double.NaN)])]));
|
|
}
|
|
|
|
[Fact]
|
|
public void Overlapping_buckets_are_refused()
|
|
{
|
|
var buckets = new[] { Bucket(D(2025, 1, 1), D(2025, 2, 1)), Bucket(D(2025, 1, 15), D(2025, 1, 16), BucketSize.Day) };
|
|
|
|
Assert.Throws<ArgumentException>(() => CostCalculator.Parts(buckets));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_standing_charge_row_names_its_scope_and_a_meter_with_a_line_is_not_a_row()
|
|
{
|
|
var buckets = Buckets(BucketSize.Month, D(2025, 1, 1), D(2025, 1, 31));
|
|
|
|
// A-18: a meter's own row is for a meter without a line; with one, its charge accrues on the line.
|
|
Assert.Throws<ArgumentException>(() => Price(buckets, Book(), [], standing: [new StandingChargeScope(TariffScope.Meter, null, null)]));
|
|
Assert.Throws<ArgumentException>(() => Price(
|
|
buckets, Book(), [Line(Grid, Electricity, Each(buckets, 1))], standing: [StandingChargeScope.ForMeter(Grid, null)]));
|
|
Assert.Throws<ArgumentException>(() => Price(buckets, Book(), [], standing: [new StandingChargeScope(TariffScope.EnergyType, null, null)]));
|
|
Assert.Throws<ArgumentException>(() => Price(
|
|
buckets, Book(), [], standing: [StandingChargeScope.Global(null), StandingChargeScope.Global(null)]));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_service_period_cannot_end_before_it_starts()
|
|
{
|
|
Assert.Throws<ArgumentException>(() => new ServicePeriod(D(2025, 2, 1), D(2025, 1, 31)));
|
|
}
|
|
|
|
[Fact]
|
|
public void An_empty_request_prices_nothing()
|
|
{
|
|
var result = Price([], Book(), []);
|
|
|
|
Assert.Empty(result.Totals);
|
|
Assert.Null(result.Total.Cost);
|
|
Assert.Equal("EUR", result.Currency);
|
|
}
|
|
}
|