-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
97 lines (85 loc) · 4.33 KB
/
app.js
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
/*
* Code specific to IslandCompare app
* Contains functions for initialising state and populating ORM
*/
let stateFetched = false; // Prevent fetching more than once
let historyFetched = false; // Prevent fetching more than once
export let invocationsFetched = false;
import { api as galaxy } from 'galaxy-client'
import {workflow_tag, application_tag, workflow_owner} from "./app.config";
import {getOrCreateUUID, getAPIKey, setGlobalKey} from "./auth";
// Fetch all required state from api
export async function fetchState(createUUID = false) {
if (stateFetched) return; //Only fetch once across entire application for lifetime of window
stateFetched = true;
let key = '';
if (createUUID) key= await getOrCreateUUID();
else key = await getAPIKey();
setGlobalKey(key);
if (!key) {
stateFetched = false;
throw "No api key";
}
await galaxy.histories.History.fetch();
await galaxy.workflows.StoredWorkflow.fetch({params: {show_published: true}});
// Workflow inputs are not loaded in fetch, load them.
const workflow = getConfiguredWorkflow();
if (!workflow) throw("Failed to load workflow");
if (Object.keys(workflow.inputs).length === 0) {
workflow.reload(); //Get input details
}
// Fetch workflow invocations for histories that are not deleted
const histories = galaxy.histories.History.query().where('deleted', false).where('tags', tags => tags.includes(application_tag) || tags.includes(workflow.id)).get();
//Promise.all(histories.map(h=>galaxy.workflows.WorkflowInvocation.fetch({params: {view: "element", step_details: true, history_id: h.id}}))).then(() => invocationsFetched = true);
Promise.all([workflow.fetch_invocations(histories),
// Try to recover workflow id from history tags for jobs of an older workflow
// Query must be done on workflow id as subworkflows will pollute the fetched invocations.
// Queries for all invocations associated with a history is also very very slow.
...[...new Set(histories.map(h=>h.tags.find(t=>t !== application_tag && t !== workflow.id && t.length === 16)).filter(t=>t))].map(workflow_id=>{
const w = new galaxy.workflows.StoredWorkflow();
w.id = workflow_id;
const hist = histories.filter(h=>h.tags.includes(workflow_id));
return w.fetch_invocations(hist);
})]).then(() => invocationsFetched = true);
}
export function makeUploadHistory(history) {
history.name = "Unnamed project";
history.tags.push('user_data');
history.put(['name', 'tags']); // TODO https://github.com/galaxyproject/galaxy/issues/11679
return history;
}
export async function fetchStateAndUploadHistories(createUUID = false) {
await fetchState(createUUID);
// Load or create the user_data histories and all its datasets
let histories = getUploadHistories();
if (!histories || histories.length === 0) {
historyFetched = true;
histories = [makeUploadHistory(await galaxy.histories.History.post())];
} else if (histories && !historyFetched) {
historyFetched = true;
histories.forEach(h=>h.loadContents());
}
return histories;
}
export function getConfiguredWorkflow() {
// Look for workflow with owner but fail back to any owner
return galaxy.workflows.StoredWorkflow.query().where('owner', workflow_owner).where('tags', tags=>tags.includes(workflow_tag)).with('inputs|steps').first() ||
galaxy.workflows.StoredWorkflow.query().where('tags', tags=>tags.includes(workflow_tag)).with('inputs|steps').first();
}
export function getInvocations(workflow) {
return galaxy.workflows.WorkflowInvocation.query().whereHas('history', q => {
q.where('deleted', false)
.where('tags', tags=>tags.includes(application_tag) || tags.includes(workflow.id))
}).with('history|workflow|steps.jobs').get();
}
export function getUploadHistories() {
const histories = galaxy.histories.History.query().where('deleted', false).where('tags', tags=>tags.includes('user_data')).with('datasets.history').get();
histories.sort((a,b)=>new Date(b.update_time)-new Date(a.update_time));
return histories;
}
export async function onInvocation(invocation) {
invocation = await invocation;
const history = galaxy.histories.History.find(invocation.history_id);
history.tags.push(application_tag);
history.put(['tags']);
}