-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathELN-interface.html
More file actions
321 lines (278 loc) · 10.9 KB
/
ELN-interface.html
File metadata and controls
321 lines (278 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ELN interface: requesting for the computation of Fibonacci numbers</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
margin-bottom: 30px;
}
.input-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #555;
}
input[type="number"] {
width: 100%;
padding: 10px;
border: 2px solid #ddd;
border-radius: 4px;
font-size: 16px;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
color: white;
padding: 12px 30px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
font-weight: 600;
}
button:hover {
background-color: #45a049;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.status {
margin-top: 30px;
padding: 15px;
background-color: #e3f2fd;
border-left: 4px solid #2196F3;
border-radius: 4px;
display: none;
}
.warning {
margin-top: 20px;
padding: 15px;
background-color: #fff3cd;
border-left: 4px solid #ffc107;
border-radius: 4px;
display: none;
}
.warning a {
color: #856404;
font-weight: bold;
}
.results {
margin-top: 20px;
}
.result-box {
padding: 15px;
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 15px;
}
.result-box h3 {
margin-top: 0;
color: #333;
}
.final-result {
background-color: #d4edda;
border-left: 4px solid #28a745;
}
.partial-result {
background-color: #e3f2fd;
border-left: 4px solid #2196F3;
}
pre {
background-color: #f4f4f4;
padding: 10px;
border-radius: 4px;
overflow-x: auto;
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="container">
<h1>ELN interface: requesting for the computation of Fibonacci numbers</h1>
<div class="input-group">
<label for="startingIndex">Index of first Fibonacci number:</label>
<input type="number" id="startingIndex" min="0" placeholder="Enter a number (e.g., 10)">
</div>
<button id="submitBtn" onclick="submitRequest()">Submit Request</button>
<div class="status" id="statusBox"></div>
<div class="warning" id="warningBox"></div>
<div class="results" id="resultsContainer"></div>
</div>
<script>
let requestId = null;
let monitoringInterval = null;
const API_BASE = 'http://localhost:8000';
async function submitRequest() {
const startingIndex = document.getElementById('startingIndex').value;
if (!startingIndex || startingIndex === '') {
alert('Please enter a valid number');
return;
}
const indexValue = parseInt(startingIndex);
if (isNaN(indexValue)) {
alert('Please enter a valid integer');
return;
}
// Disable submit button
document.getElementById('submitBtn').disabled = true;
console.log('indexValue:', indexValue);
try {
const response = await fetch(`${API_BASE}/requests`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
body: JSON.stringify({ starting_index: indexValue })
})
});
if (!response.ok) {
console.log(response);
throw new Error('Failed to submit request');
}
const data = await response.json();
requestId = data.id;
// Show status
const statusBox = document.getElementById('statusBox');
statusBox.style.display = 'block';
statusBox.innerHTML = `<strong>Submitted!</strong> Request ID = ${requestId}<br>Monitoring for partial results or final results...`;
// Clear previous results
document.getElementById('resultsContainer').innerHTML = '';
document.getElementById('warningBox').style.display = 'none';
// Start monitoring
startMonitoring();
} catch (error) {
alert('Error submitting request: ' + error.message);
document.getElementById('submitBtn').disabled = false;
}
}
function startMonitoring() {
// Clear any existing interval
if (monitoringInterval) {
clearInterval(monitoringInterval);
}
// Check immediately
checkResults();
// Then check every 5 seconds
monitoringInterval = setInterval(checkResults, 5000);
}
async function checkResults() {
if (!requestId) return;
try {
// Check for final response
const finalResponse = await fetch(`${API_BASE}/requests/${requestId}/response`);
if (finalResponse.ok) {
const finalData = await finalResponse.json();
if (finalData && finalData.body) {
// Final result found - stop monitoring
clearInterval(monitoringInterval);
displayFinalResult(finalData);
document.getElementById('submitBtn').disabled = false;
document.getElementById('warningBox').style.display = 'none';
const statusBox = document.getElementById('statusBox');
statusBox.innerHTML = `<strong>Completed!</strong> Request ID = ${requestId}<br>Final result received.`;
return;
}
}
// Check for partial responses
const partialResponse = await fetch(`${API_BASE}/requests/${requestId}/partial-responses`);
if (partialResponse.ok) {
const partialData = await partialResponse.json();
if (partialData && partialData.length > 0) {
displayPartialResults(partialData);
// Check for human feedback URL in the most recent partial response
const latestPartial = partialData[partialData.length - 1];
checkHumanFeedbackUrl(latestPartial);
}
}
} catch (error) {
console.error('Error checking results:', error);
}
}
function displayFinalResult(result) {
const container = document.getElementById('resultsContainer');
const resultBox = document.createElement('div');
resultBox.className = 'result-box final-result';
let bodyContent;
try {
const parsed = JSON.parse(result.body);
bodyContent = JSON.stringify(parsed, null, 2);
} catch {
bodyContent = result.body;
}
resultBox.innerHTML = `
<h3>✓ Final Result</h3>
<p><strong>Response ID:</strong> ${result.id}</p>
<p><strong>Created:</strong> ${new Date(result.ctime).toLocaleString()}</p>
<p><strong>Body:</strong></p>
<pre>${bodyContent}</pre>
`;
container.innerHTML = '';
container.appendChild(resultBox);
}
function displayPartialResults(partialResults) {
const container = document.getElementById('resultsContainer');
container.innerHTML = '<h2>Partial Results:</h2>';
const partial = partialResults[partialResults.length - 1];
//partialResults.forEach(partial => {
const resultBox = document.createElement('div');
resultBox.className = 'result-box partial-result';
let bodyContent;
try {
const parsed = JSON.parse(partial.body);
bodyContent = JSON.stringify(parsed, null, 2);
} catch {
bodyContent = partial.body;
}
resultBox.innerHTML = `
<p><strong>Last Modified:</strong> ${new Date(partial.mtime).toLocaleString()}</p>
<p><strong>Body:</strong></p>
<pre>${bodyContent}</pre>
`;
container.appendChild(resultBox);
//});
}
function checkHumanFeedbackUrl(latestPartial) {
const warningBox = document.getElementById('warningBox');
try {
const body = JSON.parse(latestPartial.body);
if (body.human_feedback_url && body.human_feedback_url !== null) {
warningBox.style.display = 'block';
console.log('Human feedback URL found:', body.human_feedback_url);
warningBox.innerHTML = `
⚠️ <strong>There is some user interaction needed!</strong>
Click <a href="${body.human_feedback_url}" target="_blank">here</a>
`;
} else {
warningBox.style.display = 'none';
}
} catch (error) {
// If we can't parse the body, hide the warning
warningBox.style.display = 'none';
}
}
</script>
</body>
</html>