d2818e2ef2
- V3NodeIds.ValidateSegment is the single rule Uns()/UnsChild() enforce; the VirtualTag modal ([UnsLeafName]), UnsTreeService (VirtualTag create/update, SetReferenceOverrideAsync) and the deploy gate (VirtualTagNameInvalid, UnsEffectiveNameInvalid) all call it (C-1, C-4). - Effective name is stated once (EquipmentReferenceMap.EffectiveName): an empty override falls back to the tag name in the gate and the Monaco catalog, as at both compose seams (C-4). - UnsSegment patterns anchored \A..\z, so a trailing newline is rejected (C-7). - VirtualTagScriptNotFound / ScriptedAlarmScriptNotFound deploy-gate rules; ScriptEdit refuses to delete a script used by a VirtualTag, ScriptedAlarm or Calculation tag and names them (C-2). - R-5 backstop: VirtualTagHostActor skips (Error log) a plan whose NodeId cannot be built instead of crashing on every apply; AddressSpaceApplier counts it as a failed node instead of aborting the VirtualTag materialise pass.
73 lines
3.9 KiB
C#
73 lines
3.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
|
|
|
/// <summary>
|
|
/// The script-delete usage check (lmxopcua#978, review C-2). <c>VirtualTag.ScriptId</c>,
|
|
/// <c>ScriptedAlarm.PredicateScriptId</c> and a Calculation tag's <c>TagConfig.scriptId</c> carry no FK, so
|
|
/// a delete used to succeed against a script in use; the next deploy then silently dropped the scripted alarm
|
|
/// (and deployed the VirtualTag with an empty expression). The deploy gate now refuses such a draft
|
|
/// (<c>VirtualTagScriptNotFound</c> / <c>ScriptedAlarmScriptNotFound</c> / <c>CalculationScriptNotFound</c>);
|
|
/// this stops the delete that would create it, and says which rows hold the script.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A check-then-delete is not serialised against a concurrent "bind a VirtualTag to this script" — the deploy
|
|
/// gate is the backstop for that window. A real FK (<c>Restrict</c>) would close it but needs a migration.
|
|
/// </remarks>
|
|
public static class ScriptUsage
|
|
{
|
|
/// <summary>How many using rows are named in the refusal before it summarises the rest.</summary>
|
|
private const int NamedLimit = 5;
|
|
|
|
/// <summary>
|
|
/// Describes every row that references <paramref name="scriptId"/>, or returns <see langword="null"/> when
|
|
/// nothing does and the script may be deleted.
|
|
/// </summary>
|
|
/// <param name="db">The context the delete will run on.</param>
|
|
/// <param name="scriptId">The script about to be deleted.</param>
|
|
/// <param name="ct">Cancellation token.</param>
|
|
/// <returns>A refusal naming the users, or <see langword="null"/> when the script is unused.</returns>
|
|
public static async Task<string?> DescribeBlockingUsageAsync(
|
|
OtOpcUaConfigDbContext db, string scriptId, CancellationToken ct = default)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(db);
|
|
ArgumentException.ThrowIfNullOrEmpty(scriptId);
|
|
|
|
var users = new List<string>();
|
|
|
|
var vtags = await db.VirtualTags.AsNoTracking()
|
|
.Where(v => v.ScriptId == scriptId)
|
|
.OrderBy(v => v.VirtualTagId)
|
|
.Select(v => new { v.VirtualTagId, v.Name, v.EquipmentId })
|
|
.ToListAsync(ct);
|
|
users.AddRange(vtags.Select(v => $"virtual tag '{v.Name}' ({v.VirtualTagId}) on equipment {v.EquipmentId}"));
|
|
|
|
var alarms = await db.ScriptedAlarms.AsNoTracking()
|
|
.Where(a => a.PredicateScriptId == scriptId)
|
|
.OrderBy(a => a.ScriptedAlarmId)
|
|
.Select(a => new { a.ScriptedAlarmId, a.Name, a.EquipmentId })
|
|
.ToListAsync(ct);
|
|
users.AddRange(alarms.Select(a => $"scripted alarm '{a.Name}' ({a.ScriptedAlarmId}) on equipment {a.EquipmentId}"));
|
|
|
|
// Calculation tags hold the id inside schemaless JSON: narrow in SQL by substring, then confirm with the
|
|
// same parser the deploy gate uses, so a script id that merely appears in another key never blocks.
|
|
var calcCandidates = await db.Tags.AsNoTracking()
|
|
.Where(t => t.TagConfig.Contains(scriptId))
|
|
.OrderBy(t => t.TagId)
|
|
.Select(t => new { t.TagId, t.Name, t.TagConfig })
|
|
.ToListAsync(ct);
|
|
users.AddRange(calcCandidates
|
|
.Where(t => string.Equals(DraftValidator.ParseScriptId(t.TagConfig), scriptId, StringComparison.Ordinal))
|
|
.Select(t => $"calculation tag '{t.Name}' ({t.TagId})"));
|
|
|
|
if (users.Count == 0) return null;
|
|
|
|
var named = string.Join("; ", users.Take(NamedLimit));
|
|
var more = users.Count > NamedLimit ? $"; and {users.Count - NamedLimit} more" : string.Empty;
|
|
return $"This script is still used by {users.Count} item(s): {named}{more}. "
|
|
+ "Point them at another script or delete them first — a deployment would reject them otherwise.";
|
|
}
|
|
}
|