X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/4a8d85d10073d2555253bdb631d293eaf7deccbf..c354d605e3a09baa791be57443c2a0ea32d5bd67:/src/models/workflow.ts diff --git a/src/models/workflow.ts b/src/models/workflow.ts index 2054550d..abc92c62 100644 --- a/src/models/workflow.ts +++ b/src/models/workflow.ts @@ -2,12 +2,180 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { Resource } from "../common/api/common-resource-service"; -import { ResourceKind } from "./kinds"; +import { Resource, ResourceKind } from "./resource"; +import { safeLoad } from 'js-yaml'; export interface WorkflowResource extends Resource { - kind: ResourceKind.Workflow; + kind: ResourceKind.WORKFLOW; name: string; description: string; definition: string; -} \ No newline at end of file +} +export interface WorkflowResourceDefinition { + cwlVersion: string; + $graph?: Array; +} +export interface Workflow { + class: 'Workflow'; + doc?: string; + id?: string; + inputs: CommandInputParameter[]; + outputs: any[]; + steps: any[]; +} + +export interface CommandLineTool { + class: 'CommandLineTool'; + id: string; + inputs: CommandInputParameter[]; + outputs: any[]; +} + +export type CommandInputParameter = + BooleanCommandInputParameter | + IntCommandInputParameter | + LongCommandInputParameter | + FloatCommandInputParameter | + DoubleCommandInputParameter | + StringCommandInputParameter | + FileCommandInputParameter | + DirectoryCommandInputParameter | + StringArrayCommandInputParameter | + IntArrayCommandInputParameter | + FloatArrayCommandInputParameter | + FileArrayCommandInputParameter | + DirectoryArrayCommandInputParameter | + EnumCommandInputParameter; + +export enum CWLType { + NULL = 'null', + BOOLEAN = 'boolean', + INT = 'int', + LONG = 'long', + FLOAT = 'float', + DOUBLE = 'double', + STRING = 'string', + FILE = 'File', + DIRECTORY = 'Directory', +} + +export interface CommandInputEnumSchema { + symbols: string[]; + type: 'enum'; + label?: string; + name?: string; +} + +export interface CommandInputArraySchema { + items: ItemType; + type: 'array'; + label?: string; +} + +export interface File { + class: CWLType.FILE; + location?: string; + path?: string; + basename?: string; +} + +export interface Directory { + class: CWLType.DIRECTORY; + location?: string; + path?: string; + basename?: string; +} + +export interface GenericCommandInputParameter { + id: string; + label?: string; + doc?: string | string[]; + default?: Value; + type?: Type | Array; + value?: Value; + disabled?: boolean; +} +export type GenericArrayCommandInputParameter = GenericCommandInputParameter, Value[]>; + +export type BooleanCommandInputParameter = GenericCommandInputParameter; +export type IntCommandInputParameter = GenericCommandInputParameter; +export type LongCommandInputParameter = GenericCommandInputParameter; +export type FloatCommandInputParameter = GenericCommandInputParameter; +export type DoubleCommandInputParameter = GenericCommandInputParameter; +export type StringCommandInputParameter = GenericCommandInputParameter; +export type FileCommandInputParameter = GenericCommandInputParameter; +export type DirectoryCommandInputParameter = GenericCommandInputParameter; +export type EnumCommandInputParameter = GenericCommandInputParameter; + +export type StringArrayCommandInputParameter = GenericArrayCommandInputParameter; +export type IntArrayCommandInputParameter = GenericArrayCommandInputParameter; +export type FloatArrayCommandInputParameter = GenericArrayCommandInputParameter; +export type FileArrayCommandInputParameter = GenericArrayCommandInputParameter; +export type DirectoryArrayCommandInputParameter = GenericArrayCommandInputParameter; + +export type WorkflowInputsData = { + [key: string]: boolean | number | string | File | Directory; +}; +export const parseWorkflowDefinition = (workflow: WorkflowResource): WorkflowResourceDefinition => { + const definition = safeLoad(workflow.definition); + return definition; +}; + +export const getWorkflow = (workflowDefinition: WorkflowResourceDefinition) => { + if (!workflowDefinition.$graph) { return undefined; } + const mainWorkflow = workflowDefinition.$graph.find(item => item.class === 'Workflow' && item.id === '#main'); + return mainWorkflow + ? mainWorkflow + : undefined; +}; + +export const getWorkflowInputs = (workflowDefinition: WorkflowResourceDefinition) => { + if (!workflowDefinition) { return undefined; } + return getWorkflow(workflowDefinition) + ? getWorkflow(workflowDefinition)!.inputs + : undefined; +}; + +export const getInputLabel = (input: CommandInputParameter) => { + return `${input.label || input.id}`; +}; + +export const isRequiredInput = ({ type }: CommandInputParameter) => { + if (type instanceof Array) { + for (const t of type) { + if (t === CWLType.NULL) { + return false; + } + } + } + return true; +}; + +export const isPrimitiveOfType = (input: GenericCommandInputParameter, type: CWLType) => + input.type instanceof Array + ? input.type.indexOf(type) > -1 + : input.type === type; + +export const isArrayOfType = (input: GenericCommandInputParameter, type: CWLType) => + typeof input.type === 'object' && + input.type.type === 'array' + ? input.type.items === type + : false; + +export const stringifyInputType = ({ type }: CommandInputParameter) => { + if (typeof type === 'string') { + return type; + } else if (type instanceof Array) { + return type.join(' | '); + } else if (typeof type === 'object') { + if (type.type === 'enum') { + return 'enum'; + } else if (type.type === 'array') { + return `${type.items}[]`; + } else { + return 'unknown'; + } + } else { + return 'unknown'; + } +};