2020-09-26 17:46:26 +02:00
|
|
|
import { ChangeDetectionStrategy, Component, Inject, LOCALE_ID, OnInit } from '@angular/core';
|
2020-09-21 14:41:12 +02:00
|
|
|
import { combineLatest, merge, Observable, of } from 'rxjs';
|
2020-09-28 18:05:42 +02:00
|
|
|
import { filter, map, scan, share, switchMap, tap } from 'rxjs/operators';
|
2020-09-25 21:11:30 +02:00
|
|
|
import { Block } from '../interfaces/electrs.interface';
|
2020-09-26 17:46:26 +02:00
|
|
|
import { OptimizedMempoolStats } from '../interfaces/node-api.interface';
|
2020-09-25 21:11:30 +02:00
|
|
|
import { MempoolInfo, TransactionStripped } from '../interfaces/websocket.interface';
|
2020-09-26 17:46:26 +02:00
|
|
|
import { ApiService } from '../services/api.service';
|
2020-09-21 14:41:12 +02:00
|
|
|
import { StateService } from '../services/state.service';
|
2020-09-26 17:46:26 +02:00
|
|
|
import * as Chartist from 'chartist';
|
|
|
|
import { formatDate } from '@angular/common';
|
|
|
|
import { WebsocketService } from '../services/websocket.service';
|
2020-09-28 11:32:48 +02:00
|
|
|
import { SeoService } from '../services/seo.service';
|
2020-10-26 20:58:29 +01:00
|
|
|
import { StorageService } from '../services/storage.service';
|
2020-09-21 14:41:12 +02:00
|
|
|
|
|
|
|
interface MempoolBlocksData {
|
|
|
|
blocks: number;
|
|
|
|
size: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface EpochProgress {
|
|
|
|
base: string;
|
|
|
|
green: string;
|
|
|
|
red: string;
|
2020-09-21 22:31:19 +02:00
|
|
|
change: number;
|
2020-09-21 14:41:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface MempoolInfoData {
|
|
|
|
memPoolInfo: MempoolInfo;
|
|
|
|
vBytesPerSecond: number;
|
|
|
|
progressWidth: string;
|
|
|
|
progressClass: string;
|
|
|
|
}
|
|
|
|
|
2020-09-26 17:46:26 +02:00
|
|
|
interface MempoolStatsData {
|
|
|
|
mempool: OptimizedMempoolStats[];
|
|
|
|
weightPerSecond: any;
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:41:12 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-dashboard',
|
|
|
|
templateUrl: './dashboard.component.html',
|
|
|
|
styleUrls: ['./dashboard.component.scss'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
|
|
})
|
|
|
|
export class DashboardComponent implements OnInit {
|
2020-10-26 20:58:29 +01:00
|
|
|
collapsed = true;
|
2020-09-21 14:41:12 +02:00
|
|
|
network$: Observable<string>;
|
|
|
|
mempoolBlocksData$: Observable<MempoolBlocksData>;
|
|
|
|
mempoolInfoData$: Observable<MempoolInfoData>;
|
|
|
|
difficultyEpoch$: Observable<EpochProgress>;
|
|
|
|
vBytesPerSecondLimit = 1667;
|
2020-09-25 21:11:30 +02:00
|
|
|
blocks$: Observable<Block[]>;
|
|
|
|
transactions$: Observable<TransactionStripped[]>;
|
|
|
|
latestBlockHeight: number;
|
2020-09-26 17:46:26 +02:00
|
|
|
mempoolTransactionsWeightPerSecondData: any;
|
|
|
|
mempoolStats$: Observable<MempoolStatsData>;
|
|
|
|
transactionsWeightPerSecondOptions: any;
|
2020-09-21 14:41:12 +02:00
|
|
|
|
|
|
|
constructor(
|
2020-09-26 17:46:26 +02:00
|
|
|
@Inject(LOCALE_ID) private locale: string,
|
2020-09-21 14:41:12 +02:00
|
|
|
private stateService: StateService,
|
2020-09-26 17:46:26 +02:00
|
|
|
private apiService: ApiService,
|
|
|
|
private websocketService: WebsocketService,
|
2020-09-28 11:32:48 +02:00
|
|
|
private seoService: SeoService,
|
2020-10-26 20:58:29 +01:00
|
|
|
private storageService: StorageService,
|
2020-09-21 14:41:12 +02:00
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2020-09-28 11:32:48 +02:00
|
|
|
this.seoService.resetTitle();
|
2020-09-26 17:46:26 +02:00
|
|
|
this.websocketService.want(['blocks', 'stats', 'mempool-blocks', 'live-2h-chart']);
|
2020-09-21 14:41:12 +02:00
|
|
|
this.network$ = merge(of(''), this.stateService.networkChanged$);
|
2020-10-26 20:58:29 +01:00
|
|
|
this.collapsed = this.storageService.getValue('dashboard-collapsed') === 'true' || false;
|
2020-09-21 14:41:12 +02:00
|
|
|
|
|
|
|
this.mempoolInfoData$ = combineLatest([
|
|
|
|
this.stateService.mempoolInfo$,
|
|
|
|
this.stateService.vbytesPerSecond$
|
|
|
|
])
|
|
|
|
.pipe(
|
|
|
|
map(([mempoolInfo, vbytesPerSecond]) => {
|
|
|
|
const percent = Math.round((Math.min(vbytesPerSecond, this.vBytesPerSecondLimit) / this.vBytesPerSecondLimit) * 100);
|
|
|
|
|
|
|
|
let progressClass = 'bg-danger';
|
|
|
|
if (percent <= 75) {
|
|
|
|
progressClass = 'bg-success';
|
|
|
|
} else if (percent <= 99) {
|
|
|
|
progressClass = 'bg-warning';
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
memPoolInfo: mempoolInfo,
|
|
|
|
vBytesPerSecond: vbytesPerSecond,
|
|
|
|
progressWidth: percent + '%',
|
|
|
|
progressClass: progressClass,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
this.difficultyEpoch$ = combineLatest([
|
|
|
|
this.stateService.blocks$.pipe(map(([block]) => block)),
|
|
|
|
this.stateService.lastDifficultyAdjustment$
|
|
|
|
])
|
|
|
|
.pipe(
|
|
|
|
map(([block, DATime]) => {
|
|
|
|
const now = new Date().getTime() / 1000;
|
|
|
|
const diff = now - DATime;
|
|
|
|
const blocksInEpoch = block.height % 2016;
|
|
|
|
const estimatedBlocks = Math.round(diff / 60 / 10);
|
2020-09-22 07:26:54 +02:00
|
|
|
const difficultyChange = (blocksInEpoch - (diff / 60 / 10)) / blocksInEpoch * 100;
|
2020-09-21 14:41:12 +02:00
|
|
|
|
|
|
|
let base = 0;
|
|
|
|
let green = 0;
|
|
|
|
let red = 0;
|
|
|
|
|
|
|
|
if (blocksInEpoch >= estimatedBlocks) {
|
|
|
|
base = estimatedBlocks / 2016 * 100;
|
|
|
|
green = (blocksInEpoch - estimatedBlocks) / 2016 * 100;
|
|
|
|
} else {
|
|
|
|
base = blocksInEpoch / 2016 * 100;
|
|
|
|
red = (estimatedBlocks - blocksInEpoch) / 2016 * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
base: base + '%',
|
|
|
|
green: green + '%',
|
|
|
|
red: red + '%',
|
2020-09-21 22:31:19 +02:00
|
|
|
change: difficultyChange,
|
2020-09-21 14:41:12 +02:00
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
this.mempoolBlocksData$ = this.stateService.mempoolBlocks$
|
|
|
|
.pipe(
|
|
|
|
map((mempoolBlocks) => {
|
|
|
|
const size = mempoolBlocks.map((m) => m.blockSize).reduce((a, b) => a + b, 0);
|
|
|
|
const vsize = mempoolBlocks.map((m) => m.blockVSize).reduce((a, b) => a + b, 0);
|
|
|
|
|
|
|
|
return {
|
|
|
|
size: size,
|
|
|
|
blocks: Math.ceil(vsize / 1000000)
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
2020-09-25 21:11:30 +02:00
|
|
|
|
|
|
|
this.blocks$ = this.stateService.blocks$
|
|
|
|
.pipe(
|
|
|
|
tap(([block]) => {
|
|
|
|
this.latestBlockHeight = block.height;
|
|
|
|
}),
|
|
|
|
scan((acc, [block]) => {
|
|
|
|
acc.unshift(block);
|
|
|
|
return acc;
|
|
|
|
}, []),
|
|
|
|
map((blocks) => blocks.slice(0, 6)),
|
|
|
|
);
|
|
|
|
|
|
|
|
this.transactions$ = this.stateService.transactions$
|
|
|
|
.pipe(
|
|
|
|
scan((acc, tx) => {
|
|
|
|
acc.unshift(tx);
|
|
|
|
return acc;
|
|
|
|
}, []),
|
|
|
|
map((txs) => txs.slice(0, 6)),
|
|
|
|
);
|
2020-09-26 17:46:26 +02:00
|
|
|
|
2020-09-28 18:05:42 +02:00
|
|
|
this.mempoolStats$ = this.stateService.connectionState$.pipe(
|
|
|
|
filter((state) => state === 2),
|
|
|
|
switchMap(() => this.apiService.list2HStatistics$()),
|
|
|
|
switchMap((mempoolStats) => {
|
|
|
|
return merge(
|
|
|
|
this.stateService.live2Chart$
|
|
|
|
.pipe(
|
|
|
|
scan((acc, stats) => {
|
|
|
|
acc.unshift(stats);
|
|
|
|
acc = acc.slice(0, acc.length - 1);
|
|
|
|
return acc;
|
|
|
|
}, mempoolStats)
|
|
|
|
),
|
|
|
|
of(mempoolStats)
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
map((mempoolStats) => {
|
|
|
|
return {
|
|
|
|
mempool: mempoolStats,
|
|
|
|
weightPerSecond: this.handleNewMempoolData(mempoolStats.concat([])),
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
share(),
|
|
|
|
);
|
2020-09-26 17:46:26 +02:00
|
|
|
|
|
|
|
this.transactionsWeightPerSecondOptions = {
|
|
|
|
showArea: false,
|
|
|
|
showLine: true,
|
|
|
|
fullWidth: true,
|
|
|
|
showPoint: false,
|
|
|
|
low: 0,
|
|
|
|
axisY: {
|
|
|
|
offset: 40
|
|
|
|
},
|
|
|
|
axisX: {
|
2020-09-27 21:51:01 +02:00
|
|
|
labelInterpolationFnc: (value: any, index: any) => index % 24 === 0 ? formatDate(value, 'HH:mm', this.locale) : null,
|
2020-10-26 17:12:32 +01:00
|
|
|
offset: 20
|
2020-09-26 17:46:26 +02:00
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
Chartist.plugins.ctTargetLine({
|
|
|
|
value: 1667
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleNewMempoolData(mempoolStats: OptimizedMempoolStats[]) {
|
|
|
|
mempoolStats.reverse();
|
|
|
|
const labels = mempoolStats.map(stats => stats.added);
|
|
|
|
|
|
|
|
return {
|
|
|
|
labels: labels,
|
|
|
|
series: [mempoolStats.map((stats) => stats.vbytes_per_second)],
|
|
|
|
};
|
2020-09-25 21:11:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
trackByBlock(index: number, block: Block) {
|
|
|
|
return block.height;
|
2020-09-21 14:41:12 +02:00
|
|
|
}
|
2020-10-26 20:58:29 +01:00
|
|
|
|
|
|
|
toggleCollapsed() {
|
|
|
|
this.collapsed = !this.collapsed;
|
|
|
|
this.storageService.setValue('dashboard-collapsed', this.collapsed);
|
|
|
|
}
|
2020-09-21 14:41:12 +02:00
|
|
|
}
|