mempool/frontend/src/app/components/statistics/statistics.component.ts

199 lines
6.7 KiB
TypeScript
Raw Normal View History

2019-07-21 17:59:47 +03:00
import { Component, OnInit, LOCALE_ID, Inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
2019-07-21 17:59:47 +03:00
import { FormGroup, FormBuilder } from '@angular/forms';
import { of, merge} from 'rxjs';
2020-03-29 23:59:04 +07:00
import { switchMap } from 'rxjs/operators';
2020-02-17 00:26:57 +07:00
import { OptimizedMempoolStats } from '../../interfaces/node-api.interface';
import { WebsocketService } from '../../services/websocket.service';
import { ApiService } from '../../services/api.service';
import { StateService } from 'src/app/services/state.service';
2020-03-24 00:52:08 +07:00
import { SeoService } from 'src/app/services/seo.service';
import { StorageService } from 'src/app/services/storage.service';
import { feeLevels, chartColors } from 'src/app/app.constants';
2019-07-21 17:59:47 +03:00
@Component({
selector: 'app-statistics',
templateUrl: './statistics.component.html',
styleUrls: ['./statistics.component.scss']
})
export class StatisticsComponent implements OnInit {
network = '';
2020-03-28 19:24:13 +07:00
2019-07-21 17:59:47 +03:00
loading = true;
spinnerLoading = false;
feeLevels = feeLevels;
chartColors = chartColors;
filterFeeIndex = 1;
dropDownOpen = false;
2019-07-21 17:59:47 +03:00
2020-02-17 00:26:57 +07:00
mempoolStats: OptimizedMempoolStats[] = [];
2019-07-21 17:59:47 +03:00
mempoolVsizeFeesData: any;
mempoolUnconfirmedTransactionsData: any;
mempoolTransactionsWeightPerSecondData: any;
radioGroupForm: FormGroup;
graphWindowPreference: string;
2021-09-26 11:41:55 -03:00
inverted: boolean;
feeLevelDropdownData = [];
2019-07-21 17:59:47 +03:00
constructor(
@Inject(LOCALE_ID) private locale: string,
private formBuilder: FormBuilder,
private route: ActivatedRoute,
private websocketService: WebsocketService,
private apiService: ApiService,
private stateService: StateService,
2020-03-24 00:52:08 +07:00
private seoService: SeoService,
private storageService: StorageService,
) { }
2019-07-21 17:59:47 +03:00
ngOnInit() {
2021-09-26 11:41:55 -03:00
this.inverted = this.storageService.getValue('inverted-graph') === 'true';
this.setFeeLevelDropdownData();
2020-12-03 18:34:19 +07:00
this.seoService.setTitle($localize`:@@5d4f792f048fcaa6df5948575d7cb325c9393383:Graphs`);
this.stateService.networkChanged$.subscribe((network) => this.network = network);
this.graphWindowPreference = this.storageService.getValue('graphWindowPreference') ? this.storageService.getValue('graphWindowPreference').trim() : '2h';
this.radioGroupForm = this.formBuilder.group({
dateSpan: this.graphWindowPreference
});
2019-07-21 17:59:47 +03:00
this.route
.fragment
.subscribe((fragment) => {
2021-11-01 22:06:10 -03:00
if (['2h', '24h', '1w', '1m', '3m', '6m', '1y', '2y', '3y'].indexOf(fragment) > -1) {
this.radioGroupForm.controls.dateSpan.setValue(fragment, { emitEvent: false });
2019-07-21 17:59:47 +03:00
}
});
merge(
of(''),
this.radioGroupForm.controls.dateSpan.valueChanges
2019-07-21 17:59:47 +03:00
)
.pipe(
switchMap(() => {
this.spinnerLoading = true;
if (this.radioGroupForm.controls.dateSpan.value === '2h') {
this.websocketService.want(['blocks', 'live-2h-chart']);
return this.apiService.list2HStatistics$();
2019-07-21 17:59:47 +03:00
}
this.websocketService.want(['blocks']);
if (this.radioGroupForm.controls.dateSpan.value === '24h') {
return this.apiService.list24HStatistics$();
2019-07-21 17:59:47 +03:00
}
if (this.radioGroupForm.controls.dateSpan.value === '1w') {
2019-07-21 17:59:47 +03:00
return this.apiService.list1WStatistics$();
}
if (this.radioGroupForm.controls.dateSpan.value === '1m') {
return this.apiService.list1MStatistics$();
2019-07-21 17:59:47 +03:00
}
if (this.radioGroupForm.controls.dateSpan.value === '3m') {
return this.apiService.list3MStatistics$();
2019-07-21 17:59:47 +03:00
}
if (this.radioGroupForm.controls.dateSpan.value === '6m') {
2020-02-17 00:26:57 +07:00
return this.apiService.list6MStatistics$();
}
2021-11-01 22:06:10 -03:00
if (this.radioGroupForm.controls.dateSpan.value === '1y') {
return this.apiService.list1YStatistics$();
}
if (this.radioGroupForm.controls.dateSpan.value === '2y') {
return this.apiService.list2YStatistics$();
}
return this.apiService.list3YStatistics$();
2019-07-21 17:59:47 +03:00
})
)
.subscribe((mempoolStats: any) => {
this.mempoolStats = mempoolStats;
this.handleNewMempoolData(this.mempoolStats.concat([]));
2019-07-21 17:59:47 +03:00
this.loading = false;
this.spinnerLoading = false;
});
this.stateService.live2Chart$
.subscribe((mempoolStats) => {
this.mempoolStats.unshift(mempoolStats);
this.mempoolStats = this.mempoolStats.slice(0, this.mempoolStats.length - 1);
this.handleNewMempoolData(this.mempoolStats.concat([]));
});
2019-07-21 17:59:47 +03:00
}
2020-02-17 00:26:57 +07:00
handleNewMempoolData(mempoolStats: OptimizedMempoolStats[]) {
2019-07-21 17:59:47 +03:00
mempoolStats.reverse();
const labels = mempoolStats.map(stats => stats.added);
this.capExtremeVbytesValues();
2019-07-21 17:59:47 +03:00
this.mempoolTransactionsWeightPerSecondData = {
labels: labels,
series: [mempoolStats.map((stats) => [stats.added * 1000, stats.vbytes_per_second])],
2019-07-21 17:59:47 +03:00
};
}
saveGraphPreference() {
this.storageService.setValue('graphWindowPreference', this.radioGroupForm.controls.dateSpan.value);
}
2021-09-26 11:41:55 -03:00
invertGraph() {
this.storageService.setValue('inverted-graph', !this.inverted);
document.location.reload();
}
setFeeLevelDropdownData() {
let _feeLevels = feeLevels
let _chartColors = chartColors;
if (!this.inverted) {
_feeLevels = [...feeLevels].reverse();
_chartColors = [...chartColors].reverse();
}
_feeLevels.forEach((fee, i) => {
if (this.inverted) {
this.feeLevelDropdownData.push({
fee: fee,
range: this.stateService.isLiquid() ? `${(_feeLevels[i] / 10).toFixed(1)} - ${(_feeLevels[i + 1] / 10).toFixed(1)}` : `${_feeLevels[i]} - ${_feeLevels[i + 1]}`,
color: _chartColors[i],
});
} else {
this.feeLevelDropdownData.push({
fee: fee,
range: this.stateService.isLiquid() ? `${(_feeLevels[i] / 10).toFixed(1)} - ${(_feeLevels[i - 1] / 10).toFixed(1)}` : `${_feeLevels[i]} - ${_feeLevels[i - 1]}`,
color: _chartColors[i - 1],
});
}
})
}
/**
* All value higher that "median * capRatio" are capped
*/
capExtremeVbytesValues() {
if (this.stateService.network.length !== 0) {
return; // Only cap on Bitcoin mainnet
}
let capRatio = 10;
if (['1m', '3m', '6m', '1y', '2y', '3y'].includes(this.graphWindowPreference)) {
capRatio = 4;
}
// Find median value
const vBytes : number[] = [];
for (const stat of this.mempoolStats) {
vBytes.push(stat.vbytes_per_second);
}
const sorted = vBytes.slice().sort((a, b) => a - b);
const middle = Math.floor(sorted.length / 2);
let median = sorted[middle];
if (sorted.length % 2 === 0) {
median = (sorted[middle - 1] + sorted[middle]) / 2;
}
// Cap
for (const stat of this.mempoolStats) {
stat.vbytes_per_second = Math.min(median * capRatio, stat.vbytes_per_second);
}
}
2019-07-21 17:59:47 +03:00
}