laconic-console/packages/console-server/src/resolvers/log.js

90 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-06-10 20:36:54 +00:00
//
// Copyright 2020 DxOS.org
//
2020-06-10 22:15:07 +00:00
import { spawnSync } from 'child_process';
2020-06-10 20:36:54 +00:00
class LogCache {
constructor(maxLines = 500) {
// Sets in JS iterate in insertion order.
this.buffer = new Set();
this.maxLines = maxLines;
}
append(lines) {
const added = [];
for (const line of lines) {
if (!this.buffer.has(line)) {
this.buffer.add(line);
added.push(line);
}
}
if (this.buffer.size > this.maxLines) {
this.buffer = new Set(Array.from(this.buffer).slice(parseInt(this.maxLines / 2)));
}
return added;
}
}
const _caches = new Map();
const getLogCache = (name) => {
let cache = _caches.get(name);
if (!cache) {
cache = new LogCache();
_caches.set(name, cache);
}
return cache;
}
const getLogs = async (name, lines = 100) => {
const command = 'wire';
const args = ['service', 'logs', '--lines', lines, name];
const child = spawnSync(command, args, { encoding: 'utf8' });
const logLines = child.stdout.split(/\n/);
const cache = getLogCache(name);
return cache.append(logLines);
};
export const logResolvers = {
Query: {
wns_log: async () => {
2020-06-10 22:17:19 +00:00
const logs = await getLogs('wns-lite');
2020-06-10 20:36:54 +00:00
return {
timestamp: new Date().toUTCString(),
json: JSON.stringify(logs)
};
2020-06-10 22:15:07 +00:00
},
signal_log: async () => {
const logs = await getLogs('signal');
return {
timestamp: new Date().toUTCString(),
json: JSON.stringify(logs)
};
},
ipfs_log: async () => {
const logs = await getLogs('ipfs');
return {
timestamp: new Date().toUTCString(),
json: JSON.stringify(logs)
};
},
ipfs_swarm_log: async () => {
const logs = await getLogs('ipfs');
return {
timestamp: new Date().toUTCString(),
json: JSON.stringify(logs)
};
},
app_log: async () => {
const logs = await getLogs('ipfs');
return {
timestamp: new Date().toUTCString(),
json: JSON.stringify(logs)
};
2020-06-10 20:36:54 +00:00
}
}
};