-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
178 lines (164 loc) · 5.85 KB
/
App.tsx
File metadata and controls
178 lines (164 loc) · 5.85 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
import { useState } from "react";
import { Layout, Typography } from "antd";
import { getJobStatus, updateJobStatusTimestamp } from "./utils/firebase";
import { getFirebaseRecipe, recipeToString } from "./utils/recipeLoader";
import { getSubmitPackingUrl, JOB_STATUS } from "./constants/aws";
import { SIMULARIUM_VIEWER_URL } from "./constants/urls";
import {
useCurrentRecipeData,
useJobId,
useOutputsDirectory,
useResultUrl,
useRunTime,
useSetJobId,
useSetPackingResults,
} from "./state/store";
import PackingInput from "./components/PackingInput";
import Viewer from "./components/Viewer";
import StatusBar from "./components/StatusBar";
import "./App.css";
const { Header, Content, Sider, Footer } = Layout;
const { Link } = Typography;
function App() {
const [jobStatus, setJobStatus] = useState<string>("");
const [jobLogs, setJobLogs] = useState<string>("");
const setJobId = useSetJobId();
const jobId = useJobId();
const setPackingResults = useSetPackingResults();
const runTime = useRunTime();
const outputDir = useOutputsDirectory();
const edits = useCurrentRecipeData()?.edits || {};
const resultUrl = useResultUrl();
const shareUrl = resultUrl ? `${SIMULARIUM_VIEWER_URL}${resultUrl}` : "";
let start = 0;
async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const recipeHasChanged = async (
recipeId: string,
recipeString: string
): Promise<boolean> => {
const originalRecipe = await getFirebaseRecipe(recipeId);
return !(recipeToString(originalRecipe) == recipeString);
};
const submitRecipe = async (
recipeId: string,
configId: string,
recipeString: string
) => {
const recipeChanged: boolean = await recipeHasChanged(
recipeId,
recipeString
);
const firebaseRecipe = recipeChanged
? undefined
: "firebase:recipes/" + recipeId;
const firebaseConfig = configId
? "firebase:configs/" + configId
: undefined;
const url = getSubmitPackingUrl(firebaseRecipe, firebaseConfig);
const requestBody = recipeChanged ? recipeString : undefined;
const request: RequestInfo = new Request(url, { method: "POST", body: requestBody });
start = Date.now();
const response = await fetch(request);
setJobStatus(JOB_STATUS.SUBMITTED);
setJobLogs("");
if (response.ok) {
const data = await response.json();
setJobId(data.jobId);
setJobStatus(JOB_STATUS.STARTING);
return data.jobId;
} else {
const errorText = await response.text();
setJobStatus(JOB_STATUS.FAILED);
setJobLogs(errorText);
}
};
const startPacking = async (
recipeId: string,
configId: string,
recipeString: string
) => {
await submitRecipe(recipeId, configId, recipeString).then(
(jobIdFromSubmit) => checkStatus(jobIdFromSubmit)
);
};
const checkStatus = async (jobIdFromSubmit: string) => {
const id = jobIdFromSubmit || jobId;
let localJobStatus = await getJobStatus(id);
if (localJobStatus) {
setJobStatus(localJobStatus.status);
}
while (
localJobStatus?.status !== JOB_STATUS.DONE &&
localJobStatus?.status !== JOB_STATUS.FAILED
) {
await sleep(500);
const newJobStatus = await getJobStatus(id);
if (
newJobStatus &&
localJobStatus?.status !== newJobStatus.status
) {
localJobStatus = newJobStatus;
setJobStatus(newJobStatus.status);
}
}
// Update the job status timestamp after reading the final status to
// ensure we have the most recent timestamp for retention policy
await updateJobStatusTimestamp(id);
const range = (Date.now() - start) / 1000;
if (localJobStatus.status == JOB_STATUS.DONE) {
setPackingResults({
jobId: id,
resultUrl: localJobStatus.result_path,
runTime: range,
outputDir: localJobStatus.outputs_directory,
edits: edits,
});
} else if (localJobStatus.status == JOB_STATUS.FAILED) {
setJobLogs(`Packing job failed: ${localJobStatus.error_message}`);
setPackingResults({
jobId: id,
resultUrl: "",
runTime: range,
outputDir: "",
edits: {}
});
}
};
return (
<Layout className="app-container">
<Header
className="header"
style={{ justifyContent: "space-between" }}
>
<h2 className="header-title">cellPACK Studio</h2>
<Link
href="https://github.com/mesoscope/cellpack"
className="header-link"
>
GitHub
</Link>
</Header>
<Layout>
<Sider width="35%" theme="light" className="sider">
<PackingInput startPacking={startPacking} />
</Sider>
<Content className="content-container">
<Viewer />
</Content>
</Layout>
<Footer className="footer">
<StatusBar
jobStatus={jobStatus}
runTime={runTime}
jobId={jobId}
errorLogs={jobLogs}
outputDir={outputDir}
shareUrl={shareUrl}
/>
</Footer>
</Layout>
);
}
export default App;