background_operations_controller.js 3.6 KB

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