blob: fc48918cb9d5374cbd8128895b26be55b40b29a9 [file] [log] [blame]
SANDHYA.JS26570112024-07-05 21:35:46 +05301/*
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 K8sAppProfileComponent.ts.
20 */
21import { Component, Injector, OnDestroy, OnInit } from '@angular/core';
22import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
23import { TranslateService } from '@ngx-translate/core';
24import { CONFIGCONSTANT, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
25import { DataService } from 'DataService';
26import { environment } from 'environment';
27import { K8sActionComponent } from 'K8sActionComponent';
28import { INFRACONFIGPAYLOAD, K8SCreateCLUSTERDATA } from 'K8sModel';
29import { LocalDataSource } from 'ng2-smart-table';
30import { RestService } from 'RestService';
31import { Subscription } from 'rxjs';
32import { SharedService } from 'SharedService';
33import { K8sInfraConfigAddComponent } from '../k8s-infra-config-add/K8sInfraConfigAddComponent';
34/**
35 * Creating Component
36 * @Component takes K8sAppProfileComponent.html as template url
37 */
38@Component({
39 selector: 'app-app-profile',
40 templateUrl: './K8sAppProfileComponent.html',
41 styleUrls: ['./K8sAppProfileComponent.scss']
42})
43/** Exporting a class @exports K8sAppProfileComponent */
44export class K8sAppProfileComponent 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.JS26570112024-07-05 21:35:46 +053072 /** 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.JS92379ec2025-06-13 17:29:35 +0530121 title: this.translateService.instant('STATE'), width: '15%', type: 'html',
SANDHYA.JS26570112024-07-05 21:35:46 +0530122 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.JS92379ec2025-06-13 17:29:35 +0530130 { value: this.operationalStateThirdStep, title: this.operationalStateFourthStep }
SANDHYA.JS26570112024-07-05 21:35:46 +0530131 ]
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.JS92379ec2025-06-13 17:29:35 +0530137 <i class="fas fa-clock text-success"></i>
138 </span>`;
SANDHYA.JS26570112024-07-05 21:35:46 +0530139 } else if (row.state === this.operationalStateSecondStep) {
140 return `<span class="icon-label" title="${row.state}">
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530141 <i class="fas fa-times-circle text-danger"></i>
142 </span>`;
SANDHYA.JS26570112024-07-05 21:35:46 +0530143 } else if (row.state === this.operationalStateFourthStep) {
144 return `<span class="icon-label" title="${row.state}">
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530145 <i class="fas fa-ban text-danger"></i>
146 </span>`;
SANDHYA.JS26570112024-07-05 21:35:46 +0530147 } else {
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530148 return `<span class="icon-label" title="${row.state}">
149 <i class="fas fa-spinner text-warning"></i>
150 </span>`;
SANDHYA.JS26570112024-07-05 21:35:46 +0530151 }
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: INFRACONFIGPAYLOAD, row: INFRACONFIGPAYLOAD): INFRACONFIGPAYLOAD => 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-app-profile' });
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 = 'app-profile';
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
207 /** Generate profile object from loop and return for the datasource @public */
208 public generateprofileData(profileData: INFRACONFIGPAYLOAD): INFRACONFIGPAYLOAD {
209 return {
210 name: profileData.name,
211 identifier: profileData._id,
212 created: this.sharedService.convertEpochTime(Number(profileData._admin.created)),
213 modified: this.sharedService.convertEpochTime(Number(profileData._admin.modified)),
214 description: profileData.description,
215 pageType: 'app-profile',
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530216 state: profileData.resourceState
SANDHYA.JS26570112024-07-05 21:35:46 +0530217 };
218 }
219
220 /** Fetching the data from server to Load in the smarttable @protected */
221 protected generateData(): void {
222 this.isLoadingResults = true;
223 this.restService.getResource(environment.K8SAPPPROFILE_URL).subscribe((profileDatas: K8SCreateCLUSTERDATA[]) => {
224 this.profileData = [];
225 profileDatas.forEach((profileData: INFRACONFIGPAYLOAD) => {
226 const profileDataObj: INFRACONFIGPAYLOAD = this.generateprofileData(profileData);
227 this.profileData.push(profileDataObj);
228 });
229 if (this.profileData.length > 0) {
230 this.checkDataClass = 'dataTables_present';
231 } else {
232 this.checkDataClass = 'dataTables_empty';
233 }
234 this.dataSource.load(this.profileData).then((data: boolean) => {
235 this.isLoadingResults = false;
236 }).catch(() => {
237 this.isLoadingResults = false;
238 });
239 }, (error: ERRORDATA) => {
240 this.restService.handleError(error, 'get');
241 this.isLoadingResults = false;
242 });
243 }
244}