diff --git a/frontend/src/app/lightning/node-statistics/node-statistics.component.html b/frontend/src/app/lightning/node-statistics/node-statistics.component.html index 6991e2b66..f45daa7fd 100644 --- a/frontend/src/app/lightning/node-statistics/node-statistics.component.html +++ b/frontend/src/app/lightning/node-statistics/node-statistics.component.html @@ -4,8 +4,8 @@
Capacity
- - + +
@@ -15,9 +15,9 @@
- {{ statistics.latest.node_count | number }} + {{ statistics.latest?.node_count || 0 | number }}
- +
@@ -27,9 +27,9 @@
- {{ statistics.latest.channel_count | number }} + {{ statistics.latest?.channel_count || 0 | number }}
- +
diff --git a/lightning-backend/src/api/explorer/nodes.api.ts b/lightning-backend/src/api/explorer/nodes.api.ts index 0b034d230..119336723 100644 --- a/lightning-backend/src/api/explorer/nodes.api.ts +++ b/lightning-backend/src/api/explorer/nodes.api.ts @@ -15,7 +15,7 @@ class NodesApi { public async $getNodeStats(public_key: string): Promise { try { - const query = `SELECT * FROM nodes_stats WHERE public_key = ? ORDER BY added DESC`; + const query = `SELECT * FROM node_stats WHERE public_key = ? ORDER BY added DESC`; const [rows]: any = await DB.query(query, [public_key]); return rows; } catch (e) { @@ -26,7 +26,7 @@ class NodesApi { public async $getTopCapacityNodes(): Promise { try { - const query = `SELECT nodes.*, nodes_stats.capacity, nodes_stats.channels FROM nodes LEFT JOIN nodes_stats ON nodes_stats.public_key = nodes.public_key ORDER BY nodes_stats.added DESC, nodes_stats.capacity DESC LIMIT 10`; + const query = `SELECT nodes.*, node_stats.capacity, node_stats.channels FROM nodes LEFT JOIN node_stats ON node_stats.public_key = nodes.public_key ORDER BY node_stats.added DESC, node_stats.capacity DESC LIMIT 10`; const [rows]: any = await DB.query(query); return rows; } catch (e) { @@ -37,7 +37,7 @@ class NodesApi { public async $getTopChannelsNodes(): Promise { try { - const query = `SELECT nodes.*, nodes_stats.capacity, nodes_stats.channels FROM nodes LEFT JOIN nodes_stats ON nodes_stats.public_key = nodes.public_key ORDER BY nodes_stats.added DESC, nodes_stats.channels DESC LIMIT 10`; + const query = `SELECT nodes.*, node_stats.capacity, node_stats.channels FROM nodes LEFT JOIN node_stats ON node_stats.public_key = nodes.public_key ORDER BY node_stats.added DESC, node_stats.channels DESC LIMIT 10`; const [rows]: any = await DB.query(query); return rows; } catch (e) { diff --git a/lightning-backend/src/database-migration.ts b/lightning-backend/src/database-migration.ts index 8c16da676..37b5301cf 100644 --- a/lightning-backend/src/database-migration.ts +++ b/lightning-backend/src/database-migration.ts @@ -76,7 +76,7 @@ class DatabaseMigration { await this.$executeQuery(this.getCreateStatisticsQuery(), await this.$checkIfTableExists('statistics')); await this.$executeQuery(this.getCreateNodesQuery(), await this.$checkIfTableExists('nodes')); await this.$executeQuery(this.getCreateChannelsQuery(), await this.$checkIfTableExists('channels')); - await this.$executeQuery(this.getCreateNodesStatsQuery(), await this.$checkIfTableExists('nodes_stats')); + await this.$executeQuery(this.getCreateNodesStatsQuery(), await this.$checkIfTableExists('node_stats')); } catch (e) { throw e; } @@ -187,8 +187,7 @@ class DatabaseMigration { channel_count int(11) NOT NULL, node_count int(11) NOT NULL, total_capacity double unsigned NOT NULL, - average_channel_size double unsigned NOT NULL, - CONSTRAINT PRIMARY KEY (id) + PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`; } @@ -197,26 +196,30 @@ class DatabaseMigration { public_key varchar(66) NOT NULL, first_seen datetime NOT NULL, updated_at datetime NOT NULL, - alias varchar(200) COLLATE utf8mb4_general_ci NOT NULL, + alias varchar(200) CHARACTER SET utf8mb4 NOT NULL, color varchar(200) NOT NULL, - CONSTRAINT PRIMARY KEY (public_key) + sockets text DEFAULT NULL, + PRIMARY KEY (public_key) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`; } private getCreateChannelsQuery(): string { return `CREATE TABLE IF NOT EXISTS channels ( - id varchar(15) NOT NULL, + id bigint(11) unsigned NOT NULL, + short_id varchar(15) NOT NULL DEFAULT '', capacity bigint(20) unsigned NOT NULL, transaction_id varchar(64) NOT NULL, transaction_vout int(11) NOT NULL, updated_at datetime DEFAULT NULL, + created datetime DEFAULT NULL, + status int(11) NOT NULL DEFAULT 0, node1_public_key varchar(66) NOT NULL, node1_base_fee_mtokens bigint(20) unsigned DEFAULT NULL, node1_cltv_delta int(11) DEFAULT NULL, node1_fee_rate bigint(11) DEFAULT NULL, node1_is_disabled tinyint(1) DEFAULT NULL, node1_max_htlc_mtokens bigint(20) unsigned DEFAULT NULL, - node1_min_htlc_mtokens bigint(20) unsigned DEFAULT NULL, + node1_min_htlc_mtokens bigint(20) DEFAULT NULL, node1_updated_at datetime DEFAULT NULL, node2_public_key varchar(66) NOT NULL, node2_base_fee_mtokens bigint(20) unsigned DEFAULT NULL, @@ -229,18 +232,21 @@ class DatabaseMigration { PRIMARY KEY (id), KEY node1_public_key (node1_public_key), KEY node2_public_key (node2_public_key), - KEY node1_public_key_2 (node1_public_key,node2_public_key) + KEY status (status), + KEY short_id (short_id), + KEY transaction_id (transaction_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`; } private getCreateNodesStatsQuery(): string { - return `CREATE TABLE nodes_stats ( + return `CREATE TABLE IF NOT EXISTS node_stats ( id int(11) unsigned NOT NULL AUTO_INCREMENT, public_key varchar(66) NOT NULL DEFAULT '', added date NOT NULL, - capacity bigint(11) unsigned DEFAULT NULL, - channels int(11) unsigned DEFAULT NULL, + capacity bigint(20) unsigned NOT NULL DEFAULT 0, + channels int(11) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (id), + UNIQUE KEY added (added,public_key), KEY public_key (public_key) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`; } diff --git a/lightning-backend/src/tasks/stats-updater.service.ts b/lightning-backend/src/tasks/stats-updater.service.ts index c96273ff1..c0a6392b1 100644 --- a/lightning-backend/src/tasks/stats-updater.service.ts +++ b/lightning-backend/src/tasks/stats-updater.service.ts @@ -36,9 +36,14 @@ class LightningStatsUpdater { const query = `SELECT nodes.public_key, c1.channels_count_left, c2.channels_count_right, c1.channels_capacity_left, c2.channels_capacity_right FROM nodes LEFT JOIN (SELECT node1_public_key, COUNT(id) AS channels_count_left, SUM(capacity) AS channels_capacity_left FROM channels GROUP BY node1_public_key) c1 ON c1.node1_public_key = nodes.public_key LEFT JOIN (SELECT node2_public_key, COUNT(id) AS channels_count_right, SUM(capacity) AS channels_capacity_right FROM channels GROUP BY node2_public_key) c2 ON c2.node2_public_key = nodes.public_key`; const [nodes]: any = await DB.query(query); + // First run we won't have any nodes yet + if (nodes.length < 10) { + return; + } + for (const node of nodes) { await DB.query( - `INSERT INTO nodes_stats(public_key, added, capacity, channels) VALUES (?, NOW(), ?, ?)`, + `INSERT INTO node_stats(public_key, added, capacity, channels) VALUES (?, NOW(), ?, ?)`, [node.public_key, (parseInt(node.channels_capacity_left || 0, 10)) + (parseInt(node.channels_capacity_right || 0, 10)), node.channels_count_left + node.channels_count_right]); }