| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241 |
1
1
1
8
4
4
35
35
35
35
35
35
32
32
12
12
20
20
20
2
2
2
2
4
2
4
2
1
1
1
1
12
6
6
6
5
5
1
6
6
4
4
4
1
1
6
10
10
21
10
10
21
21
21
21
21
12
3
3
9
9
9
12
9
19
19
19
3
16
16
16
16
| 'use strict';
;require.register("utils/polling", function (exports, require, module) {
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var App = require('app');
App.Poll = Em.Object.extend(App.ReloadPopupMixin, {
name: '',
stage: '',
label: '',
isVisible: true,
isStarted: false,
isPolling: true,
clusterName: null,
requestId: null,
temp: false,
progress: 0,
url: null,
testUrl: null,
data: null,
isError: false,
isSuccess: false,
POLL_INTERVAL: 4000,
polledData: [],
numPolls: 0,
mockDataPrefix: '/data/wizard/deploy/5_hosts',
currentTaskId: null,
barWidth: Em.computed.format('width: {0}%;', 'progress'),
isCompleted: Em.computed.or('isError', 'isSuccess'),
showLink: Em.computed.or('isPolling', 'isStarted'),
start: function start() {
if (Em.isNone(this.get('requestId'))) {
this.setRequestId();
} else {
this.startPolling();
}
},
setRequestId: function setRequestId() {
Iif (App.get('testMode')) {
this.set('requestId', '1');
this.doPolling();
return;
}
var self = this;
var url = this.get('url');
var method = 'PUT';
var data = this.get('data');
$.ajax({
type: method,
url: url,
data: data,
dataType: 'text',
timeout: App.timeout,
success: function success(data) {
var jsonData = jQuery.parseJSON(data);
if (Em.isNone(jsonData)) {
self.set('isSuccess', true);
self.set('isError', false);
} else {
var requestId = Em.get(jsonData, 'Requests.id');
self.set('requestId', requestId);
self.doPolling();
}
},
error: function error() {
self.set('isError', true);
self.set('isSuccess', false);
},
statusCode: require('data/statusCodes')
});
},
/**
* set current task id and send request
* @param taskId
*/
updateTaskLog: function updateTaskLog(taskId) {
this.set('currentTaskId', taskId);
this.pollTaskLog();
},
doPolling: function doPolling() {
if (!Em.isNone(this.get('requestId'))) {
this.startPolling();
}
},
/**
* server call to obtain task logs
*/
pollTaskLog: function pollTaskLog() {
if (!Em.isNone(this.get('currentTaskId'))) {
App.ajax.send({
name: 'background_operations.get_by_task',
sender: this,
data: {
requestId: this.get('requestId'),
taskId: this.get('currentTaskId')
},
success: 'pollTaskLogSuccessCallback'
});
}
},
/**
* update logs of current task
* @param data
*/
pollTaskLogSuccessCallback: function pollTaskLogSuccessCallback(data) {
var currentTask = this.get('polledData').findProperty('Tasks.id', data.Tasks.id);
currentTask.Tasks.stdout = data.Tasks.stdout;
currentTask.Tasks.stderr = data.Tasks.stderr;
Em.propertyDidChange(this, 'polledData');
},
/**
* start polling operation data
* @return {Boolean}
*/
startPolling: function startPolling() {
if (Em.isNone(this.get('requestId'))) return false;
this.pollTaskLog();
App.ajax.send({
name: 'background_operations.get_by_request',
sender: this,
data: {
requestId: this.get('requestId'),
callback: this.startPolling
},
success: 'reloadSuccessCallback',
error: 'reloadErrorCallback'
});
return true;
},
reloadSuccessCallback: function reloadSuccessCallback(data) {
var self = this;
var result = this.parseInfo(data);
this._super();
if (!result) {
window.setTimeout(function () {
self.startPolling();
}, this.POLL_INTERVAL);
}
},
reloadErrorCallback: function reloadErrorCallback(request, ajaxOptions, error, opt, params) {
this._super(request, ajaxOptions, error, opt, params);
if (request.status && !this.get('isSuccess')) {
this.set('isError', true);
}
},
stopPolling: function stopPolling() {
//this.set('isSuccess', true);
},
replacePolledData: function replacePolledData(polledData) {
var currentTaskId = this.get('currentTaskId');
if (!Em.isNone(currentTaskId)) {
var task = this.get('polledData').findProperty('Tasks.id', currentTaskId);
var currentTask = polledData.findProperty('Tasks.id', currentTaskId);
if (task && currentTask) {
currentTask.Tasks.stdout = task.Tasks.stdout;
currentTask.Tasks.stderr = task.Tasks.stderr;
}
}
this.set('polledData', polledData);
},
calculateProgressByTasks: function calculateProgressByTasks(tasksData) {
var queuedTasks = tasksData.filterProperty('Tasks.status', 'QUEUED').length;
var completedTasks = tasksData.filter(function (task) {
return ['COMPLETED', 'FAILED', 'ABORTED', 'TIMEDOUT'].contains(task.Tasks.status);
}).length;
var inProgressTasks = tasksData.filterProperty('Tasks.status', 'IN_PROGRESS').length;
return Math.ceil((queuedTasks * 0.09 + inProgressTasks * 0.35 + completedTasks) / tasksData.length * 100);
},
isPollingFinished: function isPollingFinished(polledData) {
var runningTasks;
runningTasks = polledData.filterProperty('Tasks.status', 'QUEUED').length;
runningTasks += polledData.filterProperty('Tasks.status', 'IN_PROGRESS').length;
runningTasks += polledData.filterProperty('Tasks.status', 'PENDING').length;
if (runningTasks === 0) {
if (polledData.everyProperty('Tasks.status', 'COMPLETED')) {
this.set('isSuccess', true);
this.set('isError', false);
} else Eif (polledData.someProperty('Tasks.status', 'FAILED') || polledData.someProperty('Tasks.status', 'TIMEDOUT') || polledData.someProperty('Tasks.status', 'ABORTED')) {
this.set('isSuccess', false);
this.set('isError', true);
}
return true;
} else {
return false;
}
},
parseInfo: function parseInfo(polledData) {
var tasksData = polledData.tasks;
var requestId = this.get('requestId');
if (polledData.Requests && !Em.isNone(polledData.Requests.id) && polledData.Requests.id != requestId) {
// We don't want to use non-current requestId's tasks data to
// determine the current install status.
// Also, we don't want to keep polling if it is not the
// current requestId.
return false;
}
this.replacePolledData(tasksData);
var totalProgress = this.calculateProgressByTasks(tasksData);
this.set('progress', totalProgress.toString());
return this.isPollingFinished(tasksData);
}
});
}); |