polling.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.Poll = Em.Object.extend({
  20. name: '',
  21. stage: '',
  22. label: '',
  23. isStarted: false,
  24. isPolling: true,
  25. clusterName: null,
  26. requestId: undefined,
  27. temp: false,
  28. progress: 0,
  29. url: null,
  30. testUrl: null,
  31. data: null,
  32. isError: false,
  33. isSuccess: false,
  34. POLL_INTERVAL: 4000,
  35. polledData: [],
  36. numPolls: 0,
  37. mockDataPrefix: '/data/wizard/deploy/5_hosts',
  38. barWidth: function () {
  39. var barWidth = 'width: ' + this.get('progress') + '%;';
  40. return barWidth;
  41. }.property('progress'),
  42. isCompleted: function () {
  43. return (this.get('isError') || this.get('isSuccess'));
  44. }.property('isError', 'isSuccess'),
  45. showLink: function () {
  46. return (this.get('isPolling') === true && this.get('isStarted') === true);
  47. }.property('isPolling', 'isStarted'),
  48. start: function () {
  49. if (this.get('requestId') === undefined) {
  50. this.setRequestId();
  51. } else {
  52. this.startPolling();
  53. }
  54. },
  55. setRequestId: function () {
  56. if (App.testMode) {
  57. this.set('requestId', '1');
  58. this.doPolling();
  59. return;
  60. }
  61. var self = this;
  62. var url = this.get('url');
  63. var method = 'PUT';
  64. var data = this.get('data');
  65. $.ajax({
  66. type: method,
  67. url: url,
  68. async: false,
  69. data: data,
  70. dataType: 'text',
  71. timeout: App.timeout,
  72. success: function (data) {
  73. var jsonData = jQuery.parseJSON(data);
  74. console.log("TRACE: Polling -> value of the url is: " + url);
  75. console.log("TRACE: Polling-> value of the sent data is: " + self.get('data'));
  76. console.log("TRACE: Polling-> value of the received data is: " + jsonData);
  77. if (jsonData === null) {
  78. self.set('isSuccess', true);
  79. self.set('isError', false);
  80. self.set('requestId',undefined);
  81. } else {
  82. var requestId = jsonData.Requests.id;
  83. self.set('requestId', requestId);
  84. self.doPolling();
  85. console.log('requestId is: ' + requestId);
  86. }
  87. },
  88. error: function () {
  89. console.log("ERROR");
  90. self.set('isError', true);
  91. self.set('isSuccess', false);
  92. },
  93. statusCode: require('data/statusCodes')
  94. });
  95. },
  96. doPolling: function () {
  97. if (this.get('requestId')) {
  98. this.startPolling();
  99. }
  100. },
  101. startPolling: function () {
  102. if (!this.get('requestId')) {
  103. return;
  104. }
  105. var self = this;
  106. var url = App.apiPrefix + '/clusters/' + App.router.getClusterName() + '/requests/' + this.get('requestId') + '?fields=tasks/*';
  107. if (App.testMode) {
  108. this.set('POLL_INTERVAL', 1);
  109. this.numPolls++;
  110. url = this.get('mockDataPrefix') + '/poll_' + this.get('numPolls') + '.json';
  111. }
  112. $.ajax({
  113. type: 'GET',
  114. url: url,
  115. async: true,
  116. dataType: 'text',
  117. timeout: App.timeout,
  118. success: function (data) {
  119. console.log("TRACE: In success function for the GET logs data");
  120. console.log("TRACE: The value is: ", jQuery.parseJSON(data));
  121. var result = self.parseInfo(jQuery.parseJSON(data));
  122. if (result !== true) {
  123. window.setTimeout(function () {
  124. self.startPolling();
  125. }, self.POLL_INTERVAL);
  126. } else {
  127. self.set('requestId', undefined);
  128. }
  129. },
  130. error: function (request, ajaxOptions, error) {
  131. console.log("TRACE: In error function for the GET data");
  132. console.log("TRACE: value of the url is: " + url);
  133. console.log("TRACE: error code status is: " + request.status);
  134. if (!self.get('isSuccess')) {
  135. self.set('isError', true);
  136. }
  137. },
  138. statusCode: require('data/statusCodes')
  139. }).retry({times: App.maxRetries, timeout: App.timeout}).then(null,
  140. function () {
  141. App.showReloadPopup();
  142. console.log('Install services all retries failed');
  143. }
  144. );
  145. },
  146. stopPolling: function () {
  147. //this.set('isSuccess', true);
  148. },
  149. replacePolledData: function (polledData) {
  150. this.polledData.clear();
  151. this.set('polledData', polledData);
  152. },
  153. calculateProgressByTasks: function (tasksData) {
  154. var queuedTasks = tasksData.filterProperty('Tasks.status', 'QUEUED').length;
  155. var completedTasks = tasksData.filter(function (task) {
  156. return ['COMPLETED', 'FAILED', 'ABORTED', 'TIMEDOUT'].contains(task.Tasks.status);
  157. }).length;
  158. var inProgressTasks = tasksData.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  159. return Math.ceil(((queuedTasks * 0.09) + (inProgressTasks * 0.35) + completedTasks ) / tasksData.length * 100)
  160. },
  161. isPollingFinished: function (polledData) {
  162. var runningTasks;
  163. runningTasks = polledData.filterProperty('Tasks.status', 'QUEUED').length;
  164. runningTasks += polledData.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  165. runningTasks += polledData.filterProperty('Tasks.status', 'PENDING').length;
  166. if (runningTasks === 0) {
  167. if (polledData.everyProperty('Tasks.status', 'COMPLETED')) {
  168. this.set('isSuccess', true);
  169. this.set('isError', false);
  170. } else if (polledData.someProperty('Tasks.status', 'FAILED') || polledData.someProperty('Tasks.status', 'TIMEDOUT') || polledData.someProperty('Tasks.status', 'ABORTED')) {
  171. this.set('isSuccess', false);
  172. this.set('isError', true);
  173. }
  174. return true;
  175. } else {
  176. return false;
  177. }
  178. },
  179. parseInfo: function (polledData) {
  180. console.log('TRACE: Entering task info function');
  181. var self = this;
  182. var totalProgress = 0;
  183. var tasksData = polledData.tasks;
  184. console.log("The value of tasksData is: ", tasksData);
  185. if (!tasksData) {
  186. console.log("ERROR: NO tasks available to process");
  187. }
  188. var requestId = this.get('requestId');
  189. if (polledData.Requests && polledData.Requests.id && polledData.Requests.id != requestId) {
  190. // We dont want to use non-current requestId's tasks data to
  191. // determine the current install status.
  192. // Also, we dont want to keep polling if it is not the
  193. // current requestId.
  194. return false;
  195. }
  196. this.replacePolledData(tasksData);
  197. var totalProgress = this.calculateProgressByTasks(tasksData);
  198. this.set('progress', totalProgress.toString());
  199. console.log("INFO: right now the progress is: " + this.get('progress'));
  200. return this.isPollingFinished(tasksData);
  201. }
  202. });