mempool/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.ts

320 lines
9.5 KiB
TypeScript
Raw Normal View History

2022-04-05 20:37:18 +02:00
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, Input, LOCALE_ID, OnInit, HostBinding } from '@angular/core';
2023-11-06 18:19:54 +00:00
import { EChartsOption } from '../../graphs/echarts';
import { Observable } from 'rxjs';
import { delay, map, retryWhen, share, startWith, switchMap, tap } from 'rxjs/operators';
2022-09-21 17:23:45 +02:00
import { ApiService } from '../../services/api.service';
import { SeoService } from '../../services/seo.service';
2022-11-28 11:55:23 +09:00
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
import { chartColors, poolsColor } from '../../app.constants';
2022-09-21 17:23:45 +02:00
import { StorageService } from '../../services/storage.service';
import { MiningService } from '../../services/mining.service';
import { download } from '../../shared/graphs.utils';
import { ActivatedRoute } from '@angular/router';
2024-01-27 22:10:18 +00:00
interface Hashrate {
timestamp: number;
avgHashRate: number;
share: number;
poolName: string;
}
@Component({
selector: 'app-hashrate-chart-pools',
templateUrl: './hashrate-chart-pools.component.html',
styleUrls: ['./hashrate-chart-pools.component.scss'],
styles: [`
.loadingGraphs {
position: absolute;
top: 50%;
left: calc(50% - 15px);
z-index: 100;
}
`],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class HashrateChartPoolsComponent implements OnInit {
@Input() right: number | string = 45;
@Input() left: number | string = 25;
miningWindowPreference: string;
2022-11-28 11:55:23 +09:00
radioGroupForm: UntypedFormGroup;
2024-01-27 22:10:18 +00:00
hashrates: Hashrate[];
chartOptions: EChartsOption = {};
chartInitOptions = {
renderer: 'svg',
};
2022-04-05 20:37:18 +02:00
@HostBinding('attr.dir') dir = 'ltr';
hashrateObservable$: Observable<any>;
isLoading = true;
2022-05-05 16:18:28 +09:00
timespan = '';
chartInstance: any = undefined;
constructor(
@Inject(LOCALE_ID) public locale: string,
private seoService: SeoService,
private apiService: ApiService,
2022-11-28 11:55:23 +09:00
private formBuilder: UntypedFormBuilder,
private cd: ChangeDetectorRef,
private storageService: StorageService,
private miningService: MiningService,
private route: ActivatedRoute,
) {
this.radioGroupForm = this.formBuilder.group({ dateSpan: '1y' });
this.radioGroupForm.controls.dateSpan.setValue('1y');
}
ngOnInit(): void {
let firstRun = true;
this.seoService.setTitle($localize`:@@mining.pools-historical-dominance:Pools Historical Dominance`);
this.miningWindowPreference = this.miningService.getDefaultTimespan('6m');
this.radioGroupForm = this.formBuilder.group({ dateSpan: this.miningWindowPreference });
this.radioGroupForm.controls.dateSpan.setValue(this.miningWindowPreference);
this.route
.fragment
.subscribe((fragment) => {
if (['6m', '1y', '2y', '3y', 'all'].indexOf(fragment) > -1) {
this.radioGroupForm.controls.dateSpan.setValue(fragment, { emitEvent: false });
}
});
this.hashrateObservable$ = this.radioGroupForm.get('dateSpan').valueChanges
.pipe(
startWith(this.radioGroupForm.controls.dateSpan.value),
switchMap((timespan) => {
if (!firstRun) {
this.storageService.setValue('miningWindowPreference', timespan);
}
2022-05-05 16:18:28 +09:00
this.timespan = timespan;
firstRun = false;
this.isLoading = true;
return this.apiService.getHistoricalPoolsHashrate$(timespan)
.pipe(
tap((response) => {
2024-01-27 22:10:18 +00:00
this.hashrates = response.body;
// Prepare series (group all hashrates data point by pool)
2024-01-27 22:10:18 +00:00
const series = this.applyHashrates();
if (series.length === 0) {
this.cd.markForCheck();
throw new Error();
}
}),
map((response) => {
return {
blockCount: parseInt(response.headers.get('x-total-count'), 10),
}
}),
retryWhen((errors) => errors.pipe(
delay(60000)
))
);
}),
share()
);
}
2024-01-27 22:10:18 +00:00
applyHashrates(): any[] {
const times: { [time: number]: { hashrates: { [pool: string]: Hashrate } } } = {};
const pools = {};
for (const hashrate of this.hashrates) {
if (!times[hashrate.timestamp]) {
times[hashrate.timestamp] = { hashrates: {} };
}
times[hashrate.timestamp].hashrates[hashrate.poolName] = hashrate;
if (!pools[hashrate.poolName]) {
2024-01-29 16:59:13 +00:00
pools[hashrate.poolName] = true;
2024-01-27 22:10:18 +00:00
}
}
const sortedTimes = Object.keys(times).sort((a,b) => parseInt(a) - parseInt(b)).map(time => ({ time: parseInt(time), hashrates: times[time].hashrates }));
2024-01-29 16:59:13 +00:00
const lastHashrates = sortedTimes[sortedTimes.length - 1].hashrates;
const sortedPools = Object.keys(pools).sort((a,b) => {
if (lastHashrates[b]?.share ?? lastHashrates[a]?.share ?? false) {
// sort by descending share of hashrate in latest period
return (lastHashrates[b]?.share || 0) - (lastHashrates[a]?.share || 0);
} else {
// tiebreak by pool name
b < a;
}
});
2024-01-27 22:10:18 +00:00
const series = [];
const legends = [];
for (const name of sortedPools) {
const data = sortedTimes.map(({ time, hashrates }) => {
return [time * 1000, (hashrates[name]?.share || 0) * 100];
});
series.push({
zlevel: 0,
stack: 'Total',
name: name,
showSymbol: false,
symbol: 'none',
data,
type: 'line',
lineStyle: { width: 0 },
areaStyle: { opacity: 1 },
smooth: true,
color: poolsColor[name.replace(/[^a-zA-Z0-9]/g, '').toLowerCase()],
emphasis: {
disabled: true,
scale: false,
},
});
legends.push({
name: name,
inactiveColor: 'rgb(110, 112, 121)',
textStyle: {
color: 'white',
},
icon: 'roundRect',
itemStyle: {
color: poolsColor[name.replace(/[^a-zA-Z0-9]/g, "").toLowerCase()],
},
});
}
this.prepareChartOptions({
legends: legends,
series: series,
});
this.isLoading = false;
return series;
}
prepareChartOptions(data) {
let title: object;
if (data.series.length === 0) {
title = {
textStyle: {
color: 'grey',
fontSize: 15
},
text: $localize`:@@23555386d8af1ff73f297e89dd4af3f4689fb9dd:Indexing blocks`,
left: 'center',
top: 'center',
};
}
this.chartOptions = {
title: title,
animation: false,
2023-02-22 14:06:08 +09:00
color: chartColors.filter(color => color !== '#FDD835'),
grid: {
right: this.right,
left: this.left,
bottom: 70,
top: this.isMobile() ? 10 : 50,
},
tooltip: {
show: !this.isMobile(),
trigger: 'axis',
axisPointer: {
type: 'line'
},
backgroundColor: 'rgba(17, 19, 31, 1)',
borderRadius: 4,
shadowColor: 'rgba(0, 0, 0, 0.5)',
textStyle: {
color: '#b1b1b1',
align: 'left',
},
borderColor: '#000',
formatter: function (data) {
const date = new Date(data[0].data[0]).toLocaleDateString(this.locale, { year: 'numeric', month: 'short', day: 'numeric' });
let tooltip = `<b style="color: white; margin-left: 2px">${date}</b><br>`;
data.sort((a, b) => b.data[1] - a.data[1]);
for (const pool of data) {
if (pool.data[1] > 0) {
tooltip += `${pool.marker} ${pool.seriesName}: ${pool.data[1].toFixed(2)}%<br>`;
}
}
return tooltip;
}.bind(this)
},
xAxis: data.series.length === 0 ? undefined : {
type: 'time',
2022-04-11 21:17:15 +09:00
splitNumber: this.isMobile() ? 5 : 10,
axisLabel: {
hideOverlap: true,
}
},
legend: (this.isMobile() || data.series.length === 0) ? undefined : {
data: data.legends
},
yAxis: data.series.length === 0 ? undefined : {
position: 'right',
axisLabel: {
color: 'rgb(110, 112, 121)',
formatter: (val) => `${val}%`,
},
splitLine: {
show: false,
},
type: 'value',
max: 100,
min: 0,
},
series: data.series,
dataZoom: [{
type: 'inside',
realtime: true,
zoomLock: true,
maxSpan: 100,
minSpan: 10,
moveOnMouseMove: false,
}, {
showDetail: false,
show: true,
type: 'slider',
brushSelect: false,
realtime: true,
left: 20,
right: 15,
selectedDataBackground: {
lineStyle: {
color: '#fff',
opacity: 0.45,
},
areaStyle: {
opacity: 0,
}
},
}],
};
2024-01-27 22:10:18 +00:00
this.cd.markForCheck();
}
2022-05-05 16:18:28 +09:00
onChartInit(ec) {
this.chartInstance = ec;
}
isMobile() {
return (window.innerWidth <= 767.98);
}
2022-05-05 16:18:28 +09:00
onSaveChart() {
// @ts-ignore
const prevBottom = this.chartOptions.grid.bottom;
const now = new Date();
// @ts-ignore
this.chartOptions.grid.bottom = 30;
this.chartOptions.backgroundColor = '#11131f';
2022-05-05 16:18:28 +09:00
this.chartInstance.setOption(this.chartOptions);
download(this.chartInstance.getDataURL({
pixelRatio: 2,
excludeComponents: ['dataZoom'],
}), `pools-dominance-${this.timespan}-${Math.round(now.getTime() / 1000)}.svg`);
2022-05-05 16:18:28 +09:00
// @ts-ignore
this.chartOptions.grid.bottom = prevBottom;
this.chartOptions.backgroundColor = 'none';
2022-05-05 16:18:28 +09:00
this.chartInstance.setOption(this.chartOptions);
}
}