b4f2edbd0a1bf9eb62ced26c874f951859b5afdb
[usecase-ui.git] /
1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {SlicingTaskServices} from '.././../../../../../core/services/slicingTaskServices';
3 import {BUSINESS_STATUS} from '../../../../../../../constants/constants';
4 import { NzModalService } from 'ng-zorro-antd';
5 import { SlicingBusinessModelComponent } from '../slicing-business-model/slicing-business-model.component';
6 @Component({
7     selector: 'app-slicing-business-table',
8     templateUrl: './slicing-business-table.component.html',
9     styleUrls: ['./slicing-business-table.component.less']
10 })
11 export class SlicingBusinessTableComponent implements OnInit {
12
13     constructor(
14         private myhttp: SlicingTaskServices,
15         private modalService: NzModalService
16         ) {
17     }
18
19     ngOnInit() {
20         this.getBusinessList()
21     }
22     ngOnDestroy() {
23         this.progressingTimer.forEach((item) => {
24             clearInterval(item.timer);
25         })
26         this.progressingTimer = [];
27     }
28     selectedValue:string = BUSINESS_STATUS[0];
29     listOfData: any[] = [];
30     pageIndex: number = 1;
31     pageSize: number = 10;
32     total: number = 0;
33     loading = false;
34     isSelect: boolean = false;
35     statusOptions: any[] = BUSINESS_STATUS;
36     progressingTimer :any[] = [];
37     terminateStart :boolean = false;
38     @ViewChild('notification') notification1: any;
39
40     getBusinessList (): void{
41         this.loading = true;
42         this.isSelect = false;
43         let paramsObj = {
44             pageNo: this.pageIndex,
45             pageSize: this.pageSize
46         };
47         if(this.selectedValue !== BUSINESS_STATUS[0]){
48             paramsObj["businessStatus"] = this.selectedValue;
49             this.isSelect = true;
50         }
51         this.myhttp.getSlicingBusinessList(paramsObj,this.isSelect).subscribe (res => {
52             const { result_header: { result_code }, result_body: { slicing_business_list,record_number } } = res;
53             if (+result_code === 200) {
54                 this.total = record_number;
55                 this.loading = false;
56                 this.listOfData = slicing_business_list.map((item)=>{
57                     if(item.last_operation_progress < 100){
58                         let updata = (prodata) => {
59                             item.last_operation_progress = prodata.operation_progress || item.last_operation_progress;
60                         };
61                         let obj = {
62                             serviceId: item.service_instance_id
63                         };
64                         this.queryProgress(obj, updata).then((res) => {
65                             item.last_operation_progress = 100;
66                         })
67                     }
68                     return item
69                 });
70             }
71         })
72     }
73     getListOfProcessingStatus(){
74         this.pageIndex = 1;
75         this.pageSize = 10;
76         this.progressingTimer.forEach((item) => {
77             clearInterval(item.timer);
78         });
79         this.progressingTimer = [];
80         this.getBusinessList();
81     }
82     searchData(reset: boolean = false) {
83         this.progressingTimer.forEach((item) => {
84             clearInterval(item.timer);
85         });
86         this.progressingTimer = [];
87         this.getBusinessList();
88     }
89     switchChange(slicing,i){
90         this.modalService.confirm({
91             nzTitle: '<i>Do you Want to'+(slicing.orchestration_status === 'activated'?'deactivated':'activated')+ 'slicing business?</i>',
92             nzContent: '<b>Name:'+slicing.service_instance_name+'</b>',
93             nzOnOk: () => {
94                 this.notification1.notificationStart('slicing business', slicing.orchestration_status === 'activated'?'deactivate':'activated', slicing.service_instance_id);
95                 let paramsObj = {
96                     serviceId:slicing.service_instance_id
97                 };
98                 if(slicing.orchestration_status === 'activated'){
99                     this.changeActivate(paramsObj,false,slicing,"deactivate","deactivated",i)
100                 }else {
101                     this.changeActivate(paramsObj,true,slicing,"activate","activated",i);
102                 }
103             },
104             nzCancelText: 'No',
105             nzOnCancel: () => {
106                 let singleSlicing = Object.assign({},this.listOfData[i]);
107                 this.listOfData[i] = singleSlicing;
108                 this.listOfData = [...this.listOfData];
109             }
110         });
111     }
112     changeActivate(paramsObj,isActivate,slicing,activateValue,finished,index){
113         this.myhttp.changeActivateSlicingService(paramsObj,isActivate).subscribe (res => {
114             const { result_header: { result_code, result_message }, result_body: { operation_id } } = res;
115             if (+result_code === 200) {
116                 slicing.last_operation_progress = 0;
117                 slicing.orchestration_status = activateValue;
118                 console.log(operation_id,"operation_id");
119                 let obj = {
120                     serviceId: slicing.service_instance_id
121                 };
122                 let updata = (prodata) => {
123                     slicing.last_operation_progress = prodata.progress || 0;
124                     slicing.orchestration_status = prodata.operation_type || activateValue;
125                 };
126                 this.queryProgress(obj, updata).then(() => {
127                     slicing.last_operation_progress = 100;
128                     slicing.orchestration_status = finished;
129                     this.notification1.notificationSuccess('slicing business', finished, slicing.service_instance_id);
130                     this.getBusinessList();
131                 })
132             }else {
133                 let singleSlicing = Object.assign({},this.listOfData[index]);
134                 this.listOfData[index] = singleSlicing;
135                 this.listOfData = [...this.listOfData];
136                 this.notification1.notificationFailed('slicing business', finished, slicing.service_instance_id);
137                 console.error(result_message);
138             }
139         },(err) => {
140             let singleSlicing = Object.assign({},this.listOfData[index]);
141             this.listOfData[index] = singleSlicing;
142             this.listOfData = [...this.listOfData];
143             this.notification1.notificationFailed('slicing business', finished, slicing.service_instance_id);
144             console.error(err);
145         })
146     }
147     terminate(slicing){
148         this.modalService.confirm({
149             nzTitle: 'Do you Want to Terminate slicing business?',
150             nzContent: '<b>Name:&nbsp;</b>'+slicing.service_instance_name,
151             nzOnOk: () => {
152                 this.notification1.notificationStart('slicing business', 'terminate', slicing.service_instance_id);
153                 let paramsObj = {
154                     serviceId:slicing.service_instance_id
155                 };
156                 this.terminateStart = true;
157                 this.myhttp.terminateSlicingService(paramsObj).subscribe (res => {
158                     const { result_header: { result_code, result_message }, result_body: { operation_id } } = res;
159                     if (+result_code === 200) {
160                         slicing.last_operation_progress = 0;
161                         slicing.orchestration_status = 'delete';
162                         console.log(operation_id,"operation_id");
163                         let obj = {
164                             serviceId: slicing.service_instance_id
165                         };
166                         let updata = (prodata) => {
167                             slicing.last_operation_progress = prodata.progress || 0;
168                             slicing.orchestration_status = prodata.operation_type || "delete";
169                         };
170                         this.queryProgress(obj, updata).then(() => {
171                             slicing.last_operation_progress = 100;
172                             slicing.orchestration_status = "delete";
173                             this.notification1.notificationSuccess('slicing business', 'terminate', slicing.service_instance_id);
174                             this.terminateStart = false;
175                             this.getBusinessList();
176                         })
177                     }else {
178                         this.notification1.notificationFailed('slicing business', 'terminate', slicing.service_instance_id);
179                         this.terminateStart = false;
180                         console.error(result_message)
181                     }
182                 },(err) => {
183                     this.notification1.notificationFailed('slicing business', 'terminate', slicing.service_instance_id);
184                     this.terminateStart = false;
185                     console.error(err)
186                 })
187             },
188             nzCancelText: 'No',
189             nzOnCancel: () => {
190                 console.info('Cancel termination')
191             }
192         });
193     }
194     showdetail(data) {
195         const BusinessModal = this.modalService.create({
196             nzTitle:"Detail",
197             nzContent: SlicingBusinessModelComponent,
198             nzWidth:"70%",
199             nzOkText: null,
200             nzCancelText: null,
201             nzComponentParams:{
202                 businessId:data.service_instance_id
203             }
204         })
205     }
206     queryProgress(obj, callback) {
207         return new Promise( res => {
208             let requery = () => {
209                 this.myhttp.getSlicingBusinessProgress(obj)
210                     .subscribe((data) => {
211                         if (data.result_body.operation_progress < 100) {
212                             callback(data.result_body);
213                             let progressSetTimeOut = setTimeout(() => {
214                                 requery();
215                             },5000);
216                             this.progressingTimer.push({
217                                 id:obj.serviceId,
218                                 timer:progressSetTimeOut
219                             })
220                         } else {
221                             this.progressingTimer.forEach((item) => {
222                                 if(item.serviceId === obj.serviceId){
223                                     clearInterval(item.timer);
224                                 }
225                             });
226                             res(data.result_body);
227                         }
228                     })
229             };
230             requery();
231         })
232     }
233 }