From 792f16c2d5f80ebeadd72ffe75a48e29ac96a289 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Mon, 9 Nov 2026 12:52:29 -0500 Subject: [PATCH] perf(templates): member add/update loads only the chain + composition closure (ScadaBridge#66) Attribute/alarm/script add (collision check) and update (parent-lock lookup) used GetAllTemplatesForAnalysisAsync, materialising every template, attribute, alarm and script in the library per edit. They now call the new ITemplateEngineRepository.GetTemplateAnalysisClosureAsync: the same untracked, body-free analysis projection, restricted to the template's inheritance chain plus the composition closure of every chain member - exactly what TemplateResolver and CollisionDetector walk (a composed module's own parent chain is never climbed, so it is not loaded). Adds use the edited template's closure, updates the parent's. The walk (Commons TemplateAnalysisClosure) loads one batch per closure depth, requests each id once, terminates on cycles and skips dangling edges. Evidence: - TemplateAnalysisClosureTests: for every template of a graph with chains, nested + diamond compositions, a derived module, cycles, dangling edges and colliding unrelated templates, ResolveAllMembers / DetectCollisions / FindMemberByCanonicalName answer identically from the closure and the whole graph (mutation-checked). - TemplateAnalysisClosureRepositoryTests (SQLite, recorded SQL): 6 templates / 12 attributes loaded instead of 47 / 94, in 4 queries (one per depth), untracked, script bodies never selected. - TemplateServiceTests: two new tests with the whole-graph load wired to throw (fail on the old service); every existing add/update test now runs against the closure via AnalysisClosureMock. ReconcileDescendantsAsync still loads the tracked full graph; not in scope. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV --- .../Repositories/ITemplateEngineRepository.cs | 13 ++ .../Templates/TemplateAnalysisClosure.cs | 113 ++++++++++++ .../Repositories/TemplateEngineRepository.cs | 32 +++- .../TemplateService.cs | 18 +- .../TemplateAnalysisClosureRepositoryTests.cs | 126 ++++++++++++++ .../AnalysisClosureMock.cs | 40 +++++ .../TemplateAnalysisClosureTests.cs | 162 ++++++++++++++++++ .../TemplateAuditShapeTests.cs | 1 + .../TemplateServiceTests.cs | 86 ++++++++++ 9 files changed, 581 insertions(+), 10 deletions(-) create mode 100644 src/ZB.MOM.WW.ScadaBridge.Commons/Types/Templates/TemplateAnalysisClosure.cs create mode 100644 tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/TemplateAnalysisClosureRepositoryTests.cs create mode 100644 tests/ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests/AnalysisClosureMock.cs create mode 100644 tests/ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests/TemplateAnalysisClosureTests.cs diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/ITemplateEngineRepository.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/ITemplateEngineRepository.cs index 5cdc8c15..68146dd1 100644 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/ITemplateEngineRepository.cs +++ b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/ITemplateEngineRepository.cs @@ -56,6 +56,19 @@ public interface ITemplateEngineRepository /// A task that resolves to a read-only list of all templates without script bodies. Task> GetAllTemplatesForAnalysisAsync(CancellationToken cancellationToken = default); /// + /// Returns the same untracked, script-body-free projection as + /// , but only for the templates an analysis walk + /// rooted at can reach: its inheritance chain plus the + /// composition closure of every chain member (see + /// ). + /// Backs member add/update validation (Gitea #66), which previously loaded every template. + /// Unknown ids yield an empty list. + /// + /// The template whose chain and composition closure to load. + /// Cancellation token. + /// A task that resolves to the closure templates without script bodies. + Task> GetTemplateAnalysisClosureAsync(int templateId, CancellationToken cancellationToken = default); + /// /// Returns an untracked identity/edge projection of every template (no children), ordered by name. /// /// Cancellation token. diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Templates/TemplateAnalysisClosure.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Templates/TemplateAnalysisClosure.cs new file mode 100644 index 00000000..11f97e76 --- /dev/null +++ b/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Templates/TemplateAnalysisClosure.cs @@ -0,0 +1,113 @@ +using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates; + +namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Templates; + +/// +/// Computes the set of templates a member add/update analysis walk can reach from one template +/// (Gitea #66): the template's inheritance chain (itself, its parent, grandparent, …) plus +/// the composition closure of every chain member (each composed module, the modules they +/// compose, and so on). +/// +/// +/// This mirrors exactly what the analysis walkers read. TemplateResolver.ResolveAllMembers +/// and CollisionDetector.DetectCollisions climb the parent chain of the template being +/// analysed and descend composition edges from every chain member, but never climb a composed +/// module's own parent chain (a derived module carries its inherited members as placeholder rows). +/// So a composed module's ancestors are not loaded — only chain members expand their parent edge. +/// Every other template in the library is irrelevant to the result, which is why member edits no +/// longer need the whole analysis graph. +/// +/// +/// +/// The walk is breadth-first by batch: each round asks loadBatch for every id not yet +/// loaded, so a store-backed loader issues one query per closure depth rather than one per +/// template. Missing ids (a dangling edge) are simply absent from the result, exactly as the +/// walkers skip them. Cycles terminate: an id is expanded at most twice (once as a module, once +/// more if it is later reached as a chain member). +/// +/// +public static class TemplateAnalysisClosure +{ + /// + /// Collects the inheritance chain and composition closure of . + /// + /// The template whose analysis closure is wanted. + /// Loads the templates with the given ids (unknown ids are omitted). + /// Cancellation token. + /// The closure templates, each once, in load order (the root first when it exists). + public static async Task> CollectAsync( + int templateId, + Func, CancellationToken, Task>> loadBatch, + CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(loadBatch); + + var loaded = new Dictionary(); + var result = new List