mempool/frontend/src/app/components/server-health/server-health.component.ts

90 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-03-06 19:34:12 +00:00
import { Component, OnInit, ChangeDetectionStrategy, SecurityContext, ChangeDetectorRef } from '@angular/core';
import { WebsocketService } from '@app/services/websocket.service';
import { Observable, Subject, map, tap } from 'rxjs';
import { StateService } from '@app/services/state.service';
import { HealthCheckHost } from '@interfaces/websocket.interface';
2024-03-03 20:31:02 +00:00
import { DomSanitizer } from '@angular/platform-browser';
@Component({
selector: 'app-server-health',
templateUrl: './server-health.component.html',
styleUrls: ['./server-health.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ServerHealthComponent implements OnInit {
2024-03-03 20:31:02 +00:00
hosts$: Observable<HealthCheckHost[]>;
maxHeight: number;
2024-03-06 19:34:12 +00:00
interval: number;
now: number = Date.now();
2024-03-03 20:31:02 +00:00
constructor(
private websocketService: WebsocketService,
private stateService: StateService,
2024-03-06 19:34:12 +00:00
private cd: ChangeDetectorRef,
2024-03-03 20:31:02 +00:00
public sanitizer: DomSanitizer,
) {}
ngOnInit(): void {
this.hosts$ = this.stateService.serverHealth$.pipe(
map((hosts) => {
2024-03-06 19:34:12 +00:00
const subpath = window.location.pathname.slice(0, -11);
2024-03-03 20:31:02 +00:00
for (const host of hosts) {
let statusUrl = '';
let linkHost = '';
if (host.socket) {
2024-03-05 23:16:20 +00:00
statusUrl = 'https://' + window.location.hostname + subpath + '/status';
linkHost = window.location.hostname + subpath;
2024-03-03 20:31:02 +00:00
} else {
const hostUrl = new URL(host.host);
statusUrl = 'https://' + hostUrl.hostname + subpath + '/status';
linkHost = hostUrl.hostname + subpath;
2024-03-03 20:31:02 +00:00
}
host.statusPage = this.sanitizer.bypassSecurityTrustResourceUrl(this.sanitizer.sanitize(SecurityContext.URL, statusUrl));
host.link = linkHost;
2024-03-05 23:16:20 +00:00
host.flag = this.parseFlag(host.host);
2024-03-03 20:31:02 +00:00
}
return hosts;
}),
tap(hosts => {
let newMaxHeight = 0;
for (const host of hosts) {
newMaxHeight = Math.max(newMaxHeight, host.latestHeight);
}
2024-03-03 20:31:02 +00:00
})
);
2024-03-06 19:34:12 +00:00
this.websocketService.want(['mempool-blocks', 'stats', 'blocks', 'tomahawk']);
this.interval = window.setInterval(() => {
this.now = Date.now();
this.cd.markForCheck();
}, 1000);
2024-03-03 20:31:02 +00:00
}
2024-03-05 23:16:20 +00:00
trackByFn(index: number, host: HealthCheckHost): string {
return host.host;
}
2024-03-06 19:34:12 +00:00
getLastUpdateSeconds(host: HealthCheckHost): string {
if (host.lastChecked) {
const seconds = Math.ceil((this.now - host.lastChecked) / 1000);
return `${seconds} s`;
2024-03-06 19:34:12 +00:00
} else {
return '~';
}
}
2024-03-05 23:16:20 +00:00
private parseFlag(host: string): string {
if (host.includes('.fra.')) {
return '🇩🇪';
} else if (host.includes('.tk7.')) {
return '🇯🇵';
} else if (host.includes('.fmt.')) {
return '🇺🇸';
} else if (host.includes('.va1.')) {
return '🇺🇸';
} else {
return '';
}
}
2024-03-03 20:31:02 +00:00
}