background_operations_controller.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. allOperations: [],
  26. allOperationsCount : 0,
  27. /**
  28. * Update it every time when background operations for services are changed
  29. */
  30. serviceOperationsChangeTime: function(){
  31. return (new Date().getTime());
  32. }.property('hdfsOperations', 'mapReduceOperations'),
  33. hdfsOperations : function(){
  34. var all = this.get('allOperations');
  35. var result = [];
  36. all.forEach(function(item){
  37. if( ['NAMENODE', 'SECONDARY_NAMENODE', 'DATANODE', 'HDFS_CLIENT', 'HDFS_SERVICE_CHECK'].contains(item.role)){
  38. result.push(item);
  39. }
  40. })
  41. return result;
  42. }.property('allOperations.@each'),
  43. mapReduceOperations : function(){
  44. var all = this.get('allOperations');
  45. var result = [];
  46. all.forEach(function(item){
  47. if( ['MAPREDUCE_CLIENT', 'JOBTRACKER', 'TASKTRACKER', 'MAPREDUCE_SERVICE_CHECK'].contains(item.role)){
  48. result.push(item);
  49. }
  50. })
  51. return result;
  52. }.property('allOperations.@each'),
  53. getOperationsFor: function(serviceName){
  54. switch(serviceName.toUpperCase()){
  55. case 'HDFS':
  56. return this.get('hdfsOperations');
  57. case 'MAPREDUCE':
  58. return this.get('mapReduceOperations');
  59. default:
  60. return [];
  61. }
  62. },
  63. updateInterval: 6000,
  64. url : '',
  65. generateUrl: function(){
  66. var url = App.testMode ?
  67. '/data/background_operations/list_on_start.json' :
  68. '/api/clusters/' + App.router.getClusterName() + '/requests/?fields=tasks/*&tasks/Tasks/status!=COMPLETED';
  69. this.set('url', url);
  70. return url;
  71. },
  72. /**
  73. * Reload operations
  74. * We can call it manually <code>controller.loadOperations();</code>
  75. * or it fires automatically, when <code>isWorking</code> becomes <code>true</code>
  76. */
  77. loadOperations : function(){
  78. if(!this.get('isWorking')){
  79. return;
  80. }
  81. var self = this;
  82. var url = this.get('url');
  83. if(!url){
  84. url = this.generateUrl();
  85. }
  86. $.ajax({
  87. type: "GET",
  88. url: url,
  89. dataType: 'json',
  90. timeout: 5000,
  91. success: function (data) {
  92. //refresh model
  93. self.updateBackgroundOperations(data);
  94. //load data again if isWorking = true
  95. if(self.get('isWorking')){
  96. setTimeout(function(){
  97. self.loadOperations();
  98. }, self.get('updateInterval'));
  99. }
  100. },
  101. error: function (request, ajaxOptions, error) {
  102. console.log('cannot load background operations array');
  103. //next code is temporary code to fix testMode issues
  104. self.set('url', '/data/background_operations/list_on_start.json');
  105. //load data again if isWorking = true
  106. if(self.get('isWorking')){
  107. setTimeout(function(){
  108. self.loadOperations();
  109. }, self.get('updateInterval'));
  110. }
  111. },
  112. statusCode: require('data/statusCodes')
  113. });
  114. }.observes('isWorking'),
  115. /**
  116. * Add new operations to <code>this.allOperations</code> variable
  117. * @param data json loaded from server
  118. */
  119. updateBackgroundOperations : function(data){
  120. var runningTasks = [];
  121. data.items.forEach(function (item) {
  122. item.tasks.forEach(function (task) {
  123. if (task.Tasks.status == 'QUEUED' || task.Tasks.status == 'PENDING') {
  124. runningTasks.push(task.Tasks);
  125. }
  126. });
  127. });
  128. var currentTasks = this.get('allOperations');
  129. runningTasks.forEach(function(item){
  130. var task = currentTasks.findProperty('id', item.id);
  131. if(task){
  132. currentTasks[currentTasks.indexOf(task)] = item;
  133. } else {
  134. currentTasks.pushObject(item);
  135. }
  136. });
  137. for(var i = currentTasks.length-1; i>=0; i--){
  138. var isTaskFinished = !runningTasks.someProperty('id', currentTasks[i].id);
  139. if(isTaskFinished){
  140. currentTasks.removeAt(i);
  141. }
  142. }
  143. this.set('allOperationsCount', currentTasks.length);
  144. },
  145. /**
  146. * Onclick handler for background operations number located right to logo
  147. */
  148. showPopup: function(){
  149. App.ModalPopup.show({
  150. headerClass: Ember.View.extend({
  151. controllerBinding: 'App.router.backgroundOperationsController',
  152. template:Ember.Handlebars.compile('{{allOperationsCount}} Background Operations Running')
  153. }),
  154. bodyClass: Ember.View.extend({
  155. controllerBinding: 'App.router.backgroundOperationsController',
  156. templateName: require('templates/main/background_operations_popup')
  157. }),
  158. onPrimary: function() {
  159. this.hide();
  160. },
  161. secondary : null
  162. });
  163. }
  164. });