58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
"use strict";
|
|
|
|
import pretty_ms from 'pretty-ms';
|
|
|
|
import { once } from './misc/events.mjs';
|
|
|
|
class SystemQueryClient {
|
|
constructor() {
|
|
|
|
}
|
|
|
|
async status() {
|
|
return this.fetch_json(`/api/status`);
|
|
}
|
|
|
|
async *table(table_name) {
|
|
const abort = new AbortController();
|
|
const source = new EventSource(`/api/table/${table_name}`);
|
|
|
|
source.addEventListener(`end`, () => {
|
|
source.close();
|
|
abort.abort();
|
|
}, { once: true });
|
|
|
|
const timings = [{ id: null, hostname: Symbol("START"), timing: `+0ms` }];
|
|
const start = new Date();
|
|
let count = 0;
|
|
while(source.readyState !== EventSource.CLOSED) {
|
|
const item = await once(source, "table", abort.signal);
|
|
if(item.type === "abort") break;
|
|
|
|
const item_parsed = JSON.parse(item.data);
|
|
timings.push({
|
|
id: item_parsed.peer.id.substring(0, 7),
|
|
hostname: item_parsed.peer.name,
|
|
timing: `+${pretty_ms(new Date() - start)}`
|
|
});
|
|
yield item_parsed;
|
|
count++;
|
|
}
|
|
console.log(`FETCH TABLE ${table_name}: ${count} items returned in ${pretty_ms(new Date() - start)}`);
|
|
console.table(timings);
|
|
}
|
|
|
|
async fetch_json(path) {
|
|
let response = await fetch(path, {
|
|
headers: {
|
|
"accept": "application/json"
|
|
},
|
|
credentials: "same-origin"
|
|
});
|
|
console.log(`FETCH ${path} ${response.status} ${response.statusText}`);
|
|
return await response.json();
|
|
}
|
|
}
|
|
|
|
export default SystemQueryClient;
|