background_operations_controller.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.BackgroundOperationsController = Em.Controller.extend({
  20. name: 'backgroundOperationsController',
  21. /**
  22. * Whether we need to refresh background operations or not
  23. */
  24. isWorking : false,
  25. allOperationsCount : 0,
  26. /**
  27. * For host component popup
  28. */
  29. services:[],
  30. serviceTimestamp: null,
  31. /**
  32. * Possible levels:
  33. * REQUESTS_LIST
  34. * HOSTS_LIST
  35. * TASKS_LIST
  36. * TASK_DETAILS
  37. */
  38. levelInfo: Em.Object.create({
  39. name: 'REQUESTS_LIST',
  40. requestId: null,
  41. taskId: null,
  42. sync: false
  43. }),
  44. /**
  45. * Start polling, when <code>isWorking</code> become true
  46. */
  47. startPolling: function(){
  48. if(this.get('isWorking')){
  49. this.requestMostRecent();
  50. App.updater.run(this, 'requestMostRecent', 'isWorking', App.bgOperationsUpdateInterval);
  51. }
  52. }.observes('isWorking'),
  53. /**
  54. * Get requests data from server
  55. * @param callback
  56. */
  57. requestMostRecent: function (callback) {
  58. var queryParams = this.getQueryParams();
  59. App.ajax.send({
  60. 'name': queryParams.name,
  61. 'sender': this,
  62. 'success': queryParams.successCallback,
  63. 'callback': callback,
  64. 'data': queryParams.data
  65. });
  66. },
  67. /**
  68. * construct params of ajax query regarding displayed level
  69. */
  70. getQueryParams: function () {
  71. var levelInfo = this.get('levelInfo');
  72. var result = {
  73. name: 'background_operations.get_most_recent',
  74. successCallback: 'callBackForMostRecent',
  75. data: {}
  76. };
  77. if (levelInfo.get('name') === 'TASK_DETAILS' && !App.testMode) {
  78. result.name = 'background_operations.get_by_task';
  79. result.successCallback = 'callBackFilteredByTask';
  80. result.data = {
  81. 'taskId': levelInfo.get('taskId'),
  82. 'requestId': levelInfo.get('requestId'),
  83. 'sync': levelInfo.get('sync')
  84. };
  85. } else if (levelInfo.get('name') === 'TASKS_LIST' || levelInfo.get('name') === 'HOSTS_LIST') {
  86. result.name = 'background_operations.get_by_request';
  87. result.successCallback = 'callBackFilteredByRequest';
  88. result.data = {
  89. 'requestId': levelInfo.get('requestId'),
  90. 'sync': levelInfo.get('sync')
  91. };
  92. }
  93. levelInfo.set('sync', false);
  94. return result;
  95. },
  96. /**
  97. * Push hosts and their tasks to request
  98. * @param data
  99. * @param ajaxQuery
  100. * @param params
  101. */
  102. callBackFilteredByRequest: function (data, ajaxQuery, params) {
  103. var hostsMap = {};
  104. var request = this.get('services').findProperty('id', data.Requests.id);
  105. data.tasks.forEach(function (task) {
  106. if (hostsMap[task.Tasks.host_name]) {
  107. hostsMap[task.Tasks.host_name].logTasks.push(task);
  108. } else {
  109. hostsMap[task.Tasks.host_name] = {
  110. name: task.Tasks.host_name,
  111. publicName: task.Tasks.host_name,
  112. logTasks: [task]
  113. };
  114. }
  115. }, this);
  116. request.set('hostsMap', hostsMap);
  117. this.set('serviceTimestamp', new Date().getTime());
  118. },
  119. /**
  120. * Update task, with uploading two additional properties: stdout and stderr
  121. * @param data
  122. * @param ajaxQuery
  123. * @param params
  124. */
  125. callBackFilteredByTask: function (data, ajaxQuery, params) {
  126. var request = this.get('services').findProperty('id', data.Tasks.request_id);
  127. var host = request.get('hostsMap')[data.Tasks.host_name];
  128. var task = host.logTasks.findProperty('Tasks.id', data.Tasks.id);
  129. task.Tasks.status = data.Tasks.status;
  130. task.Tasks.stdout = data.Tasks.stdout;
  131. task.Tasks.stderr = data.Tasks.stderr;
  132. this.set('serviceTimestamp', new Date().getTime());
  133. },
  134. /**
  135. * Prepare, received from server, requests for host component popup
  136. * @param data
  137. */
  138. callBackForMostRecent: function (data) {
  139. var runningServices = 0;
  140. var self = this;
  141. var currentRequestIds = [];
  142. data.items.forEach(function (request) {
  143. var rq = self.get("services").findProperty('id', request.Requests.id);
  144. var isRunning = (request.Requests.task_count -
  145. (request.Requests.aborted_task_count + request.Requests.completed_task_count + request.Requests.failed_task_count
  146. + request.Requests.timed_out_task_count - request.Requests.queued_task_count)) > 0;
  147. var requestParams = this.parseRequestContext(request.Requests.request_context);
  148. currentRequestIds.push(request.Requests.id);
  149. if (rq) {
  150. rq.set('progress', Math.ceil(request.Requests.progress_percent));
  151. rq.set('status', request.Requests.request_status);
  152. rq.set('isRunning', isRunning);
  153. } else {
  154. rq = Em.Object.create({
  155. id: request.Requests.id,
  156. name: requestParams.requestContext,
  157. displayName: requestParams.requestContext,
  158. progress: Math.ceil(request.Requests.progress_percent),
  159. status: request.Requests.request_status,
  160. isRunning: isRunning,
  161. hostsMap: {},
  162. tasks: [],
  163. dependentService: requestParams.dependentService
  164. });
  165. self.get("services").unshift(rq);
  166. }
  167. runningServices += ~~isRunning;
  168. }, this);
  169. //remove old request if it's absent in API response
  170. self.get('services').forEach(function(service, index, services){
  171. if(!currentRequestIds.contains(service.id)) {
  172. services.splice(index, 1);
  173. }
  174. });
  175. self.set("allOperationsCount", runningServices);
  176. self.set('serviceTimestamp', new Date().getTime());
  177. },
  178. /**
  179. * parse request context and if keyword "_PARSE_" is present then format it
  180. * @param requestContext
  181. * @return {Object}
  182. */
  183. parseRequestContext: function (requestContext) {
  184. var parsedRequestContext;
  185. var service;
  186. var command;
  187. if (requestContext) {
  188. if (requestContext.indexOf("_PARSE_") !== -1) {
  189. command = requestContext.split('.')[1];
  190. service = requestContext.split('.')[2];
  191. if (service === 'ALL_SERVICES') {
  192. parsedRequestContext = Em.I18n.t("requestInfo." + command.toLowerCase()).format(Em.I18n.t('common.allServices'));
  193. } else {
  194. parsedRequestContext = Em.I18n.t("requestInfo." + command.toLowerCase()).format(App.Service.DisplayNames[service]);
  195. }
  196. } else {
  197. parsedRequestContext = requestContext;
  198. }
  199. } else {
  200. parsedRequestContext = Em.I18n.t('requestInfo.unspecified');
  201. }
  202. return {
  203. requestContext: parsedRequestContext,
  204. dependentService: service
  205. }
  206. },
  207. popupView: null,
  208. /**
  209. * Onclick handler for background operations number located right to logo
  210. * @return PopupObject For testing purposes
  211. */
  212. showPopup: function(){
  213. App.updater.immediateRun('requestMostRecent');
  214. if(this.get('popupView') && App.HostPopup.get('isBackgroundOperations') && App.HostPopup.get('showServices')){
  215. this.set ('popupView.isNotShowBgChecked', !App.router.get('mainAdminUserSettingsController').loadShowBgChecked());
  216. this.set('popupView.isOpen', true);
  217. $(this.get('popupView.element')).appendTo('#wrapper');
  218. } else {
  219. this.set('popupView', App.HostPopup.initPopup("", this, true));
  220. }
  221. }
  222. });