heatmap.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. * Heatmap metrics that are available choices on the page
  25. */
  26. heatmapCategories: [],
  27. allHeatmaps:[],
  28. layoutNameSuffix: "_heatmap",
  29. sectionNameSuffix: "_HEATMAPS",
  30. 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',
  31. loadHeatmapsUrlParams: function() {
  32. var serviceName = this.get('content.serviceName');
  33. if (serviceName) {
  34. return 'WidgetInfo/widget_type=HEATMAP&WidgetInfo/scope=CLUSTER&WidgetInfo/metrics.matches(.*\"service_name\":\"' + serviceName + '\".*)&fields=WidgetInfo/metrics';
  35. } else {
  36. return 'WidgetInfo/widget_type=HEATMAP&WidgetInfo/scope=CLUSTER&fields=WidgetInfo/metrics';
  37. }
  38. }.property('content.serviceName'),
  39. selectedMetric: null,
  40. inputMaximum: '',
  41. /**
  42. * Heatmap widget currently shown on the page
  43. */
  44. activeWidget: function() {
  45. if (this.get('widgets') && this.get('widgets').length) {
  46. return this.get('widgets')[0];
  47. } else {
  48. return false;
  49. }
  50. }.property('widgets.@each'),
  51. /**
  52. * This function is called from the binded view of the controller
  53. */
  54. loadPageData: function() {
  55. var self = this;
  56. this.resetPageData();
  57. this.getAllHeatMaps().done(function(allHeatmapData){
  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.loadActiveWidgetLayout();
  64. });
  65. },
  66. /**
  67. * categorize heatmaps with respect to service names
  68. * @param {Array} allHeatmaps
  69. * @return {Array}
  70. */
  71. categorizeByServiceName: function(allHeatmaps) {
  72. var categories = [];
  73. allHeatmaps.forEach(function(_heatmap){
  74. var serviceNames = JSON.parse(_heatmap.metrics).mapProperty('service_name').uniq();
  75. serviceNames.forEach(function(_serviceName){
  76. var category = categories.findProperty('serviceName',_serviceName);
  77. if (!category) {
  78. categories.pushObject(Em.Object.create({
  79. serviceName: _serviceName,
  80. displayName: _serviceName === 'STACK' ? 'Host' : App.StackService.find().findProperty('serviceName',_serviceName).get('displayName'),
  81. heatmaps: [_heatmap]
  82. }));
  83. } else {
  84. category.get('heatmaps').pushObject(_heatmap);
  85. }
  86. },this);
  87. },this);
  88. return categories;
  89. },
  90. /**
  91. * clears/resets the data. This function should be called every time user navigates to heatmap page
  92. */
  93. resetPageData: function() {
  94. this.get('heatmapCategories').clear();
  95. this.get('allHeatmaps').clear();
  96. },
  97. /**
  98. * success callback of <code>loadActiveWidgetLayout()</code>
  99. * @overrriden
  100. * @param {object|null} data
  101. */
  102. loadActiveWidgetLayoutSuccessCallback: function (data) {
  103. if (data.items[0]) {
  104. App.widgetMapper.map(data.items[0].WidgetLayoutInfo);
  105. App.widgetLayoutMapper.map(data);
  106. this.set('activeWidgetLayout', App.WidgetLayout.find().findProperty('layoutName', this.get('defaultLayoutName')));
  107. this.set('isWidgetsLoaded', true);
  108. }
  109. },
  110. /**
  111. * Gets all heatmap widgets that should be available in select metrics dropdown on heatmap page
  112. * @return {$.ajax}
  113. */
  114. getAllHeatMaps: function() {
  115. var urlParams = this.get('loadHeatmapsUrlParams');
  116. return App.ajax.send({
  117. name: 'widgets.get',
  118. sender: this,
  119. data: {
  120. urlParams: urlParams
  121. }
  122. });
  123. },
  124. /**
  125. * get hosts from server
  126. */
  127. loadRacks: function () {
  128. this.get('racks').clear();
  129. this.set('rackMap', {});
  130. var urlParams = this.get('loadRacksUrlParams');
  131. return App.ajax.send({
  132. name: 'hosts.heatmaps',
  133. sender: this,
  134. data: {
  135. urlParams: urlParams
  136. }
  137. });
  138. },
  139. loadRacksSuccessCallback: function (data, opt, params) {
  140. var hosts = [];
  141. data.items.forEach(function (item) {
  142. hosts.push({
  143. hostName: item.Hosts.host_name,
  144. publicHostName: item.Hosts.public_host_name,
  145. osType: item.Hosts.os_type,
  146. ip: item.Hosts.ip,
  147. rack: item.Hosts.rack_info,
  148. diskTotal: item.metrics ? item.metrics.disk.disk_total : 0,
  149. diskFree: item.metrics ? item.metrics.disk.disk_free : 0,
  150. cpuSystem: item.metrics ? item.metrics.cpu.cpu_system : 0,
  151. cpuUser: item.metrics ? item.metrics.cpu.cpu_user : 0,
  152. memTotal: item.metrics ? item.metrics.memory.mem_total : 0,
  153. memFree: item.metrics ? item.metrics.memory.mem_free : 0,
  154. hostComponents: item.host_components.mapProperty('HostRoles.component_name')
  155. });
  156. });
  157. var rackMap = this.indexByRackId(hosts);
  158. var racks = this.toList(rackMap);
  159. //this list has an empty host array property
  160. this.set('rackMap', rackMap);
  161. this.set('racks', racks);
  162. },
  163. indexByRackId: function (hosts) {
  164. var rackMap = {};
  165. hosts.forEach(function (host) {
  166. var rackId = host.rack;
  167. if(!rackMap[rackId]) {
  168. rackMap[rackId] =
  169. Em.Object.create({
  170. name: rackId,
  171. rackId: rackId,
  172. hosts: [host]
  173. });
  174. } else {
  175. rackMap[rackId].hosts.push(host);
  176. }
  177. });
  178. return rackMap;
  179. },
  180. toList: function (rackMap) {
  181. var racks = [];
  182. var i = 0;
  183. for (var rackKey in rackMap) {
  184. if (rackMap.hasOwnProperty(rackKey)) {
  185. racks.push(
  186. Em.Object.create({
  187. name: rackKey,
  188. rackId: rackKey,
  189. hosts: rackMap[rackKey].hosts,
  190. isLoaded: false,
  191. index: i++
  192. })
  193. );
  194. }
  195. }
  196. return racks;
  197. },
  198. validation: function () {
  199. if (this.get('selectedMetric')) {
  200. if (/^\d+$/.test(this.get('inputMaximum'))) {
  201. $('#inputMaximum').removeClass('error');
  202. this.set('selectedMetric.maximumValue', this.get('inputMaximum'));
  203. } else {
  204. $('#inputMaximum').addClass('error');
  205. }
  206. }
  207. }.observes('inputMaximum'),
  208. addRackView: function (view) {
  209. this.get('rackViews').push(view);
  210. if (this.get('rackViews').length == this.get('racks').length) {
  211. this.displayAllRacks();
  212. }
  213. },
  214. displayAllRacks: function () {
  215. if (this.get('rackViews').length) {
  216. this.get('rackViews').pop().displayHosts();
  217. this.displayAllRacks();
  218. }
  219. },
  220. showHeatMapMetric: function (event) {
  221. var self = this;
  222. var metricItem = Em.Object.create(event.context);
  223. this.saveWidgetLayout([metricItem]).done(function(){
  224. self.loadActiveWidgetLayout();
  225. });
  226. },
  227. hostToSlotMap: function () {
  228. return this.get('selectedMetric.hostToSlotMap');
  229. }.property('selectedMetric.hostToSlotMap'),
  230. /**
  231. * return class name for to be used for containing each rack.
  232. *
  233. * @this App.MainChartsHeatmapController
  234. */
  235. rackClass: function () {
  236. var rackCount = this.get('racks.length');
  237. if (rackCount < 2) {
  238. return "span12";
  239. } else if (rackCount == 2) {
  240. return "span6";
  241. } else {
  242. return "span4";
  243. }
  244. }.property('racks.length')
  245. });