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