progress_popup_controller.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * During loading and calculations show popup with spinner
  52. * @type {Object}
  53. */
  54. spinnerPopup: null,
  55. /**
  56. * Get info for <code>requestIds</code> and initialize <code>App.HostPopup</code>
  57. * @param popupTitle {String}
  58. * @param requestIds {Array}
  59. * @param progressController {App.HighAvailabilityProgressPageController}
  60. * @param showSpinner {Boolean}
  61. */
  62. initPopup: function (popupTitle, requestIds, progressController, showSpinner) {
  63. if(showSpinner){
  64. var loadingPopup = App.ModalPopup.show({
  65. header: Em.I18n.t('jobs.loadingTasks'),
  66. primary: false,
  67. secondary: false,
  68. bodyClass: Ember.View.extend({
  69. template: Ember.Handlebars.compile('<div class="spinner"></div>')
  70. })
  71. });
  72. this.set('spinnerPopup', loadingPopup);
  73. }
  74. this.set('progressController', progressController);
  75. this.set('popupTitle', popupTitle);
  76. this.set('requestIds', requestIds);
  77. this.set('hostsData', []);
  78. this.getHosts();
  79. },
  80. /**
  81. * Send AJAX request to get hosts tasks data
  82. */
  83. getHosts: function () {
  84. var requestIds = this.get('requestIds');
  85. requestIds.forEach(function (requestId) {
  86. App.ajax.send({
  87. name: 'admin.high_availability.polling',
  88. sender: this,
  89. data: {
  90. requestId: requestId
  91. },
  92. success: 'onGetHostsSuccess'
  93. })
  94. }, this);
  95. },
  96. /**
  97. * Callback for <code>getHosts</code> request
  98. * @param data
  99. */
  100. onGetHostsSuccess: function (data) {
  101. var hostsData = this.get('hostsData');
  102. hostsData.push(data);
  103. if (this.get('requestIds.length') === this.get('hostsData.length')) {
  104. var popupTitle = this.get('popupTitle');
  105. this.calculateHostsData(hostsData);
  106. App.HostPopup.initPopup(popupTitle, this);
  107. if (this.isRequestRunning(hostsData)) {
  108. this.addObserver('progressController.logs.length', this, 'getDataFromProgressController');
  109. }
  110. }
  111. if(this.get('spinnerPopup')){
  112. this.get('spinnerPopup').hide();
  113. this.set('spinnerPopup', null);
  114. }
  115. },
  116. /**
  117. * Convert data to format used in <code>App.HostPopup</code>
  118. * @param data {Array}
  119. */
  120. calculateHostsData: function (data) {
  121. var hosts = [];
  122. var hostsMap = {};
  123. var popupTitle = this.get('popupTitle');
  124. data.forEach(function (request) {
  125. request.tasks.forEach(function (task) {
  126. var host = task.Tasks.host_name;
  127. if (hostsMap[host]) {
  128. hostsMap[host].logTasks.push(task);
  129. } else {
  130. hostsMap[host] = {
  131. name: task.Tasks.host_name,
  132. publicName: task.Tasks.host_name,
  133. logTasks: [task]
  134. };
  135. }
  136. });
  137. });
  138. for (var host in hostsMap) {
  139. hosts.push(hostsMap[host]);
  140. }
  141. this.set('services', [
  142. {name: popupTitle, hosts: hosts}
  143. ]);
  144. this.set('serviceTimestamp', App.dateTime());
  145. if (!this.isRequestRunning(data)) {
  146. this.removeObserver('progressController.logs.length', this, 'getDataFromProgressController');
  147. }
  148. },
  149. /**
  150. * Get hosts tasks data from <code>progressController</code>
  151. */
  152. getDataFromProgressController: function () {
  153. var data = this.get('hostsData');
  154. var tasksData = this.get('progressController.logs');
  155. if (tasksData.length) {
  156. var tasks = [];
  157. tasksData.forEach(function (logs) {
  158. tasks.pushObjects(logs);
  159. }, this);
  160. data.forEach(function (request) {
  161. tasks = tasks.filterProperty('Tasks.request_id', request.Requests.id);
  162. request.tasks = tasks;
  163. });
  164. this.calculateHostsData(data);
  165. }
  166. },
  167. /**
  168. * Identify whether request is running by task counters
  169. * @param requests {Array}
  170. * @return {Boolean}
  171. */
  172. isRequestRunning: function (requests) {
  173. var result = false;
  174. requests.forEach(function (request) {
  175. if ((request.Requests.task_count -
  176. (request.Requests.aborted_task_count + request.Requests.completed_task_count + request.Requests.failed_task_count
  177. + request.Requests.timed_out_task_count - request.Requests.queued_task_count)) > 0) {
  178. result = true;
  179. }
  180. });
  181. return result;
  182. }
  183. });