-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay-messages.html
More file actions
167 lines (139 loc) · 4.21 KB
/
display-messages.html
File metadata and controls
167 lines (139 loc) · 4.21 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
<html>
<script src="https://d3js.org/d3.v5.js"></script>
<body>
<h1>Hello, World!</h1>
</body>
<script>
let headers = new Map();
let lineNumbers = new Map();
let running = new Set();
let requestCount = 0;
function merge(a, b) {
var r = {};
for (var k in a) {
if (a.hasOwnProperty(k)) {
r[k] = a[k];
}
}
for (var k in b) {
if (b.hasOwnProperty(k)) {
r[k] = b[k];
}
}
return r;
}
// Display a message so ther user can see it
function disp(s) {
const el = document.createElement("div");
el.innerText = s
document.querySelector("body").appendChild(el);
}
// Send a message from the Web-View to WV Linewise
function send(msg) {
let j = JSON.stringify(msg)
disp("< " + j);
external.invoke(j);
}
// Seperate a CSV row into an array.
function csvRowToAr(csvRow) {
return d3.csvParseRows(csvRow)[0];
}
// Once we have an array of data fields from a line of CSV data we can use
// the headers to convert that to an object.
function convertToJson(streamName, csvRow) {
if (!headers.has(streamName)) {
throw new Error("Have no record of " + streamName);
}
return headers.get(streamName).reduce(
function (acc, k, i) {
const r = {};
if ((!csvRow) || (!csvRow.length) || (csvRow.length < i)) {
return acc;
}
r[k] = csvRow[i];
return merge(acc, r);
},
{}
);
}
// This is where messages from WV Linewise are received in the Web View.
function _globalWvLinewise(msg) {
disp("> " + JSON.stringify(msg));
// A line is a line of data from a file / STDIN which has been read by read
// by WV Linewise
if (msg.type == "line") {
// If we have not sent headers for this stream yet, send them and record
// the line number.
if (!headers.has(msg.name)) {
headers.set(msg.name, csvRowToAr(msg.data));
lineNumbers.set(msg.name, 0);
return;
}
// Decorate the data received with the stream name and the row number.
lineNumbers.set(msg.name, lineNumbers.get(msg.name) + 1);
const j = merge(
{
"Stream Name": msg.name,
"Line Number": lineNumbers.get(msg.name),
},
convertToJson(msg.name, csvRowToAr(msg.data))
);
// Send it to STDOUT.
send({
msg: "out",
descriptor: 1,
data: JSON.stringify(j)
});
}
// If there is a parameter "request_count" then use that as the amount of
// lines to request.
function filterForRequestCount(ob) {
if (ob && ob.name && ob.name == "request_count") {
return true;
}
return false;
}
if (msg.type == "params") {
msg.params.filter(filterForRequestCount).forEach(
function(ob) {
requestCount = parseInt(ob.value, 10);
}
);
send({"msg":"streamList"});
}
// Start all streams that don't include the word "in".
if (msg.type == "streamList") {
msg.streams.forEach(function(strm) {
send({ msg: "streamStart", name: strm, count: requestCount });
running.add(strm);
});
}
// All data from WV Linewise is processed immediately (with no promises or
// callbacks) so as soon as a stream pauses, start it right back up again.
if (msg.type == "paused") {
setTimeout(function() {
send({ "msg": "streamContinue", "name" :msg.name });
}, 1000);
}
// If we have anythink we haven't started, start it... But if we don't have
// anything we haven't started then exit.
//
// We start "lookups" on launch and put "in" into otherStreams, so when
// "lookups" finishes, we start "in" and then when "in" finishes we exit.
if (msg.type == "finished") {
running.delete(msg.name);
if (running.size == 0) {
disp('SENDING \'{"msg":"exit", "status": 0}\' - in 10 seconds');
setTimeout(function() {
send({"msg":"exit", "status": 0});
}, 10000);
}
}
}
window.onerror = function(e) { disp(e && e.msg || e) };
setInterval(function() {
document.querySelector("h1").innerText = new Date();
}, 1000);
send({"msg":"params"});
</script>
</html>