Skip to content

Commit c2c8f00

Browse files
committed
Handle adding nodes
1 parent f93ceee commit c2c8f00

File tree

7 files changed

+79
-20
lines changed

7 files changed

+79
-20
lines changed

src/components/cylc/gscan/GScan.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ export default {
312312
}
313313
},
314314
computed: {
315-
...mapState('workflows', ['workflows', 'gscan']),
315+
...mapState('workflows', ['gscan']),
316316
// workflowNodes () {
317317
// // NOTE: In case we decide to allow the user to switch between hierarchical and flat
318318
// // gscan view, then all we need to do is just pass a boolean data-property to
@@ -350,7 +350,7 @@ export default {
350350
deep: true,
351351
immediate: false,
352352
handler: function (newVal) {
353-
this.filteredWorkflows = this.filterHierarchically(this.gscan.tree.children || [], this.searchWorkflows, this.workflowStates, this.taskStates)
353+
this.filteredWorkflows = this.filterHierarchically(this.gscan.tree, this.searchWorkflows, this.workflowStates, this.taskStates)
354354
}
355355
},
356356
/**
@@ -360,13 +360,14 @@ export default {
360360
searchWorkflows: {
361361
immediate: false,
362362
handler: function (newVal) {
363-
this.filteredWorkflows = this.filterHierarchically(this.gscan.tree.children || [], newVal, this.workflowStates, this.taskStates)
363+
this.filteredWorkflows = this.filterHierarchically(this.gscan.tree, newVal, this.workflowStates, this.taskStates)
364364
}
365365
},
366366
gscan: {
367367
immediate: true,
368+
deep: true,
368369
handler: function () {
369-
this.filteredWorkflows = this.filterHierarchically(this.gscan.tree.children || [], this.searchWorkflows, this.workflowStates, this.taskStates)
370+
this.filteredWorkflows = this.filterHierarchically(this.gscan.tree, this.searchWorkflows, this.workflowStates, this.taskStates)
370371
}
371372
}
372373
},

src/components/cylc/gscan/deltas.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { createWorkflowNode } from '@/components/cylc/gscan/nodes'
2121
* Deltas added.
2222
*
2323
* @param {DeltasAdded} added
24-
* @param {GScan} gscan
24+
* @param {import('./index').GScan} gscan
2525
* @param {*} options
2626
* @returns {Result}
2727
*/
@@ -51,7 +51,7 @@ function applyDeltasAdded (added, gscan, options) {
5151
* Deltas updated.
5252
*
5353
* @param {DeltasUpdated} updated
54-
* @param {GScan} gscan
54+
* @param {import('./index').GScan} gscan
5555
* @param {*} options
5656
* @returns {Result}
5757
*/
@@ -61,7 +61,6 @@ function applyDeltasUpdated (updated, gscan, options) {
6161
}
6262
if (updated.workflow) {
6363
const updatedData = updated.workflow
64-
const hierarchical = options.hierarchical || true
6564
try {
6665
const existingData = gscan.lookup[updatedData.id]
6766
if (!existingData) {
@@ -72,7 +71,8 @@ function applyDeltasUpdated (updated, gscan, options) {
7271
options
7372
])
7473
} else {
75-
const workflowNode = createWorkflowNode(updatedData, hierarchical)
74+
// TODO: hierarchy is always false here?
75+
const workflowNode = createWorkflowNode(updatedData, false)
7676
updateWorkflow(workflowNode, gscan, options)
7777
}
7878
} catch (error) {
@@ -92,14 +92,15 @@ function applyDeltasUpdated (updated, gscan, options) {
9292
* Deltas pruned.
9393
*
9494
* @param {DeltasPruned} pruned
95-
* @param {GScan} gscan
95+
* @param {import('./index').GScan} gscan
9696
* @param {*} options
9797
* @returns {Result}
9898
*/
9999
function applyDeltasPruned (pruned, gscan, options) {
100100
const result = {
101101
errors: []
102102
}
103+
// TODO: why not pruned.workflows???
103104
if (pruned.workflow) {
104105
const workflowId = pruned.workflow
105106
try {

src/components/cylc/gscan/index.js

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,80 @@
1717

1818
/**
1919
* @typedef {Object} GScan
20+
* @property {Array<TreeNode>} tree
21+
* @property {Lookup} lookup
2022
*/
2123

2224
/**
2325
* @typedef {Object<String, TreeNode>} Lookup
2426
*/
2527

26-
/**
27-
* @typedef {Object<String, TreeNode>} Tree
28-
*/
28+
import { sortedIndexBy } from '@/components/cylc/common/sort'
29+
import { sortWorkflowNamePartNodeOrWorkflowNode } from '@/components/cylc/gscan/sort'
2930

3031
/**
31-
* @param {WorkflowGraphQLData} workflow
32+
* @param {TreeNode} workflow
3233
* @param {GScan} gscan
3334
* @param {*} options
3435
*/
3536
function addWorkflow (workflow, gscan, options) {
37+
const hierarchical = options.hierarchical || true
38+
if (hierarchical) {
39+
addHierarchicalWorkflow(workflow, gscan.lookup, gscan.tree, options)
40+
} else {
41+
gscan.lookup[workflow.id] = workflow
42+
gscan.tree.push(workflow)
43+
}
44+
}
3645

46+
/**
47+
* @param workflow
48+
* @param {Lookup} lookup
49+
* @param {Array<TreeNode>} tree
50+
* @param {*} options
51+
*/
52+
function addHierarchicalWorkflow (workflow, lookup, tree, options) {
53+
if (!lookup[workflow.id]) {
54+
// a new node, let's add this node and its descendants to the lookup
55+
lookup[workflow.id] = workflow
56+
if (workflow.children) {
57+
const stack = [...workflow.children]
58+
while (stack.length) {
59+
const currentNode = stack.shift()
60+
lookup[currentNode.id] = currentNode
61+
if (currentNode.children) {
62+
stack.push(...currentNode.children)
63+
}
64+
}
65+
}
66+
// and now add the top-level node to the tree
67+
// Here we calculate what is the index for this element. If we decide to have ASC and DESC,
68+
// then we just need to invert the location of the element, something like
69+
// `sortedIndex = (array.length - sortedIndex)`.
70+
const sortedIndex = sortedIndexBy(
71+
tree,
72+
workflow,
73+
(n) => n.name,
74+
sortWorkflowNamePartNodeOrWorkflowNode
75+
)
76+
tree.splice(sortedIndex, 0, workflow)
77+
} else {
78+
// we will have to merge the hierarchies
79+
const existingNode = lookup[workflow.id]
80+
// TODO: combine states summaries?
81+
// Copy array since we will iterate it, and modify existingNode.children
82+
if (existingNode.children) {
83+
const children = [...workflow.children]
84+
for (const child of children) {
85+
// Recursion
86+
addHierarchicalWorkflow(child, lookup, existingNode.children, options)
87+
}
88+
}
89+
}
3790
}
3891

3992
/**
40-
* @param {WorkflowGraphQLData} workflow
93+
* @param {TreeNode} workflow
4194
* @param {GScan} gscan
4295
* @param {*} options
4396
*/
@@ -46,7 +99,7 @@ function updateWorkflow (workflow, gscan, options) {
4699
}
47100

48101
/**
49-
* @param {WorkflowGraphQLData} workflow
102+
* @param {TreeNode} workflow
50103
* @param {GScan} gscan
51104
* @param {*} options
52105
*/

src/components/cylc/gscan/nodes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function newWorkflowPartNode (id, part) {
8585
*
8686
* @param {WorkflowGraphQLData} workflow
8787
* @param {boolean} hierarchy - whether to parse the Workflow name and create a hierarchy or not
88-
* @returns {WorkflowGScanNode|WorkflowNamePartGScanNode|null}
88+
* @returns {TreeNode}
8989
*/
9090
function createWorkflowNode (workflow, hierarchy) {
9191
if (!hierarchy) {

src/components/cylc/tree/TreeItem.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,12 @@ export default {
284284
}
285285
},
286286
created () {
287+
// console.log(`TreeItem ${this.node.id} created!`)
287288
this.$emit('tree-item-created', this)
288289
},
290+
updated () {
291+
// console.log(`TreeItem ${this.node.id} updated!`)
292+
},
289293
beforeDestroy () {
290294
this.$emit('tree-item-destroyed', this)
291295
},

src/components/cylc/tree/nodes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import Vue from 'vue'
3838
* Create a workflow node. Uses the same properties (by reference) as the given workflow,
3939
* only adding new properties such as type, children, etc.
4040
*
41-
* @param workflow {WorkflowGraphQLData} workflow
41+
* @param {WorkflowGraphQLData} workflow workflow
4242
* @return {WorkflowNode}
4343
*/
4444
function createWorkflowNode (workflow) {
@@ -70,7 +70,7 @@ function createWorkflowNode (workflow) {
7070
* - 'a|b' results in a cycle point node ID 'a|b'
7171
* - '' results in a cycle point node ID ''
7272
*
73-
* @param node {GraphQLData} a tree node
73+
* @param {GraphQLData} node a tree node
7474
* @throws {Error} - if there was an error extracting the cycle point ID
7575
* @return {string} - the cycle point ID
7676
*/

src/store/workflows.module.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const state = {
3232
* and the lookup is a helper structure for quick access to nodes in the tree.
3333
*/
3434
gscan: {
35-
tree: {},
35+
tree: [],
3636
lookup: {}
3737
},
3838
/**
@@ -71,7 +71,7 @@ const mutations = {
7171
})
7272
}
7373
state.gscan = {
74-
tree: {},
74+
tree: [],
7575
lookup: {}
7676
}
7777
},

0 commit comments

Comments
 (0)