fix(editor): #1025 analyse scripted-alarm predicates as bool Run(AlarmPredicateContext) — predicate mode on the analysis endpoints, Monaco and the ScriptEdit page

This commit is contained in:
Joseph Doherty
2026-12-03 15:03:17 -05:00
parent 29c44d1f04
commit 16c8b7ffad
9 changed files with 371 additions and 55 deletions
@@ -21,6 +21,42 @@ 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>Which seams use a script.</summary>
/// <param name="UsedAsValue">An (any) VirtualTag or Calculation tag runs it as a value script.</param>
/// <param name="UsedAsPredicate">An (any) scripted alarm runs it as its predicate.</param>
public readonly record struct SeamUsage(bool UsedAsValue, bool UsedAsPredicate)
{
/// <summary>
/// The editor analysis mode (<c>ScriptAnalysisModes</c>) a script should open in (lmxopcua#1025): the predicate
/// shape only when scripted alarms are its sole users — a script also run as a value script stays a value
/// script (the author can switch), and an unused script is a value script.
/// </summary>
public string DefaultMode =>
UsedAsPredicate && !UsedAsValue ? ScriptAnalysis.ScriptAnalysisModes.AlarmPredicate : ScriptAnalysis.ScriptAnalysisModes.Value;
}
/// <summary>Classifies which seams use <paramref name="scriptId"/> (same rows, same Calculation parser as the delete check).</summary>
/// <param name="db">The config context.</param>
/// <param name="scriptId">The script.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>Where the script is used.</returns>
public static async Task<SeamUsage> ClassifyAsync(OtOpcUaConfigDbContext db, string scriptId, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(db);
ArgumentException.ThrowIfNullOrEmpty(scriptId);
var predicate = await db.ScriptedAlarms.AsNoTracking().AnyAsync(a => a.PredicateScriptId == scriptId, ct);
var value = await db.VirtualTags.AsNoTracking().AnyAsync(v => v.ScriptId == scriptId, ct);
if (!value)
{
var calcCandidates = await db.Tags.AsNoTracking()
.Where(t => t.TagConfig.Contains(scriptId))
.Select(t => t.TagConfig)
.ToListAsync(ct);
value = calcCandidates.Any(c => string.Equals(DraftValidator.ParseScriptId(c), scriptId, StringComparison.Ordinal));
}
return new SeamUsage(value, predicate);
}
/// <summary>
/// Describes every row that references <paramref name="scriptId"/>, or returns <see langword="null"/> when
/// nothing does and the script may be deleted.