-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html.js
More file actions
478 lines (421 loc) · 15.6 KB
/
Copy pathindex.html.js
File metadata and controls
478 lines (421 loc) · 15.6 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
///////////////////////////////////////////////////////////////////////////////
// Javascript for 'index.html'
//
// Typical invocation will look like:
// <script src="/static/django_gui/index.html.js"></script>
// <script src="/static/django_gui/websocket.js"></script>
//
//
////////////////////////////////////////////////////////////////////
//
// The 'now' element was previously only updated when a message
// was received from the server. This is probably not the
// intended behaviour. I think we want to update the 'now'
// element more or less in real time, regardless of whether we
// have contact with the server or not.
//
////////////////////////////////////////////////////////////////////
var time_td = (function() {
var el = document.getElementById('time_td');
if (!el) { return; }
var t = 1000;
function update_now() {
if (!el) { return; }
el.innerHTML = Date().substring(0, 24);
}
var interval = setInterval(update_now, t);
// We could probably wait one second to write the time, but...
update_now();
})();
////////////////////////////////////////////////////////////////////
//
// Handle the updates of the status_time_td
//
////////////////////////////////////////////////////////////////////
var status_td = (function() {
var el = document.getElementById('status_time_td');
el.setAttribute('data-bs-toggle', 'tooltip');
el.setAttribute('data-bs-placement', 'top');
el.setAttribute('title', 'Last status update from LoggerManager');
if (el) {
el.className = '';
}
// Default timeout 10 seconds, over-ride by config
var o = window.odas || {};
var oT = o.Timeouts || {};
var timeout_ms = oT.Status * 1000 || 10000;
var timer = setTimeout(timed_out, timeout_ms);
function timed_out() {
if (el) {
el.className = 'text-danger';
}
}
var update = function() {
clearTimeout(timer);
timer = setTimeout(timed_out, timeout_ms);
if (el) {
el.innerHTML = Date().substring(0,24);
el.className = '';
}
};
return {
update: update
};
})();
////////////////////////////////////////////////////////////////////
//
// Handle the updates of the server_time_td
//
////////////////////////////////////////////////////////////////////
var server_td = (function() {
var el = document.getElementById('server_time_td');
el.setAttribute('data-bs-toggle', 'tooltip');
el.setAttribute('data-bs-placement', 'top');
el.setAttribute('title', 'Last communication from CDS');
el.className = '';
// Default timeout 5 seconds, over-ride by config
// use error-less fallback in case config not loaded
var o = odas || {};
var oT = o.Timeouts || {};
var timeout_ms = oT.Server * 1000 || 5000;
var timer = setTimeout(timed_out, timeout_ms);
function timed_out() {
if (el) {
el.className = 'text-danger';
}
}
var update = function() {
clearTimeout(timer);
timer = setTimeout(timed_out, timeout_ms);
if (el) {
el.innerHTML = Date().substring(0, 24);
el.className = '';
}
};
return {
update: update
};
})();
////////////////////////////////////////////////////////////////////
//
// Called by the websocket's onmessage callback.
// Updates server_time
// Processes messages based on data type
//
////////////////////////////////////////////////////////////////////
function process_message(message_str) {
// Update time of last message from websocket
server_td.update();
var message = JSON.parse(message_str);
// If something went wrong, complain, and let server know we're ready
// for next message.
if (message.status != 200) {
// Maybe set a badge somewhere?
iziToast.warning({
title: 'Error from Cached Data Server',
message: message_str,
});
//console.warn('Error from server: ' + message_str);
return;
}
// Now go through all the types of messages we know about and
// deal with them.
switch (message.type) {
case 'data':
process_data_message(message.data);
break;
case 'subscribe':
break;
case undefined:
Q = "Error: meesage has no type field:";
iziToast.warning({
title: 'CDS message has no type field',
message: message_str,
});
break;
default:
Q = "Error: unknown message type";
iziToast.warning({
title: 'CDS message has unknown type',
message: Q,
});
}
}
// WOW!! Lots and lots of code here. Any way to make this
// *less* code?
var CruiseDef = (function() {
var our_timestamp = 0;
var el_empty = document.getElementById('empty_loggers');
var el_populated = document.getElementById('status_and_loggers');
var loggers = {};
function diff_loggers(config_loggers) {
var new_loggers = []; // new loggers we don't have
var old_loggers = []; // loggers we had that are gone
// Hmmm. performance wise, it is faster to do it this way?
var new_set = new Set(Object.keys(config_loggers));
var old_set = new Set(Object.keys(loggers));
for (var logger of new_set) {
if (!old_set.has(logger)) {
new_loggers.push(logger);
}
}
for (logger of old_set) {
if (!new_set.has(logger)) {
old_loggers.push(logger);
}
}
return {
new: new_loggers,
old: old_loggers,
size: new_loggers.length + old_loggers.length,
};
}
function show_correct_divs(config) {
// Interwebs says using the classes works better
// than setting visibility on mobile. May be true.
// Why are we actually doing this?
if (config.loggers) {
el_empty.className = "d-none";
el_populated.className = "d-block";
} else {
el_empty.className = "d-block";
el_populated.className = "d-none";
}
}
function update_cruise_id(cruise_id, filename) {
var el = document.getElementById('cruise_id');
if (el) {
var txtNode = el.childNodes[0];
txtNode.nodeValue = cruise_id;
}
el = document.getElementById('reload_config_link');
if (el) {
el.setAttribute('data-bs-toggle', 'tooltip');
el.setAttribute('title', filename);
}
}
function update_logger_rows(loggers) {
}
// Called when a message of type "status:cruise_definition"
// arrives on the websocket
var status_message = function(timestamp, config) {
if (timestamp < our_timestamp) {
console.debug('Stale status:cruise_definition message');
return;
}
our_timestamp = timestamp;
show_correct_divs(config);
// Ensure all the correct parts are there
// The API sorts these, but the code exists in the Django
// GUI, supposedly for a reason....
var keys = ['modes', 'active_mode', 'loggers'];
for (var index in keys) {
if (keys[index] in config) {
continue;
}
msg = 'Cruise def has no ' + keys[index] + ' - ignoring.';
console.warn(msg);
return;
}
// we always get two messages at page startup. This is a hack
// to not report on the first one.
if (odas.api) {
iziToast.success({
title: 'Loaded new configuration file',
message: config.filename,
});
}
// console.info('Loaded new configuration');
odas.api = config;
// update cruise name on navbar
if (config.cruise_id) {
update_cruise_id(config.cruise_id, config.filename);
}
// Set the cruise mode button to the active mode
active_mode = config.active_mode;
cruise_mode.update(active_mode);
// get diff between old loggers and config.loggers
// We could just add the new ones and delete the old ones,
// but that would change the order. I think we care...
var diff = diff_loggers(config.loggers);
loggers = config.loggers;
var logger_name;
if (diff.size == 0) {
// Set the logger button texts
for (logger_name in loggers) {
var mode = loggers[logger_name].active;
LoggerButton.mode_update(logger_name, mode);
}
return {};
} else {
update_logger_rows(loggers);
}
// If stuff has gotten updated, need to update CDS fields to
// subscribe to.
var new_fields = {};
////////////////////////////////
// If the list of loggers has changed, we need to rebuild the
// list. Begin by emptying out table rows except for header row.
var table = document.getElementById('logger_table_body');
try {
var row_count = table.rows.length;
var keep_first_num_rows = 3;
for (var i = keep_first_num_rows; i < row_count; i++) {
table.deleteRow(keep_first_num_rows);
}
} catch {}
// Stash our new logger list in the globals, then update the
// loggers, creating one new row for each.
for (logger_name in loggers) {
//console.log('setting up logger ' + logger_name);
var logger = loggers[logger_name];
// table row creation
var tr = document.createElement('tr');
tr.setAttribute('id', logger_name + '_row');
var config_td = document.createElement('td');
config_td.setAttribute('id', logger_name + '_config_td');
var button = LoggerButton.create(logger_name, logger.active);
config_td.appendChild(button);
tr.appendChild(config_td);
var stderr_td = document.createElement('td');
var stderr_div = document.createElement('div');
stderr_div.setAttribute('id', logger_name + '_stderr');
stderr_div.className = 'stderr_window';
stderr_div.addEventListener('contextmenu', STDERR.ctxmenu);
stderr_td.appendChild(stderr_div);
tr.appendChild(stderr_td);
table.appendChild(tr);
// Also, since we now have a new logger, we'll want to
// subscribe to stderr updates for it. Send each logger
// window with at greatest of one hour past log messges or
// most recent 100 messages
var name = 'stderr:logger:' + logger_name;
var time_window = {'seconds':6*60*60, 'back_records': 100};
new_fields[name] = time_window;
new_fields['stderr:logger:' + logger_name] = time_window;
}
if (Object.keys(new_fields).length) {
var time_window = {'seconds':1*60*60, 'back_records': 100};
new_fields['stderr:logger_manager'] = time_window;
new_fields['status:cruise_definition'] = {'seconds':-1};
new_fields['status:cruise_mode'] = {'seconds':-1};
new_fields['status:logger_status'] = {'seconds':-1};
new_fields['status:file_update'] = {'seconds':0};
var subscribe_message = {'type':'subscribe', 'fields':new_fields};
WS.write(subscribe_message);
}
};
return {
status_message: status_message,
};
})()
////////////////////////////
////////////////////////////
// We've got data fields - process and put them where they belong in
// the HTML...
function process_data_message(message) {
// We expect to receive a field dict, format:
// {
// 'data_id': ..., # optional
// 'fields': {
// field_name: [(timestamp, value), (timestamp, value),...],
// field_name: [(timestamp, value), (timestamp, value),...],
// ...
// }
// }
for (var field_name in message) {
// If we receive a status update, update the status time
if (field_name.startsWith('status:')) {
status_td.update();
}
var value_list = message[field_name];
// Get the [timestamp, value] pair at the end of the list.
// That should be the most recent.
var [ timestamp, values ] = value_list.pop();
switch (field_name) {
// Cruise definition updatee
case 'status:cruise_definition':
CruiseDef.status_message(timestamp, values);
break;
// Cruise Mode update
case 'status:cruise_mode':
cruise_mode.status_update(timestamp, values);
break;
// Logger Status update
case 'status:logger_status':
LoggerButton.update(timestamp, values);
break;
//////////////////
// The file from which our definition came has been updated. Make
// the section that offers to reload it visible. If user is not
// authenticated, this element will not exist.
// File_update update
case 'status:file_update':
var o = odas || {};
var oA = o.api || {};
config_timestamp = oA.config_timestamp || 0;
var staleSpan = document.getElementById('stale_span');
if (! staleSpan) { return; }
if (values > config_timestamp) {
staleSpan.classList.remove('d-none');
} else {
staleSpan.classList.add('d-none');
}
break;
case 'stderr:logger_manager':
STDERR.process('logger_manager_stderr', value_list);
break;
////////////////////////////////////////////////////
// If something else, see if it's a logger status update
default:
var LOGGER_STDERR_PREFIX = 'stderr:logger:';
if (field_name.startsWith(LOGGER_STDERR_PREFIX)) {
var logger_name = field_name.split(':')[2];
// Defined in stderr_log_utils.js
var target = logger_name + '_stderr';
STDERR.process(target, value_list);
}
}
}
}
var cruise_mode = (function() {
// Methods should be:
// CreateButton (construct button, attach events)
// ButtonPresseda(show modal)
// status_update(message from CDS)
// update(mode)
var mode_button = document.getElementById('mode_button');
var active_mode = 'off';
var status_timestamp = 0;
function init() {
// attach events to the button ?? Handle Modal here?
}
function status_update(timestamp, values) {
if (timestamp < status_timestamp) {
console.debug("Stale status:cruise_mode message");
return;
}
status_timestamp = timestamp;
if (values.active_mode != active_mode) {
update(values.active_mode);
}
}
// Called when we get a message saying the mode has changed
// This method also called directly when cruise config is updated
function update(new_mode) {
active_mode = new_mode;
if (mode_button) {
mode_button.innerHTML = new_mode;
}
}
// not called anywhere, but...
function get_mode() {
return active_mode;
}
init();
return {
update: update,
get_mode: get_mode,
status_update: status_update,
};
})();