2024-03-06 19:34:12 +00:00
|
|
|
import { Component, OnInit, ChangeDetectionStrategy, SecurityContext, ChangeDetectorRef } from '@angular/core';
|
2024-10-22 21:05:01 +09:00
|
|
|
import { WebsocketService } from '@app/services/websocket.service';
|
2024-09-05 20:55:48 +00:00
|
|
|
import { Observable, Subject, map, tap } from 'rxjs';
|
2024-10-22 21:05:01 +09:00
|
|
|
import { StateService } from '@app/services/state.service';
|
2024-10-23 11:09:38 +09:00
|
|
|
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,
|
|
|
|
})
|
2024-03-04 18:35:34 +00:00
|
|
|
export class ServerHealthComponent implements OnInit {
|
2024-03-03 20:31:02 +00:00
|
|
|
hosts$: Observable<HealthCheckHost[]>;
|
2024-09-05 20:55:48 +00:00
|
|
|
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 {
|
2024-03-04 18:35:34 +00:00
|
|
|
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;
|
2024-09-05 20:55:48 +00:00
|
|
|
}),
|
|
|
|
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);
|
2024-06-20 03:09:54 +00:00
|
|
|
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
|
|
|
}
|