Analysis: one selected period, one set of numbers, on every page
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:
Florian Schmidt
2026-09-20 10:29:13 +02:00
parent c0f52dbb6f
commit 8940ef25c3
384 changed files with 82753 additions and 4518 deletions
@@ -0,0 +1,226 @@
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
using Npgsql;
namespace MeterVault.Integration.Tests.Performance;
/// <summary>The timings of one scenario: every measured run, and the SQL a separate counted run sent.</summary>
internal sealed record ScenarioTiming(
string Id,
string Description,
IReadOnlyList<double> Milliseconds,
int Commands,
IReadOnlyList<string> Statements,
string Shape)
{
/// <summary>The summed duration of the SQL commands in the counted run (execution to reader close).</summary>
public double SqlMilliseconds { get; init; }
public double Median => Percentile(0.5);
public double P95 => Percentile(0.95);
public double Min => Milliseconds.Min();
public double Max => Milliseconds.Max();
/// <summary>Nearest-rank percentile; the median of an even count is the mean of the middle two.</summary>
private double Percentile(double p)
{
var sorted = Milliseconds.Order().ToArray();
if (p == 0.5 && sorted.Length % 2 == 0)
{
return (sorted[(sorted.Length / 2) - 1] + sorted[sorted.Length / 2]) / 2;
}
var rank = (int)Math.Ceiling(p * sorted.Length);
return sorted[Math.Clamp(rank - 1, 0, sorted.Length - 1)];
}
}
/// <summary>Markdown building and the facts about the machine and database a result was taken on.</summary>
internal static class PerfReport
{
public static string F(double value, int digits = 1) => value.ToString("F" + digits.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);
public static string N(long value) => value.ToString("N0", CultureInfo.InvariantCulture);
/// <summary>A markdown table cell: pipes escaped, newlines flattened.</summary>
public static string Cell(string text) => text.Replace("|", "\\|", StringComparison.Ordinal).Replace('\n', ' ');
/// <summary>CPU model, cores, memory, OS and runtime.</summary>
public static IReadOnlyList<(string Name, string Value)> Machine()
{
var memory = GC.GetGCMemoryInfo().TotalAvailableMemoryBytes;
return
[
("CPU", CpuModel()),
("Logical processors", Environment.ProcessorCount.ToString(CultureInfo.InvariantCulture)),
("Memory visible to .NET", F(memory / 1024d / 1024 / 1024) + " GiB"),
("OS", RuntimeInformation.OSDescription),
(".NET", RuntimeInformation.FrameworkDescription),
("Docker", Docker()),
];
}
/// <summary>The share of all logical processors busy over <paramref name="window"/> (Windows only): how loaded the machine was.</summary>
public static async Task<string> HostLoadAsync(TimeSpan window)
{
if (!OperatingSystem.IsWindows() || !NativeMethods.GetSystemTimes(out var idle1, out var kernel1, out var user1))
{
return "n/a";
}
await Task.Delay(window);
if (!NativeMethods.GetSystemTimes(out var idle2, out var kernel2, out var user2))
{
return "n/a";
}
// Kernel time includes idle time.
var total = (kernel2 - kernel1) + (user2 - user1);
var busy = total - (idle2 - idle1);
return total <= 0 ? "n/a" : F(100d * busy / total, 0) + " % of all logical processors busy (sampled over " + F(window.TotalSeconds, 0) + " s)";
}
/// <summary>PostgreSQL/Timescale versions and the settings that shape plans.</summary>
public static async Task<IReadOnlyList<(string Name, string Value)>> DatabaseAsync(NpgsqlConnection connection)
{
var rows = new List<(string, string)>
{
("PostgreSQL", (await PerfDatabase.ScalarAsync(connection, "SHOW server_version"))?.ToString() ?? "?"),
("TimescaleDB", (await PerfDatabase.ScalarAsync(connection, "SELECT extversion FROM pg_extension WHERE extname = 'timescaledb'"))?.ToString() ?? "?"),
};
await using var command = new NpgsqlCommand(
"""
SELECT name, setting || coalesce(' ' || unit, '') FROM pg_settings
WHERE name IN ('shared_buffers', 'work_mem', 'effective_cache_size', 'max_parallel_workers_per_gather', 'max_worker_processes',
'jit', 'random_page_cost', 'timescaledb.max_tuples_decompressed_per_dml_transaction')
ORDER BY name
""", connection);
await using var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
rows.Add((reader.GetString(0), reader.GetString(1)));
}
return rows;
}
/// <summary>Row counts and on-disk sizes of the tables the pipeline and the reader use.</summary>
public static async Task<IReadOnlyList<(string Table, long Rows, string Size, string Note)>> TablesAsync(NpgsqlConnection connection)
{
var result = new List<(string, long, string, string)>();
(string Table, bool Hypertable)[] tables =
[
("reading", true), ("consumption", true), ("consumption_rollup", false), ("consumption_rollup_month", false),
("meter_coverage", false), ("meter_rollup_state", false), ("meter_event", false), ("meter", false), ("tariff", false),
("manual_cost", false), ("meter_link", false), ("cost_category_member", false),
];
foreach (var (table, hypertable) in tables)
{
var rows = Convert.ToInt64(await PerfDatabase.ScalarAsync(connection, $"SELECT count(*) FROM {table}"), CultureInfo.InvariantCulture);
var bytes = Convert.ToInt64(
await PerfDatabase.ScalarAsync(connection, hypertable ? $"SELECT hypertable_size('{table}')" : $"SELECT pg_total_relation_size('{table}')"),
CultureInfo.InvariantCulture);
var note = string.Empty;
if (hypertable)
{
note = (await PerfDatabase.ScalarAsync(
connection,
$"SELECT count(*) || ' chunks, ' || count(*) FILTER (WHERE is_compressed) || ' compressed' FROM timescaledb_information.chunks WHERE hypertable_name = '{table}'"))
?.ToString() ?? string.Empty;
}
result.Add((table, rows, F(bytes / 1024d / 1024) + " MiB", note));
}
return result;
}
private static string CpuModel()
{
try
{
if (OperatingSystem.IsWindows())
{
using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");
if (key?.GetValue("ProcessorNameString") is string name)
{
return name.Trim();
}
}
else if (File.Exists("/proc/cpuinfo"))
{
var line = File.ReadLines("/proc/cpuinfo").FirstOrDefault(l => l.StartsWith("model name", StringComparison.Ordinal));
if (line is not null)
{
return line[(line.IndexOf(':', StringComparison.Ordinal) + 1)..].Trim();
}
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or System.Security.SecurityException)
{
return "unknown (" + ex.Message + ")";
}
return "unknown";
}
private static string Docker()
{
try
{
using var process = Process.Start(new ProcessStartInfo("docker", "info --format \"{{.ServerVersion}}|{{.OperatingSystem}}|{{.NCPU}} CPUs|{{.MemTotal}}\"")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
});
if (process is null)
{
return "unknown";
}
var output = process.StandardOutput.ReadToEnd().Trim();
process.WaitForExit(10_000);
var parts = output.Split('|');
if (parts.Length == 4 && long.TryParse(parts[3], CultureInfo.InvariantCulture, out var bytes))
{
parts[0] = "Engine " + parts[0];
parts[3] = F(bytes / 1024d / 1024 / 1024) + " GiB";
return string.Join(", ", parts);
}
return output.Length > 0 ? output : "unknown";
}
catch (Exception ex) when (ex is System.ComponentModel.Win32Exception or InvalidOperationException)
{
return "unknown (" + ex.Message + ")";
}
}
/// <summary>Appends a markdown table.</summary>
public static void Table(StringBuilder md, IReadOnlyList<string> header, IEnumerable<IReadOnlyList<string>> rows)
{
md.Append("| ").AppendJoin(" | ", header).AppendLine(" |");
md.Append('|').AppendJoin("|", header.Select(_ => "---")).AppendLine("|");
foreach (var row in rows)
{
md.Append("| ").AppendJoin(" | ", row.Select(Cell)).AppendLine(" |");
}
md.AppendLine();
}
private static class NativeMethods
{
[DllImport("kernel32.dll", SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetSystemTimes(out long idleTime, out long kernelTime, out long userTime);
}
}