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

74 lines
3 KiB
TypeScript
Raw Normal View History

2020-03-29 23:59:04 +07:00
import { Component, OnInit } from '@angular/core';
2022-09-21 17:23:45 +02:00
import { WebsocketService } from '../../services/websocket.service';
2020-02-17 00:26:57 +07:00
import { OptimizedMempoolStats } from '../../interfaces/node-api.interface';
2022-09-21 17:23:45 +02:00
import { StateService } from '../../services/state.service';
import { ApiService } from '../../services/api.service';
import { SeoService } from '../../services/seo.service';
import { ActivatedRoute } from '@angular/router';
import { map, scan, startWith, switchMap, tap } from 'rxjs/operators';
2022-02-09 10:18:51 +09:00
import { interval, merge, Observable } from 'rxjs';
import { ChangeDetectionStrategy } from '@angular/core';
2019-07-27 18:43:17 +03:00
@Component({
selector: 'app-television',
templateUrl: './television.component.html',
styleUrls: ['./television.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
2019-07-27 18:43:17 +03:00
})
export class TelevisionComponent implements OnInit {
2020-02-17 00:26:57 +07:00
mempoolStats: OptimizedMempoolStats[] = [];
statsSubscription$: Observable<OptimizedMempoolStats[]>;
2022-02-09 10:18:51 +09:00
fragment: string;
2019-07-27 18:43:17 +03:00
constructor(
private websocketService: WebsocketService,
private apiService: ApiService,
private stateService: StateService,
2020-03-24 00:52:08 +07:00
private seoService: SeoService,
private route: ActivatedRoute
2019-07-27 18:43:17 +03:00
) { }
2022-02-09 10:18:51 +09:00
refreshStats(time: number, fn: Observable<OptimizedMempoolStats[]>) {
return interval(time).pipe(startWith(0), switchMap(() => fn));
}
2019-07-27 18:43:17 +03:00
ngOnInit() {
2020-12-03 18:34:19 +07:00
this.seoService.setTitle($localize`:@@46ce8155c9ab953edeec97e8950b5a21e67d7c4e:TV view`);
2020-03-14 13:48:01 +07:00
this.websocketService.want(['blocks', 'live-2h-chart', 'mempool-blocks']);
2022-02-09 10:18:51 +09:00
this.statsSubscription$ = merge(
this.stateService.live2Chart$.pipe(map(stats => [stats])),
2022-02-09 10:18:51 +09:00
this.route.fragment
.pipe(
tap(fragment => { this.fragment = fragment ?? '2h'; }),
2022-02-09 10:18:51 +09:00
switchMap((fragment) => {
const minute = 60000; const hour = 3600000;
switch (fragment) {
case '24h': return this.apiService.list24HStatistics$();
case '1w': return this.refreshStats(5 * minute, this.apiService.list1WStatistics$());
case '1m': return this.refreshStats(30 * minute, this.apiService.list1MStatistics$());
case '3m': return this.refreshStats(2 * hour, this.apiService.list3MStatistics$());
case '6m': return this.refreshStats(3 * hour, this.apiService.list6MStatistics$());
case '1y': return this.refreshStats(8 * hour, this.apiService.list1YStatistics$());
case '2y': return this.refreshStats(8 * hour, this.apiService.list2YStatistics$());
case '3y': return this.refreshStats(12 * hour, this.apiService.list3YStatistics$());
default /* 2h */: return this.apiService.list2HStatistics$();
2022-02-09 10:18:51 +09:00
}
})
)
)
.pipe(
scan((mempoolStats, newStats) => {
if (newStats.length > 1) {
mempoolStats = newStats;
2022-02-09 10:18:51 +09:00
} else if (['2h', '24h'].includes(this.fragment)) {
mempoolStats.unshift(newStats[0]);
mempoolStats = mempoolStats.slice(0, mempoolStats.length - 1);
2022-02-09 10:18:51 +09:00
}
return mempoolStats;
2022-02-09 10:18:51 +09:00
})
);
2019-07-27 18:43:17 +03:00
}
}