main.js 3.9 KB

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