2022-07-16 15:56:36 +02:00
import { ChangeDetectionStrategy , Component , Input , OnInit } from '@angular/core' ;
import { ActivatedRoute } from '@angular/router' ;
2022-09-09 14:56:18 +02:00
import { map , Observable , share } from 'rxjs' ;
2022-09-21 17:23:45 +02:00
import { ApiService } from '../../services/api.service' ;
import { SeoService } from '../../services/seo.service' ;
import { getFlagEmoji } from '../../shared/common.utils' ;
import { GeolocationData } from '../../shared/components/geolocation/geolocation.component' ;
2022-07-16 15:56:36 +02:00
@Component ( {
selector : 'app-nodes-per-country' ,
templateUrl : './nodes-per-country.component.html' ,
styleUrls : [ './nodes-per-country.component.scss' ] ,
changeDetection : ChangeDetectionStrategy.OnPush ,
} )
export class NodesPerCountry implements OnInit {
nodes$ : Observable < any > ;
2022-07-18 00:55:47 +02:00
country : { name : string , flag : string } ;
2022-07-16 15:56:36 +02:00
2022-09-06 11:01:46 +02:00
skeletonLines : number [ ] = [ ] ;
2022-07-16 15:56:36 +02:00
constructor (
private apiService : ApiService ,
2022-07-16 23:25:44 +02:00
private seoService : SeoService ,
2022-07-16 15:56:36 +02:00
private route : ActivatedRoute ,
2022-09-06 11:01:46 +02:00
) {
for ( let i = 0 ; i < 20 ; ++ i ) {
this . skeletonLines . push ( i ) ;
}
}
2022-07-16 15:56:36 +02:00
ngOnInit ( ) : void {
this . nodes $ = this . apiService . getNodeForCountry $ ( this . route . snapshot . params . country )
. pipe (
2022-07-16 23:25:44 +02:00
map ( response = > {
2022-09-09 14:56:18 +02:00
this . seoService . setTitle ( $localize ` Lightning nodes in ${ response . country . en } ` ) ;
2023-08-30 20:26:07 +09:00
this . seoService . setDescription ( $localize ` :@@meta.description.lightning.nodes-country:Explore all the Lightning nodes hosted in ${ response . country . en } and see an overview of each node's capacity, number of open channels, and more. ` ) ;
2022-09-09 14:56:18 +02:00
2022-07-18 00:55:47 +02:00
this . country = {
name : response.country.en ,
flag : getFlagEmoji ( this . route . snapshot . params . country )
} ;
2022-08-18 17:14:09 +02:00
for ( const i in response . nodes ) {
response . nodes [ i ] . geolocation = < GeolocationData > {
country : response.nodes [ i ] . country ? . en ,
city : response.nodes [ i ] . city ? . en ,
subdivision : response.nodes [ i ] . subdivision ? . en ,
iso : response.nodes [ i ] . iso_code ,
} ;
}
2023-08-30 20:26:07 +09:00
2022-09-09 14:56:18 +02:00
const sumLiquidity = response . nodes . reduce ( ( partialSum , a ) = > partialSum + a . capacity , 0 ) ;
const sumChannels = response . nodes . reduce ( ( partialSum , a ) = > partialSum + a . channels , 0 ) ;
const isps = { } ;
const topIsp = {
count : 0 ,
id : '' ,
name : '' ,
} ;
for ( const node of response . nodes ) {
if ( ! node . isp ) {
continue ;
}
if ( ! isps [ node . isp ] ) {
isps [ node . isp ] = {
count : 0 ,
asns : [ ] ,
} ;
}
if ( isps [ node . isp ] . asns . indexOf ( node . as_number ) === - 1 ) {
isps [ node . isp ] . asns . push ( node . as_number ) ;
}
isps [ node . isp ] . count ++ ;
2023-08-30 20:26:07 +09:00
2022-09-09 14:56:18 +02:00
if ( isps [ node . isp ] . count > topIsp . count ) {
topIsp . count = isps [ node . isp ] . count ;
topIsp . id = isps [ node . isp ] . asns . join ( ',' ) ;
topIsp . name = node . isp ;
}
}
2023-08-30 20:26:07 +09:00
2022-09-09 14:56:18 +02:00
return {
nodes : response.nodes ,
sumLiquidity : sumLiquidity ,
sumChannels : sumChannels ,
topIsp : topIsp ,
ispCount : Object.keys ( isps ) . length
} ;
} ) ,
share ( )
2022-07-16 15:56:36 +02:00
) ;
}
2022-09-09 14:56:18 +02:00
trackByPublicKey ( index : number , node : any ) : string {
2022-07-16 15:56:36 +02:00
return node . public_key ;
}
}