Merge "Remove redundancy file and do relative modification"
[yardstick.git] / gui / app / scripts / controllers / main.js
1 'use strict';
2
3 angular.module('yardStickGui2App')
4     .controller('MainCtrl', ['$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.project = 0;
10             $scope.showloading = false;
11             $scope.showEnvrionment = false;
12             $scope.loadingOPENrc = false;
13             $scope.uuidEnv = null;
14             $scope.showPod = null;
15             $scope.showImage = null;
16             $scope.showContainer = null;
17             $scope.showNextOpenRc = null;
18             $scope.showNextPod = null;
19             $scope.displayContainerInfo = [];
20             $scope.containerList = [{ value: 'create_influxdb', name: "InfluxDB" }, { value: 'create_grafana', name: "Grafana" }]
21
22             $scope.$on('$destroy', function() {
23                 $interval.cancel($scope.intervalImgae)
24             });
25             $scope.showImageStatus = 0;
26
27
28
29
30
31
32             function init() {
33
34
35                 $scope.gotoProject = gotoProject;
36                 $scope.gotoEnvironment = gotoEnvironment;
37                 $scope.gotoTask = gotoTask;
38                 $scope.gotoExcute = gotoExcute;
39                 $scope.gotoReport = gotoReport;
40                 $scope.deleteEnvItem = deleteEnvItem;
41                 $scope.addInfo = addInfo;
42                 $scope.submitOpenRcFile = submitOpenRcFile;
43                 $scope.uploadFilesPod = uploadFilesPod;
44                 $scope.uploadFiles = uploadFiles;
45                 $scope.showEnvriomentStatus = showEnvriomentStatus;
46                 $scope.openEnvironmentDialog = openEnvironmentDialog;
47                 $scope.getEnvironmentList = getEnvironmentList;
48                 $scope.gotoDetail = gotoDetail;
49                 $scope.addEnvironment = addEnvironment;
50                 $scope.createContainer = createContainer;
51                 $scope.chooseResult = chooseResult;
52
53                 getEnvironmentList();
54                 // getImageList();
55
56             }
57
58             function gotoProject() {
59                 $scope.project = 1;
60             }
61
62             function gotoEnvironment() {
63                 $scope.project = 0;
64             }
65
66             function gotoTask() {
67                 $scope.project = 2;
68             }
69
70             function gotoExcute() {
71                 $scope.project = 3;
72
73             }
74
75             function gotoReport() {
76                 $scope.project = 4;
77             }
78             $scope.skipPod = function skipPod() {
79                 $scope.showContainer = 1;
80
81             }
82             $scope.skipContainer = function skipContainer() {
83                 getEnvironmentList();
84                 ngDialog.close();
85             }
86
87             $scope.goToImage = function goToImage() {
88                 getImageListSimple();
89                 $scope.showImage = 1;
90             }
91             $scope.goToPod = function goToPod() {
92                 $scope.showPod = 1;
93             }
94             $scope.goToPodPrev = function goToPodPrev() {
95                 $scope.showImage = null;
96
97             }
98             $scope.skipPodPrev = function skipPodPrev() {
99                 $scope.showImage = 1;
100                 $scope.showPod = null;
101
102             }
103             $scope.skipContainerPrev = function skipContainerPrev() {
104                 $scope.showPod = 1;
105                 $scope.showContainer = null;
106             }
107
108             $scope.envInfo = [
109                 { name: 'OS_USERNAME', value: '' },
110                 { name: 'OS_PASSWORD', value: '' },
111                 { name: 'OS_TENANT_NAME', value: '' },
112                 { name: 'EXTERNAL_NETWORK', value: '' }
113             ];
114
115
116             function deleteEnvItem(index) {
117                 $scope.envInfo.splice(index, 1);
118             }
119
120             function addInfo() {
121                 var tempKey = null;
122                 var tempValue = null;
123                 var temp = {
124                     name: tempKey,
125                     value: tempValue
126                 }
127                 $scope.envInfo.push(temp);
128
129             }
130
131             function submitOpenRcFile() {
132                 $scope.showloading = true;
133
134                 var postData = {};
135                 postData['action'] = 'update_openrc';
136                 rebuildEnvInfo();
137                 postData['args'] = {};
138                 postData.args["openrc"] = $scope.postEnvInfo;
139                 postData.args['environment_id'] = $scope.uuidEnv;
140                 mainFactory.postEnvironmentVariable().post(postData).$promise.then(function(response) {
141                     $scope.showloading = false;
142
143                     if (response.status == 1) {
144
145                         $scope.openrcInfo = response.result;
146                         toaster.pop({
147                             type: 'success',
148                             title: 'create success',
149                             body: 'you can go next step',
150                             timeout: 3000
151                         });
152                         $scope.showEnvrionment = true;
153                         // $scope.showImage = response.status;
154                         $scope.showNextOpenRc = 1;
155                     } else {
156                         toaster.pop({
157                             type: 'error',
158                             title: 'fail',
159                             body: response.error_msg,
160                             timeout: 3000
161                         });
162                     }
163
164                 }, function(error) {
165                     toaster.pop({
166                         type: 'error',
167                         title: 'fail',
168                         body: 'unknow error',
169                         timeout: 3000
170                     });
171
172                 })
173             }
174
175             function uploadFiles($file, $invalidFiles) {
176                 $scope.openrcInfo = {};
177                 $scope.loadingOPENrc = true;
178                 $scope.displayOpenrcFile = $file;
179                 timeConstruct($scope.displayOpenrcFile.lastModified);
180                 Upload.upload({
181                     url: Base_URL + '/api/v2/yardstick/openrcs',
182                     data: { file: $file, 'environment_id': $scope.uuidEnv, 'action': 'upload_openrc' }
183                 }).then(function(response) {
184
185                     $scope.loadingOPENrc = false;
186                     if (response.data.status == 1) {
187                         toaster.pop({
188                             type: 'success',
189                             title: 'upload success',
190                             body: 'you can go next step',
191                             timeout: 3000
192                         });
193                         $scope.openrcInfo = response.data.result;
194
195                         getItemIdDetailforOpenrc();
196                         $scope.showNextOpenRc = 1;
197                     } else {
198                         toaster.pop({
199                             type: 'error',
200                             title: 'fail',
201                             body: response.error_msg,
202                             timeout: 3000
203                         });
204                     }
205
206                 }, function(error) {
207                     $scope.uploadfile = null;
208                     toaster.pop({
209                         type: 'error',
210                         title: 'fail',
211                         body: 'unknow error',
212                         timeout: 3000
213                     });
214                 })
215             }
216
217             //reconstruc EnvInfo
218             function rebuildEnvInfo() {
219                 $scope.postEnvInfo = {};
220                 for (var i = 0; i < $scope.envInfo.length; i++) {
221                     $scope.postEnvInfo[$scope.envInfo[i].name] = $scope.envInfo[i].value;
222                 }
223
224             }
225             function uploadFilesPod($file, $invalidFiles) {
226                 $scope.loadingOPENrc = true;
227
228                 $scope.displayPodFile = $file;
229                 timeConstruct($scope.displayPodFile.lastModified);
230                 Upload.upload({
231                     url: Base_URL + '/api/v2/yardstick/pods',
232                     data: { file: $file, 'environment_id': $scope.uuidEnv, 'action': 'upload_pod_file' }
233                 }).then(function(response) {
234
235                     $scope.loadingOPENrc = false;
236                     if (response.data.status == 1) {
237                         toaster.pop({
238                             type: 'success',
239                             title: 'upload success',
240                             body: 'you can go next step',
241                             timeout: 3000
242                         });
243
244                         $scope.podData = response.data.result;
245
246
247                     } else {
248                         toaster.pop({
249                             type: 'error',
250                             title: 'fail',
251                             body: response.error_msg,
252                             timeout: 3000
253                         });
254                     }
255
256                 }, function(error) {
257                     $scope.uploadfile = null;
258                     toaster.pop({
259                         type: 'error',
260                         title: 'fail',
261                         body: 'unknow error',
262                         timeout: 3000
263                     });
264                 })
265             }
266
267             function timeConstruct(array) {
268                 var date = new Date(1398250549490);
269                 var Y = date.getFullYear() + '-';
270                 var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
271                 var D = date.getDate() + ' ';
272                 var h = date.getHours() + ':';
273                 var m = date.getMinutes() + ':';
274                 var s = date.getSeconds();
275                 $scope.filelastModified = Y + M + D + h + m + s;
276
277             }
278
279             //display environment
280             function showEnvriomentStatus() {
281                 $scope.showEnvironment = true;
282             }
283
284             //open Environment dialog
285             function openEnvironmentDialog() {
286                 $scope.showEnvrionment = false;
287                 $scope.loadingOPENrc = false;
288                 $scope.uuidEnv = null;
289                 $scope.showPod = null;
290                 $scope.showImage = null;
291                 $scope.showContainer = null;
292                 $scope.showNextOpenRc = null;
293                 $scope.showNextPod = null;
294                 $scope.displayContainerInfo = [];
295
296                 $scope.displayPodFile = null;
297                 $scope.name = null;
298                 $scope.openrcInfo = null;
299                 $scope.envInfo = [
300                     { name: 'OS_USERNAME', value: '' },
301                     { name: 'OS_PASSWORD', value: '' },
302                     { name: 'OS_TENANT_NAME', value: '' },
303                     { name: 'EXTERNAL_NETWORK', value: '' }
304                 ];
305                 $scope.displayOpenrcFile = null;
306                 $scope.podData = null;
307                 $scope.displayContainerInfo = null;
308                 ngDialog.open({
309                     preCloseCallback: function(value) {
310                         getEnvironmentList();
311                         // getImageList();
312                     },
313                     template: 'views/modal/environmentDialog.html',
314                     scope: $scope,
315                     className: 'ngdialog-theme-default',
316                     width: 950,
317                     showClose: true,
318                     closeByDocument: false
319                 })
320             }
321
322             function getEnvironmentList() {
323                 $loading.start('key');
324
325                 mainFactory.getEnvironmentList().get().$promise.then(function(response) {
326                     $scope.environmentList = response.result.environments;
327                     $loading.finish('key');
328
329                 }, function(error) {
330                     $loading.finish('key');
331                     toaster.pop({
332                         type: 'error',
333                         title: 'fail',
334                         body: 'unknow error',
335                         timeout: 3000
336                     });
337
338                 })
339             }
340
341             //go to detail page
342             function gotoDetail(ifNew, uuid) {
343
344                 $state.go('app.environmentDetail', { uuid: uuid, ifNew: ifNew });
345             }
346
347
348             function addEnvironment(name) {
349                 mainFactory.addEnvName().post({
350                     'action': 'create_environment',
351                     args: {
352                         'name': name
353                     }
354                 }).$promise.then(function(response) {
355                     if (response.status == 1) {
356                         toaster.pop({
357                             type: 'success',
358                             title: 'create name success',
359                             body: 'you can go next step',
360                             timeout: 3000
361                         });
362                         $scope.uuidEnv = response.result.uuid;
363                         $scope.name = name;
364
365                     }
366
367                 }, function(error) {
368                     toaster.pop({
369                         type: 'error',
370                         title: 'fail',
371                         body: 'unknow error',
372                         timeout: 3000
373                     });
374                 })
375             }
376
377
378
379             $scope.goBack = function goBack() {
380                     $state.go('app.projectList');
381                 }
382             $scope.displayContainerInfo = [];
383
384             function createContainer(selectContainer) {
385
386                 $scope.showloading = true;
387                 mainFactory.runAcontainer().post({
388                     'action': selectContainer.value,
389                     'args': {
390                         'environment_id': $scope.uuidEnv,
391                     }
392                 }).$promise.then(function(response) {
393                     $scope.showloading = false;
394                     if (response.status == 1) {
395                         toaster.pop({
396                             type: 'success',
397                             title: 'create container success',
398                             body: 'you can go next step',
399                             timeout: 3000
400                         });
401
402                         setTimeout(function() {
403                             getItemIdDetail();
404                         }, 10000);
405                         $scope.ifskipOrClose = 1;
406                     } else {
407                         toaster.pop({
408                             type: 'error',
409                             title: 'Wrong',
410                             body: response.result,
411                             timeout: 3000
412                         });
413                     }
414
415                 }, function(error) {
416                     toaster.pop({
417                         type: 'error',
418                         title: 'fail',
419                         body: 'unknow error',
420                         timeout: 3000
421                     });
422                 })
423             }
424
425             function getConDetail(id) {
426                 mainFactory.containerDetail().get({
427                     'containerId': id
428                 }).$promise.then(function(response) {
429                     if (response.status == 1) {
430                         // $scope.podData = response.result;
431                         $scope.displayContainerInfo.push(response.result.container);
432
433                     }
434
435                 }, function(error) {
436                     toaster.pop({
437                         type: 'error',
438                         title: 'fail',
439                         body: 'unknow error',
440                         timeout: 3000
441                     });
442                 })
443
444             }
445
446             function chooseResult(name) {
447                 $scope.selectContainer = name;
448             }
449
450             function getItemIdDetail() {
451                 $scope.displayContainerInfo = [];
452                 mainFactory.ItemDetail().get({
453                     'envId': $scope.uuidEnv
454                 }).$promise.then(function(response) {
455                     if (response.status == 1) {
456                         $scope.envName = response.result.environment.name;
457                         $scope.containerId = response.result.environment.container_id;
458                         if ($scope.containerId != null) {
459
460                             var keysArray = Object.keys($scope.containerId);
461                             for (var k in $scope.containerId) {
462                                 getConDetail($scope.containerId[k]);
463
464                             }
465
466
467                         } else {
468                             $scope.podData = null;
469                         }
470
471                     }
472                 }, function(error) {
473                     toaster.pop({
474                         type: 'error',
475                         title: 'fail',
476                         body: 'unknow error',
477                         timeout: 3000
478                     });
479                 })
480             }
481
482             $scope.uploadImage = function uploadImage() {
483                 $scope.imageStatus = 0;
484                 $scope.showImageStatus = 1;
485                 $scope.showloading = true;
486                 mainFactory.uploadImage().post({
487                     'action': 'load_image',
488                     'args': {
489                         'environment_id': $scope.uuid
490
491                     }
492                 }).$promise.then(function(response) {
493                     $scope.showloading = false;
494                     if (response.status == 1) {
495                         toaster.pop({
496                             type: 'success',
497                             title: 'create success',
498                             body: 'you can go next step',
499                             timeout: 3000
500                         });
501                         setTimeout(function() {
502                             getImageList();
503                         }, 10000);
504                         $scope.showNextPod = 1;
505
506                     } else {
507                         toaster.pop({
508                             type: 'error',
509                             title: 'failed',
510                             body: 'something wrong',
511                             timeout: 3000
512                         });
513
514                     }
515                 }, function(error) {
516                     toaster.pop({
517                         type: 'error',
518                         title: 'failed',
519                         body: 'something wrong',
520                         timeout: 3000
521                     });
522                 })
523             }
524
525             function getImageList() {
526                 if ($scope.intervalImgae != undefined) {
527                     $interval.cancel($scope.intervalImgae);
528                 }
529                 mainFactory.ImageList().get({}).$promise.then(function(response) {
530                     if (response.status == 1) {
531                         $scope.imageListData = response.result.images;
532                         $scope.imageStatus = response.result.status;
533
534                         if ($scope.imageStatus == 0) {
535                             $scope.intervalImgae = $interval(function() {
536                                 getImageList();
537                             }, 5000);
538                         } else if ($scope.intervalImgae != undefined) {
539                             $interval.cancel($scope.intervalImgae);
540                         }
541
542                     } else {
543                         toaster.pop({
544                             type: 'error',
545                             title: 'get data failed',
546                             body: 'please retry',
547                             timeout: 3000
548                         });
549                     }
550                 }, function(error) {
551                     toaster.pop({
552                         type: 'error',
553                         title: 'get data failed',
554                         body: 'please retry',
555                         timeout: 3000
556                     });
557                 })
558             }
559
560             function getImageListSimple() {
561
562                 mainFactory.ImageList().get({}).$promise.then(function(response) {
563                     if (response.status == 1) {
564                         $scope.imageListData = response.result.images;
565                         $scope.imageStatus = response.result.status;
566
567                     } else {
568                         toaster.pop({
569                             type: 'error',
570                             title: 'get data failed',
571                             body: 'please retry',
572                             timeout: 3000
573                         });
574                     }
575                 }, function(error) {
576                     toaster.pop({
577                         type: 'error',
578                         title: 'get data failed',
579                         body: 'please retry',
580                         timeout: 3000
581                     });
582                 })
583             }
584
585             $scope.openDeleteEnv = function openDeleteEnv(id, name) {
586                 $scope.deleteName = name;
587                 $scope.deleteId = id;
588                 ngDialog.open({
589                     template: 'views/modal/deleteConfirm.html',
590                     scope: $scope,
591                     className: 'ngdialog-theme-default',
592                     width: 500,
593                     showClose: true,
594                     closeByDocument: false
595                 })
596
597             }
598
599             $scope.deleteEnv = function deleteEnv() {
600                 mainFactory.deleteEnv().delete({ 'env_id': $scope.deleteId }).$promise.then(function(response) {
601                     if (response.status == 1) {
602                         toaster.pop({
603                             type: 'success',
604                             title: 'delete environment success',
605                             body: 'you can go next step',
606                             timeout: 3000
607                         });
608                         ngDialog.close();
609                         getEnvironmentList();
610                     } else {
611                         toaster.pop({
612                             type: 'error',
613                             title: 'Wrong',
614                             body: response.result,
615                             timeout: 3000
616                         });
617                     }
618
619                 }, function(error) {
620                     toaster.pop({
621                         type: 'error',
622                         title: 'fail',
623                         body: 'unknow error',
624                         timeout: 3000
625                     });
626                 })
627             }
628
629
630
631
632
633
634             function getItemIdDetailforOpenrc() {
635
636                 mainFactory.ItemDetail().get({
637                     'envId': $scope.uuidEnv
638                 }).$promise.then(function(response) {
639                     if (response.status == 1) {
640                         $scope.baseElementInfo = response.result.environment;
641
642
643                         if ($scope.ifNew != 'true') {
644                             $scope.baseElementInfo = response.result.environment;
645                             if ($scope.baseElementInfo.openrc_id != null) {
646                                 getOpenrcDetailForOpenrc($scope.baseElementInfo.openrc_id);
647                             }
648                         }
649
650                     } else {
651                         toaster.pop({
652                             type: 'error',
653                             title: 'fail',
654                             body: response.error_msg,
655                             timeout: 3000
656                         });
657
658                     }
659                 }, function(error) {
660
661                     toaster.pop({
662                         type: 'error',
663                         title: 'fail',
664                         body: 'unknow error',
665                         timeout: 3000
666                     });
667
668                 })
669             }
670
671
672
673             //getopenRcid
674             function getOpenrcDetailForOpenrc(openrcId) {
675                 mainFactory.getEnvironmentDetail().get({
676                     'openrc_id': openrcId
677                 }).$promise.then(function(response) {
678                     $scope.openrcInfo = response.result;
679                     buildToEnvInfoOpenrc($scope.openrcInfo.openrc)
680                 }, function(response) {
681                     toaster.pop({
682                         type: 'error',
683                         title: 'error',
684                         body: 'unknow error',
685                         timeout: 3000
686                     });
687                 })
688             }
689
690             //buildtoEnvInfo
691             function buildToEnvInfoOpenrc(object) {
692                 var tempKeyArray = Object.keys(object);
693                 $scope.envInfo = [];
694
695
696                 for (var i = 0; i < tempKeyArray.length; i++) {
697                     var tempkey = tempKeyArray[i];
698                     var tempValue = object[tempKeyArray[i]];
699                     var temp = {
700                         name: tempkey,
701                         value: tempValue
702                     };
703                     $scope.envInfo.push(temp);
704                 }
705             }
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720         }
721     ]);