Two threads that ended up in the same files. One is navigation: a meter
swap that happened today had no click path at all, and most per-meter
tasks were reachable only by knowing which admin page owned them. The
other is attribution: readings on 1 August and 16 September showed six
weeks of water under September and nothing under August.
Meter events from the UI
Swap, counter reset, tank level, delivery and note are recorded through
MeterEventService rather than ad-hoc inserts, so the dialog's verdict and
the saved result come from the same Validate call, and every record or
delete recomputes the meter inside one transaction. MeterEventRules
decides which events a mode offers -- a tank has no register to swap, and
Correction is offered nowhere because nothing reads it.
A swap is stored as the event at T plus a manual reading of the new
register's start value at exactly T. That pairing is the whole trick: the
boundary window is (previousReading, reading], so the old register's tail
books at T and every later reading counts from the new start. Writing the
old final value as the reading at T instead -- the obvious thing -- double
counts the tail and then rejects every reading the new register produces.
Deleting a swap removes that start reading only while it is still the
untouched start value, and only Manual readings can be deleted at all.
Navigation
The meter page is now the hub: primary entry by mode, a "Record event"
menu, and Edit through a shared MeterEditor that also owns tank setup.
Other pages link into it with MeterLinks (/meters/{id}?tab=...&action=...),
whose action is consumed once after the interactive render and dropped
from the address -- the reverse order flashes the dialog and closes it,
because a circuit's first location change dismisses every open dialog.
The app bar gains a "Find a meter" dialog with the same quick entry.
A source that has no usable connector now links to creating (or enabling)
one and comes back to the same source dialog with the connector picked
and everything typed still there; the draft survives in a circuit-scoped
DraftStore, and the way back is a meter id rather than a URL, so the page
cannot be made to redirect anywhere else. The connector list shows which
meters use each connector, import batches list the meters and categories
they wrote to, the meter editor owns the meter's own cost categories, and
the dashboard's empty cost panel names the first missing step instead of
listing every admin page.
Months
A reading is an instant, and what it measures accrued over the time since
the previous one. Booking the whole delta at the closing reading misfiles
it whenever the interval crosses a month boundary, so a plain increase is
now divided at local month boundaries in proportion to elapsed time, each
share stamped inside its month and marked estimated: the meter recorded a
total, not a shape. The parts always sum to the original.
Imported monthly tables are the exception that keeps the golden fixtures
reconciling. "Mai 2026" carries the register at the end of May but is
stamped on the 1st, so the importer -- the only place that still knows
whether the date cell named a month or a day -- flags it MonthLabel, and
the engine reads it as the end of its month. Inferring that from the
stamp instead would catch day-dated rows: a sheet with "01.08.2026" in it
is not a monthly table, and reading it as one moves two thirds of July
into August.
ReadingTimeline is the single ordering built on that: effective time,
then stamp. The register normalizers walk it, and so do the decrease
guard and the event dialog, which is what stops them disagreeing about
which reading is "previous" -- a sheet imported after live readings of the
same month used to count that month twice, and a mid-month reading below
the month's end value was rejected as a drop. A swap detected in a
monthly table applies from the start of that local month, i.e. to the
first reading in it, and a recorded start value never counts above the
reading it lands on.
Every reader buckets in the configured timezone rather than a hardcoded
one, and turns a requested date into that zone's local midnight, so the
divided shares are read back under the months they were stamped in. The
zone id is normalised to its IANA form, because .NET accepts a Windows id
that PostgreSQL will not bucket by, and both are checked at startup.
Stored consumption is derived, so a rule change reaches a meter only at
its next reading -- weeks, for a meter read monthly. NormalizationUpgrade
records the revision and zone the stored series was built with and
rebuilds everything once at startup when either differs, each meter in
its own transaction. A meter that fails is logged, kept in
normalization_pending and retried at the next start: one bad series must
never keep the application down.
What an operator sees once
Existing charts change on the first start after the update: months that
carried a neighbour's use give it back. Rows of earlier imports from
monthly tables are marked as such before anything is recomputed, and if
that marking fails nothing is rebuilt or recorded, so the upgrade simply
runs again next time rather than shifting every imported month by one. A
wizard import whose date format was left on auto-detect is treated as a
monthly table when all of its rows sit on the 1st across at least two
months -- exactly how those rows were attributed before -- and each such
batch is named in the log, because a day-dated sheet always read on the
1st looks identical; revert and re-import it with the day format if that
is what it was.
Tests: 120 unit and 230 integration, including the reference fixtures,
which still reconcile month for month.
71 lines
2.3 KiB
C#
71 lines
2.3 KiB
C#
using MeterVault.Core.Domain;
|
|
|
|
namespace MeterVault.Core.Tests;
|
|
|
|
/// <summary>Terse builders for readings/events/configs in normalizer tests.</summary>
|
|
internal static class TestData
|
|
{
|
|
public static DateTimeOffset Month(int year, int month) =>
|
|
new(new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Utc));
|
|
|
|
/// <summary>
|
|
/// An imported reading. At midnight UTC on the 1st it is a row of a monthly table ("Mai 2026"), flagged
|
|
/// the way the importer flags month names — the shape of the reference data these tests model.
|
|
/// </summary>
|
|
public static Reading Reading(int meterId, DateTimeOffset time, double value) => new()
|
|
{
|
|
MeterId = meterId,
|
|
Time = time,
|
|
Value = value,
|
|
Quality = ReadingQuality.Imported,
|
|
Flags = time.UtcDateTime is { Day: 1, TimeOfDay.Ticks: 0 } ? ReadingFlags.MonthLabel : ReadingFlags.None,
|
|
};
|
|
|
|
/// <summary>An imported reading from a day-dated row ("01.08.2026"): an instant, whatever its clock time.</summary>
|
|
public static Reading DayReading(int meterId, DateTimeOffset time, double value) => new()
|
|
{
|
|
MeterId = meterId,
|
|
Time = time,
|
|
Value = value,
|
|
Quality = ReadingQuality.Imported,
|
|
};
|
|
|
|
public static MeterEvent Swap(int meterId, DateTimeOffset time, double? prevValue = null,
|
|
double? newValue = null, double? amount = null) => new()
|
|
{
|
|
MeterId = meterId,
|
|
Time = time,
|
|
EventType = MeterEventType.MeterSwap,
|
|
PrevValue = prevValue,
|
|
NewValue = newValue,
|
|
Amount = amount,
|
|
};
|
|
|
|
public static MeterEvent Reset(int meterId, DateTimeOffset time, double? newValue = 0, double? prevValue = null) => new()
|
|
{
|
|
MeterId = meterId,
|
|
Time = time,
|
|
EventType = MeterEventType.CounterReset,
|
|
PrevValue = prevValue,
|
|
NewValue = newValue,
|
|
};
|
|
|
|
public static MeterEvent Delivery(int meterId, DateTimeOffset time, double litres) => new()
|
|
{
|
|
MeterId = meterId,
|
|
Time = time,
|
|
EventType = MeterEventType.Delivery,
|
|
Amount = litres,
|
|
Unit = "L",
|
|
};
|
|
|
|
public static MeterEvent TankLevelCm(int meterId, DateTimeOffset time, double cm) => new()
|
|
{
|
|
MeterId = meterId,
|
|
Time = time,
|
|
EventType = MeterEventType.TankLevel,
|
|
Amount = cm,
|
|
Unit = "cm",
|
|
};
|
|
}
|