Get nodes count per AS by calling /lightning/nodes/asShare API

This commit is contained in:
nymkappa 2022-07-12 22:32:13 +02:00
parent d8a1c0ac1b
commit 2fd34cbd91
No known key found for this signature in database
GPG key ID: E155910B16E8BD04
2 changed files with 42 additions and 0 deletions

View file

@ -93,6 +93,35 @@ class NodesApi {
throw e; throw e;
} }
} }
public async $getNodesAsShare() {
try {
let query = `SELECT names, COUNT(*) as nodesCount from nodes
JOIN geo_names ON geo_names.id = nodes.as_number
GROUP BY as_number
ORDER BY COUNT(*) DESC
LIMIT 20
`;
const [nodesCountPerAS]: any = await DB.query(query);
query = `SELECT COUNT(*) as total FROM nodes WHERE as_number IS NOT NULL`;
const [nodesWithAS]: any = await DB.query(query);
const nodesPerAs: any[] = [];
for (const as of nodesCountPerAS) {
nodesPerAs.push({
name: JSON.parse(as.names),
count: as.nodesCount,
share: Math.floor(as.nodesCount / nodesWithAS[0].total * 10000) / 100,
})
}
return nodesPerAs;
} catch (e) {
logger.err(`Cannot get nodes grouped by AS. Reason: ${e instanceof Error ? e.message : e}`);
throw e;
}
}
} }
export default new NodesApi(); export default new NodesApi();

View file

@ -8,6 +8,7 @@ class NodesRoutes {
app app
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/search/:search', this.$searchNode) .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/search/:search', this.$searchNode)
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/top', this.$getTopNodes) .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/top', this.$getTopNodes)
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/asShare', this.$getNodesAsShare)
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/:public_key/statistics', this.$getHistoricalNodeStats) .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/:public_key/statistics', this.$getHistoricalNodeStats)
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/:public_key', this.$getNode) .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/:public_key', this.$getNode)
; ;
@ -56,6 +57,18 @@ class NodesRoutes {
res.status(500).send(e instanceof Error ? e.message : e); res.status(500).send(e instanceof Error ? e.message : e);
} }
} }
private async $getNodesAsShare(req: Request, res: Response) {
try {
const nodesPerAs = await nodesApi.$getNodesAsShare();
res.header('Pragma', 'public');
res.header('Cache-control', 'public');
res.setHeader('Expires', new Date(Date.now() + 1000 * 600).toUTCString());
res.json(nodesPerAs);
} catch (e) {
res.status(500).send(e instanceof Error ? e.message : e);
}
}
} }
export default new NodesRoutes(); export default new NodesRoutes();