update_controller.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.UpdateController = Em.Controller.extend({
  20. name:'updateController',
  21. isUpdated:false,
  22. cluster:null,
  23. isWorking: false,
  24. timeIntervalId: null,
  25. clusterName:function () {
  26. return App.router.get('clusterController.clusterName');
  27. }.property('App.router.clusterController.clusterName'),
  28. getUrl:function (testUrl, url) {
  29. return (App.testMode) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
  30. },
  31. /**
  32. * Wrapper for all updates
  33. */
  34. updateAll:function(){
  35. if(this.get('isWorking')) {
  36. this.update('updateHost');
  37. this.update('updateServiceMetric');
  38. this.update('graphsUpdate');
  39. }
  40. }.observes('isWorking'),
  41. /**
  42. * States for each update method (each field - method name)
  43. */
  44. states: {
  45. 'updateHost': null,
  46. 'updateServiceMetric': null,
  47. 'graphsUpdate': null
  48. },
  49. /**
  50. * Callback for each update method
  51. * @param {String} name - state name
  52. * @return {Function}
  53. */
  54. updateCallback: function(name) {
  55. var self = this;
  56. return function() {
  57. self.update(name);
  58. }
  59. },
  60. /**
  61. * Common method that executes provided by name update method (name from states object)
  62. * @param {String} name - key in the states object
  63. * @return {Boolean}
  64. */
  65. update: function(name) {
  66. if(!this.get('isWorking')) {
  67. return false;
  68. }
  69. clearTimeout(this.states[name]);
  70. var self = this;
  71. this.states[name] = setTimeout(function() {
  72. self[name](self.updateCallback(name));
  73. }, App.contentUpdateInterval);
  74. },
  75. updateHost:function(callback) {
  76. var self = this;
  77. var hostsUrl = this.getUrl('/data/hosts/hosts.json', '/hosts?fields=Hosts/host_name,Hosts/public_host_name,Hosts/cpu_count,Hosts/total_mem,Hosts/host_status,Hosts/last_heartbeat_time,Hosts/os_arch,Hosts/os_type,Hosts/ip,host_components,metrics/disk,metrics/cpu,metrics/load,metrics/memory');
  78. App.HttpClient.get(hostsUrl, App.hostsMapper, {
  79. complete:function (jqXHR, textStatus) {
  80. callback();
  81. }
  82. });
  83. },
  84. graphs: [],
  85. graphsUpdate: function (callback) {
  86. var existedGraphs = [];
  87. this.get('graphs').forEach(function (_graph) {
  88. var view = Em.View.views[_graph.id];
  89. if (view) {
  90. existedGraphs.push(_graph);
  91. //console.log('updated graph', _graph.name);
  92. view.loadData();
  93. //if graph opened as modal popup update it to
  94. if($(".modal-graph-line .modal-body #" + _graph.popupId + "-container-popup").length) {
  95. view.loadData();
  96. }
  97. }
  98. });
  99. callback();
  100. this.set('graphs', existedGraphs);
  101. },
  102. /**
  103. * Updates the services information.
  104. *
  105. * @param callback
  106. * @param isInitialLoad If true, only basic information is loaded.
  107. */
  108. updateServiceMetric: function (callback, isInitialLoad) {
  109. var self = this;
  110. self.set('isUpdated', false);
  111. var servicesUrl = isInitialLoad ?
  112. this.getUrl('/data/dashboard/services.json', '/services?fields=components/ServiceComponentInfo,components/host_components,components/host_components/HostRoles') :
  113. this.getUrl('/data/dashboard/services.json', '/services?fields=components/ServiceComponentInfo,components/host_components,components/host_components/HostRoles,components/host_components/metrics/jvm/memHeapUsedM,components/host_components/metrics/jvm/memHeapCommittedM,components/host_components/metrics/mapred/jobtracker/trackers_decommissioned');
  114. var callback = callback || function (jqXHR, textStatus) {
  115. self.set('isUpdated', true);
  116. };
  117. App.HttpClient.get(servicesUrl, App.servicesMapper, {
  118. complete: function() {
  119. callback();
  120. }
  121. });
  122. }
  123. });