fix(history): a folder's event history reads the Galaxy objects behind its native conditions (#886)
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
This commit is contained in:
Joseph Doherty
2026-11-12 11:34:50 -05:00
parent 1e1fad76c4
commit 3a312ffdd1
25 changed files with 1531 additions and 95 deletions
@@ -454,31 +454,7 @@ public sealed class GalaxyDriver
/// on the property name for authoring tolerance; returns null on an absent/blank/non-string value
/// or unparseable blob (the entry is then skipped, never dialled).
/// </summary>
private static string? TryReadAttributeRef(string tagConfigJson)
{
if (string.IsNullOrWhiteSpace(tagConfigJson)) return null;
try
{
using var doc = JsonDocument.Parse(tagConfigJson);
if (doc.RootElement.ValueKind != JsonValueKind.Object) return null;
foreach (var prop in doc.RootElement.EnumerateObject())
{
if (!prop.NameEquals("attributeRef")
&& !string.Equals(prop.Name, "attributeRef", StringComparison.OrdinalIgnoreCase))
{
continue;
}
if (prop.Value.ValueKind != JsonValueKind.String) return null;
var v = prop.Value.GetString();
return string.IsNullOrWhiteSpace(v) ? null : v;
}
}
catch (JsonException)
{
// Malformed blob — treat as un-dialable; BuildRawTagTables logs the skip.
}
return null;
}
private static string? TryReadAttributeRef(string tagConfigJson) => GalaxyAttributeRef.TryRead(tagConfigJson);
/// <summary>RawPath → Galaxy attributeRef for MXAccess dialling; identity on a miss.</summary>
private string ToAttributeRef(string rawPath) =>
@@ -24,13 +24,13 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
internal static class MxAccessSeverityMapper
{
/// <summary>OPC UA Part 9 numeric severity for the Low bucket (0-249 MxAccess).</summary>
public const int OpcUaSeverityLow = 250;
public const int OpcUaSeverityLow = GalaxyAlarmPriority.OpcUaSeverityLow;
/// <summary>OPC UA Part 9 numeric severity for the Medium bucket (250-499 MxAccess).</summary>
public const int OpcUaSeverityMedium = 500;
public const int OpcUaSeverityMedium = GalaxyAlarmPriority.OpcUaSeverityMedium;
/// <summary>OPC UA Part 9 numeric severity for the High bucket (500-749 MxAccess).</summary>
public const int OpcUaSeverityHigh = 750;
public const int OpcUaSeverityHigh = GalaxyAlarmPriority.OpcUaSeverityHigh;
/// <summary>OPC UA Part 9 numeric severity for the Critical bucket (750+ MxAccess).</summary>
public const int OpcUaSeverityCritical = 1000;
public const int OpcUaSeverityCritical = GalaxyAlarmPriority.OpcUaSeverityCritical;
/// <summary>
/// Translate a raw MXAccess severity into the four-bucket
@@ -38,20 +38,6 @@ internal static class MxAccessSeverityMapper
/// </summary>
/// <param name="rawMxAccessSeverity">The raw MXAccess severity value (0-999 range, clamped if out of range).</param>
/// <returns>The mapped four-bucket <see cref="AlarmSeverity"/> and its OPC UA Part 9 numeric severity.</returns>
public static (AlarmSeverity Bucket, int OpcUaSeverity) Map(int rawMxAccessSeverity)
{
if (rawMxAccessSeverity < 250)
{
return (AlarmSeverity.Low, OpcUaSeverityLow);
}
if (rawMxAccessSeverity < 500)
{
return (AlarmSeverity.Medium, OpcUaSeverityMedium);
}
if (rawMxAccessSeverity < 750)
{
return (AlarmSeverity.High, OpcUaSeverityHigh);
}
return (AlarmSeverity.Critical, OpcUaSeverityCritical);
}
// One table for live and history (Gitea #886): GalaxyAlarmPriority, which AVEVA history rows map their priority through.
public static (AlarmSeverity Bucket, int OpcUaSeverity) Map(int rawMxAccessSeverity) => GalaxyAlarmPriority.Map(rawMxAccessSeverity);
}
@@ -8,6 +8,20 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Mapping;
/// Maps a gateway wire event (<see cref="HistorianEvent"/>) onto the driver-agnostic
/// <see cref="HistoricalEvent"/> consumed by the Server's HistoryReadEvents path.
/// </summary>
/// <remarks>
/// <para>
/// Property names are matched case-insensitively (Gitea #886). AVEVA stores every event property under a lower-case
/// key: its own A&amp;E rows carry <c>priority</c>, <c>alarm_id</c>, <c>source_conditionvariable</c>, and OtOpcUa's own
/// events, written as <c>AlarmName</c> / <c>AlarmId</c> / <c>Severity</c> / <c>Message</c>, come back as
/// <c>alarmname</c> / <c>alarmid</c> / <c>severity</c> / <c>message</c>.
/// </para>
/// <para>
/// Severity: an AVEVA row's <c>priority</c> maps through <see cref="GalaxyAlarmPriority"/>, the table live Galaxy alarms
/// use, so a row and its live event agree. AVEVA's own <c>severity</c> on such a row (1–4, 0 for a system event) is not on
/// the OPC UA scale and is ignored. A row with no <c>priority</c> (OtOpcUa's own) takes <c>severity</c>, clamped to
/// 1–1000.
/// </para>
/// </remarks>
internal static class EventMapper
{
/// <summary>OPC UA severity range (Part 9): 1 (lowest) … 1000 (highest).</summary>
@@ -19,13 +33,16 @@ internal static class EventMapper
/// <returns>The driver-agnostic historical event.</returns>
public static HistoricalEvent ToHistoricalEvent(HistorianEvent historianEvent)
{
var properties = historianEvent.Properties;
// Message: prefer the "Message" property, else fall back to the event Type (best-effort
// render); never null-crash on a missing property.
string? message;
if (historianEvent.Properties.TryGetValue("Message", out var m) && !string.IsNullOrEmpty(m))
message = m;
else
message = string.IsNullOrEmpty(historianEvent.Type) ? null : historianEvent.Type;
var message = Property(properties, "Message")
?? (string.IsNullOrEmpty(historianEvent.Type) ? null : historianEvent.Type);
// AVEVA's reference to the alarm condition (tag_name.AlarmAttribute); the server resolves it to the native
// condition that dials it, when one fires on the folder being read.
var conditionReference = Property(properties, "source_conditionvariable");
return new HistoricalEvent(
EventId: historianEvent.Id,
@@ -33,15 +50,32 @@ internal static class EventMapper
EventTimeUtc: historianEvent.EventTime?.ToDateTime() ?? default, // Utc kind
ReceivedTimeUtc: historianEvent.ReceivedTime?.ToDateTime() ?? default, // Utc kind
Message: message,
Severity: ParseSeverity(historianEvent.Properties),
Severity: ParseSeverity(properties),
// What GatewayAlarmHistorianWriter sent (AlarmEventMapper): Type = AlarmTypeName, AlarmName / AlarmId properties.
EventTypeName: string.IsNullOrEmpty(historianEvent.Type) ? null : historianEvent.Type,
ConditionName: Property(historianEvent.Properties, "AlarmName"),
AlarmId: Property(historianEvent.Properties, "AlarmId"));
ConditionName: Property(properties, "AlarmName") ?? AttributeOf(conditionReference),
AlarmId: Property(properties, "AlarmId") ?? Property(properties, "alarm_id"),
ConditionReference: conditionReference);
}
private static string? Property(IDictionary<string, string> properties, string name) =>
properties.TryGetValue(name, out var v) && !string.IsNullOrEmpty(v) ? v : null;
/// <summary>A property by name, exact first, else case-insensitively; null when absent or empty.</summary>
private static string? Property(IDictionary<string, string> properties, string name)
{
if (properties.TryGetValue(name, out var exact)) return string.IsNullOrEmpty(exact) ? null : exact;
foreach (var (key, value) in properties)
if (string.Equals(key, name, StringComparison.OrdinalIgnoreCase))
return string.IsNullOrEmpty(value) ? null : value;
return null;
}
/// <summary>The attribute part of a Galaxy reference (<c>Oven_007.TempHiAlarm</c> ⇒ <c>TempHiAlarm</c>), or the whole
/// reference when it has no object prefix.</summary>
private static string? AttributeOf(string? reference)
{
if (reference is null) return null;
var dot = reference.IndexOf('.');
return dot > 0 && dot < reference.Length - 1 ? reference[(dot + 1)..] : reference;
}
/// <summary>Maps a batch of gateway events to historical events, in order.</summary>
/// <param name="events">The gateway wire events.</param>
@@ -55,20 +89,20 @@ internal static class EventMapper
}
/// <summary>
/// Parses an OPC UA severity from the "Severity" property (else "Priority"), clamped to
/// <c>[1, 1000]</c>. Missing or unparseable values default to the minimum severity (1).
/// The OPC UA severity: an AVEVA row's <c>priority</c> through the live Galaxy table, else the <c>Severity</c> property
/// clamped to <c>[1, 1000]</c>. Missing or unparseable values default to the minimum severity (1).
/// </summary>
private static ushort ParseSeverity(IDictionary<string, string> properties)
{
string? raw = null;
if (properties.TryGetValue("Severity", out var severity))
raw = severity;
else if (properties.TryGetValue("Priority", out var priority))
raw = priority;
if (TryParseInt(Property(properties, "Priority"), out var priority))
return (ushort)GalaxyAlarmPriority.Map(priority).OpcUaSeverity;
if (int.TryParse(raw, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
if (TryParseInt(Property(properties, "Severity"), out var value))
return (ushort)Math.Clamp(value, MinSeverity, MaxSeverity);
return MinSeverity;
}
private static bool TryParseInt(string? raw, out int value) =>
int.TryParse(raw, NumberStyles.Integer, CultureInfo.InvariantCulture, out value);
}