56bb4855bb
M1: the time-weighted family (incl. Total, formerly a pushed-down SUM) streams raw window rows into the C# evaluator. New shared knob ServerHistorian:MaxTimeWeightedRawRows (default 1,000,000; <=0 unbounded with a Validate warning) flows through HistorianBackendRegistry into both providers. The window query is capped at cap+1 (SQL Server TOP (@maxRows), QuestDB LIMIT $4); one row past the cap throws the new Core.Abstractions HistoryReadLimitExceededException, which the node manager maps to BadTooManyOperations for that node. Not counted as a backend health failure; bound rows are not counted; per-sample aggregates are not capped. M2: reverse Interpolative is anchored at the later time (Part 13): points at End - i*interval, emitted latest first. 10:00->09:00 @1h yields 10:00. L1: Partial bit (0x4, = Opc.Ua.AggregateBits.Partial) on non-Bad results of a truncated last interval (time-weighted family; per-sample family is a follow-up). L2/L3: aligned with the SDK AggregateCalculator.GetTimeBasedStatusCode (decompiled from Opc.Ua.Server 1.5.378.106): TreatUncertainAsBad=false counts Uncertain time (Uncertain samples, held-into-outage, extrapolation) as Good time; data but no non-Bad time => generic Bad (0x80000000); BadNoData only when there is no data at all. Consequence: a live tag's latest interval reads Good (documented). L5: release note no longer claims Total matches the Gateway Integral exactly (AVEVA's divisor is unverified); it notes Total is now evaluated in C# and subject to the cap. Tests: unit (reverse Interpolative x3, Partial, Bad vs BadNoData incl. an open-ended outage, cap helpers, SDK bit oracles); conformance (reverse Interpolative, raw-row cap at/over/0, bounds not counted, health unaffected); node manager BadTooManyOperations; options default + warning. Each new behaviour falsified by mutation (aggregator x9, data-source cap checks on both backends against the real DBs, node-manager catch).
81 lines
3.9 KiB
C#
81 lines
3.9 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Conformance;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Sql.Common;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.SqlServer.IntegrationTests;
|
|
|
|
/// <summary>
|
|
/// The SQL Server historian as a conformance backend: a fresh unique schema per instance (inside the fixture's
|
|
/// throwaway database), seeded by plain INSERTs through <see cref="ReadTestDb"/>.
|
|
/// </summary>
|
|
internal sealed class SqlServerConformanceBackend : IConformanceBackend
|
|
{
|
|
private readonly ReadTestDb _db;
|
|
|
|
private SqlServerConformanceBackend(ReadTestDb db) => _db = db;
|
|
|
|
/// <summary>Creates a backend over a fresh schema; skips the calling test when the gate is closed.</summary>
|
|
public static async Task<IConformanceBackend> CreateAsync(SqlHistorianFixture fx) =>
|
|
new SqlServerConformanceBackend(await ReadTestDb.CreateAsync(fx));
|
|
|
|
/// <inheritdoc/>
|
|
public long ResolutionTicks => 1;
|
|
|
|
/// <inheritdoc/>
|
|
public IHistorianDataSource CreateDataSource(TimeProvider? clock = null) => _db.Source(clock);
|
|
|
|
/// <inheritdoc/>
|
|
public IHistorianDataSource CreateDataSource(int maxTimeWeightedRawRows) => _db.Source(null, maxTimeWeightedRawRows);
|
|
|
|
/// <inheritdoc/>
|
|
public IHistorianValueWriter CreateValueWriter(Func<DateTime>? utcNow = null) =>
|
|
new SqlServerHistorianValueWriter(_db.Conn, _db.Boot, new SqlServerTagCatalog(_db.Conn, _db.Boot), utcNow: utcNow);
|
|
|
|
/// <inheritdoc/>
|
|
public IHistorianDataUpdater CreateUpdater() =>
|
|
new SqlServerHistorianDataUpdater(_db.Conn, new SqlServerTagCatalog(_db.Conn, _db.Boot));
|
|
|
|
/// <inheritdoc/>
|
|
public async Task SeedAsync(string tag, params (DateTime T, HistorianValue V, uint Status)[] samples) =>
|
|
await _db.SeedAsync(tag, samples);
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<IReadOnlyList<(DateTime T, HistorianValue V, uint Status)>> ReadBackAsync(string tag)
|
|
{
|
|
await using var c = await _db.Conn.OpenAsync(CancellationToken.None);
|
|
await using var cmd = _db.Conn.CreateCommand(c,
|
|
$"SELECT s.SourceTimeUtc, s.ValueKind, s.NumValue, s.IntValue, s.StrValue, s.StatusCode FROM {_db.Conn.Q("Sample")} s " +
|
|
$"JOIN {_db.Conn.Q("Tag")} t ON t.TagId = s.TagId WHERE t.Name = @n ORDER BY s.SourceTimeUtc");
|
|
cmd.Parameters.Add(new SqlParameter("@n", System.Data.SqlDbType.NVarChar, -1) { Value = tag });
|
|
var list = new List<(DateTime, HistorianValue, uint)>();
|
|
await using var r = await cmd.ExecuteReaderAsync();
|
|
while (await r.ReadAsync())
|
|
{
|
|
var v = SampleColumnMapper.FromColumns(r.GetByte(1), r.IsDBNull(2) ? null : r.GetDouble(2),
|
|
r.IsDBNull(3) ? null : r.GetInt64(3), r.IsDBNull(4) ? null : r.GetString(4));
|
|
list.Add((SqlHistorianConnection.GetUtc(r, 0), v, SampleColumnMapper.FromDbStatus(r.GetInt32(5))));
|
|
}
|
|
return list;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<bool> TagExistsAsync(string tag) => await ScalarTagAsync("COUNT(*)", tag) is > 0;
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<DriverDataType?> TagDataTypeAsync(string tag) =>
|
|
await ScalarTagAsync("MAX(CONVERT(bigint, DataType))", tag) is { } v ? (DriverDataType)(byte)v : null; // MAX over no row is NULL
|
|
|
|
private async Task<long?> ScalarTagAsync(string expr, string tag)
|
|
{
|
|
await using var c = await _db.Conn.OpenAsync(CancellationToken.None);
|
|
await using var cmd = _db.Conn.CreateCommand(c, $"SELECT {expr} FROM {_db.Conn.Q("Tag")} WHERE Name = @n");
|
|
cmd.Parameters.Add(new SqlParameter("@n", System.Data.SqlDbType.NVarChar, -1) { Value = tag });
|
|
var o = await cmd.ExecuteScalarAsync();
|
|
return o is null or DBNull ? null : Convert.ToInt64(o);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public ValueTask DisposeAsync() => ValueTask.CompletedTask; // the fixture drops the whole database
|
|
}
|