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.
251 lines
11 KiB
C#
251 lines
11 KiB
C#
using System.Text.Json;
|
|
using MeterVault.Core.Analysis;
|
|
using MeterVault.Core.Analysis.Quantities;
|
|
using MeterVault.Core.Analysis.Virtual;
|
|
using MeterVault.Core.Domain;
|
|
|
|
namespace MeterVault.Core.Tests.Analysis;
|
|
|
|
/// <summary>
|
|
/// A virtual meter's definition lives in <c>Meter.Meta</c> next to other keys such as <c>role</c> (D-25). Writing it
|
|
/// must never lose those keys, the referenced ids must follow the expression rather than whatever was stored, and
|
|
/// reading must never throw — a bad blob becomes a "malformed" result for that one meter.
|
|
/// </summary>
|
|
public sealed class VirtualDefinitionJsonTests
|
|
{
|
|
private static readonly VirtualDefinition SummeSolar = new("m4 + m5", QuantityKind.Generation, "kWh", VirtualCostRule.None);
|
|
|
|
[Fact]
|
|
public void A_definition_round_trips_and_keeps_the_role_and_every_other_key()
|
|
{
|
|
const string existing = """{"role":"grid_import","custom":{"a":[1,2]},"note":"keep me"}""";
|
|
|
|
var written = VirtualDefinitionJson.Write(existing, SummeSolar);
|
|
var read = VirtualDefinitionJson.Read(written);
|
|
|
|
Assert.Equal(VirtualDefinitionReadStatus.Present, read.Status);
|
|
Assert.Equal(SummeSolar, read.Definition);
|
|
Assert.False(read.ReferencedIdsStale);
|
|
Assert.Equal(MeterRoles.GridImport, MeterMeta.Role(written));
|
|
Assert.Equal("keep me", MeterMeta.ReadString(written, "note"));
|
|
using var doc = JsonDocument.Parse(written);
|
|
Assert.Equal("[1,2]", doc.RootElement.GetProperty("custom").GetProperty("a").GetRawText());
|
|
}
|
|
|
|
[Fact]
|
|
public void Written_keys_use_the_documented_tokens_and_derive_the_referenced_ids()
|
|
{
|
|
using var doc = JsonDocument.Parse(VirtualDefinitionJson.Write("{}", SummeSolar));
|
|
var root = doc.RootElement;
|
|
|
|
Assert.Equal("m4 + m5", root.GetProperty("expression").GetString());
|
|
Assert.Equal("[4,5]", root.GetProperty("referencedMeterIds").GetRawText());
|
|
Assert.Equal("generation", root.GetProperty("resultKind").GetString());
|
|
Assert.Equal("kWh", root.GetProperty("resultUnit").GetString());
|
|
Assert.Equal("none", root.GetProperty("costRule").GetString());
|
|
|
|
using var consumption = JsonDocument.Parse(VirtualDefinitionJson.Write(
|
|
"{}", new VirtualDefinition("m1 + m3", QuantityKind.Consumption, "kWh", VirtualCostRule.SourceCosts)));
|
|
Assert.Equal("sourceCosts", consumption.RootElement.GetProperty("costRule").GetString());
|
|
}
|
|
|
|
[Fact]
|
|
public void Writing_a_definition_that_leaves_its_kind_or_cost_rule_to_inference_is_refused()
|
|
{
|
|
// A-08: what is stored is the effective definition. Storing "m4 + m5" without its kind would make every reader
|
|
// take Summe Solar for consumption unless it re-validated the whole catalog first.
|
|
const string meta = """{"role":"total_load"}""";
|
|
|
|
Assert.Throws<ArgumentException>(() => VirtualDefinitionJson.Write(meta, new VirtualDefinition("m1 - m2")));
|
|
Assert.Throws<ArgumentException>(() => VirtualDefinitionJson.Write(meta, new VirtualDefinition("m1 - m2", QuantityKind.Consumption)));
|
|
Assert.Throws<ArgumentException>(() => VirtualDefinitionJson.Write(meta, new VirtualDefinition("m9", QuantityKind.Runtime, "h", VirtualCostRule.None)));
|
|
}
|
|
|
|
[Fact]
|
|
public void Saving_stores_the_inferred_kind_unit_and_cost_rule_so_readers_never_infer_them_again()
|
|
{
|
|
var catalog = new MeterCatalog(VirtualFixtures.SeededElectricity());
|
|
var validation = VirtualValidator.Validate(new VirtualDefinition("m4 + m5"), 6, catalog);
|
|
|
|
var read = VirtualDefinitionJson.Read(VirtualDefinitionJson.Write("""{"note":"x"}""", validation.EffectiveDefinition!));
|
|
|
|
Assert.Equal(SummeSolar, read.Definition);
|
|
var quantity = NormalizedQuantity.Of(MeterMode.Virtual, "kWh", null, null, read.Definition!.DeclaredResult);
|
|
Assert.Equal(new NormalizedQuantity(QuantityKind.Generation, "kWh"), quantity);
|
|
}
|
|
|
|
[Fact]
|
|
public void The_unit_is_stored_in_canonical_spelling_and_a_blank_one_is_left_out()
|
|
{
|
|
using var water = JsonDocument.Parse(VirtualDefinitionJson.Write("{}", new VirtualDefinition("m7", QuantityKind.Consumption, " m3 ", VirtualCostRule.None)));
|
|
Assert.Equal("m³", water.RootElement.GetProperty("resultUnit").GetString());
|
|
|
|
using var unitless = JsonDocument.Parse(VirtualDefinitionJson.Write("""{"resultUnit":"kWh"}""", new VirtualDefinition("m7", QuantityKind.Consumption, null, VirtualCostRule.None)));
|
|
Assert.False(unitless.RootElement.TryGetProperty("resultUnit", out _));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("runtime")]
|
|
[InlineData("export")]
|
|
[InlineData("cost")]
|
|
public void A_stored_kind_no_virtual_meter_may_have_is_malformed(string token)
|
|
{
|
|
var read = VirtualDefinitionJson.Read($$"""{"expression":"m1","resultKind":"{{token}}"}""");
|
|
|
|
Assert.Equal(VirtualDefinitionReadStatus.Malformed, read.Status);
|
|
Assert.Null(read.Definition!.ResultKind);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("expression", "LONE")]
|
|
[InlineData("resultUnit", "LONEx")]
|
|
[InlineData("costRule", "LONE")]
|
|
public void Text_that_cannot_be_decoded_is_malformed_not_an_exception(string key, string value)
|
|
{
|
|
// A lone surrogate escape is valid JSON but no .NET string; jsonb refuses it, an import file does not.
|
|
var escaped = value.Replace("LONE", "\\ud800", StringComparison.Ordinal);
|
|
var meta = key == "expression"
|
|
? $$"""{"expression":"{{escaped}}"}"""
|
|
: $$"""{"expression":"m1","{{key}}":"{{escaped}}"}""";
|
|
|
|
Assert.Equal(VirtualDefinitionReadStatus.Malformed, VirtualDefinitionJson.Read(meta).Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void Stored_referenced_ids_are_never_trusted_over_the_expression()
|
|
{
|
|
var read = VirtualDefinitionJson.Read("""{"expression":"m1 + m2","referencedMeterIds":[9]}""");
|
|
|
|
Assert.Equal(VirtualDefinitionReadStatus.Present, read.Status);
|
|
Assert.Equal([1, 2], read.Definition!.ReferencedMeterIds);
|
|
Assert.True(read.ReferencedIdsStale);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
[InlineData("{}")]
|
|
[InlineData("""{"role":"grid_import"}""")]
|
|
[InlineData("""{"expression":null}""")]
|
|
[InlineData("""{"expression":" "}""")]
|
|
public void Meta_without_an_expression_is_a_legacy_meter_not_an_error(string? meta)
|
|
{
|
|
var read = VirtualDefinitionJson.Read(meta);
|
|
|
|
Assert.Equal(VirtualDefinitionReadStatus.Absent, read.Status);
|
|
Assert.Null(read.Definition);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("not json")]
|
|
[InlineData("{\"expression\":")]
|
|
[InlineData("[1,2,3]")]
|
|
[InlineData("\"m1 + m2\"")]
|
|
[InlineData("""{"expression":5}""")]
|
|
[InlineData("""{"expression":["m1"]}""")]
|
|
public void Unreadable_meta_is_malformed_and_never_throws(string meta)
|
|
{
|
|
var read = VirtualDefinitionJson.Read(meta);
|
|
|
|
Assert.Equal(VirtualDefinitionReadStatus.Malformed, read.Status);
|
|
Assert.NotNull(read.Problem);
|
|
}
|
|
|
|
[Fact]
|
|
public void Json_nested_beyond_the_reader_depth_is_malformed_not_an_exception()
|
|
{
|
|
var deep = """{"expression":"m1","x":""" + new string('[', 200) + new string(']', 200) + "}";
|
|
|
|
Assert.Equal(VirtualDefinitionReadStatus.Malformed, VirtualDefinitionJson.Read(deep).Status);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("""{"expression":"m1","resultKind":"power"}""")]
|
|
[InlineData("""{"expression":"m1","resultKind":3}""")]
|
|
[InlineData("""{"expression":"m1","costRule":"cheap"}""")]
|
|
[InlineData("""{"expression":"m1","resultUnit":true}""")]
|
|
public void A_bad_optional_key_is_malformed_but_keeps_the_expression_for_repair(string meta)
|
|
{
|
|
var read = VirtualDefinitionJson.Read(meta);
|
|
|
|
Assert.Equal(VirtualDefinitionReadStatus.Malformed, read.Status);
|
|
Assert.Equal("m1", read.Definition!.Expression);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_syntax_error_is_the_validators_business_not_a_json_problem()
|
|
{
|
|
var read = VirtualDefinitionJson.Read("""{"expression":"m1 +"}""");
|
|
|
|
Assert.Equal(VirtualDefinitionReadStatus.Present, read.Status);
|
|
Assert.False(read.Definition!.Parsed.Success);
|
|
Assert.Equal([1], read.Definition.ReferencedMeterIds);
|
|
}
|
|
|
|
[Fact]
|
|
public void Tokens_are_read_case_insensitively()
|
|
{
|
|
var read = VirtualDefinitionJson.Read("""{"expression":"m1","resultKind":"Generation","costRule":"SOURCECOSTS","resultUnit":" kWh "}""");
|
|
|
|
Assert.Equal(new VirtualDefinition("m1", QuantityKind.Generation, "kWh", VirtualCostRule.SourceCosts), read.Definition);
|
|
}
|
|
|
|
[Fact]
|
|
public void Rewriting_meter_ids_follows_an_import_renumbering_and_keeps_the_role()
|
|
{
|
|
const string meta = """{"role":"total_load","expression":"m1 - m2","referencedMeterIds":[1,2],"resultKind":"consumption"}""";
|
|
var map = new Dictionary<int, int> { [1] = 11, [2] = 12 };
|
|
|
|
var rewritten = VirtualDefinitionJson.RewriteMeterIds(meta, id => map[id]);
|
|
var read = VirtualDefinitionJson.Read(rewritten);
|
|
|
|
Assert.Equal("m11 - m12", read.Definition!.Expression);
|
|
Assert.Equal([11, 12], read.Definition.ReferencedMeterIds);
|
|
Assert.False(read.ReferencedIdsStale);
|
|
Assert.Equal(QuantityKind.Consumption, read.Definition.ResultKind);
|
|
Assert.Equal(MeterRoles.TotalLoad, MeterMeta.Role(rewritten));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("""{"role":"grid_import"}""")]
|
|
[InlineData("not json")]
|
|
[InlineData("")]
|
|
public void Rewriting_meter_ids_leaves_meta_without_an_expression_untouched(string meta)
|
|
{
|
|
Assert.Equal(meta, VirtualDefinitionJson.RewriteMeterIds(meta, _ => throw new InvalidOperationException("no ids to map")));
|
|
}
|
|
|
|
[Fact]
|
|
public void Removing_a_definition_keeps_every_other_key()
|
|
{
|
|
var withDefinition = VirtualDefinitionJson.Write("""{"role":"grid_export"}""", SummeSolar);
|
|
|
|
var removed = VirtualDefinitionJson.Remove(withDefinition);
|
|
|
|
Assert.Equal(VirtualDefinitionReadStatus.Absent, VirtualDefinitionJson.Read(removed).Status);
|
|
Assert.Equal(MeterRoles.GridExport, MeterMeta.Role(removed));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("[1,2]")]
|
|
[InlineData("not json")]
|
|
[InlineData("""{"a":1,"a":2}""")]
|
|
public void Meta_that_is_not_a_json_object_is_replaced_on_write(string meta)
|
|
{
|
|
var written = VirtualDefinitionJson.Write(meta, SummeSolar);
|
|
|
|
Assert.Equal(SummeSolar, VirtualDefinitionJson.Read(written).Definition);
|
|
}
|
|
|
|
[Fact]
|
|
public void Changing_the_expression_with_a_with_expression_reparses_it()
|
|
{
|
|
var changed = SummeSolar with { Expression = "m7 * 2" };
|
|
|
|
Assert.Equal([7], changed.ReferencedMeterIds);
|
|
Assert.Equal(Formula.Parse("m7 * 2"), changed.Formula);
|
|
Assert.Equal([4, 5], SummeSolar.ReferencedMeterIds);
|
|
}
|
|
}
|