forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoolStore.ts
More file actions
352 lines (317 loc) · 10.9 KB
/
toolStore.ts
File metadata and controls
352 lines (317 loc) · 10.9 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
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
/**
* Requests tools, and various panel views
*/
import axios, { type AxiosResponse } from "axios";
import { defineStore } from "pinia";
import Vue, { computed, type Ref, ref, shallowRef } from "vue";
import { FAVORITES_KEYS, filterTools, type types_to_icons } from "@/components/Panels/utilities";
import { parseHelpForSummary } from "@/components/ToolsList/utilities";
import { useUserLocalStorage } from "@/composables/userLocalStorage";
import { getAppRoot } from "@/onload/loadConfig";
import { rethrowSimple } from "@/utils/simple-error";
export interface FilterSettings {
[key: string]: string | undefined;
name?: string;
section?: string;
ontology?: string;
id?: string;
owner?: string;
help?: string;
}
export interface Panel {
id: string;
model_class: string;
name: string;
description: string;
view_type: keyof typeof types_to_icons;
searchable: boolean;
}
export interface Tool {
model_class: string;
id: string;
name: string;
version: string;
description: string;
labels: string[];
edam_operations: string[];
edam_topics: string[];
hidden: "" | boolean;
is_workflow_compatible: boolean;
xrefs: string[];
config_file: string;
link: string;
min_width: number;
target: string;
panel_section_id: string;
panel_section_name: string | null;
form_style: string;
disabled?: boolean;
icon?: string;
tool_shed_repository?: {
name: string;
owner: string;
changeset_revision: string;
tool_shed: string;
};
}
export interface ToolSection {
model_class: string;
id: string;
name: string;
title?: string;
version?: string;
description?: string;
links?: Record<string, string>;
tools?: (string | ToolSectionLabel)[];
elems?: (Tool | ToolSection)[];
}
export interface ToolSectionLabel {
model_class: string;
id: string;
text: string;
version?: string;
description?: string | null;
links?: Record<string, string> | null;
}
export type ToolHelpData = {
help?: string;
summary?: string;
};
export const useToolStore = defineStore("toolStore", () => {
const currentPanelView: Ref<string> = useUserLocalStorage("tool-store-view", "");
const defaultPanelView: Ref<string> = ref("");
const loading = ref(false);
const panels = ref<Record<string, Panel>>({});
const searchWorker = ref<Worker | undefined>(undefined);
const toolsById = shallowRef<Record<string, Tool>>({});
const toolResults = ref<Record<string, string[]>>({});
const toolSections = ref<Record<string, Record<string, Tool | ToolSection>>>({});
const fetchedHelpIds = ref<Map<string, Promise<void>>>(new Map());
const helpDataCached = ref<Record<string, ToolHelpData>>({});
const currentToolSections = computed(() => {
const effectiveView = currentPanelView.value;
return toolSections.value[effectiveView] || {};
});
const getLinkById = computed(() => {
return (toolId: string) => {
const tool = toolsById.value[toolId];
const appRoot = getAppRoot();
if (tool && tool.model_class === "DataSourceTool") {
return `${appRoot}tool_runner/data_source_redirect?tool_id=${encodeURIComponent(toolId)}`;
} else if (tool?.model_class) {
return `${appRoot}?tool_id=${encodeURIComponent(toolId)}&version=latest`;
} else {
// accommodates hacky toolbox markdown directive overload
return undefined;
}
};
});
const getTargetById = computed(() => {
return (toolId: string) => {
const tool = toolsById.value[toolId];
return tool?.model_class === "DataSourceTool" ? "_top" : "galaxy_main";
};
});
const getToolsById = computed(() => {
return (q?: string) => {
if (!q?.trim()) {
return toolsById.value;
} else {
return filterTools(toolsById.value, toolResults.value[q] || []);
}
};
});
const getInteractiveTools = computed(() => {
return () => {
return Object.values(toolsById.value).filter((tool) => tool.model_class === "InteractiveTool");
};
});
const getToolForId = computed(() => {
return (toolId: string) => toolsById.value[toolId];
});
const getToolNameById = computed(() => {
return (toolId: string) => {
const details = toolsById.value[toolId];
return details?.name || "...";
};
});
const isPanelPopulated = computed(() => {
return Object.keys(toolsById.value).length > 0 && Object.keys(currentToolSections.value).length > 0;
});
/** These are filtered tool sections (`ToolSection[]`) for the `currentPanel`;
* Only sections that are typically referenced by `Tool.panel_section_id`
* are included for the `default` view, while for ontologies, only ontologies are
* included and `Uncategorized` section is skipped.
*/
const panelSections = computed(() => {
return (panelView: string) => {
return Object.values(toolSections.value[panelView] || {}).filter((section) => {
const sec = section as ToolSection;
return (
sec.tools &&
sec.tools.length > 0 &&
sec.id !== "uncategorized" &&
sec.id !== "builtin_converters" &&
sec.name !== undefined
);
}) as ToolSection[];
};
});
const sectionDatalist = computed(() => {
return (panelView: string) => {
return panelSections.value(panelView).map((section) => {
return { value: section.id, text: section.name };
});
};
});
async function fetchToolSections(panelView: string) {
try {
if (panelView && !toolSections.value[panelView]) {
loading.value = true;
const { data } = await axios.get(`${getAppRoot()}api/tool_panels/${panelView}`);
saveToolSections(panelView, data);
}
} catch (e) {
rethrowSimple(e);
} finally {
loading.value = false;
}
}
async function fetchPanels() {
try {
if (!defaultPanelView.value || Object.keys(panels.value).length === 0) {
const { data } = await axios.get(`${getAppRoot()}api/tool_panels`);
defaultPanelView.value = data.default_panel_view;
panels.value = data.views;
}
} catch (e) {
rethrowSimple(e);
}
}
async function fetchToolForId(toolId: string) {
try {
const { data } = await axios.get(`${getAppRoot()}api/tools/${toolId}`);
saveToolForId(toolId, data);
} catch (e) {
rethrowSimple(e);
}
}
async function fetchTools(q?: string) {
try {
loading.value = true;
// Backend search
if (q?.trim()) {
// We have either cached the backend search result,
// or it is a favorites search (which we always repeat for changes)
if (!toolResults.value[q] || FAVORITES_KEYS.includes(q.trim())) {
const { data } = await axios.get(`${getAppRoot()}api/tools`, { params: { q } });
saveToolResults(q, data);
}
}
// Fetch all tools by IDs if not already fetched
if (Object.keys(toolsById.value).length === 0) {
const { data } = await axios.get(`${getAppRoot()}api/tools?in_panel=False`);
saveAllTools(data as Tool[]);
}
} catch (e) {
rethrowSimple(e);
} finally {
loading.value = false;
}
}
async function fetchHelpForId(toolId: string) {
if (helpDataCached.value[toolId]) {
return;
}
const existing = fetchedHelpIds.value.get(toolId);
if (existing) {
return existing;
}
const promise = (async () => {
try {
const toolHelpData: ToolHelpData = {};
const { data } = (await axios.get(
`${getAppRoot()}api/tools/${encodeURIComponent(toolId)}/build`,
)) as AxiosResponse<ToolHelpData>;
const help = data.help;
if (help && help !== "\n") {
toolHelpData.help = help;
toolHelpData.summary = parseHelpForSummary(help);
} else {
toolHelpData.help = ""; // for cases where helpText == '\n'
}
Vue.set(helpDataCached.value, toolId, toolHelpData);
} catch (error) {
console.error("Error fetching help:", error);
fetchedHelpIds.value.delete(toolId); // Allow retrying on next request
}
})();
fetchedHelpIds.value.set(toolId, promise);
return promise;
}
async function initializePanel() {
try {
currentPanelView.value = currentPanelView.value || defaultPanelView.value;
await setPanel(currentPanelView.value);
} catch (e) {
await setPanel(defaultPanelView.value);
}
}
function saveAllTools(toolsData: Tool[]) {
toolsById.value = toolsData.reduce(
(acc, item) => {
acc[item.id] = item;
return acc;
},
{} as Record<string, Tool>,
);
}
function saveToolSections(panelView: string, newPanel: { [id: string]: ToolSection | Tool }) {
Vue.set(toolSections.value, panelView, newPanel);
}
function saveToolForId(toolId: string, toolData: Tool) {
Vue.set(toolsById.value, toolId, toolData);
}
function saveToolResults(whooshQuery: string, toolsData: Array<string>) {
Vue.set(toolResults.value, whooshQuery, toolsData);
}
async function setPanel(panelView: string) {
try {
await fetchToolSections(panelView);
currentPanelView.value = panelView;
} catch (e) {
rethrowSimple(e);
}
}
return {
currentPanelView,
currentToolSections,
defaultPanelView,
fetchHelpForId,
fetchedHelpIds,
fetchToolSections,
fetchPanels,
fetchToolForId,
fetchTools,
getLinkById,
getTargetById,
helpDataCached,
initializePanel,
isPanelPopulated,
loading,
getToolForId,
getToolNameById,
getToolsById,
getInteractiveTools,
panels,
panelSections,
saveAllTools,
saveToolForId,
saveToolResults,
searchWorker,
sectionDatalist,
setPanel,
toolsById,
toolSections,
};
});