main.js 3.7 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. require('models/background_operation');
  20. App.MainController = Em.Controller.extend({
  21. name: 'mainController',
  22. backgroundOperations: [],
  23. backgroundOperationsCount : 0,
  24. backgroundOperationsUrl : '',
  25. intervalId: false,
  26. updateOperationsInterval: 6000,
  27. startLoadOperationsPeriodically: function() {
  28. this.loadBackgroundOperations();
  29. this.intervalId = setInterval(this.loadBackgroundOperations, this.get('updateOperationsInterval'));
  30. },
  31. stopLoadOperationsPeriodically:function () {
  32. if(this.intervalId) {
  33. clearInterval(this.intervalId);
  34. }
  35. this.intervalId = false;
  36. },
  37. loadBackgroundOperations: function(){
  38. var self = App.router.get('mainController');
  39. var url = self.get('backgroundOperationsUrl');
  40. if(!url){
  41. //cache url, not to execute <code>getClusterName</code> everytime
  42. url = (App.testMode) ?
  43. '/data/background_operations/list_on_start.json' :
  44. '/api/clusters/' + App.router.getClusterName() + '/requests/?fields=tasks/*&tasks/Tasks/status!=COMPLETED';
  45. self.set('backgroundOperationsUrl', url);
  46. }
  47. $.ajax({
  48. type: "GET",
  49. url: url,
  50. dataType: 'json',
  51. timeout: 5000,
  52. success: function (data) {
  53. self.updateBackgroundOperations(data);
  54. },
  55. error: function (request, ajaxOptions, error) {
  56. //do something
  57. },
  58. statusCode: require('data/statusCodes')
  59. });
  60. },
  61. /**
  62. * Add new operations to <code>this.backgroundOperations</code> variable
  63. * @param data json loaded from server
  64. */
  65. updateBackgroundOperations : function(data){
  66. var runningTasks = [];
  67. data.items.forEach(function (item) {
  68. item.tasks.forEach(function (task) {
  69. if (task.Tasks.status == 'QUEUED') {
  70. runningTasks.push(task.Tasks);
  71. }
  72. });
  73. });
  74. var currentTasks = this.get('backgroundOperations');
  75. runningTasks.forEach(function(item){
  76. var task = currentTasks.findProperty('id', item.id);
  77. if(task){
  78. currentTasks[currentTasks.indexOf(task)] = item;
  79. } else {
  80. currentTasks.pushObject(item);
  81. }
  82. });
  83. for(var i = currentTasks.length-1; i>=0; i--){
  84. var isTaskFinished = !runningTasks.someProperty('id', currentTasks[i].id);
  85. if(isTaskFinished){
  86. currentTasks.removeAt(i);
  87. }
  88. }
  89. this.set('backgroundOperationsCount', currentTasks.length);
  90. },
  91. showBackgroundOperationsPopup: function(){
  92. App.ModalPopup.show({
  93. headerClass: Ember.View.extend({
  94. controllerBinding: 'App.router.mainController',
  95. template:Ember.Handlebars.compile('{{backgroundOperationsCount}} Background Operations Running')
  96. }),
  97. bodyClass: Ember.View.extend({
  98. controllerBinding: 'App.router.mainController',
  99. templateName: require('templates/main/background_operations_popup')
  100. }),
  101. onPrimary: function() {
  102. this.hide();
  103. }
  104. });
  105. }
  106. })