Display Human-readable timeout in Manual Task popup
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / modals / change-management-manual-tasks-controller / change-management-manual-tasks.controller.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 (function () {
22     'use strict';
23
24     appDS2.controller("changeManagementManualTasksController", ["$uibModalInstance", "jobInfo", "MsoService", "COMPONENT",
25         "$log", "moment", changeManagementManualTasksController]);
26
27     function changeManagementManualTasksController($uibModalInstance, jobInfo, MsoService, COMPONENT, $log, moment) {
28         var vm = this;
29
30         vm.manualTasks = [];
31         vm.MANUAL_TASKS = COMPONENT.MANUAL_TASKS;
32         var init = function() {
33             vm.requestState = jobInfo.requestState;
34
35             if (jobInfo && jobInfo.details) {
36                 vm.content = jobInfo.details;
37             } else {
38                 vm.content = "The VNF change alerted due to unknown reason.";
39             }
40
41             loadAvailableTasks(jobInfo.job.requestId);
42
43         };
44
45         function loadAvailableTasks(requestId) {
46             return MsoService.getManualTasks(requestId)
47                 .then(function(response) {
48                     vm.task = response.data[0];
49                     vm.manualTasks = vm.task && vm.task.validResponses;
50                     vm.description = vm.task && vm.task.description || null;
51                     vm.timeout = vm.task && vm.task.timeout || null;
52                 })
53                 .catch(function(error) {
54                     $log.error(error);
55                 });
56         }
57
58         vm.completeTask = function(task) {
59             MsoService.completeTask(vm.task.taskId, task)
60                 .then(function(response) {
61                     vm.manualTasks = response.data;
62                     $uibModalInstance.close(task + " action completed successfully.");
63                 })
64                 .catch(function(error) {
65                     $uibModalInstance.close(task + " action failed.");
66                     $log.error(error);
67                 });
68         };
69
70         vm.close = function () {
71             $uibModalInstance.close();
72         };
73
74         vm.isTaskAvailable = function(task) {
75             return vm.manualTasks.includes(task);
76         };
77
78         vm.timeoutHumanized = function() {
79             // moment.duration() can parse ISO 8601 time-intervals,
80             // e.g. "P1Y2M10DT2H30M"
81             // https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
82             let duration = moment.duration(vm.timeout);
83
84             return isDurationValid()
85                 ? durationAsHoursAndMinutes() + ' hours (' + vm.timeout + ')'
86                 : vm.timeout;
87
88
89             function isDurationValid() {
90                 return duration.isValid() && duration.toISOString() !== 'P0D';
91             }
92
93             function durationAsHoursAndMinutes() {
94                 return ''
95                     + Math.floor(duration.asHours())
96                     + ':'
97                     + withLeadingZero(duration.minutes());
98             }
99
100             function withLeadingZero(x) {
101                 return ("00" + Math.round(x)).slice(-2);
102             }
103         };
104
105         vm.__test_only__ = {
106             loadAvailableTasks: loadAvailableTasks,
107         };
108
109         init();
110     }
111 })();