Merge "Add common openstack opertation scenarios: network"
[yardstick.git] / gui / app / scripts / controllers / projectDetail.controller.js
1 'use strict';
2
3 angular.module('yardStickGui2App')
4     .controller('ProjectDetailController', ['$scope', '$state', '$stateParams', 'mainFactory', 'Upload', 'toaster', 'ngDialog', '$localStorage', '$loading', '$interval',
5         function($scope, $state, $stateParams, mainFactory, Upload, toaster, ngDialog, $localStorage, $loading, $interval) {
6
7
8             init();
9             // $scope.taskListDisplay = [];
10             $scope.blisterPackTemplates = [{ id: 1, name: "Test Case" }, { id: 2, name: "Test Suite" }]
11             $scope.selectType = null;
12             $scope.ifHasEnv = false;
13             $scope.ifHasCase = false;
14             $scope.ifHasSuite = false;
15             $scope.$on('$destroy', function() {
16                 $interval.cancel($scope.intervalCount)
17             });
18             $scope.finalTaskListDisplay = [];
19
20
21             function init() {
22
23
24                 getProjectDetail();
25
26                 $scope.openCreate = openCreate;
27                 $scope.createTask = createTask;
28                 $scope.constructTestSuit = constructTestSuit;
29                 $scope.addEnvToTask = addEnvToTask;
30                 $scope.triggerContent = triggerContent;
31                 $scope.constructTestCase = constructTestCase;
32                 $scope.getTestDeatil = getTestDeatil;
33                 $scope.confirmAddCaseOrSuite = confirmAddCaseOrSuite;
34                 $scope.runAtask = runAtask;
35                 $scope.gotoDetail = gotoDetail;
36                 $scope.gotoReport = gotoReport;
37                 $scope.gotoModify = gotoModify;
38                 $scope.goBack = goBack;
39                 $scope.goToExternal = goToExternal;
40
41
42             }
43
44             function getProjectDetail() {
45                 if ($scope.intervalCount != undefined) {
46                     $interval.cancel($scope.intervalCount);
47                 }
48                 $loading.start('key');
49                 $scope.taskListDisplay = [];
50                 $scope.finalTaskListDisplay = [];
51                 mainFactory.getProjectDetail().get({
52                     project_id: $stateParams.projectId
53                 }).$promise.then(function(response) {
54                     $loading.finish('key');
55                     if (response.status == 1) {
56
57                         $scope.projectData = response.result.project;
58                         if ($scope.projectData.tasks.length != 0) {
59
60
61                             for (var i = 0; i < $scope.projectData.tasks.length; i++) {
62                                 getDetailTaskForList($scope.projectData.tasks[i]);
63                             }
64                             $scope.intervalCount = $interval(function() {
65                                 getDetailForEachTask();
66                             }, 10000);
67                         } else {
68
69                             if ($scope.intervalCount != undefined) {
70                                 $interval.cancel($scope.intervalCount);
71                             }
72                         }
73                     } else {
74
75                     }
76                 }, function(error) {
77                     toaster.pop({
78                         type: 'error',
79                         title: 'fail',
80                         body: 'unknow error',
81                         timeout: 3000
82                     });
83
84                 })
85             }
86
87             // function getProjectDetailSimple() {
88             //     getDetailForEachTask();
89             // }
90
91             function openCreate() {
92                 $scope.newUUID = null;
93                 $scope.displayEnvName = null;
94                 $scope.selectEnv = null;
95                 $scope.selectCase = null;
96                 $scope.selectType = null;
97                 $scope.contentInfo = null;
98                 $scope.ifHasEnv = false;
99                 $scope.ifHasCase = false;
100                 $scope.ifHasSuite = false;
101
102                 // getEnvironmentList();
103                 $scope.selectEnv = null;
104                 ngDialog.open({
105                     template: 'views/modal/taskCreate.html',
106                     scope: $scope,
107                     className: 'ngdialog-theme-default',
108                     width: 800,
109                     showClose: true,
110                     closeByDocument: false,
111                     preCloseCallback: function(value) {
112                         getProjectDetail();
113                     },
114                 })
115             }
116
117             function createTask(name) {
118                 mainFactory.createTask().post({
119                     'action': 'create_task',
120                     'args': {
121                         'name': name,
122                         'project_id': $stateParams.projectId
123                     }
124                 }).$promise.then(function(response) {
125                     if (response.status == 1) {
126                         toaster.pop({
127                             type: 'success',
128                             title: 'create task success',
129                             body: 'you can go next step',
130                             timeout: 3000
131                         });
132                         $scope.newUUID = response.result.uuid;
133                         getEnvironmentList();
134
135                     } else {
136                         toaster.pop({
137                             type: 'error',
138                             title: 'create task wrong',
139                             body: 'you can go next step',
140                             timeout: 3000
141                         });
142                     }
143
144
145
146                 }, function(error) {
147                     toaster.pop({
148                         type: 'error',
149                         title: 'create task wrong',
150                         body: 'you can go next step',
151                         timeout: 3000
152                     });
153                 })
154             }
155
156             function getDetailTaskForList(id) {
157
158                 mainFactory.getTaskDetail().get({
159                     'taskId': id
160                 }).$promise.then(function(response) {
161
162                     if (response.status == 1) {
163                         if (response.result.task.status == -1) {
164                             response.result.task['stausWidth'] = '5%';
165                         } else if (response.result.task.status == 0) {
166                             response.result.task['stausWidth'] = '50%';
167                         } else if (response.result.task.status == 1) {
168                             response.result.task['stausWidth'] = '100%';
169                         } else if (response.result.task.status == 2) {
170                             response.result.task['stausWidth'] = 'red';
171                         }
172                         $scope.taskListDisplay.push(response.result.task);
173                         console.log($scope.taskListDisplay);
174
175                         $scope.finalTaskListDisplay = $scope.taskListDisplay;
176
177                     }
178                 }, function(error) {
179                     toaster.pop({
180                         type: 'error',
181                         title: 'fail',
182                         body: 'unknow error',
183                         timeout: 3000
184                     });
185
186                 })
187             }
188
189             function getDetailTaskForListSimple(id, index) {
190
191                 mainFactory.getTaskDetail().get({
192                     'taskId': id
193                 }).$promise.then(function(response) {
194
195                     if (response.status == 1) {
196                         if (response.result.task.status == -1) {
197
198                             $scope.finalTaskListDisplay[index].stausWidth = '5%';
199                             $scope.finalTaskListDisplay[index].status = response.result.task.status;
200                         } else if (response.result.task.status == 0) {
201
202                             $scope.finalTaskListDisplay[index].stausWidth = '50%';
203                             $scope.finalTaskListDisplay[index].status = response.result.task.status;
204                         } else if (response.result.task.status == 1) {
205
206                             $scope.finalTaskListDisplay[index].stausWidth = '100%';
207                             $scope.finalTaskListDisplay[index].status = response.result.task.status;
208                         } else if (response.result.task.status == 2) {
209
210                             $scope.finalTaskListDisplay[index].stausWidth = 'red';
211                             $scope.finalTaskListDisplay[index].status = response.result.task.status;
212                         }
213
214
215                     }
216                 }, function(error) {
217                     toaster.pop({
218                         type: 'error',
219                         title: 'fail',
220                         body: 'unknow error',
221                         timeout: 3000
222                     });
223
224                 })
225             }
226
227             function getDetailForEachTask() {
228                 for (var i = 0; i < $scope.finalTaskListDisplay.length; i++) {
229                     if ($scope.finalTaskListDisplay[i].status != 1 && $scope.finalTaskListDisplay[i].status != -1) {
230                         getDetailTaskForListSimple($scope.finalTaskListDisplay[i].uuid, i);
231                     }
232                 }
233             }
234
235             function getEnvironmentList() {
236                 mainFactory.getEnvironmentList().get().$promise.then(function(response) {
237                     $scope.environmentList = response.result.environments;
238                 }, function(error) {
239                     toaster.pop({
240                         type: 'error',
241                         title: 'fail',
242                         body: 'unknow error',
243                         timeout: 3000
244                     });
245
246                 })
247             }
248
249             function constructTestSuit(id, name) {
250                 $scope.displayEnvName = name;
251                 $scope.selectEnv = id;
252
253             }
254
255             function constructTestCase(name) {
256
257                 $scope.selectCase = name;
258                 if ($scope.selectType.name == 'Test Case') {
259                     getCaseInfo();
260                 } else {
261                     getSuiteInfo();
262                 }
263
264             }
265
266
267
268
269             function addEnvToTask() {
270                 mainFactory.taskAddEnv().put({
271                     'taskId': $scope.newUUID,
272                     'action': 'add_environment',
273                     'args': {
274                         'task_id': $scope.newUUID,
275                         'environment_id': $scope.selectEnv
276                     }
277                 }).$promise.then(function(response) {
278                     if (response.status == 1) {
279                         toaster.pop({
280                             type: 'success',
281                             title: 'add environment success',
282                             body: 'you can go next step',
283                             timeout: 3000
284                         });
285                         $scope.ifHasEnv = true;
286
287
288                     } else {
289                         toaster.pop({
290                             type: 'error',
291                             title: 'create task wrong',
292                             body: 'you can go next step',
293                             timeout: 3000
294                         });
295                     }
296
297
298
299                 }, function(error) {
300                     toaster.pop({
301                         type: 'error',
302                         title: 'create task wrong',
303                         body: 'you can go next step',
304                         timeout: 3000
305                     });
306                 })
307             }
308
309             function triggerContent(name) {
310                 $scope.selectCase = null;
311                 $scope.displayTable = true;
312
313                 $scope.selectType = name;
314                 if (name.name == 'Test Case') {
315                     getTestcaseList();
316                 } else if (name.name == 'Test Suite') {
317                     getsuiteList();
318                 }
319             }
320
321             function getTestcaseList() {
322                 mainFactory.getTestcaselist().get({
323
324                 }).$promise.then(function(response) {
325                     if (response.status == 1) {
326                         $scope.testcaselist = response.result;
327
328
329                     }
330                 }, function(error) {
331                     toaster.pop({
332                         type: 'error',
333                         title: 'fail',
334                         body: 'unknow error',
335                         timeout: 3000
336                     });
337                 })
338             }
339
340             function getsuiteList() {
341                 mainFactory.suiteList().get({
342
343                 }).$promise.then(function(response) {
344                     if (response.status == 1) {
345                         $scope.testsuitlist = response.result.testsuites;
346
347                     }
348                 }, function(error) {
349                     toaster.pop({
350                         type: 'error',
351                         title: 'fail',
352                         body: 'unknow error',
353                         timeout: 3000
354                     });
355                 })
356             }
357
358             function getTestDeatil() {
359
360
361                 if ($scope.selectType.name == 'Test Case') {
362                     getTestcaseDetail();
363                 } else {
364                     getSuiteDetail();
365                 }
366
367             }
368
369             function getCaseInfo() {
370
371
372
373                 mainFactory.getTestcaseDetail().get({
374                     'testcasename': $scope.selectCase
375
376                 }).$promise.then(function(response) {
377                     if (response.status == 1) {
378
379                         $scope.contentInfo = response.result.testcase;
380
381                     }
382                 }, function(error) {
383                     toaster.pop({
384                         type: 'error',
385                         title: 'fail',
386                         body: 'unknow error',
387                         timeout: 3000
388                     });
389                 })
390             }
391
392             function getSuiteInfo() {
393                 mainFactory.suiteDetail().get({
394                     'suiteName': $scope.selectCase.split('.')[0]
395
396                 }).$promise.then(function(response) {
397                     if (response.status == 1) {
398                         $scope.contentInfo = response.result.testsuite;
399
400                     }
401                 }, function(error) {
402                     toaster.pop({
403                         type: 'error',
404                         title: 'fail',
405                         body: 'unknow error',
406                         timeout: 3000
407                     });
408                 })
409             }
410
411
412             function getSuiteDetail() {
413                 mainFactory.suiteDetail().get({
414                     'suiteName': $scope.selectCase.split('.')[0]
415
416                 }).$promise.then(function(response) {
417                     if (response.status == 1) {
418                         $scope.displayTable = false;
419                         $scope.contentInfo = response.result.testsuite;
420
421                     }
422                 }, function(error) {
423                     toaster.pop({
424                         type: 'error',
425                         title: 'fail',
426                         body: 'unknow error',
427                         timeout: 3000
428                     });
429                 })
430             }
431
432
433             function getTestcaseDetail() {
434                 mainFactory.getTestcaseDetail().get({
435                     'testcasename': $scope.selectCase
436
437                 }).$promise.then(function(response) {
438                     if (response.status == 1) {
439
440                         $scope.displayTable = false;
441                         $scope.contentInfo = response.result.testcase;
442
443                     }
444                 }, function(error) {
445                     toaster.pop({
446                         type: 'error',
447                         title: 'fail',
448                         body: 'unknow error',
449                         timeout: 3000
450                     });
451                 })
452             }
453
454             function addCasetoTask(content) {
455                 mainFactory.taskAddEnv().put({
456                     'taskId': $scope.newUUID,
457                     'action': 'add_case',
458                     'args': {
459                         'task_id': $scope.newUUID,
460                         'case_name': $scope.selectCase,
461                         'case_content': content
462                     }
463                 }).$promise.then(function(response) {
464                     if (response.status == 1) {
465                         toaster.pop({
466                             type: 'success',
467                             title: 'add test case success',
468                             body: 'you can go next step',
469                             timeout: 3000
470                         });
471                         $scope.ifHasCase = true;
472
473
474                     } else {
475                         toaster.pop({
476                             type: 'error',
477                             title: 'create task wrong',
478                             body: 'you can go next step',
479                             timeout: 3000
480                         });
481                     }
482                 }, function(error) {
483                     toaster.pop({
484                         type: 'error',
485                         title: 'create task wrong',
486                         body: 'you can go next step',
487                         timeout: 3000
488                     });
489                 })
490             }
491
492             function addSuitetoTask(content) {
493                 mainFactory.taskAddEnv().put({
494                     'taskId': $scope.newUUID,
495                     'action': 'add_suite',
496                     'args': {
497                         'task_id': $scope.newUUID,
498                         'suite_name': $scope.selectCase.split('.')[0],
499                         'suite_content': content
500                     }
501                 }).$promise.then(function(response) {
502                     if (response.status == 1) {
503                         toaster.pop({
504                             type: 'success',
505                             title: 'add test suite success',
506                             body: 'you can go next step',
507                             timeout: 3000
508                         });
509                         $scope.ifHasSuite = true;
510
511
512                     } else {
513                         toaster.pop({
514                             type: 'error',
515                             title: 'create task wrong',
516                             body: 'wrong',
517                             timeout: 3000
518                         });
519                     }
520                 }, function(error) {
521                     toaster.pop({
522                         type: 'error',
523                         title: 'create task wrong',
524                         body: 'something wrong',
525                         timeout: 3000
526                     });
527                 })
528             }
529
530             function confirmAddCaseOrSuite(content) {
531                 if ($scope.selectType.name == "Test Case") {
532                     addCasetoTask(content);
533                 } else {
534                     addSuitetoTask(content);
535                 }
536             }
537
538             function runAtask(id) {
539                 mainFactory.taskAddEnv().put({
540                     'taskId': id,
541                     'action': 'run',
542                     'args': {
543                         'task_id': id
544                     }
545                 }).$promise.then(function(response) {
546                     if (response.status == 1) {
547                         toaster.pop({
548                             type: 'success',
549                             title: 'run a task success',
550                             body: 'you can go next step',
551                             timeout: 3000
552                         });
553                         ngDialog.close();
554                         // getProjectDetail();
555                     } else {
556
557                     }
558                 }, function(error) {
559                     toaster.pop({
560                         type: 'error',
561                         title: 'fail',
562                         body: 'unknow error',
563                         timeout: 3000
564                     });
565
566                 })
567             }
568
569             $scope.runAtaskForTable = function runAtaskForTable(id) {
570                 mainFactory.taskAddEnv().put({
571                     'taskId': id,
572                     'action': 'run',
573                     'args': {
574                         'task_id': id
575                     }
576                 }).$promise.then(function(response) {
577                     if (response.status == 1) {
578                         // toaster.pop({
579                         //     type: 'success',
580                         //     title: 'run a task success',
581                         //     body: 'you can go next step',
582                         //     timeout: 3000
583                         // });
584                         // ngDialog.close();
585                         getProjectDetail();
586                     } else {
587                         toaster.pop({
588                             type: 'error',
589                             title: 'fail',
590                             body: response.result,
591                             timeout: 3000
592                         });
593
594                     }
595                 }, function(error) {
596                     toaster.pop({
597                         type: 'error',
598                         title: 'fail',
599                         body: 'unknow error',
600                         timeout: 3000
601                     });
602
603                 })
604             }
605
606             function gotoDetail(id) {
607
608
609                 $state.go('app.tasklist', { taskId: id });
610
611             }
612
613             function gotoReport(id) {
614                 $state.go('app.report', { taskId: id });
615             }
616
617             function gotoModify(id) {
618                 $state.go('app.taskModify', { taskId: id });
619             }
620
621             function goBack() {
622                 window.history.back();
623             }
624
625             function goToExternal() {
626                 window.open(External_URL, '_blank');
627             }
628
629             $scope.openDeleteEnv = function openDeleteEnv(id, name) {
630                 $scope.deleteName = name;
631                 $scope.deleteId = id;
632                 ngDialog.open({
633                     template: 'views/modal/deleteConfirm.html',
634                     scope: $scope,
635                     className: 'ngdialog-theme-default',
636                     width: 500,
637                     showClose: true,
638                     closeByDocument: false
639                 })
640
641             }
642
643             $scope.deleteTask = function deleteTask() {
644                 mainFactory.deleteTask().delete({ 'task_id': $scope.deleteId }).$promise.then(function(response) {
645                     if (response.status == 1) {
646                         toaster.pop({
647                             type: 'success',
648                             title: 'delete Task success',
649                             body: 'you can go next step',
650                             timeout: 3000
651                         });
652                         ngDialog.close();
653                         getProjectDetail();
654                     } else {
655                         toaster.pop({
656                             type: 'error',
657                             title: 'Wrong',
658                             body: response.result,
659                             timeout: 3000
660                         });
661                     }
662
663                 }, function(error) {
664                     toaster.pop({
665                         type: 'error',
666                         title: 'fail',
667                         body: 'unknow error',
668                         timeout: 3000
669                     });
670
671                 })
672             }
673
674             $scope.gotoLog = function gotoLog(task_id) {
675                 $state.go('app2.taskLog', { taskId: task_id });
676             }
677         }
678     ]);