background_operations_controller.js 3.6 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}).slice( 0, 10);
  61. data.items.forEach(function(request){
  62. var rq = Em.Object.create({
  63. id:request.Requests.id,
  64. name: 'Request name not specified',
  65. displayName: 'Request name not specified',
  66. progress:10,
  67. status: "",
  68. isRunning: false,
  69. hosts: []
  70. });
  71. if(request.Requests.request_context != ""){
  72. rq.name = request.Requests.request_context;
  73. rq.displayName = request.Requests.request_context;
  74. }
  75. var runningTasks = 0;
  76. runningTasks = request.tasks.filterProperty('Tasks.status', 'QUEUED').length;
  77. runningTasks += request.tasks.filterProperty('Tasks.status', 'IN_PROGRESS').length;
  78. runningTasks += request.tasks.filterProperty('Tasks.status', 'PENDING').length;
  79. if(runningTasks > 0){
  80. rq.set('isRunning', true);
  81. runningServices++;
  82. } else {
  83. rq.set('isRunning', false);
  84. }
  85. var hostNames = request.tasks.mapProperty('Tasks.host_name').uniq();
  86. hostNames.forEach(function (name) {
  87. var tasks = request.tasks.filterProperty("Tasks.host_name",name);
  88. rq.get("hosts").push({
  89. name: name,
  90. publicName: name,
  91. logTasks: tasks
  92. });
  93. });
  94. self.get("services").push(rq);
  95. });
  96. self.set("allOperationsCount",runningServices);
  97. self.set('serviceTimestamp', new Date().getTime());
  98. },
  99. /**
  100. * Onclick handler for background operations number located right to logo
  101. * @return PopupObject For testing purposes
  102. */
  103. showPopup: function(){
  104. if(!App.testMode){
  105. App.updater.immediateRun('requestMostRecent');
  106. }
  107. return App.HostPopup.initPopup("", this, true);
  108. }
  109. });