Remove mso-oof-adapter from SO
[so.git] / so-monitoring / so-monitoring-ui / src / main / frontend / src / app / home / home.component.ts
1 /**
2 ============LICENSE_START=======================================================
3  Copyright (C) 2018 Ericsson. 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
17 SPDX-License-Identifier: Apache-2.0
18 ============LICENSE_END=========================================================
19
20 @authors: ronan.kenny@ericsson.com, waqas.ikram@ericsson.com, andrei.barcovschi@ericsson.com
21 */
22
23 import { Component, OnInit, ViewChild, ElementRef, Input, ViewEncapsulation } from '@angular/core';
24 import { DataService } from '../data.service';
25 import { ActivatedRoute, Router } from "@angular/router";
26 import { BpmnInfraRequest } from '../model/bpmnInfraRequest.model';
27 import { ProcessInstanceId } from '../model/processInstanceId.model';
28 import { ToastrNotificationService } from '../toastr-notification-service.service';
29 import { MatSelectModule } from '@angular/material/select';
30 import { FormsModule, FormControl } from '@angular/forms';
31 import { SearchData } from '../model/searchData.model';
32 import { MatDatepickerModule } from '@angular/material/datepicker';
33 import { SearchRequest } from '../model/SearchRequest.model';
34 import { NgxSpinnerService } from 'ngx-spinner';
35 import { MatFormFieldModule, MatInputModule, MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
36 import { Constants } from './home.constant';
37
38 @Component({
39   selector: 'app-home',
40   templateUrl: './home.component.html',
41   styleUrls: ['./home.component.scss'],
42   encapsulation: ViewEncapsulation.None
43 })
44
45 export class HomeComponent {
46
47   totalVal = 0;
48   completeVal = 0;
49   inProgressVal = 0;
50   failedVal = 0;
51   pendingVal = 0;
52   unlockedVal = 0;
53   percentageComplete = 0;
54   percentageFailed = 0;
55   percentageInProg = 0;
56   percentagePending = 0;
57   percentageUnlocked = 0;
58
59   options = Constants.OPTIONS;
60   statusOptions = Constants.STATUS_OPTIONS;
61   hourOptions = Constants.HOUR_OPTIONS;
62   minuteOptions = Constants.MINUTE_OPTIONS;
63   displayedColumns = Constants.DISPLAYED_COLUMNS;
64   pageSizeOptions = Constants.DEFAULT_PAGE_SIZE_OPTIONS;
65
66   searchData: SearchData;
67   startingDate: Date;
68   processData: MatTableDataSource<BpmnInfraRequest>;
69
70   @ViewChild(MatPaginator) paginator: MatPaginator;
71   @ViewChild(MatSort) sort: MatSort;
72
73   constructor(private route: ActivatedRoute, private data: DataService,
74     private router: Router, private popup: ToastrNotificationService,
75     private spinner: NgxSpinnerService) {
76     this.searchData = new SearchData();
77   }
78
79   makeCall() {
80     this.spinner.show();
81
82     var search = this.searchData.getSearchRequest().subscribe((result: SearchRequest) => {
83
84       this.data.getBpmnInfraRequest(result.getFilters(), result.getStartTimeInMilliseconds(), result.getEndTimeInMilliseconds())
85         .subscribe((data: BpmnInfraRequest[]) => {
86           this.spinner.hide();
87           var processData: BpmnInfraRequest[] = data;
88           this.processData = new MatTableDataSource<BpmnInfraRequest>(processData);
89           this.processData.sort = this.sort;
90           this.processData.paginator = this.paginator;
91           this.processData.paginator.firstPage();
92
93           this.popup.info("Number of records found: " + data.length)
94
95           // Calculate Statistics for Service Statistics tab
96           this.completeVal = processData.filter(i => i.requestStatus === "COMPLETE").length;
97           this.inProgressVal = processData.filter(i => i.requestStatus === "IN_PROGRESS").length;
98           this.failedVal = processData.filter(i => i.requestStatus === "FAILED").length;
99           this.pendingVal = processData.filter(i => i.requestStatus === "PENDING").length;
100           this.unlockedVal = processData.filter(i => i.requestStatus === "UNLOCKED").length;
101           this.totalVal = processData.length;
102
103           // Calculate percentages to 2 decimal places and compare to 0 to avoid NaN error
104           if (this.totalVal != 0) {
105             this.percentageComplete = Math.round(((this.completeVal / this.totalVal) * 100) * 100) / 100;
106             this.percentageFailed = Math.round(((this.failedVal / this.totalVal) * 100) * 100) / 100;
107             this.percentageInProg = Math.round(((this.inProgressVal / this.totalVal) * 100) * 100) / 100;
108             this.percentagePending = Math.round(((this.pendingVal / this.totalVal) * 100) * 100) / 100;
109             this.percentageUnlocked = Math.round(((this.unlockedVal / this.totalVal) * 100) * 100) / 100;
110           }
111           console.log("COMPLETE: " + this.completeVal);
112           console.log("FAILED: " + this.failedVal);
113         }, error => {
114           console.log(error);
115           this.popup.error("Unable to perform search Error code:" + error.status);
116           this.spinner.hide();
117         });
118     }, error => {
119       console.log("Data validation error " + error);
120       this.popup.error(error);
121       this.spinner.hide();
122     });
123   }
124
125   getProcessInstanceId(requestId: string) {
126     this.spinner.show();
127
128     var response = this.data.getProcessInstanceId(requestId).subscribe((data) => {
129       if (data.status == 200) {
130         this.spinner.hide();
131         var processInstanceId = (data.body as ProcessInstanceId).processInstanceId;
132         this.router.navigate(['/details/' + processInstanceId]);
133       } else {
134         this.popup.error('No process instance id found: ' + requestId);
135         this.spinner.hide();
136         console.log('No process instance id found: ' + requestId);
137       }
138     });
139   }
140 }