Merge branch 'master' into 14231-multiple-collections-input
[arvados-workbench2.git] / src / models / workflow.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Resource, ResourceKind } from "./resource";
6 import { safeLoad } from 'js-yaml';
7
8 export interface WorkflowResource extends Resource {
9     kind: ResourceKind.WORKFLOW;
10     name: string;
11     description: string;
12     definition: string;
13 }
14 export interface WorkflowResoruceDefinition {
15     cwlVersion: string;
16     $graph: Array<Workflow | CommandLineTool>;
17 }
18 export interface Workflow {
19     class: 'Workflow';
20     doc?: string;
21     id?: string;
22     inputs: CommandInputParameter[];
23     outputs: any[];
24     steps: any[];
25 }
26
27 export interface CommandLineTool {
28     class: 'CommandLineTool';
29     id: string;
30     inputs: CommandInputParameter[];
31     outputs: any[];
32 }
33
34 export type CommandInputParameter =
35     BooleanCommandInputParameter |
36     IntCommandInputParameter |
37     LongCommandInputParameter |
38     FloatCommandInputParameter |
39     DoubleCommandInputParameter |
40     StringCommandInputParameter |
41     FileCommandInputParameter |
42     DirectoryCommandInputParameter |
43     StringArrayCommandInputParameter |
44     FileArrayCommandInputParameter |
45     DirectoryArrayCommandInputParameter |
46     EnumCommandInputParameter;
47
48 export enum CWLType {
49     NULL = 'null',
50     BOOLEAN = 'boolean',
51     INT = 'int',
52     LONG = 'long',
53     FLOAT = 'float',
54     DOUBLE = 'double',
55     STRING = 'string',
56     FILE = 'File',
57     DIRECTORY = 'Directory',
58 }
59
60 export interface CommandInputEnumSchema {
61     symbols: string[];
62     type: 'enum';
63     label?: string;
64     name?: string;
65 }
66
67 export interface CommandInputArraySchema<ItemType> {
68     items: ItemType;
69     type: 'array';
70     label?: string;
71 }
72
73 export interface File {
74     class: CWLType.FILE;
75     location?: string;
76     path?: string;
77     basename?: string;
78 }
79
80 export interface Directory {
81     class: CWLType.DIRECTORY;
82     location?: string;
83     path?: string;
84     basename?: string;
85 }
86
87 export interface GenericCommandInputParameter<Type, Value> {
88     id: string;
89     label?: string;
90     doc?: string | string[];
91     default?: Value;
92     type?: Type | Array<Type | CWLType.NULL>;
93 }
94 export type GenericArrayCommandInputParameter<Type, Value> = GenericCommandInputParameter<CommandInputArraySchema<Type>, Value[]>;
95
96 export type BooleanCommandInputParameter = GenericCommandInputParameter<CWLType.BOOLEAN, boolean>;
97 export type IntCommandInputParameter = GenericCommandInputParameter<CWLType.INT, number>;
98 export type LongCommandInputParameter = GenericCommandInputParameter<CWLType.LONG, number>;
99 export type FloatCommandInputParameter = GenericCommandInputParameter<CWLType.FLOAT, number>;
100 export type DoubleCommandInputParameter = GenericCommandInputParameter<CWLType.DOUBLE, number>;
101 export type StringCommandInputParameter = GenericCommandInputParameter<CWLType.STRING, string>;
102 export type FileCommandInputParameter = GenericCommandInputParameter<CWLType.FILE, File>;
103 export type DirectoryCommandInputParameter = GenericCommandInputParameter<CWLType.DIRECTORY, Directory>;
104 export type EnumCommandInputParameter = GenericCommandInputParameter<CommandInputEnumSchema, string>;
105
106 export type StringArrayCommandInputParameter = GenericArrayCommandInputParameter<CWLType.STRING, string>;
107 export type FileArrayCommandInputParameter = GenericArrayCommandInputParameter<CWLType.FILE, File>;
108 export type DirectoryArrayCommandInputParameter = GenericArrayCommandInputParameter<CWLType.DIRECTORY, Directory>;
109
110 export type WorkflowInputsData = {
111     [key: string]: boolean | number | string | File | Directory;
112 };
113 export const parseWorkflowDefinition = (workflow: WorkflowResource): WorkflowResoruceDefinition => {
114     const definition = safeLoad(workflow.definition);
115     return definition;
116 };
117
118 export const getWorkflowInputs = (workflowDefinition: WorkflowResoruceDefinition) => {
119     const mainWorkflow = workflowDefinition.$graph.find(item => item.class === 'Workflow' && item.id === '#main');
120     return mainWorkflow
121         ? mainWorkflow.inputs
122         : undefined;
123 };
124 export const getInputLabel = (input: CommandInputParameter) => {
125     return `${input.label || input.id}`;
126 };
127
128 export const isRequiredInput = ({ type }: CommandInputParameter) => {
129     if (type instanceof Array) {
130         for (const t of type) {
131             if (t === CWLType.NULL) {
132                 return false;
133             }
134         }
135     }
136     return true;
137 };
138
139 export const isPrimitiveOfType = (input: GenericCommandInputParameter<any, any>, type: CWLType) =>
140     input.type instanceof Array
141         ? input.type.indexOf(type) > -1
142         : input.type === type;
143
144 export const isArrayOfType = (input: GenericCommandInputParameter<any, any>, type: CWLType) =>
145     typeof input.type === 'object' &&
146         input.type.type === 'array'
147         ? input.type.items === type
148         : false;
149
150 export const stringifyInputType = ({ type }: CommandInputParameter) => {
151     if (typeof type === 'string') {
152         return type;
153     } else if (type instanceof Array) {
154         return type.join(' | ');
155     } else if (typeof type === 'object') {
156         if (type.type === 'enum') {
157             return 'enum';
158         } else if (type.type === 'array') {
159             return `${type.items}[]`;
160         } else {
161             return 'unknown';
162         }
163     } else {
164         return 'unknown';
165     }
166 };