heatmap.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. App.MainChartsHeatmapController = Em.Controller.extend(App.WidgetSectionMixin, {
  19. name: 'mainChartsHeatmapController',
  20. rackMap: {},
  21. racks: [],
  22. rackViews: [],
  23. /**
  24. * @type {boolean}
  25. */
  26. isLoaded: false,
  27. /**
  28. * Heatmap metrics that are available choices on the page
  29. */
  30. heatmapCategories: [],
  31. allHeatmaps:[],
  32. layoutNameSuffix: "_heatmap",
  33. sectionNameSuffix: "_HEATMAPS",
  34. loadRacksUrlParams: 'fields=Hosts/rack_info,Hosts/host_name,Hosts/public_host_name,Hosts/os_type,Hosts/ip,host_components,metrics/disk,metrics/cpu/cpu_system,metrics/cpu/cpu_user,metrics/memory/mem_total,metrics/memory/mem_free&minimal_response=true',
  35. loadHeatmapsUrlParams: function() {
  36. var serviceName = this.get('content.serviceName');
  37. if (serviceName) {
  38. return 'WidgetInfo/widget_type=HEATMAP&WidgetInfo/scope=CLUSTER&WidgetInfo/metrics.matches(.*\"service_name\":\"' + serviceName + '\".*)&fields=WidgetInfo/metrics';
  39. } else {
  40. return 'WidgetInfo/widget_type=HEATMAP&WidgetInfo/scope=CLUSTER&fields=WidgetInfo/metrics';
  41. }
  42. }.property('content.serviceName'),
  43. selectedMetric: null,
  44. inputMaximum: '',
  45. /**
  46. * Heatmap widget currently shown on the page
  47. */
  48. activeWidget: Em.computed.alias('widgets.firstObject'),
  49. /**
  50. * This function is called from the bound view of the controller
  51. */
  52. loadPageData: function () {
  53. var self = this;
  54. this.loadRacks().always(function () {
  55. self.resetPageData();
  56. self.getAllHeatMaps().done(function (allHeatmapData) {
  57. self.set('isLoaded', true);
  58. allHeatmapData.items.forEach(function (_allHeatmapData) {
  59. self.get('allHeatmaps').pushObject(_allHeatmapData.WidgetInfo);
  60. });
  61. var categories = self.categorizeByServiceName(self.get('allHeatmaps'));
  62. self.set('heatmapCategories', categories);
  63. self.getActiveWidgetLayout();
  64. });
  65. });
  66. },
  67. /**
  68. * categorize heatmaps with respect to service names
  69. * @param {Array} allHeatmaps
  70. * @return {Array}
  71. */
  72. categorizeByServiceName: function(allHeatmaps) {
  73. var categories = [];
  74. allHeatmaps.forEach(function(_heatmap){
  75. var serviceNames = JSON.parse(_heatmap.metrics).mapProperty('service_name').uniq();
  76. serviceNames.forEach(function(_serviceName){
  77. var category = categories.findProperty('serviceName',_serviceName);
  78. if (!category) {
  79. categories.pushObject(Em.Object.create({
  80. serviceName: _serviceName,
  81. displayName: _serviceName === 'STACK' ? 'Host' : App.StackService.find().findProperty('serviceName',_serviceName).get('displayName'),
  82. heatmaps: [_heatmap]
  83. }));
  84. } else {
  85. category.get('heatmaps').pushObject(_heatmap);
  86. }
  87. },this);
  88. },this);
  89. return categories;
  90. },
  91. /**
  92. * clears/resets the data. This function should be called every time user navigates to heatmap page
  93. */
  94. resetPageData: function() {
  95. this.get('heatmapCategories').clear();
  96. this.get('allHeatmaps').clear();
  97. },
  98. /**
  99. * Gets all heatmap widgets that should be available in select metrics dropdown on heatmap page
  100. * @return {$.ajax}
  101. */
  102. getAllHeatMaps: function() {
  103. var urlParams = this.get('loadHeatmapsUrlParams');
  104. return App.ajax.send({
  105. name: 'widgets.get',
  106. sender: this,
  107. data: {
  108. urlParams: urlParams,
  109. sectionName: this.get('sectionName')
  110. }
  111. });
  112. },
  113. /**
  114. * get hosts from server
  115. */
  116. loadRacks: function () {
  117. this.get('racks').clear();
  118. this.set('rackMap', {});
  119. var urlParams = this.get('loadRacksUrlParams');
  120. return App.ajax.send({
  121. name: 'hosts.heatmaps',
  122. sender: this,
  123. data: {
  124. urlParams: urlParams
  125. },
  126. success: 'loadRacksSuccessCallback'
  127. });
  128. },
  129. loadRacksSuccessCallback: function (data, opt, params) {
  130. var hosts = [];
  131. data.items.forEach(function (item) {
  132. hosts.push({
  133. hostName: item.Hosts.host_name,
  134. publicHostName: item.Hosts.public_host_name,
  135. osType: item.Hosts.os_type,
  136. ip: item.Hosts.ip,
  137. rack: item.Hosts.rack_info,
  138. diskTotal: item.metrics ? item.metrics.disk.disk_total : 0,
  139. diskFree: item.metrics ? item.metrics.disk.disk_free : 0,
  140. cpuSystem: item.metrics ? item.metrics.cpu.cpu_system : 0,
  141. cpuUser: item.metrics ? item.metrics.cpu.cpu_user : 0,
  142. memTotal: item.metrics ? item.metrics.memory.mem_total : 0,
  143. memFree: item.metrics ? item.metrics.memory.mem_free : 0,
  144. hostComponents: item.host_components.mapProperty('HostRoles.component_name')
  145. });
  146. });
  147. var rackMap = this.indexByRackId(hosts);
  148. var racks = this.toList(rackMap);
  149. //this list has an empty host array property
  150. this.set('rackMap', rackMap);
  151. this.set('racks', racks);
  152. },
  153. indexByRackId: function (hosts) {
  154. var rackMap = {};
  155. hosts.forEach(function (host) {
  156. var rackId = host.rack;
  157. if(!rackMap[rackId]) {
  158. rackMap[rackId] =
  159. Em.Object.create({
  160. name: rackId,
  161. rackId: rackId,
  162. hosts: [host]
  163. });
  164. } else {
  165. rackMap[rackId].hosts.push(host);
  166. }
  167. });
  168. return rackMap;
  169. },
  170. toList: function (rackMap) {
  171. var racks = [];
  172. var i = 0;
  173. for (var rackKey in rackMap) {
  174. if (rackMap.hasOwnProperty(rackKey)) {
  175. racks.push(
  176. Em.Object.create({
  177. name: rackKey,
  178. rackId: rackKey,
  179. hosts: rackMap[rackKey].hosts,
  180. isLoaded: false,
  181. index: i++
  182. })
  183. );
  184. }
  185. }
  186. return racks;
  187. },
  188. validation: function () {
  189. if (this.get('selectedMetric')) {
  190. if (/^\d+$/.test(this.get('inputMaximum'))) {
  191. $('#inputMaximum').removeClass('error');
  192. this.set('selectedMetric.maximumValue', this.get('inputMaximum'));
  193. } else {
  194. $('#inputMaximum').addClass('error');
  195. }
  196. }
  197. }.observes('inputMaximum'),
  198. addRackView: function (view) {
  199. this.get('rackViews').push(view);
  200. if (this.get('rackViews').length == this.get('racks').length) {
  201. this.displayAllRacks();
  202. }
  203. },
  204. displayAllRacks: function () {
  205. if (this.get('rackViews').length) {
  206. this.get('rackViews').pop().displayHosts();
  207. this.displayAllRacks();
  208. }
  209. },
  210. showHeatMapMetric: function (event) {
  211. var self = this;
  212. var metricItem = Em.Object.create(event.context);
  213. this.saveWidgetLayout([metricItem]).done(function(){
  214. self.getActiveWidgetLayout();
  215. });
  216. },
  217. hostToSlotMap: Em.computed.alias('selectedMetric.hostToSlotMap'),
  218. /**
  219. * return class name for to be used for containing each rack.
  220. *
  221. * @this App.MainChartsHeatmapController
  222. */
  223. rackClass: function () {
  224. var rackCount = this.get('racks.length');
  225. if (rackCount < 2) {
  226. return "span12";
  227. } else if (rackCount == 2) {
  228. return "span6";
  229. } else {
  230. return "span4";
  231. }
  232. }.property('racks.length')
  233. });