-
Notifications
You must be signed in to change notification settings - Fork 7
/
ringserverweb.ts
469 lines (409 loc) · 11.2 KB
/
ringserverweb.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
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
/*
* Philip Crotwell
* University of South Carolina, 2019
* https://www.seis.sc.edu
*/
import {DateTime, Duration} from "luxon";
import {NslcId} from "./fdsnsourceid";
import * as util from "./util"; // for util.log
import {
doIntGetterSetter,
doStringGetterSetter,
doFloatGetterSetter,
checkProtocol,
isNonEmptyStringArg,
isNumArg,
isDef,
TEXT_MIME,
doFetchWithTimeout,
defaultFetchInitObj,
isoToDateTime,
} from "./util";
export const SEEDLINK_PATH = "/seedlink";
export const DATALINK_PATH = "/datalink";
export type RingserverVersion = {
ringserverVersion: string;
serverId: string;
};
export type StreamsResult = {
accessTime: DateTime;
streams: Array<StreamStat>;
};
export const IRIS_HOST = "rtserve.iris.washington.edu";
const ORG = "Organization: ";
/**
* Web connection to a Ringserver.
*
*
* @param host optional host to connect to, defaults to IRIS. This maybe a full URL.
* @param port optional host to connect to, defaults to 80
*/
export class RingserverConnection {
/** @private */
_host: string;
/** @private */
_port: number;
/** @private */
_prefix: string;
/** @private */
_timeoutSec: number;
constructor(host?: string, port?: number) {
const hostStr = isNonEmptyStringArg(host) ? host : IRIS_HOST;
if (hostStr.startsWith("http")) {
const rs_url = new URL(hostStr);
this._host = rs_url.hostname;
this._port = parseInt(rs_url.port);
if (!Number.isInteger(this._port)) {
this._port = 80;
}
this._prefix = rs_url.pathname;
} else {
this._host = hostStr;
this._port = 80;
this._prefix = "";
}
// override port in URL if given
if (isNumArg(port)) {
this._port = port;
}
this._timeoutSec = 30;
}
/**
* Gets/Sets the remote host to connect to.
*
* @param value optional new value if setting
* @returns new value if getting, this if setting
*/
host(value?: string): RingserverConnection {
doStringGetterSetter(this, "host", value);
return this;
}
getHost(): string {
return this._host;
}
/**
* Gets/Sets the remote port to connect to.
*
* @param value optional new value if setting
* @returns new value if getting, this if setting
*/
port(value?: number): number | RingserverConnection {
doIntGetterSetter(this, "port", value);
return this;
}
getPort(): number | undefined {
return this._port;
}
/**
* Get/Set the timeout in seconds for the request. Default is 30.
*
* @param value optional new value if setting
* @returns new value if getting, this if setting
*/
timeout(value?: number): RingserverConnection {
doFloatGetterSetter(this, "timeoutSec", value);
return this;
}
getTimeout(): number | undefined {
return this._timeoutSec;
}
/**
* Pulls id result from ringserver /id parsed into an object with
* 'ringserverVersion' and 'serverId' fields.
*
* @returns Result as a Promise.
*/
pullId(): Promise<RingserverVersion> {
return this.pullRaw(this.formIdURL()).then(raw => {
const lines = raw.split("\n");
let organization = lines[1];
if (organization.startsWith(ORG)) {
organization = organization.substring(ORG.length);
}
return {
ringserverVersion: lines[0],
serverId: organization,
};
});
}
/**
* Use numeric level (1-6) to pull just IDs from ringserver.
* In a default ringserver,
* level=1 would return all networks like
* CO
* and level=2 would return all stations like
* CO_JSC
* If level is falsy/missing, level=6 is used.
* The optional matchPattern is a regular expression, so for example
* '.+_JSC_00_HH.' would get all HH? channels from any station name JSC.
*
* @param level 1-6
* @param matchPattern regular expression to match
* @returns Result as a Promise.
*/
pullStreamIds(level: number, matchPattern: string): Promise<Array<string>> {
let queryParams = "level=6";
if (isNumArg(level) && level > 0) {
queryParams = "level=" + level;
}
if (matchPattern) {
queryParams = queryParams + "&match=" + matchPattern;
}
const url = this.formStreamIdsURL(queryParams);
return this.pullRaw(url).then(raw => {
return raw.split("\n").filter(line => line.length > 0);
});
}
/**
* Pull streams, including start and end times, from the ringserver.
* The optional matchPattern is a regular expression, so for example
* '.+_JSC_00_HH.' would get all HH? channels from any station name JSC.
* Result returned is an Promise.
*
* @param matchPattern regular expression to match
* @returns promise to object with 'accessTime' as a DateTime
* and 'streams' as an array of StreamStat objects.
*/
pullStreams(matchPattern: string): Promise<StreamsResult> {
let queryParams = "";
if (matchPattern) {
queryParams = "match=" + matchPattern;
}
const url = this.formStreamsURL(queryParams);
return this.pullRaw(url).then(raw => {
const lines = raw.split("\n");
const out: StreamsResult = {
accessTime: DateTime.utc(),
streams: [],
};
for (const line of lines) {
if (line.length === 0) {
continue;
}
const vals = line.split(/\s+/);
if (vals.length === 0) {
// blank line, skip
continue;
} else if (vals.length >= 2) {
out.streams.push(new StreamStat(vals[0], vals[1], vals[2]));
} else {
util.log("Bad /streams line, skipping: '" + line + "'");
}
}
return out;
});
}
/**
* Utility method to pull raw result from ringserver url.
* Result returned is an Promise.
*
* @param url the url
* @returns promise to string result
*/
pullRaw(url: string): Promise<string> {
const fetchInit = defaultFetchInitObj(TEXT_MIME);
return doFetchWithTimeout(url, fetchInit, this._timeoutSec * 1000).then(
response => {
if (response.status === 200) {
return response.text();
} else {
throw new Error(`Status not 200: ${response.status}`);
}
},
);
}
getDataLinkURL(): string {
let proto = "ws:";
if (checkProtocol() === "https:") {
proto = "wss:";
}
return (
proto +
"//" +
this._host +
(this._port === 80 ? "" : ":" + this._port) +
this._prefix +
DATALINK_PATH
);
}
getSeedLinkURL(): string {
let proto = "ws:";
if (checkProtocol() === "https:") {
proto = "wss:";
}
return (
proto +
"//" +
this._host +
(this._port === 80 ? "" : ":" + this._port) +
this._prefix +
SEEDLINK_PATH
);
}
/**
* Forms base url from protocol, host and port.
*
* @returns the string url
*/
formBaseURL(): string {
if (this._port === 0) {
this._port = 80;
}
return (
checkProtocol() +
"//" +
this._host +
(this._port === 80 ? "" : ":" + this._port) +
this._prefix
);
}
/**
* Forms the ringserver id url.
*
* @returns the id url
*/
formIdURL(): string {
return this.formBaseURL() + "/id";
}
/**
* Forms the ringserver streams url using the query parameters.
*
* @param queryParams optional string of query parameters
* @returns the streams url
*/
formStreamsURL(queryParams?: string): string {
return (
this.formBaseURL() +
"/streams" +
(isNonEmptyStringArg(queryParams) && queryParams.length > 0
? "?" + queryParams
: "")
);
}
/**
* Forms the ringserver stream ids url using the query parameters.
*
* @param queryParams optional string of query parameters
* @returns the stream ids url
*/
formStreamIdsURL(queryParams: string): string {
return (
this.formBaseURL() +
"/streamids" +
(queryParams && queryParams.length > 0 ? "?" + queryParams : "")
);
}
}
/**
* Extract one StreamStat per station from an array of channel level
* StreamStats. The start and end times are the min and max times for all
* the channels within the station. Can be used to get most time of most
* recent packet from the stations to give an idea of current latency.
*
* @param streams array of channel level StreamStats
* @returns array of station level StreamStats
*/
export function stationsFromStreams(
streams: Array<StreamStat>,
): Array<StreamStat> {
const out: Map<string, StreamStat> = new Map();
for (const s of streams) {
const nslc_type = nslcSplit(s.key);
const nslc = nslc_type.nslc;
const staKey = nslc.networkCode + "." + nslc.stationCode;
let stat = out.get(staKey);
if (!isDef(stat)) {
stat = new StreamStat(staKey, s.startRaw, s.endRaw);
out.set(staKey, stat);
} else {
if (stat.start > s.start) {
stat.start = s.start;
stat.startRaw = s.startRaw;
}
if (stat.end < s.end) {
stat.end = s.end;
stat.endRaw = s.endRaw;
}
}
}
return Array.from(out.values());
}
export class NslcWithType {
type: string;
nslc: NslcId;
constructor(type: string, nslc: NslcId) {
this.type = type;
this.nslc = nslc;
}
}
/**
* Split type, networkCode, stationCode, locationCode and channelCode
* from a ringserver id formatted like net_sta_loc_chan/type
*
* @param id id string to split
* @returns object with the split fields
*/
export function nslcSplit(id: string): NslcWithType {
const split = id.split("/");
const nslc = split[0].split("_");
if (nslc.length === 4) {
// assume net, station, loc, chan
return new NslcWithType(split[1], new NslcId(nslc[0], nslc[1], nslc[2], nslc[3]));
} else {
throw new Error("tried to split, did not find 4 elements in array: " + id);
}
}
/**
* Object to hold start and end times for a key, usually channel or station.
*
* @param key id, usually station or channel
* @param start start time
* @param end end time
*/
export class StreamStat {
key: string;
startRaw: string;
endRaw: string;
start: DateTime;
end: DateTime;
constructor(key: string, start: string, end: string) {
this.key = key;
this.startRaw = start;
this.endRaw = end;
if (
this.startRaw.indexOf(".") !== -1 &&
this.startRaw.indexOf(".") < this.startRaw.length - 4
) {
this.startRaw = this.startRaw.substring(
0,
this.startRaw.indexOf(".") + 4,
);
}
if (this.startRaw.charAt(this.startRaw.length - 1) !== "Z") {
this.startRaw = this.startRaw + "Z";
}
if (
this.endRaw.indexOf(".") !== -1 &&
this.endRaw.indexOf(".") < this.endRaw.length - 4
) {
this.endRaw = this.endRaw.substring(0, this.endRaw.indexOf(".") + 4);
}
if (this.endRaw.charAt(this.endRaw.length - 1) !== "Z") {
this.endRaw = this.endRaw + "Z";
}
this.start = isoToDateTime(this.startRaw);
this.end = isoToDateTime(this.endRaw);
this.startRaw = start; // reset to unchanged strings
this.endRaw = end;
}
/**
* Calculates latency time difference between last packet and current time.
*
* @param accessTime time latency is calculated relative to
* @returns latency
*/
calcLatency(accessTime?: DateTime): Duration {
if (!accessTime) accessTime = DateTime.utc();
return this.end.diff(accessTime);
}
}