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,229 @@
|
||||
using System.Text.Json;
|
||||
using MeterVault.Core.Analysis;
|
||||
using MeterVault.Core.Analysis.Virtual;
|
||||
using MeterVault.Core.Domain;
|
||||
using MeterVault.Core.Normalization;
|
||||
using MeterVault.Infrastructure.Analysis;
|
||||
using MeterVault.Infrastructure.Normalization;
|
||||
using MeterVault.Infrastructure.Options;
|
||||
using MeterVault.Infrastructure.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace MeterVault.Integration.Tests.Analysis;
|
||||
|
||||
/// <summary>
|
||||
/// Managing virtual meters outside an analysis read: the startup conversion of expression-less ("legacy") virtual
|
||||
/// meters to explicit definitions (D-28), and the dependents a meter's delete dialog must name (D-33). Every test
|
||||
/// creates its own energy type and meters and removes them again; assertions only look at those meters, because
|
||||
/// the upgrade runs over the whole instance.
|
||||
/// </summary>
|
||||
[Collection("Timescale")]
|
||||
public sealed class VirtualManagementTests(TimescaleFixture fx) : IAsyncLifetime
|
||||
{
|
||||
private const string Zone = "Europe/Berlin";
|
||||
|
||||
private readonly List<int> _meters = [];
|
||||
private readonly List<short> _types = [];
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
await using var db = fx.CreateContext();
|
||||
var ids = _meters.ToArray();
|
||||
await db.Consumption.Where(c => ids.Contains(c.MeterId)).ExecuteDeleteAsync();
|
||||
await db.Readings.Where(r => ids.Contains(r.MeterId)).ExecuteDeleteAsync();
|
||||
await db.Meters.Where(m => ids.Contains(m.Id)).ExecuteDeleteAsync();
|
||||
var types = _types.ToArray();
|
||||
await db.EnergyTypes.Where(t => types.Contains(t.Id)).ExecuteDeleteAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Legacy_meters_get_the_sum_their_links_imply_in_dependency_order()
|
||||
{
|
||||
var type = await TypeAsync();
|
||||
var solar1 = await MeterAsync(type, MeterMode.GenerationCounter);
|
||||
var solar2 = await MeterAsync(type, MeterMode.GenerationCounter);
|
||||
var solar3 = await MeterAsync(type, MeterMode.GenerationCounter);
|
||||
var house = await MeterAsync(type, MeterMode.CumulativeCounter);
|
||||
|
||||
// B sums A (itself legacy) and Solar 3, so A has to be written first. A keeps a key of its own.
|
||||
var b = await MeterAsync(type, MeterMode.Virtual);
|
||||
var a = await MeterAsync(type, MeterMode.Virtual, meta: """{"note":"kept"}""");
|
||||
await LinksAsync((solar1, a), (solar2, a), (a, b), (solar3, b));
|
||||
|
||||
// Not convertible: consumption plus generation, and nothing linked at all.
|
||||
var mixed = await MeterAsync(type, MeterMode.Virtual);
|
||||
await LinksAsync((house, mixed), (solar1, mixed));
|
||||
var lonely = await MeterAsync(type, MeterMode.Virtual);
|
||||
|
||||
// Never touched: an explicit expression (the authority, whatever links say) and a malformed blob.
|
||||
var definedMeta = VirtualDefinitionJson.Write("{}", new VirtualDefinition($"m{solar1}", QuantityKind.Generation, "kWh", VirtualCostRule.SourceCosts));
|
||||
var defined = await MeterAsync(type, MeterMode.Virtual, meta: definedMeta);
|
||||
var malformed = await MeterAsync(type, MeterMode.Virtual, meta: """{"expression":5}""");
|
||||
await LinksAsync((solar2, defined), (solar1, malformed), (solar2, malformed));
|
||||
var before = await MetasAsync();
|
||||
|
||||
var result = await UpgradeAsync();
|
||||
|
||||
// A before B, as the derivation saw them; nothing else of this test converted.
|
||||
Assert.Equal([a, b], result.Converted.Where(_meters.Contains));
|
||||
Assert.DoesNotContain(result.Failed, _meters.Contains);
|
||||
var unresolved = result.NeedsConfiguration.Where(u => _meters.Contains(u.MeterId)).ToDictionary(u => u.MeterId, u => u.Outcome);
|
||||
Assert.Equal(LegacyDerivationOutcome.MixedKinds, unresolved[mixed]);
|
||||
Assert.Equal(LegacyDerivationOutcome.NoSources, unresolved[lonely]);
|
||||
Assert.Equal(2, unresolved.Count);
|
||||
|
||||
var after = await MetasAsync();
|
||||
var aRead = VirtualDefinitionJson.Read(after[a]);
|
||||
Assert.Equal(VirtualDefinitionReadStatus.Present, aRead.Status);
|
||||
Assert.Equal($"m{solar1} + m{solar2}", aRead.Definition!.Expression);
|
||||
Assert.Equal(QuantityKind.Generation, aRead.Definition.ResultKind);
|
||||
Assert.Equal("kWh", aRead.Definition.ResultUnit);
|
||||
Assert.Equal(VirtualCostRule.None, aRead.Definition.CostRule); // a generation sum is not costed (A-15)
|
||||
Assert.False(aRead.ReferencedIdsStale);
|
||||
using (var doc = JsonDocument.Parse(after[a]))
|
||||
{
|
||||
Assert.Equal("kept", doc.RootElement.GetProperty("note").GetString());
|
||||
}
|
||||
|
||||
var bRead = VirtualDefinitionJson.Read(after[b]);
|
||||
Assert.Equal(new[] { a, solar3 }.Order(), bRead.Definition!.ReferencedMeterIds);
|
||||
Assert.True(bRead.Definition.Formula!.IsPureSum);
|
||||
Assert.Equal(QuantityKind.Generation, bRead.Definition.ResultKind);
|
||||
|
||||
// What cannot be converted, or needs no conversion, is left exactly as it was.
|
||||
foreach (var id in new[] { mixed, lonely, defined, malformed })
|
||||
{
|
||||
Assert.Equal(before[id], after[id]);
|
||||
}
|
||||
|
||||
// The converted meters record the kind their definition now declares.
|
||||
await using (var db = fx.CreateContext())
|
||||
{
|
||||
var state = await db.MeterRollupStates.AsNoTracking().SingleAsync(s => s.MeterId == a);
|
||||
Assert.Equal(QuantityKind.Generation, state.Kind);
|
||||
Assert.Equal("kWh", state.NormalizedUnit);
|
||||
}
|
||||
|
||||
// The reader now evaluates the stored definitions; the links are topology only.
|
||||
var catalog = await Reader().LoadCatalogAsync();
|
||||
Assert.Equal(VirtualMeterStatus.Valid, catalog.Find(a)!.VirtualStatus);
|
||||
Assert.Equal(VirtualMeterStatus.Valid, catalog.Find(b)!.VirtualStatus);
|
||||
Assert.Equal(VirtualMeterStatus.NeedsConfiguration, catalog.Find(mixed)!.VirtualStatus);
|
||||
Assert.Equal(VirtualMeterStatus.Malformed, catalog.Find(malformed)!.VirtualStatus);
|
||||
|
||||
// Idempotent: a second run converts nothing and changes nothing.
|
||||
var rerun = await UpgradeAsync();
|
||||
Assert.DoesNotContain(rerun.Converted, _meters.Contains);
|
||||
Assert.Equal(after, await MetasAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_converted_calculation_no_longer_follows_its_links()
|
||||
{
|
||||
var type = await TypeAsync();
|
||||
var solar1 = await MeterAsync(type, MeterMode.GenerationCounter);
|
||||
var solar2 = await MeterAsync(type, MeterMode.GenerationCounter);
|
||||
var sum = await MeterAsync(type, MeterMode.Virtual);
|
||||
await LinksAsync((solar1, sum), (solar2, sum));
|
||||
|
||||
await UpgradeAsync();
|
||||
|
||||
// Brief §5.2: after the conversion, editing the flow links must not secretly change the calculation.
|
||||
await using (var db = fx.CreateContext())
|
||||
{
|
||||
await db.MeterLinks.Where(l => l.FromMeterId == solar2 && l.ToMeterId == sum).ExecuteDeleteAsync();
|
||||
}
|
||||
|
||||
var catalog = await Reader().LoadCatalogAsync();
|
||||
Assert.Equal(VirtualMeterStatus.Valid, catalog.Find(sum)!.VirtualStatus);
|
||||
Assert.Equal(new[] { solar1, solar2 }.Order(), catalog.Find(sum)!.Formula!.MeterIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Dependents_name_every_virtual_meter_that_reads_a_meter()
|
||||
{
|
||||
var type = await TypeAsync();
|
||||
var p = await MeterAsync(type, MeterMode.GenerationCounter, name: "P");
|
||||
var q = await MeterAsync(type, MeterMode.GenerationCounter, name: "Q");
|
||||
var direct = await VirtualAsync(type, "Direct", $"m{p} + m{q}", QuantityKind.Generation, VirtualCostRule.SourceCosts);
|
||||
var nested = await VirtualAsync(type, "Nested", $"m{direct} + m{q}", QuantityKind.Generation, VirtualCostRule.SourceCosts);
|
||||
var unrelated = await VirtualAsync(type, "Only Q", $"m{q}", QuantityKind.Generation, VirtualCostRule.SourceCosts);
|
||||
|
||||
// An invalid formula still breaks when p goes, and a legacy sum reads p through its link.
|
||||
var invalid = await VirtualAsync(type, "Invalid", $"m{p} * m{q}", QuantityKind.Generation, VirtualCostRule.None);
|
||||
var legacy = await MeterAsync(type, MeterMode.Virtual, name: "Legacy");
|
||||
await LinksAsync((p, legacy));
|
||||
|
||||
var service = new VirtualMeterService(Reader());
|
||||
var ofP = (await service.GetDependentsAsync(p)).ToDictionary(d => d.MeterId);
|
||||
|
||||
Assert.Equal(new[] { direct, nested, invalid, legacy }.Order(), ofP.Keys.Order());
|
||||
Assert.DoesNotContain(unrelated, ofP.Keys);
|
||||
Assert.True(ofP[direct].IsDirect);
|
||||
Assert.Equal([direct, p], ofP[direct].Path);
|
||||
Assert.False(ofP[nested].IsDirect);
|
||||
Assert.Equal([nested, direct, p], ofP[nested].Path);
|
||||
Assert.Equal(VirtualMeterStatus.Invalid, ofP[invalid].Status);
|
||||
Assert.Equal(VirtualMeterStatus.Legacy, ofP[legacy].Status);
|
||||
Assert.Equal("Direct", ofP[direct].Name);
|
||||
|
||||
var ofQ = (await service.GetDependentsAsync(q)).Select(d => d.MeterId).Order();
|
||||
Assert.Equal(new[] { direct, nested, unrelated, invalid }.Order(), ofQ);
|
||||
|
||||
// Nothing reads the outermost sum.
|
||||
Assert.Empty(await service.GetDependentsAsync(nested));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------ helpers
|
||||
|
||||
private AnalysisReader Reader() =>
|
||||
new(fx, Microsoft.Extensions.Options.Options.Create(new MeterVaultOptions { TimeZone = Zone }));
|
||||
|
||||
private async Task<VirtualDefinitionUpgradeResult> UpgradeAsync()
|
||||
{
|
||||
await using var db = fx.CreateContext();
|
||||
var options = Microsoft.Extensions.Options.Options.Create(new MeterVaultOptions { TimeZone = Zone });
|
||||
var normalization = new NormalizationService(db, NormalizationEngine.CreateDefault(), options);
|
||||
return await new VirtualDefinitionUpgrade(db, normalization, NullLogger<VirtualDefinitionUpgrade>.Instance).RunAsync();
|
||||
}
|
||||
|
||||
private async Task<Dictionary<int, string>> MetasAsync()
|
||||
{
|
||||
await using var db = fx.CreateContext();
|
||||
var ids = _meters.ToArray();
|
||||
return await db.Meters.AsNoTracking().Where(m => ids.Contains(m.Id)).ToDictionaryAsync(m => m.Id, m => m.Meta);
|
||||
}
|
||||
|
||||
private async Task<short> TypeAsync()
|
||||
{
|
||||
await using var db = fx.CreateContext();
|
||||
var type = new EnergyType { Key = $"virtual-{Guid.NewGuid():N}", DisplayName = "Virtual test", BaseUnit = "kWh", DefaultMode = MeterMode.CumulativeCounter };
|
||||
db.EnergyTypes.Add(type);
|
||||
await db.SaveChangesAsync();
|
||||
_types.Add(type.Id);
|
||||
return type.Id;
|
||||
}
|
||||
|
||||
private async Task<int> MeterAsync(short type, MeterMode mode, string meta = "{}", string? name = null)
|
||||
{
|
||||
await using var db = fx.CreateContext();
|
||||
var meter = new Meter { Name = name ?? $"virtual-{Guid.NewGuid():N}", EnergyTypeId = type, Mode = mode, Unit = "kWh", Meta = meta };
|
||||
db.Meters.Add(meter);
|
||||
await db.SaveChangesAsync();
|
||||
_meters.Add(meter.Id);
|
||||
return meter.Id;
|
||||
}
|
||||
|
||||
private Task<int> VirtualAsync(short type, string name, string expression, QuantityKind kind, VirtualCostRule rule) =>
|
||||
MeterAsync(type, MeterMode.Virtual, VirtualDefinitionJson.Write("{}", new VirtualDefinition(expression, kind, "kWh", rule)), name);
|
||||
|
||||
private async Task LinksAsync(params (int From, int To)[] links)
|
||||
{
|
||||
await using var db = fx.CreateContext();
|
||||
db.MeterLinks.AddRange(links.Select(l => new MeterLink { FromMeterId = l.From, ToMeterId = l.To }));
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user