blob: f5c93ae2fa2ac964ec49a2581dd9f33a696a1cbd [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 K8sInfraConfigProfileComponent.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, K8SCREATEDATADISPLAY } 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 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 */
44export 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
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.JSa3ff32a2025-02-13 16:24:46 +0530124 title: this.translateService.instant('GITSTATE'), width: '15%', type: 'html',
SANDHYA.JS26570112024-07-05 21:35:46 +0530125 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: K8SCREATEDATADISPLAY, row: K8SCREATEDATADISPLAY): K8SCREATEDATADISPLAY => 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-controller' });
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-controller';
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 /** Generate profile object from loop and return for the datasource @public */
217 public generateprofileData(profileData: INFRACONFIGPAYLOAD): INFRACONFIGPAYLOAD {
218 return {
219 name: profileData.name,
220 identifier: profileData._id,
221 created: this.sharedService.convertEpochTime(Number(profileData._admin.created)),
222 modified: this.sharedService.convertEpochTime(Number(profileData._admin.modified)),
223 description: profileData.description,
224 pageType: 'infra-controller',
225 state: profileData.state
226 };
227 }
228
229 /** Fetching the data from server to Load in the smarttable @protected */
230 protected generateData(): void {
231 this.isLoadingResults = true;
232 this.restService.getResource(environment.K8SINFRACONTROLLERPROFILE_URL).subscribe((profileDatas: K8SCreateCLUSTERDATA[]) => {
233 this.profileData = [];
234 profileDatas.forEach((profileData: INFRACONFIGPAYLOAD) => {
235 const profileDataObj: INFRACONFIGPAYLOAD = this.generateprofileData(profileData);
236 this.profileData.push(profileDataObj);
237 });
238 if (this.profileData.length > 0) {
239 this.checkDataClass = 'dataTables_present';
240 } else {
241 this.checkDataClass = 'dataTables_empty';
242 }
243 this.dataSource.load(this.profileData).then((data: boolean) => {
244 this.isLoadingResults = false;
245 }).catch(() => {
246 this.isLoadingResults = false;
247 });
248 }, (error: ERRORDATA) => {
249 this.restService.handleError(error, 'get');
250 this.isLoadingResults = false;
251 });
252 }
253}