| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 TATA ELXSI |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | |
| 16 | Author: SANDHYA JS (sandhya.j@tataelxsi.co.in) |
| 17 | */ |
| 18 | /** |
| 19 | * @file Info K8s Page |
| 20 | */ |
| 21 | import { Component, HostListener, Injector, OnInit } from '@angular/core'; |
| 22 | import { ActivatedRoute } from '@angular/router'; |
| 23 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; |
| 24 | import { ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel'; |
| 25 | import { DeleteComponent } from 'DeleteComponent'; |
| 26 | import { environment } from 'environment'; |
| 27 | import { K8SCLUSTERDATA, K8SCLUSTERDATADISPLAY, K8SNODEDATA } from 'K8sModel'; |
| 28 | import { KSUAddComponent } from 'KSUAddComponent'; |
| 29 | import { NodeAddComponent } from 'NodeAddComponent'; |
| 30 | import { RestService } from 'RestService'; |
| 31 | import { isNullOrUndefined, SharedService } from 'SharedService'; |
| 32 | import { ShowInfoComponent } from 'ShowInfoComponent'; |
| 33 | |
| 34 | /** |
| 35 | * Creating component |
| 36 | * @Component K8sInfoComponent.html as template url |
| 37 | */ |
| 38 | @Component({ |
| 39 | selector: 'app-info-k8s', |
| 40 | templateUrl: './K8sInfoComponent.html', |
| 41 | styleUrls: ['./K8sInfoComponent.scss'] |
| 42 | }) |
| 43 | /** Exporting a class @exports K8sInfoComponent */ |
| 44 | export class K8sInfoComponent implements OnInit { |
| 45 | /** To inject services @public */ |
| 46 | public injector: Injector; |
| 47 | |
| 48 | /** Contains active button @public */ |
| 49 | public activeButton = 1; |
| 50 | |
| 51 | /** condition to show tooltip @public */ |
| 52 | public showTooltip = false; |
| 53 | |
| 54 | /** Showing more details of collapase */ |
| 55 | public isCollapsed1: boolean = true; |
| 56 | |
| 57 | /** Showing more details of collapase */ |
| 58 | public isCollapsed2: boolean = false; |
| 59 | |
| 60 | /** Check the Projects loading results @public */ |
| 61 | public isLoadingResults: boolean = false; |
| 62 | |
| 63 | /** Give the message for the loading @public */ |
| 64 | public message: string = 'PLEASEWAIT'; |
| 65 | |
| 66 | /** variables contains paramsID @public */ |
| 67 | public paramsID: string; |
| 68 | |
| 69 | /** Contains cluster name @public */ |
| 70 | public clusterName: string; |
| 71 | |
| 72 | /** Contains cluster data with count @public */ |
| 73 | public countData: K8SCLUSTERDATA = {}; |
| 74 | |
| 75 | /** Contains node count @public */ |
| 76 | public node_count: number; |
| 77 | |
| 78 | /** Contains ksu count @public */ |
| 79 | public ksu_count: number; |
| 80 | |
| 81 | /** Contains node details @public */ |
| 82 | public nodeDetail: {}[] = []; |
| 83 | |
| 84 | /** Contains ksu details @public */ |
| 85 | public ksuDetail: {}[] = []; |
| 86 | |
| 87 | /** Condition for popover visibility @public */ |
| 88 | public isPopoverVisible = false; |
| 89 | |
| 90 | /** Contains popover text to show @public */ |
| 91 | public popoverText = ''; |
| 92 | |
| 93 | /** Instance of the rest service @private */ |
| 94 | private restService: RestService; |
| 95 | |
| 96 | /** Holds teh instance of AuthService class of type AuthService @private */ |
| 97 | private activatedRoute: ActivatedRoute; |
| 98 | |
| 99 | /** Utilizes modal service for any modal operations @private */ |
| 100 | private modalService: NgbModal; |
| 101 | |
| 102 | /** Contains all methods related to shared @private */ |
| 103 | private sharedService: SharedService; |
| 104 | |
| 105 | constructor(injector: Injector) { |
| 106 | this.injector = injector; |
| 107 | this.restService = this.injector.get(RestService); |
| 108 | this.activatedRoute = this.injector.get(ActivatedRoute); |
| 109 | this.modalService = this.injector.get(NgbModal); |
| 110 | this.sharedService = this.injector.get(SharedService); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Lifecyle Hooks the trigger before component is instantiate |
| 115 | */ |
| 116 | public ngOnInit(): void { |
| 117 | this.paramsID = this.activatedRoute.snapshot.paramMap.get('id'); |
| 118 | this.generateData(); |
| 119 | } |
| 120 | |
| 121 | /** To view Details @public */ |
| 122 | public onView(identifier: string, type: number): void { |
| 123 | let pageName: string = ''; |
| 124 | let title: string = ''; |
| 125 | if (type === 1) { |
| 126 | pageName = 'k8s-node'; |
| 127 | title = 'Node Details'; |
| 128 | } else { |
| 129 | pageName = 'k8s-ksu'; |
| 130 | title = 'KSU Details'; |
| 131 | } |
| 132 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 133 | this.modalService.open(ShowInfoComponent, { backdrop: 'static' }).componentInstance.params = { |
| 134 | id: identifier, |
| 135 | page: pageName, |
| 136 | titleName: title, |
| 137 | cluster_id: this.paramsID |
| 138 | }; |
| 139 | } |
| 140 | |
| 141 | /** Edit Node @public */ |
| 142 | public onEdit(type: number): void { |
| 143 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 144 | const modalRef: NgbModalRef = this.modalService.open(NodeAddComponent, { backdrop: 'static' }); |
| 145 | if (type === 1) { |
| 146 | modalRef.componentInstance.profileType = 'card-node'; |
| 147 | } |
| 148 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 149 | if (result) { |
| 150 | this.sharedService.callData(); |
| 151 | } |
| 152 | }).catch((): void => { |
| 153 | // Catch Navigation Error |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | /** Delete Operation @public */ |
| 158 | public onDelete(id_value: string, name_value: string, type: number): void { |
| 159 | let pageName: string = ''; |
| 160 | let title: string = ''; |
| 161 | if (type === 1) { |
| 162 | pageName = 'card-node'; |
| 163 | title = 'Node Details'; |
| 164 | } else { |
| 165 | pageName = 'card-ksu'; |
| 166 | title = 'KSU Details'; |
| 167 | } |
| 168 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 169 | const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' }); |
| 170 | modalRef.componentInstance.params = { |
| 171 | id: id_value, |
| 172 | page: pageName, |
| 173 | titleName: 'Node Details', |
| 174 | cluster_id: this.paramsID, |
| 175 | name: name_value |
| 176 | }; |
| 177 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 178 | if (result) { |
| 179 | this.sharedService.callData(); |
| 180 | this.nodeData(); |
| 181 | } |
| 182 | }).catch((): void => { |
| 183 | // Catch Navigation Error |
| 184 | }); |
| 185 | } |
| 186 | |
| 187 | /** popover functionality to show full description @public */ |
| 188 | public showPopover(event: MouseEvent, description: string) { |
| 189 | this.popoverText = description; |
| 190 | this.isPopoverVisible = true; |
| 191 | const popover = (event.target as HTMLElement).nextElementSibling as HTMLElement; |
| 192 | if (popover) { |
| 193 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| 194 | popover.style.top = `${event.clientY + 10}px`; |
| 195 | popover.style.left = `${event.clientX}px`; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** To hide popover @public */ |
| 200 | public hidePopover() { |
| 201 | this.isPopoverVisible = false; |
| 202 | } |
| 203 | |
| 204 | /** Add Node/KSU @public */ |
| 205 | public addK8sCluster(type?: string): void { |
| 206 | if (type === 'card_node') { |
| 207 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 208 | const modalRef: NgbModalRef = this.modalService.open(NodeAddComponent, { backdrop: 'static' }); |
| 209 | modalRef.componentInstance.profileType = type; |
| 210 | modalRef.componentInstance.clusterId = this.paramsID; |
| 211 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 212 | if (result) { |
| 213 | this.sharedService.callData(); |
| 214 | this.nodeData(); |
| 215 | } |
| 216 | }).catch((): void => { |
| 217 | // Catch Navigation Error |
| 218 | }); |
| 219 | } else if (type === 'card_ksu') { |
| 220 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 221 | const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' }); |
| 222 | modalRef.componentInstance.profileType = 'card-add'; |
| 223 | modalRef.componentInstance.profileID = this.paramsID; |
| 224 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 225 | if (result) { |
| 226 | this.sharedService.callData(); |
| 227 | this.ksuData(); |
| 228 | } |
| 229 | }).catch((): void => { |
| 230 | // Catch Navigation Error |
| 231 | }); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** Close tooltip when clicking outside @public */ |
| 236 | @HostListener('document:click', ['$event']) |
| 237 | closeTooltip(event: Event) { |
| 238 | if (!(event.target as HTMLElement).classList.contains('short-description')) { |
| 239 | this.showTooltip = false; |
| 240 | } |
| 241 | } |
| 242 | /** Generate nodeData object from loop and return for the datasource @public */ |
| 243 | public generateNodeData(nodedata: K8SCLUSTERDATA): K8SCLUSTERDATADISPLAY { |
| 244 | return { |
| 245 | name: nodedata.name, |
| 246 | state: !isNullOrUndefined(nodedata.resourceState) ? nodedata.resourceState : 'N/A', |
| 247 | identifier: nodedata._id, |
| 248 | version: nodedata.k8s_version, |
| 249 | description: !isNullOrUndefined(nodedata.description) ? nodedata.description : '-', |
| 250 | nodeCount: nodedata.node_count, |
| 251 | nodeSize: nodedata.node_size, |
| 252 | created: !isNullOrUndefined(nodedata?._admin?.created) ? this.sharedService.convertEpochTime(Number(nodedata._admin.created)) : '-', |
| 253 | modified: !isNullOrUndefined(nodedata?._admin?.modified) ? this.sharedService.convertEpochTime(Number(nodedata._admin.modified)) : '-' |
| 254 | }; |
| 255 | } |
| 256 | /** Generate ksuData object from loop and return for the datasource @public */ |
| 257 | public generateKsuData(ksudata: K8SCLUSTERDATA): K8SCLUSTERDATADISPLAY { |
| 258 | return { |
| 259 | name: ksudata.name, |
| 260 | state: !isNullOrUndefined(ksudata.resourceState) ? ksudata.resourceState : 'N/A', |
| 261 | identifier: ksudata._id, |
| 262 | version: ksudata.k8s_version, |
| 263 | description: !isNullOrUndefined(ksudata.description) ? ksudata.description : '-', |
| 264 | package_name: ksudata.package_name, |
| 265 | package_path: ksudata.package_path, |
| 266 | created: !isNullOrUndefined(ksudata?._admin?.created) ? this.sharedService.convertEpochTime(Number(ksudata._admin.created)) : '-', |
| 267 | modified: !isNullOrUndefined(ksudata?._admin?.modified) ? this.sharedService.convertEpochTime(Number(ksudata._admin.modified)) : '-' |
| 268 | }; |
| 269 | } |
| 270 | |
| 271 | /** Generate k8s Data function @public */ |
| 272 | public generateData(): void { |
| 273 | this.isLoadingResults = true; |
| 274 | this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.paramsID).subscribe((k8sClusterDatas: K8SCLUSTERDATA) => { |
| 275 | if (!isNullOrUndefined(k8sClusterDatas)) { |
| 276 | this.node_count = k8sClusterDatas.node_count; |
| 277 | this.ksu_count = k8sClusterDatas.ksu_count; |
| 278 | this.countData = k8sClusterDatas; |
| 279 | this.clusterName = k8sClusterDatas.name; |
| 280 | this.nodeData(); |
| 281 | } |
| 282 | this.isLoadingResults = false; |
| 283 | }, (error: ERRORDATA) => { |
| 284 | this.restService.handleError(error, 'get'); |
| 285 | this.isLoadingResults = false; |
| 286 | }); |
| 287 | } |
| 288 | |
| 289 | /** Generate node Data function @public */ |
| 290 | public nodeData(): void { |
| 291 | this.isLoadingResults = true; |
| 292 | this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.paramsID + '/nodegroup').subscribe((k8sClusterDatas: K8SCLUSTERDATA) => { |
| 293 | if (!isNullOrUndefined(k8sClusterDatas)) { |
| 294 | this.nodeDetail = []; |
| 295 | this.node_count = k8sClusterDatas.count; |
| 296 | k8sClusterDatas.data?.forEach((clusterData: K8SNODEDATA) => { |
| 297 | const k8sClusterDataObj: K8SCLUSTERDATADISPLAY = this.generateNodeData(clusterData); |
| 298 | this.nodeDetail.push(k8sClusterDataObj); |
| 299 | }); |
| 300 | } |
| 301 | this.isLoadingResults = false; |
| 302 | }, (error: ERRORDATA) => { |
| 303 | this.restService.handleError(error, 'get'); |
| 304 | this.isLoadingResults = false; |
| 305 | }); |
| 306 | } |
| 307 | |
| 308 | /** Generate ksu Data function @public */ |
| 309 | public ksuData(): void { |
| 310 | this.isLoadingResults = true; |
| 311 | this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.paramsID + '/ksus').subscribe((k8sClusterDatas: K8SCLUSTERDATA) => { |
| 312 | if (!isNullOrUndefined(k8sClusterDatas)) { |
| 313 | this.ksuDetail = []; |
| 314 | this.ksu_count = k8sClusterDatas.count; |
| 315 | k8sClusterDatas.data?.forEach((clusterData: K8SNODEDATA) => { |
| 316 | const k8sClusterDataObj: K8SCLUSTERDATADISPLAY = this.generateKsuData(clusterData); |
| 317 | this.ksuDetail.push(k8sClusterDataObj); |
| 318 | }); |
| 319 | } |
| 320 | this.isLoadingResults = false; |
| 321 | }, (error: ERRORDATA) => { |
| 322 | this.restService.handleError(error, 'get'); |
| 323 | this.isLoadingResults = false; |
| 324 | }); |
| 325 | } |
| 326 | |
| 327 | /** To capture active button @public */ |
| 328 | public setActiveButton(buttonNumber: number) { |
| 329 | this.activeButton = buttonNumber; |
| 330 | if (buttonNumber === 1) { |
| 331 | this.nodeData(); |
| 332 | this.isCollapsed1 = true; |
| 333 | this.isCollapsed2 = false; |
| 334 | } else if (buttonNumber === 2) { |
| 335 | this.ksuData(); |
| 336 | this.isCollapsed2 = true; |
| 337 | this.isCollapsed1 = false; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** Scaling @public */ |
| 342 | public scaling(id: string, type?: string): void { |
| 343 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 344 | const modalRef: NgbModalRef = this.modalService.open(NodeAddComponent, { backdrop: 'static' }); |
| 345 | modalRef.componentInstance.clusterId = this.paramsID; |
| 346 | modalRef.componentInstance.profileID = id; |
| 347 | modalRef.componentInstance.profileType = type; |
| 348 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 349 | if (result) { |
| 350 | this.sharedService.callData(); |
| 351 | this.nodeData(); |
| 352 | } |
| 353 | }).catch((): void => { |
| 354 | // Catch Navigation Error |
| 355 | }); |
| 356 | } |
| 357 | |
| 358 | /** Edit KSU @public */ |
| 359 | public editKsu(id?: string): void { |
| 360 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 361 | const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' }); |
| 362 | modalRef.componentInstance.profileID = id; |
| 363 | modalRef.componentInstance.profileType = 'edit'; |
| 364 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 365 | if (result) { |
| 366 | this.sharedService.callData(); |
| 367 | this.ksuData(); |
| 368 | } |
| 369 | }).catch((): void => { |
| 370 | // Catch Navigation Error |
| 371 | }); |
| 372 | } |
| 373 | } |