background_operations_controller.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * Start polling, when <code>isWorking</code> become true
  33. */
  34. startPolling: function(){
  35. if(this.get('isWorking')){
  36. this.requestMostRecent();
  37. App.updater.run(this, 'requestMostRecent', 'isWorking', App.bgOperationsUpdateInterval);
  38. }
  39. }.observes('isWorking'),
  40. /**
  41. * Get all requests from server
  42. * @param callback
  43. */
  44. requestMostRecent: function(callback){
  45. App.ajax.send({
  46. name: 'background_operations.get_most_recent',
  47. 'sender': this,
  48. 'success': 'callBackForMostRecent',
  49. 'callback': callback
  50. });
  51. },
  52. /**
  53. * Prepare recived from server requests for host component popup
  54. * @param data
  55. */
  56. callBackForMostRecent: function(data){
  57. this.get("services").clear();
  58. var runningServices = 0;
  59. var self = this;
  60. data.items = data.items.sort(function(a,b){return b.Requests.id - a.Requests.id});
  61. data.items.forEach(function(request){
  62. var hostsMap = {};
  63. var isRunningTasks = false;
  64. request.tasks.forEach(function (task) {
  65. if (!isRunningTasks && (['QUEUED', 'IN_PROGRESS', 'PENDING'].contains(task.Tasks.status))) {
  66. isRunningTasks = true;
  67. }
  68. if (hostsMap[task.Tasks.host_name]) {
  69. hostsMap[task.Tasks.host_name].logTasks.push(task);
  70. } else {
  71. hostsMap[task.Tasks.host_name] = {
  72. name: task.Tasks.host_name,
  73. publicName: task.Tasks.host_name,
  74. logTasks: [task]
  75. };
  76. }
  77. }, this);
  78. var hosts = [];
  79. for(var hostName in hostsMap){
  80. hosts.push(hostsMap[hostName]);
  81. }
  82. var rq = Em.Object.create({
  83. id: request.Requests.id,
  84. name: request.Requests.request_context || 'Request name not specified',
  85. displayName: request.Requests.request_context || 'Request name not specified',
  86. progress: 10,
  87. status: "",
  88. isRunning: isRunningTasks,
  89. hosts: hosts
  90. });
  91. runningServices += ~~isRunningTasks;
  92. self.get("services").push(rq);
  93. });
  94. self.set("allOperationsCount",runningServices);
  95. self.set('serviceTimestamp', new Date().getTime());
  96. },
  97. popupView: null,
  98. /**
  99. * Onclick handler for background operations number located right to logo
  100. * @return PopupObject For testing purposes
  101. */
  102. showPopup: function(){
  103. App.updater.immediateRun('requestMostRecent');
  104. if(this.get('popupView') && App.HostPopup.get('showServices')){
  105. this.set('popupView.isOpen', true);
  106. $(this.get('popupView.element')).appendTo('#wrapper');
  107. } else {
  108. this.set('popupView', App.HostPopup.initPopup("", this, true));
  109. }
  110. }
  111. });