834f0c92fb04798d78e03141ee4a97cbc01c4721
[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")
100                 }else {
101                     this.changeActivate(paramsObj,true,slicing,"activate","activated");
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){
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                 this.notification1.notificationFailed('slicing business', finished, slicing.service_instance_id);
134                 console.error(result_message);
135             }
136         })
137     }
138     terminate(slicing){
139         this.modalService.confirm({
140             nzTitle: 'Do you Want to Terminate slicing business?',
141             nzContent: '<b>Name:&nbsp;</b>'+slicing.service_instance_name,
142             nzOnOk: () => {
143                 this.notification1.notificationStart('slicing business', 'terminate', slicing.service_instance_id);
144                 let paramsObj = {
145                     serviceId:slicing.service_instance_id
146                 };
147                 this.terminateStart = true;
148                 this.myhttp.terminateSlicingService(paramsObj).subscribe (res => {
149                     const { result_header: { result_code, result_message }, result_body: { operation_id } } = res;
150                     if (+result_code === 200) {
151                         slicing.last_operation_progress = 0;
152                         slicing.orchestration_status = 'delete';
153                         console.log(operation_id,"operation_id");
154                         let obj = {
155                             serviceId: slicing.service_instance_id
156                         };
157                         let updata = (prodata) => {
158                             slicing.last_operation_progress = prodata.progress || 0;
159                             slicing.orchestration_status = prodata.operation_type || "delete";
160                         };
161                         this.queryProgress(obj, updata).then(() => {
162                             slicing.last_operation_progress = 100;
163                             slicing.orchestration_status = "delete";
164                             this.notification1.notificationSuccess('slicing business', 'terminate', slicing.service_instance_id);
165                             this.terminateStart = false;
166                             this.getBusinessList();
167                         })
168                     }else {
169                         this.notification1.notificationFailed('slicing business', 'terminate', slicing.service_instance_id);
170                         this.terminateStart = false;
171                         console.error(result_message)
172                     }
173                 })
174             },
175             nzCancelText: 'No',
176             nzOnCancel: () => {
177                 console.info('Cancel termination')
178             }
179         });
180     }
181     showdetail(data) {
182         const BusinessModal = this.modalService.create({
183             nzTitle:"Detail",
184             nzContent: SlicingBusinessModelComponent,
185             nzWidth:"70%",
186             nzOkText: null,
187             nzCancelText: null,
188             nzComponentParams:{
189                 businessId:data.service_instance_id
190             }
191         })
192     }
193     queryProgress(obj, callback) {
194         return new Promise( res => {
195             let requery = () => {
196                 this.myhttp.getSlicingBusinessProgress(obj)
197                     .subscribe((data) => {
198                         if (data.result_body.operation_progress < 100) {
199                             callback(data.result_body);
200                             let progressSetTimeOut = setTimeout(() => {
201                                 requery();
202                             },5000);
203                             this.progressingTimer.push({
204                                 id:obj.serviceId,
205                                 timer:progressSetTimeOut
206                             })
207                         } else {
208                             this.progressingTimer.forEach((item) => {
209                                 if(item.serviceId === obj.serviceId){
210                                     clearInterval(item.timer);
211                                 }
212                             });
213                             res(data.result_body);
214                         }
215                     })
216             };
217             requery();
218         })
219     }
220 }