feat(deploymentmanager): IFlatteningPipeline.PreviewAsync — flatten a template+site without an instance row (create-page bindings)
Claude-Session: https://claude.ai/code/session_01G2GHPnbbKGvyyfbr2XBRVH
This commit is contained in:
@@ -81,47 +81,11 @@ public class FlatteningPipeline : IFlatteningPipeline
|
||||
if (instance == null)
|
||||
return Result<FlatteningPipelineResult>.Failure($"Instance with ID {instanceId} not found.");
|
||||
|
||||
Task<Commons.Entities.Templates.Template?> LoadTemplate(int id) =>
|
||||
_templateRepo.GetTemplateWithChildrenAsync(id, cancellationToken);
|
||||
|
||||
// Build template chain
|
||||
var templateChain = await session.GetChainAsync(instance.TemplateId, LoadTemplate);
|
||||
if (templateChain.Count == 0)
|
||||
var graph = await LoadTemplateGraphAsync(instance.TemplateId, session, cancellationToken);
|
||||
if (graph is null)
|
||||
return Result<FlatteningPipelineResult>.Failure("Template chain is empty.");
|
||||
|
||||
// Build composition maps, walking nested compositions so the flattener
|
||||
// can resolve composed-of-composed attributes / alarms / scripts (e.g.
|
||||
// a parent that composes Pump where Pump itself composes AlarmSensor
|
||||
// produces "Pump.AlarmSensor.SensorReading").
|
||||
var compositionMap = new Dictionary<int, IReadOnlyList<Commons.Entities.Templates.TemplateComposition>>();
|
||||
var composedChains = new Dictionary<int, IReadOnlyList<Commons.Entities.Templates.Template>>();
|
||||
var processedTemplateIds = new HashSet<int>();
|
||||
var pendingChains = new Queue<IReadOnlyList<Commons.Entities.Templates.Template>>();
|
||||
|
||||
pendingChains.Enqueue(templateChain);
|
||||
while (pendingChains.Count > 0)
|
||||
{
|
||||
var chain = pendingChains.Dequeue();
|
||||
foreach (var template in chain)
|
||||
{
|
||||
if (!processedTemplateIds.Add(template.Id)) continue;
|
||||
|
||||
var compositions = await session.GetCompositionsAsync(
|
||||
template.Id,
|
||||
id => _templateRepo.GetCompositionsByTemplateIdAsync(id, cancellationToken));
|
||||
if (compositions.Count == 0) continue;
|
||||
|
||||
compositionMap[template.Id] = compositions;
|
||||
foreach (var comp in compositions)
|
||||
{
|
||||
if (composedChains.ContainsKey(comp.ComposedTemplateId)) continue;
|
||||
|
||||
var composedChain = await session.GetChainAsync(comp.ComposedTemplateId, LoadTemplate);
|
||||
composedChains[comp.ComposedTemplateId] = composedChain;
|
||||
pendingChains.Enqueue(composedChain);
|
||||
}
|
||||
}
|
||||
}
|
||||
var (templateChain, compositionMap, composedChains) = graph.Value;
|
||||
|
||||
// Load data connections for the site
|
||||
var dataConnections = await session.GetDataConnectionsAsync(
|
||||
@@ -216,6 +180,86 @@ public class FlatteningPipeline : IFlatteningPipeline
|
||||
new FlatteningPipelineResult(config, hash, validation));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Result<FlattenedConfiguration>> PreviewAsync(
|
||||
int templateId,
|
||||
int siteId,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var session = CreateSession();
|
||||
|
||||
var graph = await LoadTemplateGraphAsync(templateId, session, cancellationToken);
|
||||
if (graph is null)
|
||||
return Result<FlattenedConfiguration>.Failure($"Template {templateId} not found or its chain is empty.");
|
||||
|
||||
var dataConnections = await session.GetDataConnectionsAsync(
|
||||
siteId,
|
||||
id => LoadDataConnectionsAsync(id, cancellationToken));
|
||||
|
||||
// A stand-in instance with no overrides and no bindings: the flattener sees
|
||||
// exactly what a freshly created instance would, and nothing is persisted.
|
||||
var preview = new Commons.Entities.Instances.Instance("(preview)")
|
||||
{
|
||||
TemplateId = templateId,
|
||||
SiteId = siteId
|
||||
};
|
||||
|
||||
return _flatteningService.Flatten(
|
||||
preview, graph.Value.Chain, graph.Value.CompositionMap, graph.Value.ComposedChains, dataConnections);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the template inheritance chain for <paramref name="templateId"/> plus the
|
||||
/// composition maps, walking nested compositions so the flattener can resolve
|
||||
/// composed-of-composed members (e.g. a parent composing Pump where Pump itself
|
||||
/// composes AlarmSensor produces "Pump.AlarmSensor.SensorReading").
|
||||
/// Returns <see langword="null"/> when the chain is empty (unknown template).
|
||||
/// </summary>
|
||||
private async Task<(IReadOnlyList<Commons.Entities.Templates.Template> Chain,
|
||||
Dictionary<int, IReadOnlyList<Commons.Entities.Templates.TemplateComposition>> CompositionMap,
|
||||
Dictionary<int, IReadOnlyList<Commons.Entities.Templates.Template>> ComposedChains)?>
|
||||
LoadTemplateGraphAsync(int templateId, FlattenSession session, CancellationToken cancellationToken)
|
||||
{
|
||||
Task<Commons.Entities.Templates.Template?> LoadTemplate(int id) =>
|
||||
_templateRepo.GetTemplateWithChildrenAsync(id, cancellationToken);
|
||||
|
||||
var templateChain = await session.GetChainAsync(templateId, LoadTemplate);
|
||||
if (templateChain.Count == 0)
|
||||
return null;
|
||||
|
||||
var compositionMap = new Dictionary<int, IReadOnlyList<Commons.Entities.Templates.TemplateComposition>>();
|
||||
var composedChains = new Dictionary<int, IReadOnlyList<Commons.Entities.Templates.Template>>();
|
||||
var processedTemplateIds = new HashSet<int>();
|
||||
var pendingChains = new Queue<IReadOnlyList<Commons.Entities.Templates.Template>>();
|
||||
|
||||
pendingChains.Enqueue(templateChain);
|
||||
while (pendingChains.Count > 0)
|
||||
{
|
||||
var chain = pendingChains.Dequeue();
|
||||
foreach (var template in chain)
|
||||
{
|
||||
if (!processedTemplateIds.Add(template.Id)) continue;
|
||||
|
||||
var compositions = await session.GetCompositionsAsync(
|
||||
template.Id,
|
||||
id => _templateRepo.GetCompositionsByTemplateIdAsync(id, cancellationToken));
|
||||
if (compositions.Count == 0) continue;
|
||||
|
||||
compositionMap[template.Id] = compositions;
|
||||
foreach (var comp in compositions)
|
||||
{
|
||||
if (composedChains.ContainsKey(comp.ComposedTemplateId)) continue;
|
||||
|
||||
var composedChain = await session.GetChainAsync(comp.ComposedTemplateId, LoadTemplate);
|
||||
composedChains[comp.ComposedTemplateId] = composedChain;
|
||||
pendingChains.Enqueue(composedChain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (templateChain, compositionMap, composedChains);
|
||||
}
|
||||
|
||||
private async Task<IReadOnlyDictionary<int, DataConnection>> LoadDataConnectionsAsync(
|
||||
int siteId,
|
||||
CancellationToken cancellationToken)
|
||||
|
||||
@@ -39,6 +39,21 @@ public interface IFlatteningPipeline
|
||||
bool validateScripts = true,
|
||||
FlattenSession? session = null);
|
||||
|
||||
/// <summary>
|
||||
/// Flattens <paramref name="templateId"/> as if an instance with no overrides and no
|
||||
/// bindings existed at <paramref name="siteId"/>. Attribute list only — no validation,
|
||||
/// no revision hash, nothing persisted. Used by the Create Instance page to show the
|
||||
/// module groups before the instance row exists.
|
||||
/// </summary>
|
||||
/// <param name="templateId">Id of the template to preview.</param>
|
||||
/// <param name="siteId">Id of the target site, whose data connections resolve references.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>The flattened configuration of the hypothetical instance, or a failure result.</returns>
|
||||
Task<Result<FlattenedConfiguration>> PreviewAsync(
|
||||
int templateId,
|
||||
int siteId,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a fresh <see cref="FlattenSession"/> bound to this pipeline's
|
||||
/// watermark, for a caller that wants to share one across a batch of
|
||||
|
||||
Reference in New Issue
Block a user