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.
282 lines
12 KiB
C#
282 lines
12 KiB
C#
using MeterVault.Core.Analysis.Quantities;
|
|
using MeterVault.Core.Domain;
|
|
|
|
namespace MeterVault.Core.Tests.Analysis;
|
|
|
|
public sealed class MeterRoleRulesTests
|
|
{
|
|
private const short Electricity = 1;
|
|
private const short Water = 2;
|
|
|
|
private static Meter MeterOf(
|
|
int id, string name, short energyTypeId, MeterMode mode, string? role = null, DateOnly? retiredAt = null) => new()
|
|
{
|
|
Id = id,
|
|
Name = name,
|
|
EnergyTypeId = energyTypeId,
|
|
Mode = mode,
|
|
Unit = "kWh",
|
|
Meta = MeterMeta.SetRole("{}", role),
|
|
RetiredAt = retiredAt,
|
|
};
|
|
|
|
[Theory]
|
|
[InlineData("total_load", MeterRole.TotalLoad)]
|
|
[InlineData("grid_import", MeterRole.GridImport)]
|
|
[InlineData("grid_export", MeterRole.GridExport)]
|
|
[InlineData(" GRID_IMPORT ", MeterRole.GridImport)]
|
|
public void A_stored_token_parses_to_its_role(string token, MeterRole role)
|
|
{
|
|
Assert.True(MeterRoleRules.TryParse(token, out var parsed));
|
|
Assert.Equal(role, parsed);
|
|
Assert.Equal(role, MeterRoleRules.Parse(token));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
[InlineData("pv")]
|
|
[InlineData("grid-import")]
|
|
public void Anything_else_is_no_role(string? token)
|
|
{
|
|
Assert.False(MeterRoleRules.TryParse(token, out _));
|
|
Assert.Null(MeterRoleRules.Parse(token));
|
|
}
|
|
|
|
[Fact]
|
|
public void Every_role_round_trips_through_its_stored_token()
|
|
{
|
|
foreach (var role in MeterRoleRules.All)
|
|
{
|
|
Assert.Equal(role, MeterRoleRules.Parse(MeterRoleRules.Token(role)));
|
|
}
|
|
|
|
Assert.Equal(MeterRoles.TotalLoad, MeterRoleRules.Token(MeterRole.TotalLoad));
|
|
Assert.Equal(MeterRoles.GridImport, MeterRoleRules.Token(MeterRole.GridImport));
|
|
Assert.Equal(MeterRoles.GridExport, MeterRoleRules.Token(MeterRole.GridExport));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(MeterMode.CumulativeCounter)]
|
|
[InlineData(MeterMode.DirectDelta)]
|
|
[InlineData(MeterMode.InstantRate)]
|
|
public void Meters_that_measure_a_flow_may_hold_every_role(MeterMode mode)
|
|
{
|
|
Assert.Equal(MeterRoleRules.All, MeterRoleRules.AllowedFor(mode));
|
|
Assert.All(MeterRoleRules.All, role => Assert.True(MeterRoleRules.IsAllowed(role, mode)));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(MeterMode.GenerationCounter)]
|
|
[InlineData(MeterMode.RuntimeCounter)]
|
|
[InlineData(MeterMode.ConsumableBalance)]
|
|
[InlineData(MeterMode.Virtual)]
|
|
public void Generation_runtime_tank_and_virtual_meters_hold_no_role(MeterMode mode)
|
|
{
|
|
Assert.Empty(MeterRoleRules.AllowedFor(mode));
|
|
Assert.All(MeterRoleRules.All, role => Assert.False(MeterRoleRules.IsAllowed(role, mode)));
|
|
}
|
|
|
|
[Fact]
|
|
public void The_effective_role_ignores_a_token_the_mode_cannot_hold()
|
|
{
|
|
Assert.Equal(MeterRole.GridImport, MeterRoleRules.Effective(MeterOf(1, "Netz", Electricity, MeterMode.CumulativeCounter, "grid_import")));
|
|
Assert.Null(MeterRoleRules.Effective(MeterOf(2, "Solar", Electricity, MeterMode.GenerationCounter, "grid_export")));
|
|
Assert.Null(MeterRoleRules.Effective(MeterOf(3, "Summe Solar", Electricity, MeterMode.Virtual, "total_load")));
|
|
Assert.Null(MeterRoleRules.Effective(MeterOf(4, "Auto", Electricity, MeterMode.CumulativeCounter)));
|
|
}
|
|
|
|
[Fact]
|
|
public void Assigning_a_held_role_names_the_meter_it_moves_from()
|
|
{
|
|
// The seeded installation: Haus holds total_load, Netz holds grid_import.
|
|
var meters = new[]
|
|
{
|
|
MeterOf(1, "Zähler Haus", Electricity, MeterMode.CumulativeCounter, MeterRoles.TotalLoad),
|
|
MeterOf(2, "Zähler Netz", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridImport),
|
|
MeterOf(3, "Zähler Auto", Electricity, MeterMode.CumulativeCounter),
|
|
};
|
|
|
|
var holder = MeterRoleRules.CurrentHolder(meters, MeterRole.TotalLoad, Electricity, meterId: 3);
|
|
|
|
Assert.NotNull(holder);
|
|
Assert.Equal("Zähler Haus", holder.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_free_role_has_no_holder()
|
|
{
|
|
var meters = new[]
|
|
{
|
|
MeterOf(1, "Zähler Haus", Electricity, MeterMode.CumulativeCounter, MeterRoles.TotalLoad),
|
|
MeterOf(2, "Zähler Netz", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridImport),
|
|
};
|
|
|
|
Assert.Null(MeterRoleRules.CurrentHolder(meters, MeterRole.GridExport, Electricity, meterId: 2));
|
|
}
|
|
|
|
[Fact]
|
|
public void Re_saving_the_holder_does_not_move_the_role_from_itself()
|
|
{
|
|
var meters = new[] { MeterOf(1, "Zähler Haus", Electricity, MeterMode.CumulativeCounter, MeterRoles.TotalLoad) };
|
|
|
|
Assert.Null(MeterRoleRules.CurrentHolder(meters, MeterRole.TotalLoad, Electricity, meterId: 1));
|
|
}
|
|
|
|
[Fact]
|
|
public void Roles_are_unique_per_energy_type_not_globally()
|
|
{
|
|
var meters = new[]
|
|
{
|
|
MeterOf(1, "Zähler Netz", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridImport),
|
|
MeterOf(2, "Hauswasser", Water, MeterMode.CumulativeCounter),
|
|
};
|
|
|
|
Assert.Null(MeterRoleRules.CurrentHolder(meters, MeterRole.GridImport, Water, meterId: 2));
|
|
}
|
|
|
|
[Fact]
|
|
public void Legacy_duplicates_and_invalid_leftovers_all_give_the_role_up()
|
|
{
|
|
var meters = new[]
|
|
{
|
|
MeterOf(9, "Einspeisung alt", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridExport),
|
|
MeterOf(4, "Solar 1", Electricity, MeterMode.GenerationCounter, MeterRoles.GridExport),
|
|
MeterOf(7, "Einspeisung Garage", Electricity, MeterMode.DirectDelta, MeterRoles.GridExport),
|
|
MeterOf(5, "Einspeisung neu", Electricity, MeterMode.CumulativeCounter),
|
|
MeterOf(6, "Wasser", Water, MeterMode.CumulativeCounter, MeterRoles.GridExport),
|
|
};
|
|
|
|
var holders = MeterRoleRules.Holders(meters, MeterRole.GridExport, Electricity, exceptMeterId: 5);
|
|
|
|
// Meters that really play the role first, then the leftover a generation counter cannot hold.
|
|
Assert.Equal([7, 9, 4], holders.Select(m => m.Id));
|
|
}
|
|
|
|
[Fact]
|
|
public void The_role_is_said_to_move_from_the_meter_that_played_it_not_from_an_invalid_leftover()
|
|
{
|
|
// Solar 1 (id 4) still stores a grid_export token it cannot hold; Einspeisung (id 9) is the export meter.
|
|
var meters = new[]
|
|
{
|
|
MeterOf(4, "Solar 1", Electricity, MeterMode.GenerationCounter, MeterRoles.GridExport),
|
|
MeterOf(9, "Einspeisung", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridExport),
|
|
MeterOf(12, "Einspeisung neu", Electricity, MeterMode.CumulativeCounter),
|
|
};
|
|
|
|
var holder = MeterRoleRules.CurrentHolder(meters, MeterRole.GridExport, Electricity, meterId: 12);
|
|
|
|
Assert.NotNull(holder);
|
|
Assert.Equal("Einspeisung", holder.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void An_invalid_leftover_is_named_only_when_no_meter_played_the_role()
|
|
{
|
|
var meters = new[]
|
|
{
|
|
MeterOf(4, "Solar 1", Electricity, MeterMode.GenerationCounter, MeterRoles.GridExport),
|
|
MeterOf(12, "Einspeisung neu", Electricity, MeterMode.CumulativeCounter),
|
|
};
|
|
|
|
Assert.Equal(4, MeterRoleRules.CurrentHolder(meters, MeterRole.GridExport, Electricity, meterId: 12)!.Id);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_retired_meter_keeps_its_role_when_its_replacement_takes_it()
|
|
{
|
|
// A-07: the old grid meter measured the grid import of its own service period.
|
|
var meters = new[]
|
|
{
|
|
MeterOf(2, "Zähler Netz alt", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridImport, new DateOnly(2024, 6, 30)),
|
|
MeterOf(15, "Zähler Netz", Electricity, MeterMode.CumulativeCounter),
|
|
};
|
|
var replacement = MeterOf(15, "Zähler Netz", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridImport);
|
|
|
|
Assert.Empty(MeterRoleRules.Holders(meters, MeterRole.GridImport, Electricity, exceptMeterId: 15));
|
|
Assert.Null(MeterRoleRules.CurrentHolder(meters, MeterRole.GridImport, Electricity, meterId: 15));
|
|
Assert.Empty(MeterRoleRules.Displaced(meters, replacement, MeterRole.GridImport));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_retired_meter_saved_with_a_role_displaces_no_meter_in_service()
|
|
{
|
|
var meters = new[]
|
|
{
|
|
MeterOf(15, "Zähler Netz", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridImport),
|
|
MeterOf(2, "Zähler Netz alt", Electricity, MeterMode.CumulativeCounter),
|
|
};
|
|
var retired = MeterOf(2, "Zähler Netz alt", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridImport, new DateOnly(2024, 6, 30));
|
|
|
|
Assert.Empty(MeterRoleRules.Displaced(meters, retired, MeterRole.GridImport));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_meter_in_service_saved_with_a_role_displaces_every_other_holder_in_service()
|
|
{
|
|
var meters = new[]
|
|
{
|
|
MeterOf(1, "Zähler Haus", Electricity, MeterMode.CumulativeCounter, MeterRoles.TotalLoad),
|
|
MeterOf(3, "Solar 1", Electricity, MeterMode.GenerationCounter, MeterRoles.TotalLoad),
|
|
MeterOf(8, "Haus alt", Electricity, MeterMode.CumulativeCounter, MeterRoles.TotalLoad, new DateOnly(2020, 12, 31)),
|
|
MeterOf(20, "Hauptzähler", Electricity, MeterMode.CumulativeCounter, MeterRoles.TotalLoad),
|
|
};
|
|
var taker = MeterOf(20, "Hauptzähler", Electricity, MeterMode.CumulativeCounter, MeterRoles.TotalLoad);
|
|
|
|
var displaced = MeterRoleRules.Displaced(meters, taker, MeterRole.TotalLoad);
|
|
|
|
Assert.Equal([1, 3], displaced.Select(m => m.Id));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_meter_whose_mode_cannot_hold_the_role_displaces_nobody()
|
|
{
|
|
var meters = new[] { MeterOf(2, "Zähler Netz", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridImport) };
|
|
var generation = MeterOf(4, "Solar 1", Electricity, MeterMode.GenerationCounter, MeterRoles.GridImport);
|
|
|
|
Assert.Empty(MeterRoleRules.Displaced(meters, generation, MeterRole.GridImport));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_meter_counts_as_retired_once_its_lifecycle_end_is_recorded()
|
|
{
|
|
var retired = RoleCandidate.From(MeterOf(2, "Zähler Netz alt", Electricity, MeterMode.CumulativeCounter, MeterRoles.GridImport, new DateOnly(2024, 6, 30)));
|
|
var inService = RoleCandidate.From(MeterOf(15, "Zähler Netz", Electricity, MeterMode.CumulativeCounter, " GRID_IMPORT "));
|
|
|
|
Assert.Equal(new RoleCandidate(2, Electricity, MeterMode.CumulativeCounter, MeterRoles.GridImport, IsRetired: true), retired);
|
|
Assert.False(inService.IsRetired);
|
|
Assert.Equal(MeterRole.GridImport, inService.StoredRole);
|
|
Assert.Equal(MeterRole.GridImport, inService.EffectiveRole);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_caller_decides_what_retired_means_by_building_the_candidates_itself()
|
|
{
|
|
// An analysis input that treats a deactivated meter as retired passes the flag directly.
|
|
var candidates = new[]
|
|
{
|
|
new RoleCandidate(2, 1, MeterMode.CumulativeCounter, MeterRoles.GridImport, IsRetired: true),
|
|
new RoleCandidate(4, 1, MeterMode.GenerationCounter, MeterRoles.GridImport, IsRetired: false),
|
|
new RoleCandidate(15, 1, MeterMode.CumulativeCounter, MeterRoles.GridImport, IsRetired: false),
|
|
new RoleCandidate(16, 2, MeterMode.CumulativeCounter, MeterRoles.GridImport, IsRetired: false),
|
|
};
|
|
|
|
var holders = MeterRoleRules.Holders(candidates, MeterRole.GridImport, energyTypeId: 1);
|
|
|
|
Assert.Equal([15, 4], holders.Select(c => c.MeterId));
|
|
Assert.Equal(15, MeterRoleRules.CurrentHolder(candidates, MeterRole.GridImport, 1, meterId: 30)!.MeterId);
|
|
Assert.Null(holders[1].EffectiveRole);
|
|
}
|
|
|
|
[Fact]
|
|
public void Malformed_meta_holds_no_role()
|
|
{
|
|
var broken = MeterOf(1, "Kaputt", Electricity, MeterMode.CumulativeCounter);
|
|
broken.Meta = "not json";
|
|
|
|
Assert.Empty(MeterRoleRules.Holders([broken], MeterRole.TotalLoad, Electricity));
|
|
Assert.Null(MeterRoleRules.Effective(broken));
|
|
}
|
|
}
|