feat(kpihistory): add KpiRollupHourly table + EF config + migration (plan #22 T1)
New KpiRollupHourly entity is the hourly pre-aggregated series backbone folded from raw KpiSample rows, letting long-range (30d/90d) trend reads scan ~60x fewer rows. Same (Source, Metric, Scope, ScopeKey) series key as KpiSample plus HourStartUtc bucket, folded Value, and MinValue/MaxValue/SampleCount fidelity columns. UNIQUE IX_KpiRollupHourly_Series (upsert key + range-read cover, with the default [ScopeKey] IS NOT NULL filter suppressed so Global rows participate) and IX_KpiRollupHourly_Hour for purge. Non-partitioned, [PRIMARY], no DB-role restriction. Claude-Session: https://claude.ai/code/session_01MtdgwpEeCUn6cUA5f1LMPj
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
namespace ZB.MOM.WW.ScadaBridge.Commons.Entities.Kpi;
|
||||
|
||||
/// <summary>
|
||||
/// An hourly pre-aggregated KPI measurement in the central KPI-history backbone
|
||||
/// ("KPI History & Trends"). One row per (Source, Metric, Scope, ScopeKey)
|
||||
/// per UTC hour, folded from the raw <c>KpiSample</c> rows in that hour — a
|
||||
/// tall / EAV row in the central <c>KpiRollupHourly</c> table that lets long-range
|
||||
/// trend queries read ~60× fewer rows than scanning raw samples.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Persistence-ignorant POCO; the EF Core mapping lives in the Configuration
|
||||
/// Database component. <see cref="Source"/> draws from
|
||||
/// <see cref="ZB.MOM.WW.ScadaBridge.Commons.Types.Kpi.KpiSources"/> and
|
||||
/// <see cref="Scope"/> from
|
||||
/// <see cref="ZB.MOM.WW.ScadaBridge.Commons.Types.Kpi.KpiScopes"/>; <see cref="Metric"/>
|
||||
/// is drawn from each owning source's own metric catalog — the same four-tuple
|
||||
/// series key as <see cref="KpiSample"/>, with identical semantics.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <see cref="HourStartUtc"/> is the hour-truncated UTC bucket start; the tuple
|
||||
/// (<see cref="Source"/>, <see cref="Metric"/>, <see cref="Scope"/>,
|
||||
/// <see cref="ScopeKey"/>, <see cref="HourStartUtc"/>) is the unique upsert key.
|
||||
/// <see cref="Value"/> is the folded aggregate — a per-hour sum for rate metrics
|
||||
/// or a per-hour last-value for gauge metrics (the aggregation intent lives with
|
||||
/// the metric catalog). <see cref="MinValue"/>, <see cref="MaxValue"/> and
|
||||
/// <see cref="SampleCount"/> preserve the fold's fidelity and unblock future
|
||||
/// min/max/avg charting.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class KpiRollupHourly
|
||||
{
|
||||
/// <summary>Surrogate identity key assigned by the store.</summary>
|
||||
public long Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Owning component / source — a value from
|
||||
/// <see cref="ZB.MOM.WW.ScadaBridge.Commons.Types.Kpi.KpiSources"/>.
|
||||
/// </summary>
|
||||
public required string Source { get; set; }
|
||||
|
||||
/// <summary>Metric name, drawn from the owning source's metric catalog.</summary>
|
||||
public required string Metric { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Scope discriminator — a value from
|
||||
/// <see cref="ZB.MOM.WW.ScadaBridge.Commons.Types.Kpi.KpiScopes"/>.
|
||||
/// </summary>
|
||||
public required string Scope { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Scope qualifier — the site id or node name the rollup belongs to;
|
||||
/// <c>null</c> for <see cref="ZB.MOM.WW.ScadaBridge.Commons.Types.Kpi.KpiScopes.Global"/>.
|
||||
/// </summary>
|
||||
public string? ScopeKey { get; set; }
|
||||
|
||||
/// <summary>The hour-truncated UTC start of the bucket this rollup aggregates.</summary>
|
||||
public DateTime HourStartUtc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Folded aggregate for the hour — a sum for rate metrics, a last-value for
|
||||
/// gauge metrics. Counts are exact; ages are expressed in seconds.
|
||||
/// </summary>
|
||||
public double Value { get; set; }
|
||||
|
||||
/// <summary>Minimum raw sample value observed within the hour.</summary>
|
||||
public double MinValue { get; set; }
|
||||
|
||||
/// <summary>Maximum raw sample value observed within the hour.</summary>
|
||||
public double MaxValue { get; set; }
|
||||
|
||||
/// <summary>Number of raw <see cref="KpiSample"/> rows folded into this hour.</summary>
|
||||
public int SampleCount { get; set; }
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Kpi;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Configurations;
|
||||
|
||||
/// <summary>
|
||||
/// Maps the <see cref="KpiRollupHourly"/> POCO to the central
|
||||
/// <c>KpiRollupHourly</c> table — the hourly pre-aggregated row folded from raw
|
||||
/// <c>KpiSample</c> rows by the recorder singleton. Operational history, NOT
|
||||
/// audit, so the table is non-partitioned, standard <c>[PRIMARY]</c> filegroup,
|
||||
/// no DB-role restriction (mirrors <see cref="KpiSampleEntityTypeConfiguration"/>).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Two named indexes back the access paths: the UNIQUE <c>IX_KpiRollupHourly_Series</c>
|
||||
/// on (Source/Metric/Scope/ScopeKey/HourStartUtc) is both the idempotent upsert key
|
||||
/// and the covering index for the bucketed range read, while
|
||||
/// <c>IX_KpiRollupHourly_Hour</c> backs the retention purge (delete by
|
||||
/// <see cref="KpiRollupHourly.HourStartUtc"/>).
|
||||
/// </remarks>
|
||||
public class KpiRollupHourlyEntityTypeConfiguration : IEntityTypeConfiguration<KpiRollupHourly>
|
||||
{
|
||||
/// <summary>
|
||||
/// Configures the EF Core entity type mapping for <see cref="KpiRollupHourly"/>.
|
||||
/// </summary>
|
||||
/// <param name="builder">The entity type builder to configure.</param>
|
||||
public void Configure(EntityTypeBuilder<KpiRollupHourly> builder)
|
||||
{
|
||||
builder.ToTable("KpiRollupHourly");
|
||||
|
||||
// Surrogate long identity assigned by the store.
|
||||
builder.HasKey(r => r.Id);
|
||||
|
||||
// Catalog-bounded ASCII columns — same series key and varchar sizes as
|
||||
// KpiSample (values come from the KpiSources / KpiScopes catalogs and
|
||||
// per-source metric names).
|
||||
builder.Property(r => r.Source).HasMaxLength(64).IsUnicode(false).IsRequired();
|
||||
builder.Property(r => r.Metric).HasMaxLength(64).IsUnicode(false).IsRequired();
|
||||
builder.Property(r => r.Scope).HasMaxLength(16).IsUnicode(false).IsRequired();
|
||||
|
||||
// Scope qualifier — null for the Global scope.
|
||||
builder.Property(r => r.ScopeKey).HasMaxLength(64).IsUnicode(false);
|
||||
|
||||
// Hour-truncated UTC bucket start.
|
||||
builder.Property(r => r.HourStartUtc).IsRequired();
|
||||
|
||||
// Folded aggregate + fidelity columns — double / float and int.
|
||||
builder.Property(r => r.Value);
|
||||
builder.Property(r => r.MinValue);
|
||||
builder.Property(r => r.MaxValue);
|
||||
builder.Property(r => r.SampleCount);
|
||||
|
||||
// Series index — UNIQUE, the idempotent upsert key AND the covering index
|
||||
// for the bucketed range read (filter one series, scan in hour order).
|
||||
// Names locked for migration discoverability. Global-scope rows
|
||||
// (ScopeKey IS NULL) are included and seeked via the IS NULL predicate.
|
||||
builder.HasIndex(r => new { r.Source, r.Metric, r.Scope, r.ScopeKey, r.HourStartUtc })
|
||||
.HasDatabaseName("IX_KpiRollupHourly_Series")
|
||||
.IsUnique()
|
||||
// Suppress EF's default "[ScopeKey] IS NOT NULL" filtered-index — Global-scope
|
||||
// rows (ScopeKey NULL) MUST participate in the uniqueness constraint so the
|
||||
// idempotent per-(series, hour) upsert holds for global rollups too.
|
||||
.HasFilter(null);
|
||||
|
||||
// Hour index — backs the retention purge (delete by hour bucket).
|
||||
builder.HasIndex(r => r.HourStartUtc)
|
||||
.HasDatabaseName("IX_KpiRollupHourly_Hour");
|
||||
}
|
||||
}
|
||||
+2086
File diff suppressed because it is too large
Load Diff
+54
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddKpiRollupHourlyTable : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "KpiRollupHourly",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<long>(type: "bigint", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
Source = table.Column<string>(type: "varchar(64)", unicode: false, maxLength: 64, nullable: false),
|
||||
Metric = table.Column<string>(type: "varchar(64)", unicode: false, maxLength: 64, nullable: false),
|
||||
Scope = table.Column<string>(type: "varchar(16)", unicode: false, maxLength: 16, nullable: false),
|
||||
ScopeKey = table.Column<string>(type: "varchar(64)", unicode: false, maxLength: 64, nullable: true),
|
||||
HourStartUtc = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
Value = table.Column<double>(type: "float", nullable: false),
|
||||
MinValue = table.Column<double>(type: "float", nullable: false),
|
||||
MaxValue = table.Column<double>(type: "float", nullable: false),
|
||||
SampleCount = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_KpiRollupHourly", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_KpiRollupHourly_Hour",
|
||||
table: "KpiRollupHourly",
|
||||
column: "HourStartUtc");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_KpiRollupHourly_Series",
|
||||
table: "KpiRollupHourly",
|
||||
columns: new[] { "Source", "Metric", "Scope", "ScopeKey", "HourStartUtc" },
|
||||
unique: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "KpiRollupHourly");
|
||||
}
|
||||
}
|
||||
}
|
||||
+58
@@ -709,6 +709,64 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
|
||||
b.ToTable("InstanceNativeAlarmSourceOverrides");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Kpi.KpiRollupHourly", b =>
|
||||
{
|
||||
b.Property<long>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bigint");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"));
|
||||
|
||||
b.Property<DateTime>("HourStartUtc")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<double>("MaxValue")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<string>("Metric")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.IsUnicode(false)
|
||||
.HasColumnType("varchar(64)");
|
||||
|
||||
b.Property<double>("MinValue")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<int>("SampleCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("Scope")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.IsUnicode(false)
|
||||
.HasColumnType("varchar(16)");
|
||||
|
||||
b.Property<string>("ScopeKey")
|
||||
.HasMaxLength(64)
|
||||
.IsUnicode(false)
|
||||
.HasColumnType("varchar(64)");
|
||||
|
||||
b.Property<string>("Source")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.IsUnicode(false)
|
||||
.HasColumnType("varchar(64)");
|
||||
|
||||
b.Property<double>("Value")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("HourStartUtc")
|
||||
.HasDatabaseName("IX_KpiRollupHourly_Hour");
|
||||
|
||||
b.HasIndex("Source", "Metric", "Scope", "ScopeKey", "HourStartUtc")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("IX_KpiRollupHourly_Series");
|
||||
|
||||
b.ToTable("KpiRollupHourly", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Kpi.KpiSample", b =>
|
||||
{
|
||||
b.Property<long>("Id")
|
||||
|
||||
@@ -149,6 +149,9 @@ public class ScadaBridgeDbContext : DbContext, IDataProtectionKeyContext
|
||||
/// <summary>Gets the set of KPI samples (central tall/EAV KPI-history backbone).</summary>
|
||||
public DbSet<KpiSample> KpiSamples => Set<KpiSample>();
|
||||
|
||||
/// <summary>Gets the set of hourly KPI rollups (pre-aggregated long-range trend backbone).</summary>
|
||||
public DbSet<KpiRollupHourly> KpiRollupHourly => Set<KpiRollupHourly>();
|
||||
|
||||
// Data Protection Keys (for shared ASP.NET Data Protection across nodes)
|
||||
/// <summary>Gets the set of data protection keys.</summary>
|
||||
public DbSet<DataProtectionKey> DataProtectionKeys => Set<DataProtectionKey>();
|
||||
|
||||
Reference in New Issue
Block a user