-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
319 lines (285 loc) · 8.32 KB
/
index.ts
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
console.log(`loaded index.js`);
document.lastInput = "";
document.myReplica = null;
const sourceReplicaIdKey = "Source-Replica-Id";
$(async function () {
await reRenderGraph("waiting", "");
console.log("done");
while (true) {
console.log("subscribing");
try {
await subscribeToEvents();
} catch (err) {
console.log("ERROR:", err?.message ?? err);
}
showDisconnectedAlert();
console.log("waiting before reconnecting...");
await sleep(5);
}
});
async function subscribeToEvents() {
await subscribe("/events", processEvent);
}
async function subscribe(
streamUrl: string,
processingFunc: (event: any) => Promise<void>,
) {
let response = await fetch(streamUrl);
if (response.status == 502) {
// reconnect on timeout
showDisconnectedAlert();
await subscribe(streamUrl, processingFunc);
} else if (response.status != 200) {
// errored!
showDisconnectedAlert();
console.log("ERROR:", response.statusText);
// reconnect
await sleep(3);
await subscribe(streamUrl, processingFunc);
} else {
hideDisconnectedAlert();
flashConnectedAlert();
// Get and show the message
const reader = response?.body?.getReader();
if (!reader) {
console.log("ERROR: failed to read the messages");
} else {
// read all messages
let currentMessage = "";
while (true) {
let chunk = await reader.read();
if (chunk.done) {
break;
}
currentMessage += new TextDecoder("utf-8").decode(chunk.value);
let endlineAt = -1;
do {
endlineAt = currentMessage.indexOf("\n");
if (endlineAt === -1) {
break;
}
let messageToProcess = currentMessage.substring(0, endlineAt);
currentMessage = currentMessage.substring(endlineAt + 1);
try {
let message = JSON.parse(messageToProcess);
await processingFunc(message);
} catch (err) {
console.log("MESSAGE WAS:", messageToProcess);
console.log("ERROR:", err?.message ?? err);
}
} while (true);
}
}
// Call subscribe again to try to reconnect
await sleep(1);
await subscribe(streamUrl, processingFunc);
}
}
function replaceText(selector, text: string) {
$(selector).text(text);
}
function showLastError(text: string) {
replaceText("#delayed-text", text);
}
function showLastEvent(text: string) {
replaceText("#last-event", text);
}
function showVisitorsActive(count: number) {
if (count == null) {
return;
}
replaceText("#visitors-active", `${count}`);
}
function showVisitorsActiveInCluster(count: number) {
if (count == null) {
return;
}
replaceText("#visitors-active-cluster", `${count}`);
}
function showReplicasActive(msg: string) {
if (msg == null) {
return;
}
replaceText("#replicas", `${msg}`);
}
function showTotalVisitors(count: number) {
if (count == null) {
return;
}
replaceText("#total-visitors", `${count}`);
}
function showServerRevision(text: string) {
replaceText("#server-revision", text);
}
function bindGraphClicks() {
$("span.edgeLabel").wrap('<a href="#/"></a>');
$("span.edgeLabel").on("click", function (e) {
postCommand($(this).text());
});
}
async function sleep(seconds: number) {
await new Promise((resolve) => setTimeout(resolve, seconds * 1000 /*ms*/));
}
async function processEvent(event) {
if (!event.name) {
return;
}
console.log("INCOMING_EVENT:", event);
let eventLine = formatEventIntoOneLine(event);
switch (event.name) {
case "WorkDone":
case "WorkAborted":
await reRenderGraph("waiting", "");
break;
case "WorkStarted":
await reRenderGraph("working", `...`);
break;
case "LastSeenState":
let state = `${event?.properties?.param}`;
console.log(`rendering last seen state: ${state}`);
await reRenderGraph(state, "");
break;
case "Tick":
await reRenderGraph("working", ` ${event?.properties?.param}`);
break;
case "WorkAbortRequested":
await reRenderGraph("aborting", "");
break;
case "RequestIgnored":
case "CommandRejected":
showLastError(eventLine);
// do nothing
break;
case "ResourcesRefreshed":
console.log("resources updated, reloading...");
location.reload();
break;
case "VisitorsActive":
showVisitorsActive(event?.properties?.param);
// do not show this event in the log
return;
case "TotalClusterVisitorsActive":
showVisitorsActiveInCluster(event?.properties?.param);
// do not show this event in the log
return;
case "ReplicasActive":
showReplicasActive(event?.properties?.param);
// do not show this event in the log
return;
case "ConnectedToReplica":
document.myReplica = event?.properties?.param;
// do not show this event in the log
return;
case "TotalVisitors":
showTotalVisitors(event?.properties?.param);
// do not show this event in the log
return;
case "Revision":
showServerRevision(event?.properties?.param);
return;
default:
console.log(`unhandled event: ${event.name}`);
// await reRenderGraph("", "");
break;
}
showLastEvent(eventLine);
}
async function postCommand(command: string) {
console.log("trying to post transition: ", command);
let headers: any = {
"Content-Type": "application/json",
};
headers[sourceReplicaIdKey] = document.myReplica;
try {
const response = await fetch(`/commands/${command}`, {
method: "POST",
mode: "same-origin",
cache: "no-cache",
headers,
redirect: "follow",
referrerPolicy: "no-referrer",
body: "{}",
});
await response.json();
const sourceReplicaId = response.headers.get(sourceReplicaIdKey);
if (sourceReplicaId != document.myReplica) {
addAlert(
`Command sent to another replica: ${sourceReplicaId}!=${document.myReplica}.
The state machine missed the command...`,
"info",
);
}
} catch (err) {
console.log("ERROR: posting command:", err?.message ?? err);
}
}
async function reRenderGraph(selectedState, progress) {
let input = updateGraphDefinition(selectedState, progress);
if (input === document.lastInput) {
console.log("nothing to re-render");
return;
}
document.lastInput = input;
let rendered = await mermaid.mermaidAPI.render("temporary-graph", input);
let graph = document.querySelector("#graph");
if (graph) {
graph.innerHTML = rendered.svg;
bindGraphClicks();
} else {
console.log("ERROR: could not find target element for redrawing");
}
}
function updateGraphDefinition(selectedState, progress) {
let res = `stateDiagram-v2
[*] --> waiting
waiting --> working : start
working --> aborting : abort
working --> waiting
aborting --> waiting
classDef inProgress font-style:italic, stroke-dasharray: 5 5, stroke-width:3px;
class ${selectedState} inProgress
`;
if (progress && progress.trim() !== "") {
res += `note right of working
${progress}
end note`;
}
return res;
}
function formatEventIntoOneLine(event) {
let res = `${event.timestamp}: ${event.name}`;
if (Object.keys(event?.properties ?? {}).length !== 0) {
// res+=` ${Object.entries(event.properties)})`;
res += ` [${Object.entries(event.properties)
.map((e) => e[0] + ": " + e[1])
.join(", ")}]`;
}
return res;
}
function hideDisconnectedAlert() {
$("#offline-alert").hide();
}
function showDisconnectedAlert() {
$("#offline-alert").show();
}
function flashConnectedAlert() {
$("#connected-alert").show();
$("#connected-alert").fadeTo(500, 50, function () {
$("#connected-alert").slideUp(500);
});
}
function addAlert(text: string, alertType: string) {
// https://getbootstrap.com/docs/5.3/components/alerts/
const alertPlaceholder = document.getElementById("alert-placeholder");
const wrapper = document.createElement("div");
wrapper.innerHTML = [
`<div class="alert alert-${alertType} alert-dismissible fade show" role="alert">`,
` <div>${text}</div>`,
' <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>',
"</div>",
].join("");
setTimeout(function () {
$(wrapper).find(".alert").alert("close");
}, 3000);
alertPlaceholder?.append(wrapper);
}