import { inject } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { Component, OnInit, Input } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { ConfigService } from '@services/config.service'; import { ToastrService } from "ngx-toastr"; import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'; @Component({ selector: 'app-global-ogboot-status', templateUrl: './ogboot-status.component.html', styleUrl: './ogboot-status.component.css' }) export class GlobalOgbootStatusComponent implements OnInit { public translate = inject(TranslateService); baseUrl: string; loadingBootServers: boolean = false; @Input() bootServers: any[] = []; bootServerStatuses: { [key: string]: any } = {}; selectedBootServerUuid: string = ''; errorBootServers: { [key: string]: boolean } = {}; showLabels: boolean = true; isDoughnut: boolean = true; colorScheme: any = { domain: ['#df200d', '#26a700'] }; view: [number, number] = [400, 220]; constructor( private configService: ConfigService, private toastService: ToastrService, private http: HttpClient, private sanitizer: DomSanitizer ) { this.baseUrl = this.configService.apiUrl; } ngOnInit(): void { } loadBootServerStatus(bootServerUuid: string, callback: (errorOccurred: boolean) => void): void { const timeoutId = setTimeout(() => { callback(true); }, 5000); this.http.get(`${this.baseUrl}/boot-servers/server/${bootServerUuid}/status`).subscribe( data => { if (data.code && data.code !== 200) { clearTimeout(timeoutId); callback(true); return; } const message = data.message || data; const disk = message.disk_usage; const services = message.services_status; const installedOgLives = message.installed_oglives || []; let diskUsageChartData: any[] = []; diskUsageChartData.push( { name: 'Usado', value: parseFloat(disk.used) }, { name: 'Disponible', value: parseFloat(disk.available) } ); this.bootServerStatuses[bootServerUuid] = { disk: { ...disk, used: parseFloat(disk.used.replace('GB', '')), available: parseFloat(disk.available.replace('GB', '')), total: parseFloat(disk.total.replace('GB', '')), percentage: disk.used_percentage || disk.percentage }, services: services, installedOgLives: installedOgLives, diskUsageChartData: diskUsageChartData }; clearTimeout(timeoutId); callback(false); }, error => { this.toastService.error(error.error?.['hydra:description'] || 'Error al cargar el estado del servidor de boot'); clearTimeout(timeoutId); callback(true); } ); } onBootServerChange(bootServerUuid: string): void { this.selectedBootServerUuid = bootServerUuid; if (bootServerUuid && !this.bootServerStatuses[bootServerUuid]) { this.loadBootServerStatus(bootServerUuid, (errorOccurred: boolean) => { if (errorOccurred) { this.errorBootServers[bootServerUuid] = true; } }); } } getSelectedBootServerName(): string { const selectedBootServer = this.bootServers.find(server => server.uuid === this.selectedBootServerUuid); return selectedBootServer ? selectedBootServer.name : ''; } getBootServerIframeUrl(): SafeResourceUrl { const bootServerName = this.getSelectedBootServerName(); if (bootServerName) { const encodedName = encodeURIComponent(bootServerName); const url = `${this.configService.logUrl}/d-solo/ogboot-logs/ogboot-logs?orgId=1&timezone=browser&refresh=5s&var-hostname=${encodedName}&theme=dark&panelId=1&__feature.dashboardSceneSolo`; return this.sanitizer.bypassSecurityTrustResourceUrl(url); } return this.sanitizer.bypassSecurityTrustResourceUrl(''); } retryBootServerStatus(bootServerUuid: string): void { this.loadBootServerStatus(bootServerUuid, (errorOccurred: boolean) => { this.errorBootServers[bootServerUuid] = errorOccurred; if (!errorOccurred) { this.toastService.success(this.translate.instant('bootServerStatus')); } else { this.toastService.error(this.translate.instant('errorLoadingState2')); } }); } scrollToLogs(): void { const checkAndScroll = () => { const logsSection = document.getElementById('ogboot-logs-section'); if (logsSection) { logsSection.scrollIntoView({ behavior: 'smooth', block: 'start' }); return true; } return false; }; const interval = setInterval(() => { if (checkAndScroll()) { clearInterval(interval); } }, 100); setTimeout(() => { clearInterval(interval); }, 3000); } }