torc 0.21.0

Workflow management system
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
 * DAG Visualization Module
 * Uses Cytoscape.js for rendering workflow dependency graphs
 */

class DAGVisualizer {
    constructor(containerId) {
        this.containerId = containerId;
        this.cy = null;
        this.currentWorkflowId = null;
        this.currentType = 'jobs';
    }

    // Status to color mapping
    static statusColors = {
        0: '#6c757d', // uninitialized
        1: '#ffc107', // blocked
        2: '#17a2b8', // ready
        3: '#fd7e14', // pending
        4: '#007bff', // running
        5: '#28a745', // completed
        6: '#dc3545', // failed
        7: '#6f42c1', // canceled
        8: '#adb5bd', // terminated
        9: '#e9ecef', // disabled
    };

    static statusNames = {
        0: 'Uninitialized',
        1: 'Blocked',
        2: 'Ready',
        3: 'Pending',
        4: 'Running',
        5: 'Completed',
        6: 'Failed',
        7: 'Canceled',
        8: 'Terminated',
        9: 'Disabled',
    };

    initialize() {
        const container = document.getElementById(this.containerId);
        if (!container) {
            console.error('DAG container not found:', this.containerId);
            return;
        }

        // Clear placeholder
        container.innerHTML = '';

        this.cy = cytoscape({
            container: container,
            style: [
                // Node styles
                {
                    selector: 'node',
                    style: {
                        'label': 'data(label)',
                        'text-valign': 'center',
                        'text-halign': 'center',
                        'background-color': 'data(color)',
                        'color': '#fff',
                        'text-outline-color': 'data(color)',
                        'text-outline-width': 2,
                        'font-size': '11px',
                        'width': 'label',
                        'height': 30,
                        'padding': '10px',
                        'shape': 'roundrectangle',
                    }
                },
                // Job nodes
                {
                    selector: 'node.job',
                    style: {
                        'shape': 'roundrectangle',
                    }
                },
                // File nodes
                {
                    selector: 'node.file',
                    style: {
                        'shape': 'diamond',
                        'background-color': '#20c997',
                    }
                },
                // User data nodes
                {
                    selector: 'node.userdata',
                    style: {
                        'shape': 'ellipse',
                        'background-color': '#e83e8c',
                    }
                },
                // Edge styles
                {
                    selector: 'edge',
                    style: {
                        'width': 2,
                        'line-color': '#adb5bd',
                        'target-arrow-color': '#adb5bd',
                        'target-arrow-shape': 'triangle',
                        'curve-style': 'bezier',
                        'arrow-scale': 1.2,
                    }
                },
                // Input edge (file/data -> job)
                {
                    selector: 'edge.input',
                    style: {
                        'line-color': '#28a745',
                        'target-arrow-color': '#28a745',
                        'line-style': 'dashed',
                    }
                },
                // Output edge (job -> file/data)
                {
                    selector: 'edge.output',
                    style: {
                        'line-color': '#007bff',
                        'target-arrow-color': '#007bff',
                    }
                },
                // Highlighted node
                {
                    selector: 'node:selected',
                    style: {
                        'border-width': 3,
                        'border-color': '#000',
                    }
                },
            ],
            layout: { name: 'preset' },
            wheelSensitivity: 0.3,
            minZoom: 0.1,
            maxZoom: 3,
        });

        // Add click handlers
        this.cy.on('tap', 'node', (evt) => {
            const node = evt.target;
            console.log('Node clicked:', node.data());
            this.showNodeInfo(node.data());
        });
    }

    async loadJobDependencies(workflowId) {
        this.currentWorkflowId = workflowId;
        this.currentType = 'jobs';

        try {
            // Get jobs and their dependencies
            const [jobs, dependencies] = await Promise.all([
                api.listJobs(workflowId),
                api.getJobsDependencies(workflowId),
            ]);

            if (!jobs || jobs.length === 0) {
                this.showEmpty('No jobs in this workflow');
                return;
            }

            // Create node elements
            const nodes = jobs.map(job => ({
                data: {
                    id: `job-${job.id}`,
                    label: job.name || `Job ${job.id}`,
                    color: DAGVisualizer.statusColors[job.status] || '#6c757d',
                    type: 'job',
                    jobId: job.id,
                    status: job.status,
                    statusName: DAGVisualizer.statusNames[job.status],
                },
                classes: 'job',
            }));

            // Create edge elements from dependencies
            const edges = [];
            if (dependencies) {
                for (const dep of dependencies) {
                    edges.push({
                        data: {
                            id: `edge-${dep.depends_on_job_id}-${dep.job_id}`,
                            source: `job-${dep.depends_on_job_id}`,
                            target: `job-${dep.job_id}`,
                        }
                    });
                }
            }

            this.renderGraph(nodes, edges);
        } catch (error) {
            console.error('Error loading job dependencies:', error);
            this.showEmpty('Error loading dependencies: ' + error.message);
        }
    }

    async loadFileRelationships(workflowId) {
        this.currentWorkflowId = workflowId;
        this.currentType = 'files';

        try {
            const [jobs, files, relationships] = await Promise.all([
                api.listJobs(workflowId),
                api.listFiles(workflowId),
                api.getJobFileRelationships(workflowId),
            ]);

            if (!jobs || jobs.length === 0) {
                this.showEmpty('No jobs in this workflow');
                return;
            }

            // Create job nodes
            const nodes = jobs.map(job => ({
                data: {
                    id: `job-${job.id}`,
                    label: job.name || `Job ${job.id}`,
                    color: DAGVisualizer.statusColors[job.status] || '#6c757d',
                    type: 'job',
                },
                classes: 'job',
            }));

            // Create file nodes
            if (files) {
                for (const file of files) {
                    nodes.push({
                        data: {
                            id: `file-${file.id}`,
                            label: this.truncateName(file.name || file.path || `File ${file.id}`),
                            type: 'file',
                        },
                        classes: 'file',
                    });
                }
            }

            // Create edges from relationships
            const edges = [];
            if (relationships) {
                for (const rel of relationships) {
                    if (rel.relationship_type === 'input') {
                        // File -> Job (input)
                        edges.push({
                            data: {
                                source: `file-${rel.file_id}`,
                                target: `job-${rel.job_id}`,
                            },
                            classes: 'input',
                        });
                    } else if (rel.relationship_type === 'output') {
                        // Job -> File (output)
                        edges.push({
                            data: {
                                source: `job-${rel.job_id}`,
                                target: `file-${rel.file_id}`,
                            },
                            classes: 'output',
                        });
                    }
                }
            }

            this.renderGraph(nodes, edges);
        } catch (error) {
            console.error('Error loading file relationships:', error);
            this.showEmpty('Error loading file relationships: ' + error.message);
        }
    }

    async loadUserDataRelationships(workflowId) {
        this.currentWorkflowId = workflowId;
        this.currentType = 'userdata';

        try {
            const [jobs, userData, relationships] = await Promise.all([
                api.listJobs(workflowId),
                api.listUserData(workflowId),
                api.getJobUserDataRelationships(workflowId),
            ]);

            if (!jobs || jobs.length === 0) {
                this.showEmpty('No jobs in this workflow');
                return;
            }

            // Create job nodes
            const nodes = jobs.map(job => ({
                data: {
                    id: `job-${job.id}`,
                    label: job.name || `Job ${job.id}`,
                    color: DAGVisualizer.statusColors[job.status] || '#6c757d',
                    type: 'job',
                },
                classes: 'job',
            }));

            // Create user data nodes
            if (userData) {
                for (const ud of userData) {
                    nodes.push({
                        data: {
                            id: `ud-${ud.id}`,
                            label: this.truncateName(ud.name || `UserData ${ud.id}`),
                            type: 'userdata',
                        },
                        classes: 'userdata',
                    });
                }
            }

            // Create edges from relationships
            const edges = [];
            if (relationships) {
                for (const rel of relationships) {
                    if (rel.relationship_type === 'input') {
                        edges.push({
                            data: {
                                source: `ud-${rel.user_data_id}`,
                                target: `job-${rel.job_id}`,
                            },
                            classes: 'input',
                        });
                    } else if (rel.relationship_type === 'output') {
                        edges.push({
                            data: {
                                source: `job-${rel.job_id}`,
                                target: `ud-${rel.user_data_id}`,
                            },
                            classes: 'output',
                        });
                    }
                }
            }

            this.renderGraph(nodes, edges);
        } catch (error) {
            console.error('Error loading user data relationships:', error);
            this.showEmpty('Error loading user data relationships: ' + error.message);
        }
    }

    renderGraph(nodes, edges) {
        if (!this.cy) {
            this.initialize();
        }

        // Clear existing elements
        this.cy.elements().remove();

        // Add new elements
        this.cy.add([...nodes, ...edges]);

        // Apply dagre layout
        this.cy.layout({
            name: 'dagre',
            rankDir: 'TB',  // Top to Bottom
            nodeSep: 50,
            rankSep: 80,
            padding: 30,
            animate: true,
            animationDuration: 300,
        }).run();

        // Show legend
        const legend = document.getElementById('dag-legend');
        if (legend) {
            legend.style.display = 'flex';
        }
    }

    showEmpty(message) {
        const container = document.getElementById(this.containerId);
        if (container) {
            container.innerHTML = `<div class="placeholder-message">${message}</div>`;
        }

        const legend = document.getElementById('dag-legend');
        if (legend) {
            legend.style.display = 'none';
        }
    }

    showNodeInfo(data) {
        // Could show a tooltip or sidebar with node details
        console.log('Node info:', data);
    }

    fitToView() {
        if (this.cy) {
            this.cy.fit(50);
        }
    }

    truncateName(name, maxLen = 20) {
        if (name.length > maxLen) {
            return name.substring(0, maxLen - 3) + '...';
        }
        return name;
    }

    async refresh() {
        if (!this.currentWorkflowId) return;

        switch (this.currentType) {
            case 'jobs':
                await this.loadJobDependencies(this.currentWorkflowId);
                break;
            case 'files':
                await this.loadFileRelationships(this.currentWorkflowId);
                break;
            case 'userdata':
                await this.loadUserDataRelationships(this.currentWorkflowId);
                break;
        }
    }

    destroy() {
        if (this.cy) {
            this.cy.destroy();
            this.cy = null;
        }
    }
}

// Export
const dagVisualizer = new DAGVisualizer('dag-container');