background_operations_controller.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. getOperationsForRequestId: function(requestId){
  28. return this.get('allOperations').filterProperty('request_id', requestId);
  29. },
  30. updateInterval: App.bgOperationsUpdateInterval,
  31. url : '',
  32. generateUrl: function(){
  33. var url = App.testMode ?
  34. '/data/background_operations/list_on_start.json' :
  35. App.apiPrefix + '/clusters/' + App.router.getClusterName() + '/requests/?fields=tasks/*&tasks/Tasks/status!=COMPLETED';
  36. this.set('url', url);
  37. return url;
  38. },
  39. timeoutId : null,
  40. /**
  41. * Background operations will not be working if receive <code>attemptsCount</code> response with errors
  42. */
  43. attemptsCount: 20,
  44. errorsCount: 0,
  45. /**
  46. * Call this.loadOperations with delay
  47. * @param delay time in milliseconds (updateInterval by default)
  48. * @param reason reason why we call it(used to calculate count of errors)
  49. */
  50. loadOperationsDelayed: function(delay, reason){
  51. delay = delay || this.get('updateInterval');
  52. var self = this;
  53. if(reason && reason.indexOf('error:clusterName:') === 0){
  54. var errors = this.get('errorsCount') + 1;
  55. this.set('errorsCount', errors);
  56. if(errors > this.get('attemptsCount')){
  57. console.log('Stop loading background operations: clusterName is undefined');
  58. return;
  59. }
  60. }
  61. this.set('timeoutId',
  62. setTimeout(function(){
  63. self.loadOperations();
  64. }, delay)
  65. );
  66. },
  67. /**
  68. * Reload operations
  69. * We can call it manually <code>controller.loadOperations();</code>
  70. * or it fires automatically, when <code>isWorking</code> becomes <code>true</code>
  71. */
  72. loadOperations : function(){
  73. var timeoutId = this.get('timeoutId');
  74. if(timeoutId){
  75. clearTimeout(timeoutId);
  76. this.set('timeoutId', null);
  77. }
  78. if(!this.get('isWorking')){
  79. return;
  80. }
  81. var self = this;
  82. if(!App.router.getClusterName()){
  83. this.loadOperationsDelayed(this.get('updateInterval')/2, 'error:clusterName');
  84. return;
  85. }
  86. var url = this.get('url');
  87. if(!url){
  88. url = this.generateUrl();
  89. }
  90. $.ajax({
  91. type: "GET",
  92. url: url,
  93. dataType: 'json',
  94. timeout: App.timeout,
  95. success: function (data) {
  96. //refresh model
  97. self.updateBackgroundOperations(data);
  98. self.loadOperationsDelayed();
  99. },
  100. error: function (request, ajaxOptions, error) {
  101. self.loadOperationsDelayed(null, 'error:response error');
  102. },
  103. statusCode: require('data/statusCodes')
  104. });
  105. }.observes('isWorking'),
  106. /**
  107. * Add new operations to <code>this.allOperations</code> variable
  108. * @param data json loaded from server
  109. */
  110. updateBackgroundOperations : function(data){
  111. var runningTasks = [];
  112. data.items.forEach(function (item) {
  113. item.tasks.forEach(function (task) {
  114. if (task.Tasks.status == 'QUEUED' || task.Tasks.status == 'PENDING') {
  115. runningTasks.push(task.Tasks);
  116. }
  117. });
  118. });
  119. runningTasks = runningTasks.sort(function(a,b){
  120. return a.id - b.id;
  121. });
  122. var currentTasks = this.get('allOperations');
  123. runningTasks.forEach(function(item){
  124. var task = currentTasks.findProperty('id', item.id);
  125. if(task){
  126. currentTasks[currentTasks.indexOf(task)] = item;
  127. } else {
  128. currentTasks.pushObject(item);
  129. }
  130. });
  131. for(var i = currentTasks.length-1; i>=0; i--){
  132. var isTaskFinished = !runningTasks.someProperty('id', currentTasks[i].id);
  133. if(isTaskFinished){
  134. currentTasks.removeAt(i);
  135. }
  136. }
  137. this.set('allOperationsCount', currentTasks.length);
  138. var eventsArray = this.get('eventsArray');
  139. if(eventsArray.length){
  140. var itemsToRemove = [];
  141. eventsArray.forEach(function(item){
  142. //if when returns true
  143. if(item.when(this)){
  144. //fire do method
  145. item.do();
  146. //and remove it
  147. itemsToRemove.push(item);
  148. }
  149. }, this);
  150. itemsToRemove.forEach(function(item){
  151. eventsArray.splice(eventsArray.indexOf(item), 1);
  152. });
  153. }
  154. },
  155. /**
  156. * Onclick handler for background operations number located right to logo
  157. */
  158. showPopup: function(){
  159. this.loadOperations();
  160. App.ModalPopup.show({
  161. headerClass: Ember.View.extend({
  162. controllerBinding: 'App.router.backgroundOperationsController',
  163. template:Ember.Handlebars.compile('{{allOperationsCount}} Background Operations Running')
  164. }),
  165. bodyClass: Ember.View.extend({
  166. controllerBinding: 'App.router.backgroundOperationsController',
  167. templateName: require('templates/main/background_operations_popup')
  168. }),
  169. onPrimary: function() {
  170. this.hide();
  171. },
  172. secondary : null
  173. });
  174. },
  175. /**
  176. * Exaple of data inside:
  177. * {
  178. * when : function(backgroundOperationsController){
  179. * return backgroundOperationsController.getOperationsForRequestId(requestId).length == 0;
  180. * },
  181. * do : function(){
  182. * component.set('status', 'cool');
  183. * }
  184. * }
  185. *
  186. * Function <code>do</code> will be fired once, when <code>when</code> returns true.
  187. * Example, how to use it, you can see in app\controllers\main\host\details.js
  188. */
  189. eventsArray : []
  190. });