a524b8bb46a38360055c656e5d1a2e5535811fc5
[usecase-ui.git] /
1 import {Component, OnInit} 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     }
27     selectedValue:string = BUSINESS_STATUS[0];
28     listOfData: any[] = [];
29     pageIndex: number = 1;
30     pageSize: number = 10;
31     total: number = 0;
32     loading = false;
33     isSelect: boolean = false;
34     statusOptions: any[] = BUSINESS_STATUS;
35     progressingTimer :any[] = [];
36
37
38     getBusinessList (): void{
39         this.loading = true;
40         this.isSelect = false;
41         let paramsObj = {
42             pageNo: this.pageIndex,
43             pageSize: this.pageSize
44         };
45         if(this.selectedValue !== BUSINESS_STATUS[0]){
46             paramsObj["businessStatus"] = this.selectedValue;
47             this.isSelect = true;
48         }
49         this.myhttp.getSlicingBusinessList(paramsObj,this.isSelect).subscribe (res => {
50             const { result_header: { result_code }, result_body: { slicing_business_list,record_number } } = res;
51             if (+result_code === 200) {
52                 this.total = record_number;
53                 this.loading = false;
54                 this.listOfData = slicing_business_list.map((item)=>{
55                     if(item.last_operation_progress < 100){
56                         let updata = (prodata) => {
57                             item.last_operation_progress = prodata.operation_progress || item.last_operation_progress;
58                         };
59                         let obj = {
60                             serviceId: item.service_instance_id
61                         };
62                         this.queryProgress(obj, updata).then((res) => {
63                             item.last_operation_progress = 100;
64                             item.orchestration_status = item.last_operation_type === 'activate'?'activated':item.last_operation_type === 'deactivated'?'deactivated':'terminated';
65                         })
66                     }
67                     return item
68                 });
69             }
70         })
71     }
72     getListOfProcessingStatus(){
73         this.pageIndex = 1;
74         this.pageSize = 10;
75         this.getBusinessList();
76     }
77     searchData(reset: boolean = false) {
78         this.getBusinessList();
79     }
80     switchChange(slicing,i){
81         this.modalService.confirm({
82             nzTitle: '<i>Do you Want to'+(slicing.orchestration_status === 'activated'?'deactivated':'activated')+ 'slicing business?</i>',
83             nzContent: '<b>service_instance_id:'+slicing.service_instance_id+'</b>',
84             nzOnOk: () => {
85                 let paramsObj = {
86                     serviceId:slicing.service_instance_id
87                 };
88                 if(slicing.orchestration_status === 'activated'){
89                     this.changeActivate(paramsObj,false,slicing,"deactivate","deactivated")
90                 }else {
91                     this.changeActivate(paramsObj,true,slicing,"activate","activated");
92                 }
93             },
94             nzCancelText: 'No',
95             nzOnCancel: () => {
96                 let singleSlicing = Object.assign({},this.listOfData[i]);
97                 this.listOfData[i] = singleSlicing;
98                 this.listOfData = [...this.listOfData];
99             }
100         });
101     }
102     changeActivate(paramsObj,isActivate,slicing,activateValue,finished){
103         this.myhttp.changeActivateSlicingService(paramsObj,isActivate).subscribe (res => {
104             const { result_header: { result_code, result_message }, result_body: { operation_id } } = res;
105             if (+result_code === 200) {
106                 slicing.last_operation_progress = 0;
107                 slicing.orchestration_status = activateValue;
108                 console.log(operation_id,"operation_id");
109                 let obj = {
110                     serviceId: slicing.service_instance_id
111                 }
112                 let updata = (prodata) => {
113                     slicing.last_operation_progress = prodata.progress;
114                     slicing.orchestration_status = prodata.operation_type;
115                     this.queryProgress(obj, updata).then(() => {
116                         slicing.last_operation_progress = 100;
117                         slicing.orchestration_status = finished;
118                     })
119                 }
120             }else {
121                 console.error(result_message)
122             }
123         })
124     }
125     terminate(slicing){
126         this.modalService.confirm({
127             nzTitle: 'Do you Want to Terminate slicing business?',
128             nzContent: '<b>service_instance_id:&nbsp;</b>'+slicing.service_instance_id,
129             nzOnOk: () => {
130                 let paramsObj = {
131                     serviceId:slicing.service_instance_id
132                 };
133                 this.myhttp.terminateSlicingService(paramsObj).subscribe (res => {
134                     const { result_header: { result_code, result_message }, result_body: { operation_id } } = res;
135                     if (+result_code === 200) {
136                         slicing.last_operation_progress = 0;
137                         slicing.orchestration_status = 'deactivate';
138                         console.log(operation_id,"operation_id");
139                         let obj = {
140                             serviceId: slicing.service_instance_id
141                         };
142                         let updata = (prodata) => {
143                             slicing.last_operation_progress = prodata.progress;
144                             slicing.orchestration_status = prodata.operation_type;
145                             this.queryProgress(obj, updata).then(() => {
146                                 slicing.last_operation_progress = 100;
147                                 slicing.orchestration_status = "terminated";
148                             })
149                         }
150                     }else {
151                         console.error(result_message)
152                     }
153                 })
154             },
155             nzCancelText: 'No',
156             nzOnCancel: () => {
157                 console.info('Cancel termination')
158             }
159         });
160     }
161     showdetail(data) {
162         const BusinessModal = this.modalService.create({
163             nzTitle:"Detail",
164             nzContent: SlicingBusinessModelComponent,
165             nzWidth:"70%",
166             nzOkText: null,
167             nzCancelText: null,
168             nzComponentParams:{
169                 businessId:data.service_instance_id
170             }
171         })
172     }
173     queryProgress(obj, callback) {
174         return new Promise( res => {
175             let requery = () => {
176                 this.myhttp.getSlicingBusinessProgress(obj)
177                     .subscribe((data) => {
178                         if (data.result_body.operation_progress < 100) {
179                             callback(data.result_body);
180                             let progressSetTimeOut = setTimeout(() => {
181                                 requery();
182                             },5000);
183                             this.progressingTimer.push({
184                                 id:obj.serviceId,
185                                 timer:progressSetTimeOut
186                             })
187                         } else {
188                             this.progressingTimer.forEach((item) => {
189                                 if(item.serviceId === obj.serviceId){
190                                     clearInterval(item.timer);
191                                 }
192                             });
193                             res(data.result_body);
194                         }
195                     })
196             };
197             requery();
198         })
199     }
200 }