-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtool-utils.ts
More file actions
281 lines (237 loc) · 9.52 KB
/
Copy pathtool-utils.ts
File metadata and controls
281 lines (237 loc) · 9.52 KB
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
import _ from 'lodash/fp';
import { code } from 'react-hyperscript-helpers';
import { FileExtension, getExtension } from 'src/analysis/utils/file-utils';
import { App } from 'src/libs/ajax/leonardo/models/app-models';
import { isRuntime, Runtime } from 'src/libs/ajax/leonardo/models/runtime-models';
import { isCromwellAppVisible } from 'src/libs/config';
import * as Utils from 'src/libs/utils';
import { CloudProvider, cloudProviderTypes } from 'src/workspaces/utils';
export type RuntimeToolLabel = 'Jupyter' | 'RStudio' | 'JupyterLab';
export type AppToolLabel = 'GALAXY' | 'CROMWELL' | 'WDS' | 'WORKFLOWS_APP' | 'CROMWELL_RUNNER_APP';
export type CromwellAppToolLabel = 'CROMWELL' | 'WORKFLOWS_APP' | 'CROMWELL_RUNNER_APP';
export type AppAccessScope = 'USER_PRIVATE' | 'WORKSPACE_SHARED';
export type LaunchableToolLabel = 'spark' | 'terminal' | 'RStudio' | 'JupyterLab';
export type ToolLabel = RuntimeToolLabel | AppToolLabel;
export const launchableToolLabel: Record<LaunchableToolLabel, LaunchableToolLabel> = {
RStudio: 'RStudio',
JupyterLab: 'JupyterLab',
terminal: 'terminal',
spark: 'spark',
};
export const runtimeToolLabels = {
Jupyter: 'Jupyter',
RStudio: 'RStudio',
JupyterLab: 'JupyterLab',
} as const satisfies Record<RuntimeToolLabel, RuntimeToolLabel>;
export const toolLabelDisplays: Record<ToolLabel, string> = {
Jupyter: 'Jupyter',
RStudio: 'RStudio',
JupyterLab: 'JupyterLab',
GALAXY: 'Galaxy',
CROMWELL: 'Cromwell',
WORKFLOWS_APP: 'Workflows',
CROMWELL_RUNNER_APP: 'Cromwell runner',
WDS: 'Workspace Data Service',
};
export const appToolLabels: Record<AppToolLabel, AppToolLabel> = {
GALAXY: 'GALAXY',
CROMWELL: 'CROMWELL',
WORKFLOWS_APP: 'WORKFLOWS_APP',
CROMWELL_RUNNER_APP: 'CROMWELL_RUNNER_APP',
WDS: 'WDS',
};
export const cromwellAppToolLabels: Record<CromwellAppToolLabel, CromwellAppToolLabel> = {
CROMWELL: 'CROMWELL',
WORKFLOWS_APP: 'WORKFLOWS_APP',
CROMWELL_RUNNER_APP: 'CROMWELL_RUNNER_APP',
};
export const appAccessScopes: Record<AppAccessScope, AppAccessScope> = {
USER_PRIVATE: 'USER_PRIVATE',
WORKSPACE_SHARED: 'WORKSPACE_SHARED',
};
export const isAppToolLabel = (x: ToolLabel): x is AppToolLabel => x in appToolLabels;
export const isRuntimeToolLabel = (x: ToolLabel): x is RuntimeToolLabel => x in runtimeToolLabels;
export const isToolLabel = (x: ToolLabel | string): x is ToolLabel => x in appToolLabels || x in runtimeToolLabels;
export interface BaseTool {
isLaunchUnsupported?: boolean;
isPauseUnsupported?: boolean;
}
export interface RuntimeTool extends BaseTool {
label: RuntimeToolLabel;
ext: FileExtension[];
imageIds: string[];
defaultImageId: string;
defaultExt: FileExtension;
}
export interface AppTool extends BaseTool {
label: AppToolLabel; // Alias for appType
}
export type Tool = AppTool | RuntimeTool;
export const terraSupportedRuntimeImageIds: string[] = [
'terra-base',
'terra-jupyter-bioconductor',
'terra-jupyter-hail',
'terra-jupyter-python',
'terra-jupyter-gatk',
'RStudio',
];
const RStudio: RuntimeTool = {
label: runtimeToolLabels.RStudio,
ext: ['Rmd', 'R'] as FileExtension[],
imageIds: ['RStudio', 'LegacyRStudio'],
defaultImageId: 'RStudio',
defaultExt: 'Rmd' as FileExtension,
};
const Jupyter: RuntimeTool = {
label: runtimeToolLabels.Jupyter,
ext: ['ipynb' as FileExtension],
imageIds: [
'terra-base',
'terra-jupyter-bioconductor',
'terra-jupyter-bioconductor_legacy',
'terra-jupyter-hail',
'terra-jupyter-python',
'terra-jupyter-gatk',
'Pegasus',
'terra-jupyter-gatk_legacy',
],
defaultImageId: 'terra-jupyter-gatk',
isLaunchUnsupported: true,
defaultExt: 'ipynb' as FileExtension,
};
const JupyterLab: RuntimeTool = {
label: runtimeToolLabels.JupyterLab,
ext: ['ipynb' as FileExtension],
isLaunchUnsupported: false,
defaultExt: 'ipynb' as FileExtension,
imageIds: [],
defaultImageId: '',
};
// TODO: Reenable pause for Galaxy once https://broadworkbench.atlassian.net/browse/PROD-905 is resolved
const Galaxy = { label: 'GALAXY', isPauseUnsupported: true } as const satisfies AppTool;
const Cromwell = { label: 'CROMWELL', isPauseUnsupported: true } as const satisfies AppTool;
const Workflows = { label: 'WORKFLOWS_APP', isPauseUnsupported: true } as const satisfies AppTool;
const CromwellRunner = { label: 'CROMWELL_RUNNER_APP', isPauseUnsupported: true } as const satisfies AppTool;
const Wds = { label: 'WDS', isPauseUnsupported: true } as const satisfies AppTool;
export const appTools: Record<AppToolLabel, AppTool> = {
GALAXY: Galaxy,
CROMWELL: Cromwell,
WORKFLOWS_APP: Workflows,
CROMWELL_RUNNER_APP: CromwellRunner,
WDS: Wds,
};
export const runtimeTools: Record<RuntimeToolLabel, RuntimeTool> = {
RStudio,
Jupyter,
// azure should be changed in backend to be a same as jupyter
// we should use analysis file's cloud provider to determine azure/other logic for jupyter
JupyterLab,
};
export const tools: Record<ToolLabel, AppTool | RuntimeTool> = {
...runtimeTools,
...appTools,
};
// The order of the array is important, it decides the order in AnalysisModal.
export const cloudRuntimeTools = {
GCP: [Jupyter, RStudio],
AZURE: [JupyterLab],
} as const satisfies Record<CloudProvider, readonly RuntimeTool[]>;
export const cloudAppTools = {
GCP: [Galaxy, Cromwell],
AZURE: [Cromwell],
} as const satisfies Record<CloudProvider, readonly AppTool[]>;
export interface ExtensionDisplay {
label: string;
value: FileExtension;
}
export const toolExtensionDisplay: Partial<Record<ToolLabel, ExtensionDisplay[]>> = {
RStudio: [
{ label: 'R Markdown (.Rmd)', value: 'Rmd' as FileExtension },
{ label: 'R Script (.R)', value: 'R' as FileExtension },
],
Jupyter: [{ label: 'IPython Notebook (.ipynb)', value: 'ipynb' as FileExtension }],
};
export const getPatternFromRuntimeTool = (toolLabel: RuntimeToolLabel): string => {
const patterns: Record<RuntimeToolLabel, string> = {
[runtimeToolLabels.RStudio]: '.+(\\.R|\\.Rmd)$',
[runtimeToolLabels.Jupyter]: '.*\\.ipynb',
[runtimeToolLabels.JupyterLab]: '.*\\.ipynb',
};
return patterns[toolLabel];
};
export const getToolsToDisplayForCloudProvider = (cloudProvider: CloudProvider): readonly Tool[] =>
_.remove((tool: Tool) => isToolHidden(tool.label, cloudProvider))(
(cloudRuntimeTools[cloudProvider] as readonly Tool[]).concat(cloudAppTools[cloudProvider] as readonly Tool[])
);
export const toolToExtensionMap: Record<ToolLabel, FileExtension> = _.flow(
_.filter('ext'),
_.map((tool: RuntimeTool) => ({ [tool.label]: tool.ext })),
_.reduce(_.merge, {})
)(runtimeTools);
export type AnalysisFileExtension = 'Rmd' | 'R' | 'ipynb';
const extensionToToolMap: Record<AnalysisFileExtension, ToolLabel> = {
Rmd: runtimeTools.RStudio.label,
R: runtimeTools.RStudio.label,
ipynb: runtimeTools.Jupyter.label,
};
export const getToolLabelForImage = (imageId: string): ToolLabel | undefined => {
const isImageInTool = (runtimeTool) => runtimeTool.imageIds.includes(imageId);
return _.find(isImageInTool, runtimeTools)?.label;
};
// Currently, a lot of consumers of this are not typescript, so we defensively use `getExtension` on the input
// TODO: Once all consumer are in typescript, we can remove `getExtension` from this function
export const getToolLabelFromFileExtension = (fileName: FileExtension): ToolLabel =>
extensionToToolMap[getExtension(fileName)];
export const getToolLabelFromRuntime = (cloudEnv: Runtime): RuntimeToolLabel =>
cloudEnv?.labels?.tool as RuntimeToolLabel;
export const getToolLabelFromApp = (cloudEnv: App): ToolLabel => cloudEnv?.appType as ToolLabel;
export const getToolLabelFromCloudEnv = (cloudEnv: Runtime | App): ToolLabel => {
if (isRuntime(cloudEnv)) return getToolLabelFromRuntime(cloudEnv);
return getToolLabelFromApp(cloudEnv);
};
// Returns registered appTypes.
export const allAppTypes: AppToolLabel[] = _.flow(_.map('label'), _.compact)(appTools);
export const isPauseSupported = (toolLabel: ToolLabel): boolean =>
!_.find((tool: AppTool | RuntimeTool) => tool.label === toolLabel)(tools)?.isPauseUnsupported;
export const isToolHidden = (toolLabel: ToolLabel, cloudProvider: CloudProvider): boolean =>
Utils.cond(
[
toolLabel === appToolLabels.CROMWELL,
() =>
Utils.cond(
[cloudProvider === cloudProviderTypes.GCP, () => !isCromwellAppVisible()],
[
cloudProvider === cloudProviderTypes.AZURE,
() => {
return true;
},
],
[Utils.DEFAULT, () => false]
),
],
[Utils.DEFAULT, () => false]
);
export type MountPoint = '/home/rstudio' | '/home/jupyter' | '/home/jupyter/persistent_disk';
export const mountPoints: Record<RuntimeToolLabel, MountPoint> = {
RStudio: '/home/rstudio',
Jupyter: '/home/jupyter',
JupyterLab: '/home/jupyter/persistent_disk',
};
export const getMountDir = (toolLabel: RuntimeToolLabel): MountPoint => {
if (toolLabel === runtimeToolLabels.RStudio) return mountPoints.RStudio;
if (toolLabel === runtimeToolLabels.Jupyter) return mountPoints.Jupyter;
return mountPoints.JupyterLab;
};
export const getCurrentMountDirectory = (toolLabel: RuntimeToolLabel) => {
const boldCode = function (label: RuntimeToolLabel) {
const mydir = getMountDir(label);
return code({ style: { fontWeight: 600 } }, [`${mydir}`]);
};
const defaultMsg = [
boldCode(runtimeToolLabels.Jupyter),
' for Jupyter environments and ',
boldCode(runtimeToolLabels.RStudio),
' for RStudio environments',
];
return typeof toolLabel === 'string' ? [boldCode(toolLabel)] : defaultMsg; // TODO: remove string check IA-4091
};