import { HttpClient } from '@angular/common/http'; import { Component, OnInit } from '@angular/core'; import { ConfigService } from '@services/config.service'; import { ToastrService } from 'ngx-toastr'; import { MatTabChangeEvent } from '@angular/material/tabs'; @Component({ selector: 'app-global-status', templateUrl: './global-status.component.html', styleUrl: './global-status.component.css' }) export class GlobalStatusComponent implements OnInit { baseUrl: string; loading: boolean = false; installedOgLives: any[] = []; subnets: any[] = []; showLabels: boolean = true; isDoughnut: boolean = true; colorScheme: any = { domain: ['#df200d', '#26a700'] }; view: [number, number] = [400, 220]; ogBootApiUrl: string; ogBootDiskUsage: any = {}; ogBootServicesStatus: any = {}; ogBootDiskUsageChartData: any[] = []; dhcpApiUrl: string; dhcpDiskUsage: any = {}; dhcpServicesStatus: any = {}; dhcpDiskUsageChartData: any[] = []; isDhcp: boolean = false; constructor( private configService: ConfigService, private http: HttpClient, private toastService: ToastrService ) { this.baseUrl = this.configService.apiUrl; this.ogBootApiUrl = `${this.baseUrl}/og-boot/status`; this.dhcpApiUrl = `${this.baseUrl}/og-dhcp/status`; } ngOnInit(): void { this.loadOgBootStatus(); } loadStatus(apiUrl: string, diskUsage: any, servicesStatus: any, diskUsageChartData: any[], installedOgLives: any[], isDhcp: boolean): void { this.loading = true; const timeoutId = setTimeout(() => { this.loading = false; this.toastService.error('Error al sincronizar: Tiempo de espera excedido'); }, 5000); this.http.get(apiUrl).subscribe({ next: data => { diskUsage.used = data.message.disk_usage.used; diskUsage.available = data.message.disk_usage.available; diskUsage.total = data.message.disk_usage.total; diskUsage.percentage = data.message.disk_usage.percentage; Object.assign(servicesStatus, data.message.services_status); if (isDhcp) { this.subnets.length = 0; if (data.message.subnets) { this.subnets.push(...data.message.subnets); } } else { installedOgLives.length = 0; if (data.message.installed_oglives) { installedOgLives.push(...data.message.installed_oglives); } } diskUsageChartData.length = 0; diskUsageChartData.push( { name: 'Usado', value: parseFloat(diskUsage.used) }, { name: 'Disponible', value: parseFloat(diskUsage.available) } ); this.loading = false; clearTimeout(timeoutId); }, error: error => { this.toastService.error('Error al sincronizar'); console.log(error); this.loading = false; clearTimeout(timeoutId); } }); } loadOgBootStatus(): void { this.isDhcp = false; this.loadStatus(this.ogBootApiUrl, this.ogBootDiskUsage, this.ogBootServicesStatus, this.ogBootDiskUsageChartData, this.installedOgLives, this.isDhcp); } loadDhcpStatus(): void { this.isDhcp = true; this.loadStatus(this.dhcpApiUrl, this.dhcpDiskUsage, this.dhcpServicesStatus, this.dhcpDiskUsageChartData, this.installedOgLives, this.isDhcp); } onTabChange(event: MatTabChangeEvent): void { if (event.tab.textLabel === 'OgBoot') { this.loadOgBootStatus(); } else if (event.tab.textLabel === 'Dhcp') { this.loadDhcpStatus(); } } }