mempool/frontend/src/app/components/blocks-list/blocks-list.component.ts

177 lines
6.1 KiB
TypeScript
Raw Normal View History

2022-11-01 14:01:50 -06:00
import { Component, OnInit, OnDestroy, ChangeDetectionStrategy, Input } from '@angular/core';
import { BehaviorSubject, combineLatest, concat, Observable, timer, EMPTY, Subscription, of } from 'rxjs';
import { catchError, delayWhen, map, retryWhen, scan, skip, switchMap, tap } from 'rxjs/operators';
2022-09-21 17:23:45 +02:00
import { BlockExtended } from '../../interfaces/node-api.interface';
import { ApiService } from '../../services/api.service';
import { StateService } from '../../services/state.service';
import { WebsocketService } from '../../services/websocket.service';
@Component({
selector: 'app-blocks-list',
templateUrl: './blocks-list.component.html',
styleUrls: ['./blocks-list.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
2022-11-01 14:01:50 -06:00
export class BlocksList implements OnInit, OnDestroy {
2022-03-11 17:09:13 +01:00
@Input() widget: boolean = false;
2022-06-02 21:55:33 +02:00
blocks$: Observable<BlockExtended[]> = undefined;
2022-11-01 14:01:50 -06:00
auditScores: { [hash: string]: number | void } = {};
auditScoreSubscription: Subscription;
latestScoreSubscription: Subscription;
2022-03-11 14:54:34 +01:00
indexingAvailable = false;
auditAvailable = false;
isLoading = true;
2022-12-19 19:02:50 -06:00
loadingScores = true;
2022-03-11 14:54:34 +01:00
fromBlockHeight = undefined;
paginationMaxSize: number;
page = 1;
2022-03-12 15:55:19 +01:00
lastPage = 1;
2022-03-22 15:16:15 +09:00
maxSize = window.innerWidth <= 767.98 ? 3 : 5;
2022-03-11 14:54:34 +01:00
blocksCount: number;
fromHeightSubject: BehaviorSubject<number> = new BehaviorSubject(this.fromBlockHeight);
2022-03-11 17:09:13 +01:00
skeletonLines: number[] = [];
2022-06-02 21:55:33 +02:00
lastBlockHeight = -1;
constructor(
private apiService: ApiService,
2022-03-12 15:55:19 +01:00
private websocketService: WebsocketService,
2022-03-11 14:54:34 +01:00
public stateService: StateService,
) {
}
ngOnInit(): void {
this.indexingAvailable = (this.stateService.env.BASE_MODULE === 'mempool' &&
this.stateService.env.MINING_DASHBOARD === true);
2023-02-14 12:23:26 -06:00
this.auditAvailable = this.indexingAvailable && this.stateService.env.AUDIT;
if (!this.widget) {
this.websocketService.want(['blocks']);
}
2022-03-12 15:55:19 +01:00
2022-05-12 17:05:31 +02:00
this.skeletonLines = this.widget === true ? [...Array(6).keys()] : [...Array(15).keys()];
2022-03-11 14:54:34 +01:00
this.paginationMaxSize = window.matchMedia('(max-width: 670px)').matches ? 3 : 5;
2022-03-12 15:55:19 +01:00
this.blocks$ = combineLatest([
this.fromHeightSubject.pipe(
switchMap((fromBlockHeight) => {
this.isLoading = true;
return this.apiService.getBlocks$(this.page === 1 ? undefined : fromBlockHeight)
.pipe(
tap(blocks => {
if (this.blocksCount === undefined) {
2022-03-15 23:49:20 +01:00
this.blocksCount = blocks[0].height + 1;
2022-03-12 15:55:19 +01:00
}
this.isLoading = false;
2022-06-02 21:55:33 +02:00
this.lastBlockHeight = Math.max(...blocks.map(o => o.height))
2022-03-12 15:55:19 +01:00
}),
map(blocks => {
if (this.indexingAvailable) {
for (const block of blocks) {
// @ts-ignore: Need to add an extra field for the template
block.extras.pool.logo = `/resources/mining-pools/` +
block.extras.pool.name.toLowerCase().replace(' ', '').replace('.', '') + '.svg';
}
2022-03-12 15:55:19 +01:00
}
if (this.widget) {
2022-05-12 17:05:31 +02:00
return blocks.slice(0, 6);
2022-03-12 15:55:19 +01:00
}
return blocks;
}),
retryWhen(errors => errors.pipe(delayWhen(() => timer(10000))))
2022-03-12 15:55:19 +01:00
)
2022-06-02 21:55:33 +02:00
})
2022-03-12 15:55:19 +01:00
),
this.stateService.blocks$
.pipe(
2022-06-02 21:55:33 +02:00
switchMap((block) => {
if (block[0].height < this.lastBlockHeight) {
2022-06-02 21:55:33 +02:00
return []; // Return an empty stream so the last pipe is not executed
}
this.lastBlockHeight = block[0].height;
return [block];
})
)
2022-03-12 15:55:19 +01:00
])
.pipe(
scan((acc, blocks) => {
if (this.page > 1 || acc.length === 0 || (this.page === 1 && this.lastPage !== 1)) {
this.lastPage = this.page;
return blocks[0];
}
2022-03-15 23:49:20 +01:00
this.blocksCount = Math.max(this.blocksCount, blocks[1][0].height) + 1;
if (this.stateService.env.MINING_DASHBOARD) {
// @ts-ignore: Need to add an extra field for the template
blocks[1][0].extras.pool.logo = `/resources/mining-pools/` +
blocks[1][0].extras.pool.name.toLowerCase().replace(' ', '').replace('.', '') + '.svg';
}
2022-03-12 15:55:19 +01:00
acc.unshift(blocks[1][0]);
2022-05-12 17:05:31 +02:00
acc = acc.slice(0, this.widget ? 6 : 15);
2022-03-12 15:55:19 +01:00
return acc;
}, [])
);
2022-11-01 14:01:50 -06:00
if (this.indexingAvailable && this.auditAvailable) {
2022-11-01 14:01:50 -06:00
this.auditScoreSubscription = this.fromHeightSubject.pipe(
switchMap((fromBlockHeight) => {
2022-12-19 19:02:50 -06:00
this.loadingScores = true;
2022-11-01 14:01:50 -06:00
return this.apiService.getBlockAuditScores$(this.page === 1 ? undefined : fromBlockHeight)
.pipe(
catchError(() => {
return EMPTY;
})
);
})
).subscribe((scores) => {
Object.values(scores).forEach(score => {
this.auditScores[score.hash] = score?.matchRate != null ? score.matchRate : null;
});
2022-12-19 19:02:50 -06:00
this.loadingScores = false;
2022-11-01 14:01:50 -06:00
});
this.latestScoreSubscription = this.stateService.blocks$.pipe(
switchMap((block) => {
if (block[0]?.extras?.matchRate != null) {
return of({
hash: block[0].id,
matchRate: block[0]?.extras?.matchRate,
});
}
else if (block[0]?.id && this.auditScores[block[0].id] === undefined) {
return this.apiService.getBlockAuditScore$(block[0].id)
.pipe(
catchError(() => {
return EMPTY;
})
);
} else {
return EMPTY;
}
}),
).subscribe((score) => {
if (score && score.hash) {
this.auditScores[score.hash] = score?.matchRate != null ? score.matchRate : null;
}
});
}
}
ngOnDestroy(): void {
this.auditScoreSubscription?.unsubscribe();
this.latestScoreSubscription?.unsubscribe();
2022-03-11 14:54:34 +01:00
}
pageChange(page: number) {
2022-03-15 23:49:20 +01:00
this.fromHeightSubject.next((this.blocksCount - 1) - (page - 1) * 15);
}
trackByBlock(index: number, block: BlockExtended) {
return block.height;
}
isEllipsisActive(e) {
return (e.offsetWidth < e.scrollWidth);
}
}