Add network state to UI (#128)

This commit is contained in:
Gregory Terzian
2019-03-26 04:22:43 +08:00
committed by Maciej Hirsz
parent 1058cd965c
commit 8fa36118b5
5 changed files with 43 additions and 3 deletions
+5 -1
View File
@@ -2,7 +2,7 @@ import Chain from './Chain';
import Node from './Node';
import Feed from './Feed';
import FeedSet from './FeedSet';
import { Types, FeedMessage, timestamp } from '@dotstats/common';
import { Types, FeedMessage, Maybe, timestamp } from '@dotstats/common';
export default class Aggregator {
private readonly chains = new Map<Types.ChainLabel, Chain>();
@@ -48,6 +48,10 @@ export default class Aggregator {
});
}
public getExistingChain(label: Types.ChainLabel) : Maybe<Chain> {
return this.chains.get(label);
}
private getChain(label: Types.ChainLabel): Chain {
const chain = this.chains.get(label);
+23
View File
@@ -1,7 +1,10 @@
import * as http from 'http';
import * as url from 'url';
import * as WebSocket from 'ws';
import Node from './Node';
import Feed from './Feed';
import Aggregator from './Aggregator';
import {Types} from '@dotstats/common';
const WS_PORT_TELEMETRY_SERVER = Number(process.env.TELEMETRY_SERVER || 1024);
const WS_PORT_FEED_SERVER = Number(process.env.FEED_SERVER || 8080);
@@ -47,3 +50,23 @@ logClients();
telemetryFeed.on('connection', (socket: WebSocket) => {
aggregator.addFeed(new Feed(socket));
});
http.createServer((request, response) => {
const incoming_url = request.url || "";
const parsed_url = url.parse(incoming_url, true);
const path = decodeURI(parsed_url.path || "");
if (path.startsWith("/network_state/")) {
const [chainLabel, strNodeId] = path.split('/').slice(2);
const chain = aggregator.getExistingChain(chainLabel as Types.ChainLabel);
if (chain) {
const nodeList = Array.from(chain.nodeList());
const nodeId = Number(strNodeId);
const node = nodeList.filter((node) => node.id == nodeId)[0];
if (node) {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(node.networkState);
}
}
}
response.end();
}).listen(8081);