background_operations_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 requestId = data.Requests.id;
  104. var request = this.get('services').findProperty('id', requestId);
  105. var hostsMap = {};
  106. var previousTaskStatusMap = request.get('previousTaskStatusMap');
  107. var currentTaskStatusMap = {};
  108. data.tasks.forEach(function (task) {
  109. var host = hostsMap[task.Tasks.host_name];
  110. task.Tasks.request_id = requestId;
  111. if (host) {
  112. host.logTasks.push(task);
  113. host.isModified = (host.isModified) ? true : previousTaskStatusMap[task.Tasks.id] !== task.Tasks.status;
  114. } else {
  115. hostsMap[task.Tasks.host_name] = {
  116. name: task.Tasks.host_name,
  117. publicName: task.Tasks.host_name,
  118. logTasks: [task],
  119. isModified: previousTaskStatusMap[task.Tasks.id] !== task.Tasks.status
  120. };
  121. }
  122. currentTaskStatusMap[task.Tasks.id] = task.Tasks.status;
  123. }, this);
  124. request.set('previousTaskStatusMap', currentTaskStatusMap);
  125. request.set('hostsMap', hostsMap);
  126. this.set('serviceTimestamp', App.dateTime());
  127. },
  128. /**
  129. * Update task, with uploading two additional properties: stdout and stderr
  130. * @param data
  131. * @param ajaxQuery
  132. * @param params
  133. */
  134. callBackFilteredByTask: function (data, ajaxQuery, params) {
  135. var request = this.get('services').findProperty('id', data.Tasks.request_id);
  136. var host = request.get('hostsMap')[data.Tasks.host_name];
  137. var task = host.logTasks.findProperty('Tasks.id', data.Tasks.id);
  138. task.Tasks.status = data.Tasks.status;
  139. task.Tasks.stdout = data.Tasks.stdout;
  140. task.Tasks.stderr = data.Tasks.stderr;
  141. this.set('serviceTimestamp', App.dateTime());
  142. },
  143. /**
  144. * Prepare, received from server, requests for host component popup
  145. * @param data
  146. */
  147. callBackForMostRecent: function (data) {
  148. var runningServices = 0;
  149. var self = this;
  150. var currentRequestIds = [];
  151. data.items.forEach(function (request) {
  152. var rq = self.get("services").findProperty('id', request.Requests.id);
  153. var isRunning = (request.Requests.task_count -
  154. (request.Requests.aborted_task_count + request.Requests.completed_task_count + request.Requests.failed_task_count
  155. + request.Requests.timed_out_task_count - request.Requests.queued_task_count)) > 0;
  156. var requestParams = this.parseRequestContext(request.Requests.request_context);
  157. currentRequestIds.push(request.Requests.id);
  158. if (rq) {
  159. rq.set('progress', Math.ceil(request.Requests.progress_percent));
  160. rq.set('status', request.Requests.request_status);
  161. rq.set('isRunning', isRunning);
  162. rq.set('startTime', request.Requests.start_time);
  163. rq.set('endTime', request.Requests.end_time);
  164. } else {
  165. rq = Em.Object.create({
  166. id: request.Requests.id,
  167. name: requestParams.requestContext,
  168. displayName: requestParams.requestContext,
  169. progress: Math.floor(request.Requests.progress_percent),
  170. status: request.Requests.request_status,
  171. isRunning: isRunning,
  172. hostsMap: {},
  173. tasks: [],
  174. startTime: request.Requests.start_time,
  175. endTime: request.Requests.end_time,
  176. dependentService: requestParams.dependentService,
  177. sourceRequestScheduleId: request.Requests.request_schedule && request.Requests.request_schedule.schedule_id,
  178. previousTaskStatusMap: {},
  179. contextCommand: requestParams.contextCommand
  180. });
  181. self.get("services").unshift(rq);
  182. }
  183. runningServices += ~~isRunning;
  184. }, this);
  185. //remove old request if it's absent in API response
  186. self.get('services').forEach(function(service, index, services){
  187. if(!currentRequestIds.contains(service.id)) {
  188. services.splice(index, 1);
  189. }
  190. });
  191. self.set("allOperationsCount", runningServices);
  192. self.set('serviceTimestamp', App.dateTime());
  193. },
  194. /**
  195. * parse request context and if keyword "_PARSE_" is present then format it
  196. * @param requestContext
  197. * @return {Object}
  198. */
  199. parseRequestContext: function (requestContext) {
  200. var parsedRequestContext;
  201. var service;
  202. var contextCommand;
  203. if (requestContext) {
  204. if (requestContext.indexOf(App.BackgroundOperationsController.CommandContexts.PREFIX) !== -1) {
  205. var contextSplits = requestContext.split('.');
  206. contextCommand = contextSplits[1];
  207. service = contextSplits[2];
  208. switch(contextCommand){
  209. case "STOP":
  210. case "START":
  211. if (service === 'ALL_SERVICES') {
  212. parsedRequestContext = Em.I18n.t("requestInfo." + contextCommand.toLowerCase()).format(Em.I18n.t('common.allServices'));
  213. } else {
  214. parsedRequestContext = Em.I18n.t("requestInfo." + contextCommand.toLowerCase()).format(App.Service.DisplayNames[service]);
  215. }
  216. break;
  217. case "ROLLING-RESTART":
  218. parsedRequestContext = Em.I18n.t("rollingrestart.rest.context").format(App.format.role(service), contextSplits[3], contextSplits[4]);
  219. break;
  220. }
  221. } else {
  222. parsedRequestContext = requestContext;
  223. }
  224. } else {
  225. parsedRequestContext = Em.I18n.t('requestInfo.unspecified');
  226. }
  227. return {
  228. requestContext: parsedRequestContext,
  229. dependentService: service,
  230. contextCommand: contextCommand
  231. }
  232. },
  233. popupView: null,
  234. /**
  235. * Onclick handler for background operations number located right to logo
  236. */
  237. showPopup: function(){
  238. // load the checkbox on footer first, then show popup.
  239. var self = this;
  240. App.router.get('applicationController').dataLoading().done(function (initValue) {
  241. App.updater.immediateRun('requestMostRecent');
  242. if(self.get('popupView') && App.HostPopup.get('isBackgroundOperations')){
  243. self.set ('popupView.isNotShowBgChecked', !initValue);
  244. self.set('popupView.isOpen', true);
  245. $(self.get('popupView.element')).appendTo('#wrapper');
  246. } else {
  247. self.set('popupView', App.HostPopup.initPopup("", self, true));
  248. self.set ('popupView.isNotShowBgChecked', !initValue);
  249. }
  250. });
  251. }
  252. });
  253. /**
  254. * Each background operation has a context in which it operates.
  255. * Generally these contexts are fixed messages. However, we might
  256. * want to associate semantics to this context - like showing, disabling
  257. * buttons when certain operations are in progress.
  258. *
  259. * To make this possible we have command contexts where the context
  260. * is not a human readable string, but a pattern indicating the command
  261. * it is running. When UI shows these, they are translated into human
  262. * readable strings.
  263. *
  264. * General pattern of context names is "_PARSE_.{COMMAND}.{ID}[.{Additional-Data}...]"
  265. */
  266. App.BackgroundOperationsController.CommandContexts = {
  267. PREFIX : "_PARSE_",
  268. /**
  269. * Stops all services
  270. */
  271. STOP_ALL_SERVICES : "_PARSE_.STOP.ALL_SERVICES",
  272. /**
  273. * Starts all services
  274. */
  275. START_ALL_SERVICES : "_PARSE_.START.ALL_SERVICES",
  276. /**
  277. * Starts service indicated by serviceID.
  278. * @param {String} serviceID Parameter {0}. Example: HDFS
  279. */
  280. START_SERVICE : "_PARSE_.START.{0}",
  281. /**
  282. * Stops service indicated by serviceID.
  283. * @param {String} serviceID Parameter {0}. Example: HDFS
  284. */
  285. STOP_SERVICE : "_PARSE_.STOP.{0}",
  286. /**
  287. * Performs rolling restart of componentID in batches.
  288. * This context is the batchNumber batch out of totalBatchCount batches.
  289. * @param {String} componentID Parameter {0}. Example "DATANODE"
  290. * @param {Number} batchNumber Parameter {1}. Batch number of this batch. Example 3.
  291. * @param {Number} totalBatchCount Parameter {2}. Total number of batches. Example 10.
  292. */
  293. ROLLING_RESTART : "_PARSE_.ROLLING-RESTART.{0}.{1}.{2}"
  294. }