3a312ffdd1
v2-ci / unit-tests (push) Has been cancelled
A notifier folder read one event source, its NodeId identifier (a RawPath or an EquipmentId). AVEVA stores a Galaxy object's A&E history under the object's tag_name (Source_Object), so AVEVA's own alarm and event history reached no production folder. Its lower-case property keys were also missed, so every row read Severity 1 with no ConditionName. Sources. A folder's event history now reads its own source plus the Galaxy object of every native condition that fires on it live: those parented under it and those WireAlarmNotifiers wired to it. Live and history use the same membership. The object comes from the condition's Galaxy alarm reference, its raw tag's attributeRef, up to the first '.': - The composer reads it with the driver's own parser (GalaxyAttributeRef, shared with GalaxyDriver), only for a GalaxyMxGateway tag with an alarm (RawTagPlan.NativeAlarmReference, part of plan equality). - The applier binds it after materialising the condition (IOpcUaAddressSpaceSink.BindNativeAlarmHistory, forwarded by the deferred sink). A rebuild or the condition's removal forgets it. Each object is read with the #882 rule: its own events and <Object>.<Attribute> rows, never a prefix sibling. Fan-out. historiangw's ReadEvents filter is a single predicate, so there is one request per source; the wire contract is unchanged. EventSourceFanOut merges the reads as one source: - order is (time, source position, arrival), reversed for a reverse read; ties are never re-sorted by EventId, so the boundary skip stays stable; - duplicates by EventId are dropped and the merge is cut to the cap; - a truncated source bounds the page at the time it stopped. Paging, continuation points (bound to the whole source set), the walked-back reverse read and the tie-cluster over-fetch are unchanged. A folder with one source is read exactly as before. ACL. Every source is read inside the folder's own HistoryRead, after AuthorizeHistoryRead (#826). A denied folder reads nothing and fails closed. Mapping (EventMapper, property names now case-insensitive): - Severity: an AVEVA row's priority goes through GalaxyAlarmPriority, the table live Galaxy alarms use (MxAccessSeverityMapper now delegates to it), so history matches live. Otherwise severity (OtOpcUa's own rows, which AVEVA stores lower-cased), clamped. AVEVA's own 1-4 severity is ignored. The table ranks a higher priority as more severe, which inverts AVEVA's convention: filed as #887. - An AVEVA row whose source_conditionvariable names a native condition that fires on the folder takes that condition's SourceNode and live ConditionName. Otherwise ConditionName is alarmname or the condition variable's attribute. - alarm_id/alarmid map to AlarmId. Tests: - GalaxyEquipmentEventHistoryWireTests (4, real SDK session, production Gateway backend, AVEVA-shaped fake gateway): equipment EqA backed by objects Oven_007 + OvenDoor_07. Covers object and attribute events from both objects, the prefix sibling excluded, cross-object and in-object ties, forward paging at 1-4 a page, reverse whole and paged, SourceNode/ConditionName/Severity, the raw device folder, ACL deny with no backend call plus a granted positive control, and a rebuild forgetting the bindings. - EventSourceFanOutTests (8), EventMapper AVEVA cases (11), GalaxyAttributeRefTests (16), a composer test and an applier ordering test. - Mutation-checked, 17 mutants, all killed. Covered: parent-only membership, no scope, a case-sensitive condition match, no condition rewrite, no truncation bound, EventId tie order, no sort, no cap, no stop at the bound, unreversed source order, no dedupe, no '.' boundary, priority ignored, case-sensitive keys, no applier bind, no deferred forward, and a composer that ignores the driver type. The forget-on-rematerialise guard survives as belt-and-braces: the rebuild clear covers it. - Live, read-only, no WinRM, vd03 HistorianGateway ea9f21c: equipment folder EQ-886-ZipsenLeft (objects DevWinPlatform + Left_001). It read 0 events before and 92 now, equal to the unfiltered read's rows for both objects. 1/7-event pages and reverse whole/5-event pages agree, and 73 rows resolve to their live condition. diff_runs vs the #882 run: 221/226 identical. The 5 that differ are expected: DevWinPlatform Severity 1 -> 750/1000, and #881 capabilities. Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
194 lines
10 KiB
C#
194 lines
10 KiB
C#
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
|
|
|
/// <summary>A native condition an AVEVA history row can resolve to (Gitea #886).</summary>
|
|
/// <param name="AlarmId">The condition's id (its RawPath), from which the server resolves the row's <c>SourceNode</c>.</param>
|
|
/// <param name="ConditionName">The live condition's <c>ConditionName</c>.</param>
|
|
internal sealed record EventHistoryCondition(string AlarmId, string ConditionName);
|
|
|
|
/// <summary>
|
|
/// What one notifier folder's event history reads (Gitea #886): its event sources, and the native conditions that fire
|
|
/// on it live keyed by their Galaxy alarm reference.
|
|
/// </summary>
|
|
internal sealed class EventHistoryScope
|
|
{
|
|
private static readonly IReadOnlyDictionary<string, EventHistoryCondition> NoConditions =
|
|
new Dictionary<string, EventHistoryCondition>();
|
|
|
|
private readonly IReadOnlyDictionary<string, EventHistoryCondition> _conditions;
|
|
|
|
private EventHistoryScope(IReadOnlyList<string> sources, IReadOnlyDictionary<string, EventHistoryCondition> conditions)
|
|
{
|
|
Sources = sources;
|
|
_conditions = conditions;
|
|
// One source keeps its bare name, so a pre-#886 folder's cursor is exactly what it was.
|
|
CursorKey = string.Join('\n', sources);
|
|
}
|
|
|
|
/// <summary>The sources to read: distinct, ordinal-sorted (the folder's own first when it is the only one).</summary>
|
|
public IReadOnlyList<string> Sources { get; }
|
|
|
|
/// <summary>The identity a continuation point is bound to: the whole source set.</summary>
|
|
public string CursorKey { get; }
|
|
|
|
/// <summary>A scope of one source and no Galaxy conditions (every non-Galaxy folder).</summary>
|
|
/// <param name="source">The folder's own source.</param>
|
|
/// <returns>The scope.</returns>
|
|
public static EventHistoryScope Single(string source) => new([source], NoConditions);
|
|
|
|
/// <summary>A folder's own source plus the Galaxy objects behind it.</summary>
|
|
/// <param name="ownSource">The folder's own source.</param>
|
|
/// <param name="objects">The Galaxy objects of the native conditions that fire on it (duplicates allowed).</param>
|
|
/// <param name="conditions">Those conditions by Galaxy alarm reference (case-insensitive).</param>
|
|
/// <returns>The scope.</returns>
|
|
public static EventHistoryScope Create(
|
|
string ownSource, IEnumerable<string>? objects, IReadOnlyDictionary<string, EventHistoryCondition> conditions)
|
|
{
|
|
var sources = new SortedSet<string>(StringComparer.Ordinal) { ownSource };
|
|
foreach (var o in objects ?? []) sources.Add(o);
|
|
return new EventHistoryScope(sources.ToList(), conditions);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Give each row whose <see cref="HistoricalEvent.ConditionReference"/> names one of this folder's native conditions
|
|
/// that condition's id (so its <c>SourceNode</c> resolves) and live <c>ConditionName</c>. Other rows are unchanged.
|
|
/// </summary>
|
|
/// <param name="events">A page of events.</param>
|
|
/// <returns>The page, resolved.</returns>
|
|
public IReadOnlyList<HistoricalEvent> ResolveConditions(IReadOnlyList<HistoricalEvent> events)
|
|
{
|
|
if (_conditions.Count == 0 || events.Count == 0) return events;
|
|
List<HistoricalEvent>? resolved = null;
|
|
for (var i = 0; i < events.Count; i++)
|
|
{
|
|
var e = events[i];
|
|
if (e.ConditionReference is not { } reference || !_conditions.TryGetValue(reference, out var condition)) continue;
|
|
resolved ??= [.. events];
|
|
resolved[i] = e with { AlarmId = condition.AlarmId, ConditionName = condition.ConditionName };
|
|
}
|
|
|
|
return resolved ?? events;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads several event sources as one (Gitea #886): one backend read per source (the historiangw <c>ReadEvents</c>
|
|
/// filter is a single predicate, so there is no multi-source request), merged, de-duplicated by EventId and cut to the
|
|
/// cap.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// <b>Merge order</b> is <c>(EventTimeUtc, source, arrival)</c>: time in the read's direction, then the source's
|
|
/// position in the folder's (ordinal-sorted) source list — reversed for a reverse read — then the order the source
|
|
/// returned its events in. A source's own order within a timestamp is all the paging needs to be stable (the
|
|
/// Gateway orders by event time only), so ties are never re-sorted by EventId here: re-sorting could move an
|
|
/// unread tie in front of one already sent. The reverse form is the forward form backwards, which is what the
|
|
/// server's tie-cluster over-fetch (a forward read, reversed) relies on. A walked-back reverse read (Gateway) sorts
|
|
/// its ties by EventId itself, as before.
|
|
/// </para>
|
|
/// <para>
|
|
/// <b>Truncation stays honest.</b> A source that flags "more" returned a prefix of its own stream, ending at time H.
|
|
/// Its unread events lie at H or later (in the read's direction), and at H they follow what it returned. So the
|
|
/// page keeps every event before the earliest such H, then, at H, each source's events in merge order up to and
|
|
/// including the first source truncated at H — never an event the unread ones would precede. The page is never
|
|
/// empty, because that source returned at least one event at H. When every source is read with the same cap (the
|
|
/// server always passes one) a page cut to that cap already stops there; the bound matters for an uncapped or
|
|
/// default-capped read.
|
|
/// </para>
|
|
/// <para>A single source is passed straight through, so a folder with one source reads exactly as before #886.</para>
|
|
/// </remarks>
|
|
internal static class EventSourceFanOut
|
|
{
|
|
/// <summary>Reads <paramref name="sources"/> over one window and merges them.</summary>
|
|
/// <param name="source">The backend.</param>
|
|
/// <param name="sources">The event sources (at least one).</param>
|
|
/// <param name="startUtc">The window start (the later bound for a reverse read).</param>
|
|
/// <param name="endUtc">The window end.</param>
|
|
/// <param name="maxEvents">The cap, passed to every source; <c><= 0</c> is the backend's default cap.</param>
|
|
/// <param name="reverse">Newest first.</param>
|
|
/// <param name="ct">Cancellation.</param>
|
|
/// <returns>The merged events, and a non-null continuation point when more exist.</returns>
|
|
public static async Task<HistoricalEventsResult> ReadAsync(
|
|
IHistorianDataSource source, IReadOnlyList<string> sources, DateTime startUtc, DateTime endUtc, int maxEvents,
|
|
bool reverse, CancellationToken ct)
|
|
{
|
|
if (sources.Count == 1)
|
|
return await source.ReadEventsAsync(sources[0], startUtc, endUtc, maxEvents, reverse, ct).ConfigureAwait(false);
|
|
|
|
var merged = new List<Merged>();
|
|
var seen = new HashSet<string>(StringComparer.Ordinal);
|
|
var more = false;
|
|
// Per source: the time its truncated read ended at, or null when it returned everything it holds.
|
|
var truncatedAt = new DateTime?[sources.Count];
|
|
for (var s = 0; s < sources.Count; s++)
|
|
{
|
|
// Sequential: the folder's sources share the backend's session pool, and the batch already reads folders in
|
|
// parallel.
|
|
var read = await source.ReadEventsAsync(sources[s], startUtc, endUtc, maxEvents, reverse, ct).ConfigureAwait(false);
|
|
for (var i = 0; i < read.Events.Count; i++)
|
|
{
|
|
var e = read.Events[i];
|
|
if (string.IsNullOrEmpty(e.EventId) || seen.Add(e.EventId)) merged.Add(new Merged(e, s, i));
|
|
}
|
|
|
|
if (read.ContinuationPoint is null) continue;
|
|
more = true;
|
|
// A truncated read with no events gives no bound; the cap below still flags the page incomplete.
|
|
if (read.Events.Count > 0) truncatedAt[s] = read.Events[^1].EventTimeUtc;
|
|
}
|
|
|
|
var order = new MergeOrder(reverse);
|
|
merged.Sort(order);
|
|
|
|
// H: the earliest (in the read's direction) time a truncated source stopped at.
|
|
DateTime? horizon = null;
|
|
foreach (var t in truncatedAt)
|
|
if (t is { } at && (horizon is not { } h || (reverse ? at > h : at < h))) horizon = at;
|
|
|
|
var page = new List<HistoricalEvent>(merged.Count);
|
|
foreach (var m in merged)
|
|
{
|
|
if (horizon is { } h)
|
|
{
|
|
var t = m.Event.EventTimeUtc;
|
|
if (reverse ? t < h : t > h) break;
|
|
// At H, merge order visits whole sources in turn: stop after the first one that was cut there.
|
|
if (t == h && page.Count > 0 && page[^1].EventTimeUtc == h && StoppedAt(merged, page.Count, h)) break;
|
|
}
|
|
|
|
page.Add(m.Event);
|
|
}
|
|
|
|
if (maxEvents > 0 && page.Count > maxEvents)
|
|
{
|
|
page.RemoveRange(maxEvents, page.Count - maxEvents);
|
|
more = true;
|
|
}
|
|
|
|
return new HistoricalEventsResult(page, more ? [0x01] : null);
|
|
|
|
// Whether the event before merged[next] closed a source that was truncated at h (and merged[next] is another source's).
|
|
bool StoppedAt(List<Merged> all, int next, DateTime h)
|
|
{
|
|
var previous = all[next - 1];
|
|
return previous.Source != all[next].Source && truncatedAt[previous.Source] == h;
|
|
}
|
|
}
|
|
|
|
/// <summary>One merged event: the event, its source's position in the list, and its position in that source's read.</summary>
|
|
private readonly record struct Merged(HistoricalEvent Event, int Source, int Arrival);
|
|
|
|
/// <summary>Time in the read's direction, then source position (reversed for a reverse read), then arrival.</summary>
|
|
private sealed class MergeOrder(bool reverse) : IComparer<Merged>
|
|
{
|
|
public int Compare(Merged x, Merged y)
|
|
{
|
|
var c = x.Event.EventTimeUtc.CompareTo(y.Event.EventTimeUtc);
|
|
if (c == 0) c = x.Source.CompareTo(y.Source);
|
|
if (reverse) c = -c;
|
|
return c != 0 ? c : x.Arrival.CompareTo(y.Arrival);
|
|
}
|
|
}
|
|
}
|