Fix mod ui build issues
[dcaegen2/platform.git] / mod2 / ui / src / app / comp-specs / comp-specs.component.ts
1 /* 
2  *  # ============LICENSE_START=======================================================
3  *  # Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
4  *  # ================================================================================
5  *  # Licensed under the Apache License, Version 2.0 (the "License");
6  *  # you may not use this file except in compliance with the License.
7  *  # You may obtain a copy of the License at
8  *  #
9  *  #      http://www.apache.org/licenses/LICENSE-2.0
10  *  #
11  *  # Unless required by applicable law or agreed to in writing, software
12  *  # distributed under the License is distributed on an "AS IS" BASIS,
13  *  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  # See the License for the specific language governing permissions and
15  *  # limitations under the License.
16  *  # ============LICENSE_END=========================================================
17  */
18
19 import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
20 import { compSpecsService } from '../services/comp-specs-service.service';
21 import { Table } from 'primeng/table';
22 import { MessageService } from 'primeng/api';
23 import { Ng4LoadingSpinnerService } from 'ng4-loading-spinner';
24 import { DatePipe } from '@angular/common';
25 import { trigger, state, transition, style, animate } from '@angular/animations';
26 import { ActivatedRoute } from '@angular/router';
27 import { DownloadService } from '../services/download.service';
28
29 @Component({
30   selector: 'app-comp-specs',
31   templateUrl: './comp-specs.component.html',
32   styleUrls: ['./comp-specs.component.css'],
33   animations: [
34     trigger('rowExpansionTrigger', [
35       state('void', style({
36         transform: 'translateX(-10%)',
37         opacity: 0
38       })),
39       state('active', style({
40         transform: 'translateX(0)',
41         opacity: 1
42       })),
43       transition('* <=> *', animate('400ms cubic-bezier(0.86, 0, 0.07, 1)'))
44     ])
45   ],
46   providers: [DatePipe]
47 })
48 export class CompSpecsComponent implements OnInit {
49   @ViewChild(Table, { static: false }) dt: Table;
50
51   csElements: any[] = [];
52  
53   cols: any[] = [
54     { field: 'instanceName', header: 'Instance Name' },
55     { field: 'release', header: 'Release', width: '15%' },
56     { field: 'type', header: 'Type', width: '15%' },
57     { field: 'policy', header: 'Policy', width: '15%' },
58     { field: 'status', header: 'Status', width: '15%' }
59   ];
60
61   columns: any[];
62   loadTable: boolean;
63   filteredRows: any;
64   summaryRows: any;
65   downloadItems: { label: string; command: () => void; }[];
66
67   msInstanceId: string;
68   msInstanceName: any;
69   msInstanceRelease: any;
70
71   constructor(private csApis: compSpecsService, private messageService: MessageService, 
72     private spinnerService: Ng4LoadingSpinnerService, private datePipe: DatePipe, 
73     private route: ActivatedRoute, private downloadService: DownloadService) { }
74
75   //create table of comp specs
76   ngOnInit() {
77     this.loadTable = false;
78
79     this.route.queryParams.subscribe((params) => {
80       this.msInstanceId = params['instanceId'];
81     });
82
83     this.getAllCs()
84   }
85
86   getAllCs() {
87     
88     this.csElements = [];
89
90     this.csApis.getAllCompSpecs(this.msInstanceId)
91       .subscribe((data: any[]) => {
92         this.fillTable(data)
93       })
94
95     this.columns = this.cols.map(col => ({ title: col.header, dataKey: col.field }));
96   }
97
98   //filter table
99   onTableFiltered(values) {
100     if (values) { this.filteredRows = values; }
101     else { this.filteredRows = this.summaryRows; }
102   }
103
104   /* * * * Export ms instance table to excel or csv * * * */
105   exportTable(exportTo) {
106     let downloadElements: any[] = []
107
108     //labels array not handled well by excel download so converted them to a single string
109     for (let row of this.filteredRows) {
110       let labels;
111       let notes;
112       if (exportTo === "excel") {
113         if (row.metadata.labels !== undefined) {
114           labels = row.metadata.labels.join(",")
115         }
116       } else {
117         labels = row.metadata.labels
118       }
119
120       if (row.metadata.notes !== null && row.metadata.notes !== undefined && row.metadata.notes !== '') {
121         notes = encodeURI(row.metadata.notes).replace(/%20/g, " ").replace(/%0A/g, "\\n")
122       }
123
124       downloadElements.push({
125         Instance_Name: row.instanceName,
126         Release: row.release,
127         Type: row.type,
128         Status: row.status,
129         Created_By: row.metadata.createdBy,
130         Created_On: row.metadata.createdOn,
131         Updated_By: row.metadata.updatedBy,
132         Updated_On: row.metadata.updatedOn,
133         Notes: notes,
134         Labels: labels
135       })
136     }
137
138     let arrHeader = []
139
140     if (exportTo === "csv") {
141       arrHeader = [
142         "Instance_Name",
143         "Release",
144         "Type",
145         "Status",
146         "Created_By",
147         "Created_On",
148         "Updated_By",
149         "Updated_On",
150         "Notes",
151         "Labels"
152       ];
153     }
154
155     this.downloadService.exportTableData(exportTo, downloadElements, arrHeader)
156   }
157
158   //fill object with microservice data, to be used to fill table. 
159   //checks if fields are empty and if they are, store 'N/A' as the values
160   fillTable(data) {
161     for (let elem of data) {
162       let policy = '';
163       if(elem.policyJson){policy = "Included"}
164
165       let tempCsElement: any = {
166         id: elem.id,
167         instanceName: elem.msInstanceInfo.name,
168         release: elem.msInstanceInfo.release,
169         type: elem.type,
170         policy: policy,
171         status: elem.status,
172         specContent: elem.specContent,
173         policyJson: elem.policyJson,
174         metadata: {
175           createdBy: elem.metadata.createdBy,
176           createdOn: this.datePipe.transform(elem.metadata.createdOn, 'MM-dd-yyyy HH:mm'),
177           updatedBy: elem.metadata.updatedBy,
178           updatedOn: this.datePipe.transform(elem.metadata.updatedOn, 'MM-dd-yyyy HH:mm'),
179           notes: elem.metadata.notes,
180           labels: elem.metadata.labels
181         }
182       }
183       this.csElements.unshift(tempCsElement)
184     }
185     this.msInstanceName = this.csElements[0]['instanceName']
186     this.msInstanceRelease = this.csElements[0]['release']
187     this.filteredRows = this.csElements
188     this.loadTable = true;
189     this.spinnerService.hide();
190   }
191
192   showViewCs: boolean = false;
193   specContentToView: string;
194   showViewCsDialog(data) {
195     this.showViewCs = true;
196     this.specContentToView = data.specContent;
197   }
198   
199   showViewPolicy: boolean = false;
200   policyJsonToView: string;
201   showViewPolicyDialog(data) {
202     this.showViewPolicy = true;
203     this.policyJsonToView = data.policyJson;
204   }
205
206   /* * * * Download single spec file or policy * * * */
207   download(content, contentType) {
208     let fileName = `${this.msInstanceName}_${this.msInstanceRelease}_${contentType}`
209     this.downloadService.downloadJSON(content, fileName)
210   }
211 }