| /* |
| Copyright 2020 TATA ELXSI |
| |
| Licensed under the Apache License, Version 2.0 (the 'License'); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| |
| Author: SANDHYA JS (sandhya.j@tataelxsi.co.in) |
| */ |
| /** |
| * @file Info K8s Page |
| */ |
| import { Component, HostListener, Injector, OnInit } from '@angular/core'; |
| import { ActivatedRoute } from '@angular/router'; |
| import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; |
| import { ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel'; |
| import { DeleteComponent } from 'DeleteComponent'; |
| import { environment } from 'environment'; |
| import { K8SCLUSTERDATA, K8SCLUSTERDATADISPLAY, K8SNODEDATA } from 'K8sModel'; |
| import { KSUAddComponent } from 'KSUAddComponent'; |
| import { NodeAddComponent } from 'NodeAddComponent'; |
| import { RestService } from 'RestService'; |
| import { isNullOrUndefined, SharedService } from 'SharedService'; |
| import { ShowInfoComponent } from 'ShowInfoComponent'; |
| |
| /** |
| * Creating component |
| * @Component K8sInfoComponent.html as template url |
| */ |
| @Component({ |
| selector: 'app-info-k8s', |
| templateUrl: './K8sInfoComponent.html', |
| styleUrls: ['./K8sInfoComponent.scss'] |
| }) |
| /** Exporting a class @exports K8sInfoComponent */ |
| export class K8sInfoComponent implements OnInit { |
| /** To inject services @public */ |
| public injector: Injector; |
| |
| /** Contains active button @public */ |
| public activeButton = 1; |
| |
| /** condition to show tooltip @public */ |
| public showTooltip = false; |
| |
| /** Showing more details of collapase */ |
| public isCollapsed1: boolean = true; |
| |
| /** Showing more details of collapase */ |
| public isCollapsed2: boolean = false; |
| |
| /** Check the Projects loading results @public */ |
| public isLoadingResults: boolean = false; |
| |
| /** Give the message for the loading @public */ |
| public message: string = 'PLEASEWAIT'; |
| |
| /** variables contains paramsID @public */ |
| public paramsID: string; |
| |
| /** Contains cluster name @public */ |
| public clusterName: string; |
| |
| /** Contains cluster data with count @public */ |
| public countData: K8SCLUSTERDATA = {}; |
| |
| /** Contains node count @public */ |
| public node_count: number; |
| |
| /** Contains ksu count @public */ |
| public ksu_count: number; |
| |
| /** Contains node details @public */ |
| public nodeDetail: {}[] = []; |
| |
| /** Contains ksu details @public */ |
| public ksuDetail: {}[] = []; |
| |
| /** Condition for popover visibility @public */ |
| public isPopoverVisible = false; |
| |
| /** Contains popover text to show @public */ |
| public popoverText = ''; |
| |
| /** Instance of the rest service @private */ |
| private restService: RestService; |
| |
| /** Holds teh instance of AuthService class of type AuthService @private */ |
| private activatedRoute: ActivatedRoute; |
| |
| /** Utilizes modal service for any modal operations @private */ |
| private modalService: NgbModal; |
| |
| /** Contains all methods related to shared @private */ |
| private sharedService: SharedService; |
| |
| constructor(injector: Injector) { |
| this.injector = injector; |
| this.restService = this.injector.get(RestService); |
| this.activatedRoute = this.injector.get(ActivatedRoute); |
| this.modalService = this.injector.get(NgbModal); |
| this.sharedService = this.injector.get(SharedService); |
| } |
| |
| /** |
| * Lifecyle Hooks the trigger before component is instantiate |
| */ |
| public ngOnInit(): void { |
| this.paramsID = this.activatedRoute.snapshot.paramMap.get('id'); |
| this.generateData(); |
| } |
| |
| /** To view Details @public */ |
| public onView(identifier: string, type: number): void { |
| let pageName: string = ''; |
| let title: string = ''; |
| if (type === 1) { |
| pageName = 'k8s-node'; |
| title = 'Node Details'; |
| } else { |
| pageName = 'k8s-ksu'; |
| title = 'KSU Details'; |
| } |
| // eslint-disable-next-line security/detect-non-literal-fs-filename |
| this.modalService.open(ShowInfoComponent, { backdrop: 'static' }).componentInstance.params = { |
| id: identifier, |
| page: pageName, |
| titleName: title, |
| cluster_id: this.paramsID |
| }; |
| } |
| |
| /** Edit Node @public */ |
| public onEdit(type: number): void { |
| // eslint-disable-next-line security/detect-non-literal-fs-filename |
| const modalRef: NgbModalRef = this.modalService.open(NodeAddComponent, { backdrop: 'static' }); |
| if (type === 1) { |
| modalRef.componentInstance.profileType = 'card-node'; |
| } |
| modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| if (result) { |
| this.sharedService.callData(); |
| } |
| }).catch((): void => { |
| // Catch Navigation Error |
| }); |
| } |
| |
| /** Delete Operation @public */ |
| public onDelete(id_value: string, name_value: string, type: number): void { |
| let pageName: string = ''; |
| let title: string = ''; |
| if (type === 1) { |
| pageName = 'card-node'; |
| title = 'Node Details'; |
| } else { |
| pageName = 'card-ksu'; |
| title = 'KSU Details'; |
| } |
| // eslint-disable-next-line security/detect-non-literal-fs-filename |
| const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' }); |
| modalRef.componentInstance.params = { |
| id: id_value, |
| page: pageName, |
| titleName: 'Node Details', |
| cluster_id: this.paramsID, |
| name: name_value |
| }; |
| modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| if (result) { |
| this.sharedService.callData(); |
| this.nodeData(); |
| } |
| }).catch((): void => { |
| // Catch Navigation Error |
| }); |
| } |
| |
| /** popover functionality to show full description @public */ |
| public showPopover(event: MouseEvent, description: string) { |
| this.popoverText = description; |
| this.isPopoverVisible = true; |
| const popover = (event.target as HTMLElement).nextElementSibling as HTMLElement; |
| if (popover) { |
| // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| popover.style.top = `${event.clientY + 10}px`; |
| popover.style.left = `${event.clientX}px`; |
| } |
| } |
| |
| /** To hide popover @public */ |
| public hidePopover() { |
| this.isPopoverVisible = false; |
| } |
| |
| /** Add Node/KSU @public */ |
| public addK8sCluster(type?: string): void { |
| if (type === 'card_node') { |
| // eslint-disable-next-line security/detect-non-literal-fs-filename |
| const modalRef: NgbModalRef = this.modalService.open(NodeAddComponent, { backdrop: 'static' }); |
| modalRef.componentInstance.profileType = type; |
| modalRef.componentInstance.clusterId = this.paramsID; |
| modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| if (result) { |
| this.sharedService.callData(); |
| this.nodeData(); |
| } |
| }).catch((): void => { |
| // Catch Navigation Error |
| }); |
| } else if (type === 'card_ksu') { |
| // eslint-disable-next-line security/detect-non-literal-fs-filename |
| const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' }); |
| modalRef.componentInstance.profileType = 'card-add'; |
| modalRef.componentInstance.profileID = this.paramsID; |
| modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| if (result) { |
| this.sharedService.callData(); |
| this.ksuData(); |
| } |
| }).catch((): void => { |
| // Catch Navigation Error |
| }); |
| } |
| } |
| |
| /** Close tooltip when clicking outside @public */ |
| @HostListener('document:click', ['$event']) |
| closeTooltip(event: Event) { |
| if (!(event.target as HTMLElement).classList.contains('short-description')) { |
| this.showTooltip = false; |
| } |
| } |
| /** Generate nodeData object from loop and return for the datasource @public */ |
| public generateNodeData(nodedata: K8SCLUSTERDATA): K8SCLUSTERDATADISPLAY { |
| return { |
| name: nodedata.name, |
| state: !isNullOrUndefined(nodedata.resourceState) ? nodedata.resourceState : 'N/A', |
| identifier: nodedata._id, |
| version: nodedata.k8s_version, |
| description: !isNullOrUndefined(nodedata.description) ? nodedata.description : '-', |
| nodeCount: nodedata.node_count, |
| nodeSize: nodedata.node_size, |
| created: !isNullOrUndefined(nodedata?._admin?.created) ? this.sharedService.convertEpochTime(Number(nodedata._admin.created)) : '-', |
| modified: !isNullOrUndefined(nodedata?._admin?.modified) ? this.sharedService.convertEpochTime(Number(nodedata._admin.modified)) : '-' |
| }; |
| } |
| /** Generate ksuData object from loop and return for the datasource @public */ |
| public generateKsuData(ksudata: K8SCLUSTERDATA): K8SCLUSTERDATADISPLAY { |
| return { |
| name: ksudata.name, |
| state: !isNullOrUndefined(ksudata.resourceState) ? ksudata.resourceState : 'N/A', |
| identifier: ksudata._id, |
| version: ksudata.k8s_version, |
| description: !isNullOrUndefined(ksudata.description) ? ksudata.description : '-', |
| package_name: ksudata.package_name, |
| package_path: ksudata.package_path, |
| created: !isNullOrUndefined(ksudata?._admin?.created) ? this.sharedService.convertEpochTime(Number(ksudata._admin.created)) : '-', |
| modified: !isNullOrUndefined(ksudata?._admin?.modified) ? this.sharedService.convertEpochTime(Number(ksudata._admin.modified)) : '-' |
| }; |
| } |
| |
| /** Generate k8s Data function @public */ |
| public generateData(): void { |
| this.isLoadingResults = true; |
| this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.paramsID).subscribe((k8sClusterDatas: K8SCLUSTERDATA) => { |
| if (!isNullOrUndefined(k8sClusterDatas)) { |
| this.node_count = k8sClusterDatas.node_count; |
| this.ksu_count = k8sClusterDatas.ksu_count; |
| this.countData = k8sClusterDatas; |
| this.clusterName = k8sClusterDatas.name; |
| this.nodeData(); |
| } |
| this.isLoadingResults = false; |
| }, (error: ERRORDATA) => { |
| this.restService.handleError(error, 'get'); |
| this.isLoadingResults = false; |
| }); |
| } |
| |
| /** Generate node Data function @public */ |
| public nodeData(): void { |
| this.isLoadingResults = true; |
| this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.paramsID + '/nodegroup').subscribe((k8sClusterDatas: K8SCLUSTERDATA) => { |
| if (!isNullOrUndefined(k8sClusterDatas)) { |
| this.nodeDetail = []; |
| this.node_count = k8sClusterDatas.count; |
| k8sClusterDatas.data?.forEach((clusterData: K8SNODEDATA) => { |
| const k8sClusterDataObj: K8SCLUSTERDATADISPLAY = this.generateNodeData(clusterData); |
| this.nodeDetail.push(k8sClusterDataObj); |
| }); |
| } |
| this.isLoadingResults = false; |
| }, (error: ERRORDATA) => { |
| this.restService.handleError(error, 'get'); |
| this.isLoadingResults = false; |
| }); |
| } |
| |
| /** Generate ksu Data function @public */ |
| public ksuData(): void { |
| this.isLoadingResults = true; |
| this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.paramsID + '/ksus').subscribe((k8sClusterDatas: K8SCLUSTERDATA) => { |
| if (!isNullOrUndefined(k8sClusterDatas)) { |
| this.ksuDetail = []; |
| this.ksu_count = k8sClusterDatas.count; |
| k8sClusterDatas.data?.forEach((clusterData: K8SNODEDATA) => { |
| const k8sClusterDataObj: K8SCLUSTERDATADISPLAY = this.generateKsuData(clusterData); |
| this.ksuDetail.push(k8sClusterDataObj); |
| }); |
| } |
| this.isLoadingResults = false; |
| }, (error: ERRORDATA) => { |
| this.restService.handleError(error, 'get'); |
| this.isLoadingResults = false; |
| }); |
| } |
| |
| /** To capture active button @public */ |
| public setActiveButton(buttonNumber: number) { |
| this.activeButton = buttonNumber; |
| if (buttonNumber === 1) { |
| this.nodeData(); |
| this.isCollapsed1 = true; |
| this.isCollapsed2 = false; |
| } else if (buttonNumber === 2) { |
| this.ksuData(); |
| this.isCollapsed2 = true; |
| this.isCollapsed1 = false; |
| } |
| } |
| |
| /** Scaling @public */ |
| public scaling(id: string, type?: string): void { |
| // eslint-disable-next-line security/detect-non-literal-fs-filename |
| const modalRef: NgbModalRef = this.modalService.open(NodeAddComponent, { backdrop: 'static' }); |
| modalRef.componentInstance.clusterId = this.paramsID; |
| modalRef.componentInstance.profileID = id; |
| modalRef.componentInstance.profileType = type; |
| modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| if (result) { |
| this.sharedService.callData(); |
| this.nodeData(); |
| } |
| }).catch((): void => { |
| // Catch Navigation Error |
| }); |
| } |
| |
| /** Edit KSU @public */ |
| public editKsu(id?: string): void { |
| // eslint-disable-next-line security/detect-non-literal-fs-filename |
| const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' }); |
| modalRef.componentInstance.profileID = id; |
| modalRef.componentInstance.profileType = 'edit'; |
| modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| if (result) { |
| this.sharedService.callData(); |
| this.ksuData(); |
| } |
| }).catch((): void => { |
| // Catch Navigation Error |
| }); |
| } |
| } |