progress_popup_controller.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. App.HighAvailabilityProgressPopupController = Ember.Controller.extend({
  19. name: 'highAvailabilityProgressPopupController',
  20. /**
  21. * Id of current request
  22. * @type {Array}
  23. */
  24. requestIds: [],
  25. /**
  26. * Title for popup header
  27. * @type {String}
  28. */
  29. popupTitle: '',
  30. /**
  31. * Array with Hosts tasks data used in <code>App.HostPopup</code>
  32. * @type {Array}
  33. */
  34. services: [],
  35. /**
  36. * Timestamp used in <code>App.HostPopup</code>
  37. * @type {Number}
  38. */
  39. serviceTimestamp: null,
  40. /**
  41. * Progress controller. Used to get tasks data.
  42. * @type {App.HighAvailabilityProgressPageController}
  43. */
  44. progressController: null,
  45. /**
  46. * Requests data with tasks
  47. * @type {Array}
  48. */
  49. hostsData: [],
  50. /**
  51. * StageId for the command.
  52. * @type {Number}
  53. */
  54. stageId: null,
  55. /**
  56. * During loading and calculations show popup with spinner
  57. * @type {Object}
  58. */
  59. spinnerPopup: null,
  60. isTaskPolling: false,
  61. taskInfo: null,
  62. /**
  63. * Get info for <code>requestIds</code> and initialize <code>App.HostPopup</code>
  64. * @param popupTitle {String}
  65. * @param requestIds {Array}
  66. * @param progressController {App.HighAvailabilityProgressPageController}
  67. * @param showSpinner {Boolean}
  68. * @param stageId {Number}
  69. */
  70. initPopup: function (popupTitle, requestIds, progressController, showSpinner, stageId) {
  71. if(showSpinner){
  72. var loadingPopup = App.ModalPopup.show({
  73. header: Em.I18n.t('jobs.loadingTasks'),
  74. primary: false,
  75. secondary: false,
  76. bodyClass: Ember.View.extend({
  77. template: Ember.Handlebars.compile('<div class="spinner"></div>')
  78. })
  79. });
  80. this.set('spinnerPopup', loadingPopup);
  81. }
  82. this.set('progressController', progressController);
  83. this.set('popupTitle', popupTitle);
  84. this.set('requestIds', requestIds);
  85. this.set('hostsData', []);
  86. this.set('stageId', stageId);
  87. this.getHosts();
  88. },
  89. /**
  90. * Send AJAX request to get hosts tasks data
  91. */
  92. getHosts: function () {
  93. var requestIds = this.get('requestIds');
  94. var stageId = this.get('stageId');
  95. var name = 'background_operations.get_by_request';
  96. if (!Em.isNone(stageId)) {
  97. name = 'common.request.polling';
  98. }
  99. requestIds.forEach(function (requestId) {
  100. App.ajax.send({
  101. name: name,
  102. sender: this,
  103. data: {
  104. requestId: requestId,
  105. stageId: stageId
  106. },
  107. success: 'onGetHostsSuccess'
  108. })
  109. }, this);
  110. },
  111. /**
  112. * Callback for <code>getHosts</code> request
  113. * @param data
  114. */
  115. onGetHostsSuccess: function (data) {
  116. var hostsData = this.get('hostsData');
  117. hostsData.push(data);
  118. if (this.get('requestIds.length') === this.get('hostsData.length')) {
  119. var popupTitle = this.get('popupTitle');
  120. this.calculateHostsData(hostsData);
  121. App.HostPopup.initPopup(popupTitle, this);
  122. if (this.isRequestRunning(hostsData)) {
  123. this.addObserver('progressController.logs.length', this, 'getDataFromProgressController');
  124. }
  125. }
  126. if(this.get('spinnerPopup')){
  127. this.get('spinnerPopup').hide();
  128. this.set('spinnerPopup', null);
  129. }
  130. },
  131. /**
  132. * Convert data to format used in <code>App.HostPopup</code>
  133. * @param data {Array}
  134. */
  135. calculateHostsData: function (data) {
  136. var hosts = [];
  137. var hostsMap = {};
  138. var popupTitle = this.get('popupTitle');
  139. data.forEach(function (request) {
  140. request.tasks.forEach(function (task) {
  141. var host = task.Tasks.host_name;
  142. if (hostsMap[host]) {
  143. hostsMap[host].logTasks.push(task);
  144. } else {
  145. hostsMap[host] = {
  146. name: task.Tasks.host_name,
  147. publicName: task.Tasks.host_name,
  148. logTasks: [task]
  149. };
  150. }
  151. });
  152. });
  153. for (var host in hostsMap) {
  154. hosts.push(hostsMap[host]);
  155. }
  156. this.set('services', [
  157. {name: popupTitle, hosts: hosts}
  158. ]);
  159. this.set('serviceTimestamp', App.dateTime());
  160. if (!this.isRequestRunning(data)) {
  161. this.removeObserver('progressController.logs.length', this, 'getDataFromProgressController');
  162. }
  163. },
  164. /**
  165. * Get hosts tasks data from <code>progressController</code>
  166. */
  167. getDataFromProgressController: function () {
  168. var data = this.get('hostsData');
  169. var tasksData = [];
  170. var stageId = this.get('stageId');
  171. // If the progress page is broken into stages then update host with only stage's tasks
  172. if (!!stageId) {
  173. tasksData = this.get('progressController.logs').filterProperty('Tasks.stage_id',stageId);
  174. } else {
  175. tasksData = this.get('progressController.logs');
  176. }
  177. if (tasksData.length) {
  178. var tasks = [];
  179. tasksData.forEach(function (logs) {
  180. tasks.pushObjects(logs);
  181. }, this);
  182. data.forEach(function (request) {
  183. tasks = tasks.filterProperty('Tasks.request_id', request.Requests.id);
  184. request.tasks = tasks;
  185. });
  186. this.calculateHostsData(data);
  187. }
  188. },
  189. /**
  190. * Identify whether request is running by task counters
  191. * @param requests {Array}
  192. * @return {Boolean}
  193. */
  194. isRequestRunning: function (requests) {
  195. var result = false;
  196. requests.forEach(function (request) {
  197. if ((request.Requests.task_count -
  198. (request.Requests.aborted_task_count + request.Requests.completed_task_count + request.Requests.failed_task_count
  199. + request.Requests.timed_out_task_count - request.Requests.queued_task_count)) > 0) {
  200. result = true;
  201. }
  202. });
  203. return result;
  204. },
  205. startTaskPolling: function (requestId, taskId) {
  206. this.setProperties({
  207. isTaskPolling: true,
  208. taskInfo: {
  209. id: taskId,
  210. requestId: requestId
  211. }
  212. });
  213. App.updater.run(this, 'updateTask', 'isTaskPolling', App.bgOperationsUpdateInterval);
  214. App.updater.immediateRun('updateTask');
  215. },
  216. stopTaskPolling: function () {
  217. this.set('isTaskPolling', false);
  218. },
  219. updateTask: function (callback) {
  220. App.ajax.send({
  221. name: 'background_operations.get_by_task',
  222. sender: this,
  223. data: {
  224. requestId: this.get('taskInfo.requestId'),
  225. taskId: this.get('taskInfo.id')
  226. },
  227. success: 'updateTaskSuccessCallback',
  228. callback: callback
  229. })
  230. },
  231. updateTaskSuccessCallback: function (data) {
  232. this.setProperties({
  233. 'taskInfo.stderr': data.Tasks.stderr,
  234. 'taskInfo.stdout': data.Tasks.stdout,
  235. 'taskInfo.outputLog': data.Tasks.output_log,
  236. 'taskInfo.errorLog': data.Tasks.error_log,
  237. 'isTaskPolling': !['FAILED', 'COMPLETED', 'TIMEDOUT', 'ABORTED'].contains(data.Tasks.status)
  238. });
  239. }
  240. });