From 27c39ef5573e3e13fc3171b576f8bcab35fdfac7 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Fri, 21 Oct 2022 20:09:20 +0200 Subject: [PATCH 1/2] Only show active nodes is isp page --- backend/src/api/explorer/nodes.api.ts | 42 +++++++++++++++---- .../nodes-per-isp.component.html | 2 +- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/backend/src/api/explorer/nodes.api.ts b/backend/src/api/explorer/nodes.api.ts index cbd70a34f..020dd10dd 100644 --- a/backend/src/api/explorer/nodes.api.ts +++ b/backend/src/api/explorer/nodes.api.ts @@ -462,7 +462,32 @@ class NodesApi { public async $getNodesPerISP(ISPId: string) { try { - const query = ` + let query = ` + SELECT channels.node1_public_key AS node1PublicKey, isp1.id as isp1ID, + channels.node2_public_key AS node2PublicKey, isp2.id as isp2ID + FROM channels + JOIN nodes node1 ON node1.public_key = channels.node1_public_key + JOIN nodes node2 ON node2.public_key = channels.node2_public_key + JOIN geo_names isp1 ON isp1.id = node1.as_number + JOIN geo_names isp2 ON isp2.id = node2.as_number + WHERE channels.status = 1 AND (node1.as_number IN (?) OR node2.as_number IN (?)) + ORDER BY short_id DESC + `; + + const [rows]: any = await DB.query(query, [ISPId.split(','), ISPId.split(',')]); + const nodes = {}; + + const intIspId = parseInt(ISPId); + for (const channel of rows) { + if (channel.isp1ID === intIspId) { + nodes[channel.node1PublicKey] = true; + } + if (channel.isp2ID === intIspId) { + nodes[channel.node2PublicKey] = true; + } + } + + query = ` SELECT nodes.public_key, CAST(COALESCE(nodes.capacity, 0) as INT) as capacity, CAST(COALESCE(nodes.channels, 0) as INT) as channels, nodes.alias, UNIX_TIMESTAMP(nodes.first_seen) as first_seen, UNIX_TIMESTAMP(nodes.updated_at) as updated_at, geo_names_city.names as city, geo_names_country.names as country, @@ -473,17 +498,18 @@ class NodesApi { LEFT JOIN geo_names geo_names_city ON geo_names_city.id = nodes.city_id AND geo_names_city.type = 'city' LEFT JOIN geo_names geo_names_iso ON geo_names_iso.id = nodes.country_id AND geo_names_iso.type = 'country_iso_code' LEFT JOIN geo_names geo_names_subdivision on geo_names_subdivision.id = nodes.subdivision_id AND geo_names_subdivision.type = 'division' - WHERE nodes.as_number IN (?) + WHERE nodes.public_key IN (?) ORDER BY capacity DESC `; - const [rows]: any = await DB.query(query, [ISPId.split(',')]); - for (let i = 0; i < rows.length; ++i) { - rows[i].country = JSON.parse(rows[i].country); - rows[i].city = JSON.parse(rows[i].city); - rows[i].subdivision = JSON.parse(rows[i].subdivision); + const [rows2]: any = await DB.query(query, [Object.keys(nodes)]); + for (let i = 0; i < rows2.length; ++i) { + rows2[i].country = JSON.parse(rows2[i].country); + rows2[i].city = JSON.parse(rows2[i].city); + rows2[i].subdivision = JSON.parse(rows2[i].subdivision); } - return rows; + return rows2; + } catch (e) { logger.err(`Cannot get nodes for ISP id ${ISPId}. Reason: ${e instanceof Error ? e.message : e}`); throw e; diff --git a/frontend/src/app/lightning/nodes-per-isp/nodes-per-isp.component.html b/frontend/src/app/lightning/nodes-per-isp/nodes-per-isp.component.html index 441dc429e..5f18c26b7 100644 --- a/frontend/src/app/lightning/nodes-per-isp/nodes-per-isp.component.html +++ b/frontend/src/app/lightning/nodes-per-isp/nodes-per-isp.component.html @@ -11,7 +11,7 @@ {{ isp?.id }} - Nodes + Active nodes {{ ispNodes.nodes.length }} From 9d5717f30de14b2aefe6e3091a6321600b35043a Mon Sep 17 00:00:00 2001 From: nymkappa Date: Tue, 22 Nov 2022 11:58:16 +0900 Subject: [PATCH 2/2] Make sure we handle all isp id in the queried list --- backend/src/api/explorer/nodes.api.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/backend/src/api/explorer/nodes.api.ts b/backend/src/api/explorer/nodes.api.ts index 4d49b1d67..f7b283213 100644 --- a/backend/src/api/explorer/nodes.api.ts +++ b/backend/src/api/explorer/nodes.api.ts @@ -524,15 +524,20 @@ class NodesApi { ORDER BY short_id DESC `; - const [rows]: any = await DB.query(query, [ISPId.split(','), ISPId.split(',')]); + const IPSIds = ISPId.split(','); + const [rows]: any = await DB.query(query, [IPSIds, IPSIds]); const nodes = {}; - const intIspId = parseInt(ISPId); + const intISPIds: number[] = []; + for (const ispId of IPSIds) { + intISPIds.push(parseInt(ispId, 10)); + } + for (const channel of rows) { - if (channel.isp1ID === intIspId) { + if (intISPIds.includes(channel.isp1ID)) { nodes[channel.node1PublicKey] = true; } - if (channel.isp2ID === intIspId) { + if (intISPIds.includes(channel.isp2ID)) { nodes[channel.node2PublicKey] = true; } }