| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +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 K8sInfraConfigProfileComponent.ts. |
| 20 | */ |
| 21 | import { Component, Injector, OnDestroy, OnInit } from '@angular/core'; |
| 22 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; |
| 23 | import { TranslateService } from '@ngx-translate/core'; |
| 24 | import { CONFIGCONSTANT, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel'; |
| 25 | import { DataService } from 'DataService'; |
| 26 | import { environment } from 'environment'; |
| 27 | import { K8sActionComponent } from 'K8sActionComponent'; |
| 28 | import { INFRACONFIGPAYLOAD, K8SCreateCLUSTERDATA, K8SCREATEDATADISPLAY } from 'K8sModel'; |
| 29 | import { LocalDataSource } from 'ng2-smart-table'; |
| 30 | import { RestService } from 'RestService'; |
| 31 | import { Subscription } from 'rxjs'; |
| 32 | import { SharedService } from 'SharedService'; |
| 33 | import { K8sInfraConfigAddComponent } from '../k8s-infra-config-add/K8sInfraConfigAddComponent'; |
| 34 | /** |
| 35 | * Creating Component |
| 36 | * @Component takes K8sInfraControllerProfileComponent.html as template url |
| 37 | */ |
| 38 | @Component({ |
| 39 | selector: 'app-infra-controller-profile', |
| 40 | templateUrl: './K8sInfraControllerProfileComponent.html', |
| 41 | styleUrls: ['./K8sInfraControllerProfileComponent.scss'] |
| 42 | }) |
| 43 | /** Exporting a class @exports K8sInfraControllerProfileComponent */ |
| 44 | export class K8sInfraControllerProfileComponent implements OnInit, OnDestroy { |
| 45 | /** To inject services @public */ |
| 46 | public injector: Injector; |
| 47 | |
| 48 | /** handle translate @public */ |
| 49 | public translateService: TranslateService; |
| 50 | |
| 51 | /** Data of smarttable populate through LocalDataSource @public */ |
| 52 | public dataSource: LocalDataSource = new LocalDataSource(); |
| 53 | |
| 54 | /** Columns list of the smart table @public */ |
| 55 | public columnList: object = {}; |
| 56 | |
| 57 | /** Settings for smarttable to populate the table with columns @public */ |
| 58 | public settings: object = {}; |
| 59 | |
| 60 | /** operational State created data @public */ |
| 61 | public operationalStateFirstStep: string = CONFIGCONSTANT.k8OperationalStateFirstStep; |
| 62 | |
| 63 | /** operational State in creation data @public */ |
| 64 | public operationalStateSecondStep: string = CONFIGCONSTANT.k8OperationalStateStateSecondStep; |
| 65 | |
| 66 | /** operational State in deletion data @public */ |
| 67 | public operationalStateThirdStep: string = CONFIGCONSTANT.k8OperationalStateThirdStep; |
| 68 | |
| 69 | /** operational State failed deletion data @public */ |
| 70 | public operationalStateFourthStep: string = CONFIGCONSTANT.k8OperationalStateFourthStep; |
| 71 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 72 | /** Check the loading results @public */ |
| 73 | public isLoadingResults: boolean = true; |
| 74 | |
| 75 | /** Give the message for the loading @public */ |
| 76 | public message: string = 'PLEASEWAIT'; |
| 77 | |
| 78 | /** Class for empty and present data @public */ |
| 79 | public checkDataClass: string; |
| 80 | |
| 81 | /** Instance of the rest service @private */ |
| 82 | private restService: RestService; |
| 83 | |
| 84 | /** dataService to pass the data from one component to another @private */ |
| 85 | private dataService: DataService; |
| 86 | |
| 87 | /** Formation of appropriate Data for LocalDatasource @private */ |
| 88 | private profileData: {}[] = []; |
| 89 | |
| 90 | /** Contains all methods related to shared @private */ |
| 91 | private sharedService: SharedService; |
| 92 | |
| 93 | /** Instance of the modal service @private */ |
| 94 | private modalService: NgbModal; |
| 95 | |
| 96 | /** Instance of subscriptions @private */ |
| 97 | private generateDataSub: Subscription; |
| 98 | |
| 99 | constructor(injector: Injector) { |
| 100 | this.injector = injector; |
| 101 | this.restService = this.injector.get(RestService); |
| 102 | this.dataService = this.injector.get(DataService); |
| 103 | this.sharedService = this.injector.get(SharedService); |
| 104 | this.translateService = this.injector.get(TranslateService); |
| 105 | this.modalService = this.injector.get(NgbModal); |
| 106 | } |
| 107 | /** Lifecyle Hooks the trigger before component is instantiate @public */ |
| 108 | public ngOnInit(): void { |
| 109 | this.generateColumns(); |
| 110 | this.generateSettings(); |
| 111 | this.generateData(); |
| 112 | this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); }); |
| 113 | } |
| 114 | |
| 115 | /** smart table Header Colums @public */ |
| 116 | public generateColumns(): void { |
| 117 | this.columnList = { |
| 118 | name: { title: this.translateService.instant('NAME'), width: '20%', sortDirection: 'asc' }, |
| 119 | identifier: { title: this.translateService.instant('IDENTIFIER'), width: '15%' }, |
| 120 | state: { |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 121 | title: this.translateService.instant('STATE'), width: '15%', type: 'html', |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 122 | filter: { |
| 123 | type: 'list', |
| 124 | config: { |
| 125 | selectText: 'Select', |
| 126 | list: [ |
| 127 | { value: this.operationalStateFirstStep, title: this.operationalStateFirstStep }, |
| 128 | { value: this.operationalStateSecondStep, title: this.operationalStateSecondStep }, |
| 129 | { value: this.operationalStateThirdStep, title: this.operationalStateThirdStep }, |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 130 | { value: this.operationalStateThirdStep, title: this.operationalStateFourthStep } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 131 | ] |
| 132 | } |
| 133 | }, |
| 134 | valuePrepareFunction: (cell: INFRACONFIGPAYLOAD, row: INFRACONFIGPAYLOAD): string => { |
| 135 | if (row.state === this.operationalStateFirstStep) { |
| 136 | return `<span class="icon-label" title="${row.state}"> |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 137 | <i class="fas fa-clock text-success"></i> |
| 138 | </span>`; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 139 | } else if (row.state === this.operationalStateSecondStep) { |
| 140 | return `<span class="icon-label" title="${row.state}"> |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 141 | <i class="fas fa-times-circle text-danger"></i> |
| 142 | </span>`; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 143 | } else if (row.state === this.operationalStateFourthStep) { |
| 144 | return `<span class="icon-label" title="${row.state}"> |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 145 | <i class="fas fa-ban text-danger"></i> |
| 146 | </span>`; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 147 | } else { |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 148 | return `<span class="icon-label" title="${row.state}"> |
| 149 | <i class="fas fa-spinner text-warning"></i> |
| 150 | </span>`; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | }, |
| 154 | created: { title: this.translateService.instant('CREATED'), width: '10%' }, |
| 155 | modified: { title: this.translateService.instant('MODIFIED'), width: '10%' }, |
| 156 | Actions: { |
| 157 | name: 'Action', width: '5%', filter: false, sort: false, title: this.translateService.instant('ACTIONS'), type: 'custom', |
| 158 | valuePrepareFunction: (cell: K8SCREATEDATADISPLAY, row: K8SCREATEDATADISPLAY): K8SCREATEDATADISPLAY => row, |
| 159 | renderComponent: K8sActionComponent |
| 160 | } |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | /** smart table Data Settings @public */ |
| 165 | public generateSettings(): void { |
| 166 | this.settings = { |
| 167 | columns: this.columnList, |
| 168 | actions: { add: false, edit: false, delete: false, position: 'right' }, |
| 169 | attr: this.sharedService.tableClassConfig(), |
| 170 | pager: this.sharedService.paginationPagerConfig(), |
| 171 | noDataMessage: this.translateService.instant('NODATAMSG') |
| 172 | }; |
| 173 | } |
| 174 | |
| 175 | /** smart table listing manipulation @public */ |
| 176 | public onChange(perPageValue: number): void { |
| 177 | this.dataSource.setPaging(1, perPageValue, true); |
| 178 | } |
| 179 | |
| 180 | /** smart table listing manipulation @public */ |
| 181 | public onUserRowSelect(event: MessageEvent): void { |
| 182 | Object.assign(event.data, { page: 'k8-infra-controller' }); |
| 183 | this.dataService.changeMessage(event.data); |
| 184 | } |
| 185 | |
| 186 | /** Compose new Profile @public */ |
| 187 | public addProfile(): void { |
| 188 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 189 | const modalRef: NgbModalRef = this.modalService.open(K8sInfraConfigAddComponent, { backdrop: 'static' }); |
| 190 | modalRef.componentInstance.profileType = 'infra-controller'; |
| 191 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 192 | if (result) { |
| 193 | this.sharedService.callData(); |
| 194 | } |
| 195 | }).catch((): void => { |
| 196 | // Catch Navigation Error |
| 197 | }); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Lifecyle hook which get trigger on component destruction |
| 202 | */ |
| 203 | public ngOnDestroy(): void { |
| 204 | this.generateDataSub.unsubscribe(); |
| 205 | } |
| 206 | /** Generate profile object from loop and return for the datasource @public */ |
| 207 | public generateprofileData(profileData: INFRACONFIGPAYLOAD): INFRACONFIGPAYLOAD { |
| 208 | return { |
| 209 | name: profileData.name, |
| 210 | identifier: profileData._id, |
| 211 | created: this.sharedService.convertEpochTime(Number(profileData._admin.created)), |
| 212 | modified: this.sharedService.convertEpochTime(Number(profileData._admin.modified)), |
| 213 | description: profileData.description, |
| 214 | pageType: 'infra-controller', |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 215 | state: profileData.resourceState |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 216 | }; |
| 217 | } |
| 218 | |
| 219 | /** Fetching the data from server to Load in the smarttable @protected */ |
| 220 | protected generateData(): void { |
| 221 | this.isLoadingResults = true; |
| 222 | this.restService.getResource(environment.K8SINFRACONTROLLERPROFILE_URL).subscribe((profileDatas: K8SCreateCLUSTERDATA[]) => { |
| 223 | this.profileData = []; |
| 224 | profileDatas.forEach((profileData: INFRACONFIGPAYLOAD) => { |
| 225 | const profileDataObj: INFRACONFIGPAYLOAD = this.generateprofileData(profileData); |
| 226 | this.profileData.push(profileDataObj); |
| 227 | }); |
| 228 | if (this.profileData.length > 0) { |
| 229 | this.checkDataClass = 'dataTables_present'; |
| 230 | } else { |
| 231 | this.checkDataClass = 'dataTables_empty'; |
| 232 | } |
| 233 | this.dataSource.load(this.profileData).then((data: boolean) => { |
| 234 | this.isLoadingResults = false; |
| 235 | }).catch(() => { |
| 236 | this.isLoadingResults = false; |
| 237 | }); |
| 238 | }, (error: ERRORDATA) => { |
| 239 | this.restService.handleError(error, 'get'); |
| 240 | this.isLoadingResults = false; |
| 241 | }); |
| 242 | } |
| 243 | } |