added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / modules / schedule-test-modal / schedule-test-modal.component.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Component, Inject, OnInit } from '@angular/core';\r
18 import { MatDialog, MatDialogRef, MAT_DIALOG_DATA, MatSnackBar } from '@angular/material';\r
19 import { TestInstanceService } from '../../services/test-instance.service';\r
20 import { SchedulingService } from '../../services/scheduling.service';\r
21 import { AlertSnackbarComponent } from '../alert-snackbar/alert-snackbar.component';\r
22 import { AlertModalComponent } from '../alert-modal/alert-modal.component';\r
23 \r
24 \r
25 @Component({\r
26   selector: 'app-schedule-test-modal',\r
27   templateUrl: './schedule-test-modal.component.pug',\r
28   styleUrls: ['./schedule-test-modal.component.scss']\r
29 })\r
30 export class ScheduleTestModalComponent implements OnInit {\r
31 \r
32   public data;\r
33   public test_instances;\r
34   public selectedTestInstance;\r
35   public schedule;\r
36   public search;\r
37   public timeUnit;\r
38   public timeToRun;\r
39   public numUnit;\r
40   public startDate;\r
41   public endDate;\r
42   public frequency = false;\r
43   public isSelected = false;\r
44   public scheduledJobs;\r
45   public loadingJobs;\r
46 \r
47   constructor(\r
48     private schedulingService: SchedulingService,\r
49     private testInstanceService: TestInstanceService,\r
50     public dialogRef: MatDialogRef<ScheduleTestModalComponent>,\r
51     private snack: MatSnackBar,\r
52     private dialog: MatDialog,\r
53     @Inject(MAT_DIALOG_DATA) public input_data\r
54   ) {\r
55 \r
56   }\r
57 \r
58   onNoClick(): void {\r
59     this.dialogRef.close();\r
60   }\r
61 \r
62   ngOnInit() {\r
63     this.timeUnit = 60;\r
64     this.numUnit = 0;\r
65     this.search = {};\r
66     this.selectedTestInstance = {};\r
67     this.startDate = null;\r
68     this.timeToRun = null;\r
69     this.endDate = null;\r
70     //this.search.testInstanceName = ""; \r
71     //this.test_instances = [];\r
72     this.schedule = {};\r
73     this.schedule.testInstanceExecFreqInSeconds = '';\r
74     this.scheduledJobs = [];\r
75     this.loadingJobs = true;\r
76 \r
77     //console.log(this.test_instances);\r
78     this.testInstanceService.get(this.input_data.id).subscribe(\r
79       result => {\r
80         this.selectedTestInstance = result;\r
81       }\r
82     );\r
83 \r
84     this.schedulingService.find({$limit: -1, testInstanceId: this.input_data.id}).subscribe(\r
85       result => {\r
86         for (let i = 0; i < result['length']; i++) {\r
87           result[i].data.testSchedule._testInstanceStartDate = new Date(result[i].data.testSchedule._testInstanceStartDate).toLocaleString();\r
88           if (result[i].data.testSchedule._testInstanceEndDate) {\r
89             result[i].data.testSchedule._testInstanceEndDate = new Date(result[i].data.testSchedule._testInstanceEndDate).toLocaleString();\r
90           }\r
91           this.scheduledJobs.push(result[i]);\r
92 \r
93         }\r
94         this.loadingJobs = false;\r
95       }\r
96     );\r
97   }\r
98 \r
99   convertDate(date, time = ''): Date {\r
100     let nDate = new Date(date + '');\r
101     return new Date(nDate.getMonth() + 1 + '/' + nDate.getDate() + '/' + nDate.getFullYear() + ' ' + time);\r
102   }\r
103 \r
104   createSchedule() {\r
105     this.convertDate(this.startDate, this.timeToRun);\r
106 \r
107     if (!this.selectedTestInstance || !this.startDate || !this.timeToRun) {\r
108       this.dialog.open(AlertModalComponent, {\r
109         width: '450px',\r
110         data: {\r
111           type: 'Alert',\r
112           message: 'Select start date/time before you create schedule!'\r
113         }\r
114       });\r
115       return;\r
116     }\r
117     if (this.frequency) {\r
118       this.schedule = {\r
119         testInstanceId: this.selectedTestInstance._id,\r
120         testInstanceStartDate: this.convertDate(this.startDate, this.timeToRun).toISOString(),\r
121         testInstanceExecFreqInSeconds: this.numUnit * this.timeUnit,\r
122         async: false,\r
123         asyncTopic: ''\r
124       };\r
125       \r
126 \r
127       if(this.endDate){\r
128         this.schedule.testInstanceEndDate = this.convertDate(this.endDate).toISOString();\r
129       }\r
130     } else {\r
131       this.schedule = {\r
132         testInstanceId: this.selectedTestInstance._id,\r
133         testInstanceStartDate: this.convertDate(this.startDate, this.timeToRun).toISOString(),\r
134         async: false,\r
135         asyncTopic: ''\r
136       };\r
137       //console.log(this.schedule);\r
138       \r
139     }\r
140 \r
141     this.schedulingService.create(this.schedule).subscribe((result) => {\r
142       this.snack.openFromComponent(AlertSnackbarComponent, {\r
143         duration: 1500,\r
144         data: {\r
145           message: 'Schedule Created!'\r
146         }\r
147       });\r
148       this.ngOnInit();\r
149     }, err => {\r
150       this.dialog.open(AlertModalComponent, {\r
151         data: {\r
152           type: "alert", \r
153           message: err.message\r
154         }\r
155       })\r
156     })\r
157     // console.log(this.schedule);\r
158   }\r
159 \r
160   deleteJob(job) {\r
161     var deleteJob = this.dialog.open(AlertModalComponent, {\r
162       width: '250px',\r
163       data: {\r
164         type: 'confirmation',\r
165         message: 'Are you sure you want to delete this schedule?'\r
166       }\r
167     });\r
168 \r
169     deleteJob.afterClosed().subscribe(\r
170       result => {\r
171         if (result) {\r
172           this.schedulingService.delete(job._id).subscribe(\r
173             result => {\r
174               this.ngOnInit();\r
175             }\r
176           );\r
177         }\r
178       }\r
179     );\r
180   }\r
181   // this.testInstanceId = testInstanceId;\r
182   // this.testInstanceStartDate = testInstanceStartDate;\r
183   // this.testInstanceExecFreqInSeconds = testInstanceExecFreqInSeconds;\r
184   // this.testInstanceEndDate = testInstanceEndDate;\r
185   // this.async = async;\r
186   // this.asyncTopic = asyncTopic;\r
187   // this.executorId = executorId;\r
188 \r
189 \r
190 }\r