2022-04-27 02:52:23 +04:00
import logger from '../../logger' ;
import DB from '../../database' ;
class NodesApi {
2022-04-29 03:57:27 +04:00
public async $getNode ( public_key : string ) : Promise < any > {
try {
2022-07-12 20:18:41 +02:00
const query = `
SELECT nodes . * ,
( SELECT Count ( * )
FROM channels
WHERE channels . status < 2 AND ( channels . node1_public_key = ? OR channels . node2_public_key = ? ) ) AS channel_count ,
( SELECT Sum ( capacity )
FROM channels
WHERE channels . status < 2 AND ( channels . node1_public_key = ? OR channels . node2_public_key = ? ) ) AS capacity ,
( SELECT Avg ( capacity )
FROM channels
WHERE status < 2 AND ( node1_public_key = ? OR node2_public_key = ? ) ) AS channels_capacity_avg
FROM nodes
WHERE public_key = ?
` ;
2022-05-15 16:48:38 +04:00
const [ rows ] : any = await DB . query ( query , [ public_key , public_key , public_key , public_key , public_key , public_key , public_key ] ) ;
2022-04-29 03:57:27 +04:00
return rows [ 0 ] ;
} catch ( e ) {
logger . err ( '$getNode error: ' + ( e instanceof Error ? e.message : e ) ) ;
throw e ;
}
}
2022-07-11 17:52:38 +02:00
public async $getAllNodes ( ) : Promise < any > {
try {
const query = ` SELECT * FROM nodes ` ;
const [ rows ] : any = await DB . query ( query ) ;
return rows ;
} catch ( e ) {
logger . err ( '$getAllNodes error: ' + ( e instanceof Error ? e.message : e ) ) ;
throw e ;
}
}
2022-05-05 23:19:24 +04:00
public async $getNodeStats ( public_key : string ) : Promise < any > {
try {
2022-07-02 16:46:57 +02:00
const query = ` SELECT UNIX_TIMESTAMP(added) AS added, capacity, channels FROM node_stats WHERE public_key = ? ORDER BY added DESC ` ;
2022-05-05 23:19:24 +04:00
const [ rows ] : any = await DB . query ( query , [ public_key ] ) ;
return rows ;
} catch ( e ) {
logger . err ( '$getNodeStats error: ' + ( e instanceof Error ? e.message : e ) ) ;
throw e ;
}
}
2022-04-27 02:52:23 +04:00
public async $getTopCapacityNodes ( ) : Promise < any > {
try {
2022-05-08 16:16:54 +04:00
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 ` ;
2022-04-27 02:52:23 +04:00
const [ rows ] : any = await DB . query ( query ) ;
return rows ;
} catch ( e ) {
logger . err ( '$getTopCapacityNodes error: ' + ( e instanceof Error ? e.message : e ) ) ;
throw e ;
}
}
public async $getTopChannelsNodes ( ) : Promise < any > {
try {
2022-05-08 16:16:54 +04:00
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 ` ;
2022-04-27 02:52:23 +04:00
const [ rows ] : any = await DB . query ( query ) ;
return rows ;
} catch ( e ) {
logger . err ( '$getTopChannelsNodes error: ' + ( e instanceof Error ? e.message : e ) ) ;
throw e ;
}
}
2022-05-09 18:21:42 +04:00
public async $searchNodeByPublicKeyOrAlias ( search : string ) {
try {
const searchStripped = search . replace ( '%' , '' ) + '%' ;
2022-05-15 19:22:14 +04:00
const query = ` SELECT nodes.public_key, nodes.alias, node_stats.capacity FROM nodes LEFT JOIN node_stats ON node_stats.public_key = nodes.public_key WHERE nodes.public_key LIKE ? OR nodes.alias LIKE ? GROUP BY nodes.public_key ORDER BY node_stats.capacity DESC LIMIT 10 ` ;
2022-05-09 18:21:42 +04:00
const [ rows ] : any = await DB . query ( query , [ searchStripped , searchStripped ] ) ;
return rows ;
} catch ( e ) {
logger . err ( '$searchNodeByPublicKeyOrAlias error: ' + ( e instanceof Error ? e.message : e ) ) ;
throw e ;
}
}
2022-04-27 02:52:23 +04:00
}
export default new NodesApi ( ) ;