From ed073deb5d1e25cb9135b05ce2c8ca045b63a80c Mon Sep 17 00:00:00 2001 From: Lucas Lara Date: Tue, 29 Apr 2025 14:05:43 +0200 Subject: [PATCH] Refactor runScriptContext handling in ExecuteCommandComponent and RunScriptAssistantComponent for improved data management; update related HTML to reflect changes. --- .../execute-command.component.ts | 8 ++- .../run-script-assistant.component.html | 2 +- .../run-script-assistant.component.ts | 49 ++++++++++++++----- .../app/components/groups/groups.component.ts | 18 +++---- .../partition-type-organizator.component.ts | 4 -- 5 files changed, 50 insertions(+), 31 deletions(-) diff --git a/ogWebconsole/src/app/components/commands/main-commands/execute-command/execute-command.component.ts b/ogWebconsole/src/app/components/commands/main-commands/execute-command/execute-command.component.ts index e459e66..2e64e69 100644 --- a/ogWebconsole/src/app/components/commands/main-commands/execute-command/execute-command.component.ts +++ b/ogWebconsole/src/app/components/commands/main-commands/execute-command/execute-command.component.ts @@ -10,13 +10,13 @@ import { ConfigService } from '@services/config.service'; styleUrls: ['./execute-command.component.css'] }) export class ExecuteCommandComponent implements OnInit { + @Input() runScriptContext: any = null; @Input() clientState: string = 'off'; @Input() clientData: any[] = []; @Input() buttonType: 'icon' | 'text' | 'menu-item' = 'icon'; @Input() buttonText: string = 'Ejecutar Comandos'; @Input() icon: string = 'terminal'; @Input() disabled: boolean = false; - @Input() runScriptContext: string = ''; baseUrl: string; loading: boolean = true; @@ -231,10 +231,8 @@ export class ExecuteCommandComponent implements OnInit { this.router.navigate(['/clients/run-script'], { queryParams: { clientData: JSON.stringify(clientDataToSend) , - runScriptContext: this.runScriptContext + runScriptContext: JSON.stringify(this.runScriptContext) } - }).then(() => { - console.log('Navigated to run script with data:', clientDataToSend, 'runScriptContext:', this.runScriptContext); - }); + }) } } diff --git a/ogWebconsole/src/app/components/groups/components/client-main-view/run-script-assistant/run-script-assistant.component.html b/ogWebconsole/src/app/components/groups/components/client-main-view/run-script-assistant/run-script-assistant.component.html index 0ad4fbf..a581a6a 100644 --- a/ogWebconsole/src/app/components/groups/components/client-main-view/run-script-assistant/run-script-assistant.component.html +++ b/ogWebconsole/src/app/components/groups/components/client-main-view/run-script-assistant/run-script-assistant.component.html @@ -3,7 +3,7 @@

- {{ 'runScript' | translate }} {{this.runScriptContext}} + {{ 'runScript' | translate }} {{runScriptTitle}}

diff --git a/ogWebconsole/src/app/components/groups/components/client-main-view/run-script-assistant/run-script-assistant.component.ts b/ogWebconsole/src/app/components/groups/components/client-main-view/run-script-assistant/run-script-assistant.component.ts index 23e599a..bd93e49 100644 --- a/ogWebconsole/src/app/components/groups/components/client-main-view/run-script-assistant/run-script-assistant.component.ts +++ b/ogWebconsole/src/app/components/groups/components/client-main-view/run-script-assistant/run-script-assistant.component.ts @@ -1,11 +1,11 @@ -import {Component, EventEmitter, Output} from '@angular/core'; -import {SelectionModel} from "@angular/cdk/collections"; -import {HttpClient} from "@angular/common/http"; -import {ToastrService} from "ngx-toastr"; -import {ConfigService} from "@services/config.service"; -import {ActivatedRoute, Router} from "@angular/router"; -import {SaveScriptComponent} from "./save-script/save-script.component"; -import {MatDialog} from "@angular/material/dialog"; +import { Component, EventEmitter, Output } from '@angular/core'; +import { SelectionModel } from "@angular/cdk/collections"; +import { HttpClient } from "@angular/common/http"; +import { ToastrService } from "ngx-toastr"; +import { ConfigService } from "@services/config.service"; +import { ActivatedRoute, Router } from "@angular/router"; +import { SaveScriptComponent } from "./save-script/save-script.component"; +import { MatDialog } from "@angular/material/dialog"; @Component({ selector: 'app-run-script-assistant', @@ -32,7 +32,7 @@ export class RunScriptAssistantComponent { newScript: string = ''; selection = new SelectionModel(true, []); parameterNames: string[] = Object.keys(this.parameters); - runScriptContext: string = ''; + runScriptContext: any = null; constructor( private http: HttpClient, @@ -52,7 +52,7 @@ export class RunScriptAssistantComponent { } }); this.clientId = this.clientData?.length ? this.clientData[0]['@id'] : null; - this.clientData.forEach((client: { selected: boolean; status: string}) => { + this.clientData.forEach((client: { selected: boolean; status: string }) => { if (client.status === 'og-live') { client.selected = true; } @@ -63,6 +63,31 @@ export class RunScriptAssistantComponent { this.loadScripts() } + ngOnInit(): void { + this.route.queryParams.subscribe(params => { + this.runScriptContext = params['runScriptContext'] ? JSON.parse(params['runScriptContext']) : null; + this.clientData = params['clientData'] ? JSON.parse(params['clientData']) : []; + }); + } + + get runScriptTitle(): string { + const ctx = this.runScriptContext; + if (!ctx) { + return ''; + } + // Si es un array de clientes + if (Array.isArray(ctx)) { + return ctx.map(c => c.name).join(', '); + } + // Si es un objeto con propiedad name + if (typeof ctx === 'object' && 'name' in ctx) { + return ctx.name; + } + // Si es un string plano + return String(ctx); + } + + loadScripts(): void { this.loading = true; @@ -118,7 +143,7 @@ export class RunScriptAssistantComponent { } return client.partitions - .map((p: { partitionNumber: any; size: any; filesystem: any }) => `#${p.partitionNumber} ${p.filesystem} - ${p.size / 1024 }GB`) + .map((p: { partitionNumber: any; size: any; filesystem: any }) => `#${p.partitionNumber} ${p.filesystem} - ${p.size / 1024}GB`) .join('\n'); } @@ -164,7 +189,7 @@ export class RunScriptAssistantComponent { this.http.post(`${this.baseUrl}/commands/run-script`, { clients: this.selectedClients.map((client: any) => client.uuid), - script: this.commandType === 'existing' ? this.scriptContent : this.newScript, + script: this.commandType === 'existing' ? this.scriptContent : this.newScript, }).subscribe( response => { this.toastService.success('Script ejecutado correctamente'); diff --git a/ogWebconsole/src/app/components/groups/groups.component.ts b/ogWebconsole/src/app/components/groups/groups.component.ts index 677b65e..ba768cc 100644 --- a/ogWebconsole/src/app/components/groups/groups.component.ts +++ b/ogWebconsole/src/app/components/groups/groups.component.ts @@ -830,24 +830,24 @@ export class GroupsComponent implements OnInit, OnDestroy { this.fetchClientsForNode(this.selectedNode); } - getRunScriptContext(clientData: any[]): string { + getRunScriptContext(clientData: any[]): any { const selectedClientNames = clientData.map(client => client.name); - + if (clientData.length === 1) { - return selectedClientNames[0]; + return clientData[0]; // devuelve el objeto cliente completo } else if ( clientData.length === this.selectedClients.data.length && selectedClientNames.every(name => this.selectedClients.data.some(c => c.name === name)) ) { - return this.selectedNode?.name || ''; + return this.selectedNode || null; // devuelve el nodo completo } else if (clientData.length > 1) { - return selectedClientNames.join(', '); - } else if (this.selectedNode?.name && clientData.length === 0) { - return this.selectedNode.name; + return clientData; // devuelve array de objetos cliente + } else if (this.selectedNode && clientData.length === 0) { + return this.selectedNode; } - return ''; + return null; } - + openPartitionTypeModal(event: MouseEvent, node: TreeNode | null = null): void { event.stopPropagation(); diff --git a/ogWebconsole/src/app/components/groups/shared/partition-type-organizator/partition-type-organizator.component.ts b/ogWebconsole/src/app/components/groups/shared/partition-type-organizator/partition-type-organizator.component.ts index 2f47bd9..b9eb8f5 100644 --- a/ogWebconsole/src/app/components/groups/shared/partition-type-organizator/partition-type-organizator.component.ts +++ b/ogWebconsole/src/app/components/groups/shared/partition-type-organizator/partition-type-organizator.component.ts @@ -13,8 +13,6 @@ export class PartitionTypeOrganizatorComponent implements OnInit { displayedColumns: string[] = ['diskNumber', 'partitionNumber', 'partitionCode', 'size', 'filesystem', 'memoryUsage']; ngOnInit(): void { - console.log('Data recibida en modal:', this.data); - this.simplifiedData = this.data.map((client: any) => ({ name: client.name, partitions: client.partitions.map((p: any) => ({ @@ -27,6 +25,4 @@ export class PartitionTypeOrganizatorComponent implements OnInit { })) })); } - - }