Merge branch 'master'
[arvados-workbench2.git] / src / index.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import * as ReactDOM from 'react-dom';
7 import { Provider } from "react-redux";
8 import { MainPanel } from './views/main-panel/main-panel';
9 import './index.css';
10 import { Route } from 'react-router';
11 import createBrowserHistory from "history/createBrowserHistory";
12 import { History } from "history";
13 import { configureStore, RootStore } from './store/store';
14 import { ConnectedRouter } from "react-router-redux";
15 import { ApiToken } from "./views-components/api-token/api-token";
16 import { initAuth } from "./store/auth/auth-action";
17 import { createServices } from "./services/services";
18 import { MuiThemeProvider } from '@material-ui/core/styles';
19 import { CustomTheme } from './common/custom-theme';
20 import { fetchConfig } from './common/config';
21 import { addMenuActionSet, ContextMenuKind } from './views-components/context-menu/context-menu';
22 import { rootProjectActionSet } from "./views-components/context-menu/action-sets/root-project-action-set";
23 import { projectActionSet } from "./views-components/context-menu/action-sets/project-action-set";
24 import { resourceActionSet } from './views-components/context-menu/action-sets/resource-action-set';
25 import { favoriteActionSet } from "./views-components/context-menu/action-sets/favorite-action-set";
26 import { collectionFilesActionSet } from './views-components/context-menu/action-sets/collection-files-action-set';
27 import { collectionFilesItemActionSet } from './views-components/context-menu/action-sets/collection-files-item-action-set';
28 import { collectionActionSet } from './views-components/context-menu/action-sets/collection-action-set';
29 import { collectionResourceActionSet } from './views-components/context-menu/action-sets/collection-resource-action-set';
30 import { processActionSet } from './views-components/context-menu/action-sets/process-action-set';
31 import { loadWorkbench } from './store/workbench/workbench-actions';
32 import { Routes } from '~/routes/routes';
33 import { trashActionSet } from "~/views-components/context-menu/action-sets/trash-action-set";
34 import { ServiceRepository } from '~/services/services';
35 import { initWebSocket } from '~/websocket/websocket';
36 import { Config } from '~/common/config';
37 import { addRouteChangeHandlers } from './routes/route-change-handlers';
38 import { setCurrentTokenDialogApiHost } from '~/store/current-token-dialog/current-token-dialog-actions';
39 import { processResourceActionSet } from '~/views-components/context-menu/action-sets/process-resource-action-set';
40 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
41 import { setUuidPrefix } from '~/store/workflow-panel/workflow-panel-actions';
42 import { trashedCollectionActionSet } from '~/views-components/context-menu/action-sets/trashed-collection-action-set';
43 import { ContainerRequestState } from '~/models/container-request';
44 import { MountKind } from '~/models/mount-types';
45 import { receiveTreePickerData, loadUserProject } from '~/store/tree-picker/tree-picker-actions';
46 import { loadProject, loadCollection, initUserProject } from './store/tree-picker/tree-picker-actions';
47 import { ResourceKind } from '~/models/resource';
48
49 const getBuildNumber = () => "BN-" + (process.env.REACT_APP_BUILD_NUMBER || "dev");
50 const getGitCommit = () => "GIT-" + (process.env.REACT_APP_GIT_COMMIT || "latest").substr(0, 7);
51 const getBuildInfo = () => getBuildNumber() + " / " + getGitCommit();
52
53 const buildInfo = getBuildInfo();
54
55 console.log(`Starting arvados [${buildInfo}]`);
56
57 addMenuActionSet(ContextMenuKind.ROOT_PROJECT, rootProjectActionSet);
58 addMenuActionSet(ContextMenuKind.PROJECT, projectActionSet);
59 addMenuActionSet(ContextMenuKind.RESOURCE, resourceActionSet);
60 addMenuActionSet(ContextMenuKind.FAVORITE, favoriteActionSet);
61 addMenuActionSet(ContextMenuKind.COLLECTION_FILES, collectionFilesActionSet);
62 addMenuActionSet(ContextMenuKind.COLLECTION_FILES_ITEM, collectionFilesItemActionSet);
63 addMenuActionSet(ContextMenuKind.COLLECTION, collectionActionSet);
64 addMenuActionSet(ContextMenuKind.COLLECTION_RESOURCE, collectionResourceActionSet);
65 addMenuActionSet(ContextMenuKind.TRASHED_COLLECTION, trashedCollectionActionSet);
66 addMenuActionSet(ContextMenuKind.PROCESS, processActionSet);
67 addMenuActionSet(ContextMenuKind.PROCESS_RESOURCE, processResourceActionSet);
68 addMenuActionSet(ContextMenuKind.TRASH, trashActionSet);
69
70 fetchConfig()
71     .then(({ config, apiHost }) => {
72         const history = createBrowserHistory();
73         const services = createServices(config, {
74             progressFn: (id, working) => {
75                 store.dispatch(progressIndicatorActions.TOGGLE_WORKING({ id, working }));
76             },
77             errorFn: (id, error) => {
78                 // console.error("Backend error:", error);
79                 // store.dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Backend error", kind: SnackbarKind.ERROR }));
80             }
81         });
82         const store = configureStore(history, services);
83
84         store.subscribe(initListener(history, store, services, config));
85         store.dispatch(initAuth());
86         store.dispatch(setCurrentTokenDialogApiHost(apiHost));
87         store.dispatch(setUuidPrefix(config.uuidPrefix));
88
89         const TokenComponent = (props: any) => <ApiToken authService={services.authService} {...props} />;
90         const MainPanelComponent = (props: any) => <MainPanel buildInfo={buildInfo} {...props} />;
91
92         const App = () =>
93             <MuiThemeProvider theme={CustomTheme}>
94                 <Provider store={store}>
95                     <ConnectedRouter history={history}>
96                         <div>
97                             <Route path={Routes.TOKEN} component={TokenComponent} />
98                             <Route path={Routes.ROOT} component={MainPanelComponent} />
99                         </div>
100                     </ConnectedRouter>
101                 </Provider>
102             </MuiThemeProvider>;
103
104         ReactDOM.render(
105             <App />,
106             document.getElementById('root') as HTMLElement
107         );
108     });
109
110 const initListener = (history: History, store: RootStore, services: ServiceRepository, config: Config) => {
111     let initialized = false;
112     return async () => {
113         const { router, auth } = store.getState();
114         if (router.location && auth.user && !initialized) {
115             initialized = true;
116             initWebSocket(config, services.authService, store);
117             await store.dispatch(loadWorkbench());
118             addRouteChangeHandlers(history, store);
119             // createEnumCollectorWorkflow(services);
120             store.dispatch(initUserProject('testPicker1'));
121             store.dispatch(initUserProject('testPicker2'));
122             store.dispatch(initUserProject('testPicker3'));
123             // await store.dispatch(loadCollection(
124             //     'c97qk-4zz18-9sn8ygaf62chkkd',
125             //     'testPicker',
126             // ));
127         }
128     };
129 };
130
131 const createPrimitivesCollectorWorkflow = ({ workflowService }: ServiceRepository) => {
132     workflowService.create({
133         name: 'Primitive values collector',
134         description: 'Workflow for collecting primitive values',
135         definition: "cwlVersion: v1.0\n$graph:\n- class: CommandLineTool\n  requirements:\n  - listing:\n    - entryname: input_collector.log\n      entry: |\n        \"flag\":\n          $(inputs.example_flag)\n        \"string\":\n          $(inputs.example_string)\n        \"int\":\n          $(inputs.example_int)\n        \"long\":\n          $(inputs.example_long)\n        \"float\":\n          $(inputs.example_float)\n        \"double\":\n          $(inputs.example_double)\n    class: InitialWorkDirRequirement\n  inputs:\n  - type: double\n    id: '#input_collector.cwl/example_double'\n  - type: boolean\n    id: '#input_collector.cwl/example_flag'\n  - type: float\n    id: '#input_collector.cwl/example_float'\n  - type: int\n    id: '#input_collector.cwl/example_int'\n  - type: long\n    id: '#input_collector.cwl/example_long'\n  - type: string\n    id: '#input_collector.cwl/example_string'\n  outputs:\n  - type: File\n    outputBinding:\n      glob: '*'\n    id: '#input_collector.cwl/output'\n  baseCommand: [echo]\n  id: '#input_collector.cwl'\n- class: Workflow\n  doc: Workflw for collecting primitive values\n  inputs:\n  - type: double\n    label: Double value\n    doc: This should allow for entering a decimal number (64-bit).\n    id: '#main/example_double'\n    default: 0.3333333333333333\n  - type: boolean\n    label: Boolean Flag\n    doc: This should render as in checkbox.\n    id: '#main/example_flag'\n    default: true\n  - type: float\n    label: Float value\n    doc: This should allow for entering a decimal number (32-bit).\n    id: '#main/example_float'\n    default: 0.15625\n  - type: int\n    label: Integer Number\n    doc: This should allow for entering a number (32-bit signed).\n    id: '#main/example_int'\n    default: 2147483647\n  - type: long\n    label: Long Number\n    doc: This should allow for entering a number (64-bit signed).\n    id: '#main/example_long'\n    default: 9223372036854775807\n  - type: string\n    label: Freetext\n    doc: This should allow for entering an arbitrary char sequence.\n    id: '#main/example_string'\n    default: This is a string\n  outputs:\n  - type: File\n    outputSource: '#main/input_collector/output'\n    id: '#main/log_file'\n  steps:\n  - run: '#input_collector.cwl'\n    in:\n    - source: '#main/example_double'\n      id: '#main/input_collector/example_double'\n    - source: '#main/example_flag'\n      id: '#main/input_collector/example_flag'\n    - source: '#main/example_float'\n      id: '#main/input_collector/example_float'\n    - source: '#main/example_int'\n      id: '#main/input_collector/example_int'\n    - source: '#main/example_long'\n      id: '#main/input_collector/example_long'\n    - source: '#main/example_string'\n      id: '#main/input_collector/example_string'\n    out: ['#main/input_collector/output']\n    id: '#main/input_collector'\n  id: '#main'\n",
136     });
137 };
138
139 const createEnumCollectorWorkflow = ({ workflowService }: ServiceRepository) => {
140     workflowService.create({
141         name: 'Enum values collector',
142         description: 'Workflow for collecting enum values',
143         definition: "cwlVersion: v1.0\n$graph:\n- class: CommandLineTool\n  requirements:\n  - listing:\n    - entryname: input_collector.log\n      entry: |\n        \"enum_type\":\n          $(inputs.enum_type)\n\n    class: InitialWorkDirRequirement\n  inputs:\n  - type:\n      type: enum\n      symbols: ['#input_collector.cwl/enum_type/OTU table', '#input_collector.cwl/enum_type/Pathway\n          table', '#input_collector.cwl/enum_type/Function table', '#input_collector.cwl/enum_type/Ortholog\n          table']\n    id: '#input_collector.cwl/enum_type'\n  outputs:\n  - type: File\n    outputBinding:\n      glob: '*'\n    id: '#input_collector.cwl/output'\n  baseCommand: [echo]\n  id: '#input_collector.cwl'\n- class: Workflow\n  doc: This is the description of the workflow\n  inputs:\n  - type:\n      type: enum\n      symbols: ['#main/enum_type/OTU table', '#main/enum_type/Pathway table', '#main/enum_type/Function\n          table', '#main/enum_type/Ortholog table']\n      name: '#enum_typef4179c7f-45f9-482d-a5db-1abb86698384'\n    label: Enumeration Type\n    doc: This should render as a drop-down menu.\n    id: '#main/enum_type'\n    default: OTU table\n  outputs:\n  - type: File\n    outputSource: '#main/input_collector/output'\n    id: '#main/log_file'\n  steps:\n  - run: '#input_collector.cwl'\n    in:\n    - source: '#main/enum_type'\n      id: '#main/input_collector/enum_type'\n    out: ['#main/input_collector/output']\n    id: '#main/input_collector'\n  id: '#main'\n",
144     });
145 };
146
147 const createSampleProcess = ({ containerRequestService }: ServiceRepository) => {
148     containerRequestService.create({
149         ownerUuid: 'c97qk-j7d0g-s3ngc1z0748hsmf',
150         name: 'Simple process 7',
151         state: ContainerRequestState.COMMITTED,
152         mounts: {
153             '/var/spool/cwl': {
154                 kind: MountKind.COLLECTION,
155                 writable: true,
156             },
157             'stdout': {
158                 kind: MountKind.MOUNTED_FILE,
159                 path: '/var/spool/cwl/cwl.output.json'
160             },
161             '/var/lib/cwl/workflow.json': {
162                 kind: MountKind.JSON,
163                 content: {
164                     "cwlVersion": "v1.0",
165                     "$graph": [
166                         {
167                             "class": "CommandLineTool",
168                             "requirements": [
169                                 {
170                                     "listing": [
171                                         {
172                                             "entryname": "input_collector.log",
173                                             "entry": "$(inputs.single_file.basename)\n"
174                                         }
175                                     ],
176                                     "class": "InitialWorkDirRequirement"
177                                 }
178                             ],
179                             "inputs": [
180                                 {
181                                     "type": "File",
182                                     "id": "#input_collector.cwl/single_file"
183                                 }
184                             ],
185                             "outputs": [
186                                 {
187                                     "type": "File",
188                                     "outputBinding": {
189                                         "glob": "*"
190                                     },
191                                     "id": "#input_collector.cwl/output"
192                                 }
193                             ],
194                             "baseCommand": [
195                                 "echo"
196                             ],
197                             "id": "#input_collector.cwl"
198                         },
199                         {
200                             "class": "Workflow",
201                             "doc": "This is the description of the workflow",
202                             "inputs": [
203                                 {
204                                     "type": "File",
205                                     "label": "Single File",
206                                     "doc": "This should allow for single File selection only.",
207                                     "id": "#main/single_file"
208                                 }
209                             ],
210                             "outputs": [
211                                 {
212                                     "type": "File",
213                                     "outputSource": "#main/input_collector/output",
214                                     "id": "#main/log_file"
215                                 }
216                             ],
217                             "steps": [
218                                 {
219                                     "run": "#input_collector.cwl",
220                                     "in": [
221                                         {
222                                             "source": "#main/single_file",
223                                             "id": "#main/input_collector/single_file"
224                                         }
225                                     ],
226                                     "out": [
227                                         "#main/input_collector/output"
228                                     ],
229                                     "id": "#main/input_collector"
230                                 }
231                             ],
232                             "id": "#main"
233                         }
234                     ]
235                 },
236             },
237             '/var/lib/cwl/cwl.input.json': {
238                 kind: MountKind.JSON,
239                 content: {
240                     "single_file": {
241                         "class": "File",
242                         "location": "keep:233454526794c0a2d56a305baeff3d30+145/1.txt",
243                         "basename": "fileA"
244                     }
245                 },
246             }
247         },
248         runtimeConstraints: {
249             API: true,
250             vcpus: 1,
251             ram: 1073741824,
252         },
253         containerImage: 'arvados/jobs:1.1.4.20180618144723',
254         cwd: '/var/spool/cwl',
255         command: [
256             'arvados-cwl-runner',
257             '--local',
258             '--api=containers',
259             "--project-uuid=c97qk-j7d0g-s3ngc1z0748hsmf",
260             '/var/lib/cwl/workflow.json#main',
261             '/var/lib/cwl/cwl.input.json'
262         ],
263         outputPath: '/var/spool/cwl',
264         priority: 1,
265     });
266 };
267