From 1d19a63e0b33968923b16fd7f9edb2e783d2d95c Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Mon, 13 Sep 2021 15:43:51 +1200 Subject: [PATCH] Sort sibling nodes again when a delta with an updated workflow is received --- src/components/cylc/gscan/index.js | 38 ++++++++++++++++++++++++++++-- src/components/cylc/gscan/nodes.js | 1 + 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/components/cylc/gscan/index.js b/src/components/cylc/gscan/index.js index a923780cdd..429e97f591 100644 --- a/src/components/cylc/gscan/index.js +++ b/src/components/cylc/gscan/index.js @@ -127,11 +127,45 @@ function updateWorkflow (workflow, gscan, options) { throw new Error(`Updated node [${workflow.id}] not found in workflow lookup`) } mergeWith(existingData.node, workflow, mergeWithCustomizer) - Vue.set(gscan.lookup, existingData.id, existingData) - // FIXME: we need to sort its parent again! + const hierarchical = options.hierarchical || true + if (hierarchical) { + updateHierarchicalWorkflow(existingData, gscan.lookup, gscan.tree, options) + } // TODO: create workflow hierarchy (from workflow object), then iterate // it and use lookup to fetch the existing node. Finally, combine // the gscan states (latestStateTasks & stateTotals). + Vue.set(gscan.lookup, existingData.id, existingData) +} + +function updateHierarchicalWorkflow (existingData, lookup, tree, options) { + // We need to sort its parent again. + const workflowNameParts = parseWorkflowNameParts(existingData.id) + const nodesIds = getWorkflowNamePartsNodesIds(workflowNameParts) + // Discard the last since it's the workflow ID that we already have + // in the `existingData` object. Now if not empty, we have our parent. + nodesIds.pop() + const parentId = nodesIds.length > 0 ? nodesIds.pop() : null + const parent = parentId ? lookup[parentId] : tree + if (!parent) { + throw new Error(`Invalid orphan hierarchical node: ${existingData.id}`) + } + const siblings = parent.children + // Where is this node at the moment? + const currentIndex = siblings.findIndex(node => node.id === existingData.id) + // Where should it be now? + const sortedIndex = sortedIndexBy( + parent.children, + existingData, + (n) => n.name, + sortWorkflowNamePartNodeOrWorkflowNode + ) + // If it is not where it is, we need to add it to its correct location. + if (currentIndex !== sortedIndex) { + // siblings.splice(currentIndex, 1) + // siblings.splice(sortedIndex, 0, existingData) + Vue.delete(siblings, currentIndex) + Vue.set(siblings, sortedIndex, existingData) + } } // -- Pruned diff --git a/src/components/cylc/gscan/nodes.js b/src/components/cylc/gscan/nodes.js index c1e65e7961..a74e479975 100644 --- a/src/components/cylc/gscan/nodes.js +++ b/src/components/cylc/gscan/nodes.js @@ -148,6 +148,7 @@ function getWorkflowNamePartsNodesIds (workflowNameParts) { return prefix }) nodesIds.push(workflowNameParts.workflowId) + return nodesIds } /**