widgets.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. var filters = require('views/common/filter_view');
  20. App.MainDashboardWidgetsView = Em.View.extend(App.UserPref, App.LocalStorage, App.TimeRangeMixin, {
  21. name: 'mainDashboardWidgetsView',
  22. templateName: require('templates/main/dashboard/widgets'),
  23. didInsertElement: function () {
  24. this._super();
  25. this.setWidgetsDataModel();
  26. this.setInitPrefObject();
  27. this.setOnLoadVisibleWidgets();
  28. this.set('isDataLoaded', true);
  29. App.loadTimer.finish('Dashboard Metrics Page');
  30. Em.run.next(this, 'makeSortable');
  31. },
  32. /**
  33. * List of services
  34. * @type {Ember.Enumerable}
  35. */
  36. content: [],
  37. /**
  38. * @type {boolean}
  39. */
  40. isDataLoaded: false,
  41. /**
  42. * Define if some widget is currently moving
  43. * @type {boolean}
  44. */
  45. isMoving: false,
  46. timeRangeClassName: 'pull-left',
  47. /**
  48. * Make widgets' list sortable on New Dashboard style
  49. */
  50. makeSortable: function () {
  51. var self = this;
  52. $("#sortable").sortable({
  53. items: "> div",
  54. //placeholder: "sortable-placeholder",
  55. cursor: "move",
  56. tolerance: "pointer",
  57. scroll: false,
  58. update: function (event, ui) {
  59. if (!App.get('testMode')) {
  60. // update persist then translate to real
  61. var widgetsArray = $('div[viewid]'); // get all in DOM
  62. self.getUserPref(self.get('persistKey')).complete(function () {
  63. var oldValue = self.get('currentPrefObject') || self.getDBProperty(self.get('persistKey'));
  64. var newValue = Em.Object.create({
  65. dashboardVersion: oldValue.dashboardVersion,
  66. visible: [],
  67. hidden: oldValue.hidden,
  68. threshold: oldValue.threshold
  69. });
  70. var size = oldValue.visible.length;
  71. for (var j = 0; j <= size - 1; j++) {
  72. var viewID = widgetsArray.get(j).getAttribute('viewid');
  73. var id = viewID.split("-").get(1);
  74. newValue.visible.push(id);
  75. }
  76. self.postUserPref(self.get('persistKey'), newValue);
  77. self.setDBProperty(self.get('persistKey'), newValue);
  78. //self.translateToReal(newValue);
  79. });
  80. }
  81. },
  82. activate: function (event, ui) {
  83. self.set('isMoving', true);
  84. },
  85. deactivate: function (event, ui) {
  86. self.set('isMoving', false);
  87. }
  88. }).disableSelection();
  89. },
  90. /**
  91. * Set Service model values
  92. */
  93. setWidgetsDataModel: function () {
  94. if (App.get('services.hostMetrics').length > 0) {
  95. this.set('host_metrics_model', App.get('services.hostMetrics'));
  96. }
  97. App.Service.find().forEach(function (item) {
  98. var extendedModel = App.Service.extendedModel[item.get('serviceName')];
  99. var key = item.get('serviceName').toLowerCase() + '_model';
  100. if (extendedModel && App[extendedModel].find(item.get('id'))) {
  101. this.set(key, App[extendedModel].find(item.get('id')));
  102. } else {
  103. this.set(key, item);
  104. }
  105. }, this);
  106. },
  107. /**
  108. * Load widget statuses to <code>initPrefObject</code>
  109. */
  110. setInitPrefObject: function () {
  111. //in case of some service not installed
  112. var visibleFull = [
  113. '2', '4', '11', //hdfs
  114. '6', '7', '8', '9', //host metrics
  115. '1', '5', '3', '10', //hdfs
  116. '13', '12', '14', '16', //hbase
  117. '17', '18', '19', '20', '23', // all yarn
  118. '21', // storm
  119. '22', // flume
  120. '24' // hawq
  121. ]; // all in order
  122. var hiddenFull = [
  123. ['15', 'Region In Transition']
  124. ];
  125. // Display widgets for host metrics if the stack definition has a host metrics service to display it.
  126. if (this.get('host_metrics_model') == null) {
  127. var hostMetrics = ['6', '7', '8', '9'];
  128. hostMetrics.forEach(function (item) {
  129. visibleFull = visibleFull.without(item);
  130. }, this);
  131. }
  132. if (this.get('hdfs_model') == null) {
  133. var hdfs = ['1', '2', '3', '4', '5', '10', '11'];
  134. hdfs.forEach(function (item) {
  135. visibleFull = visibleFull.without(item);
  136. }, this);
  137. }
  138. if (this.get('hbase_model') == null) {
  139. var hbase = ['12', '13', '14', '16'];
  140. hbase.forEach(function (item) {
  141. visibleFull = visibleFull.without(item);
  142. }, this);
  143. hiddenFull = [];
  144. }
  145. if (this.get('yarn_model') == null) {
  146. var yarn = ['17', '18', '19', '20', '23'];
  147. yarn.forEach(function (item) {
  148. visibleFull = visibleFull.without(item);
  149. }, this);
  150. }
  151. if (this.get('storm_model') == null) {
  152. var storm = ['21'];
  153. storm.forEach(function (item) {
  154. visibleFull = visibleFull.without(item);
  155. }, this);
  156. }
  157. if (this.get('flume_model') == null) {
  158. var flume = ['22'];
  159. flume.forEach(function (item) {
  160. visibleFull = visibleFull.without(item);
  161. }, this);
  162. }
  163. if (this.get('hawq_model') == null) {
  164. var hawq = ['24'];
  165. hawq.forEach(function (item) {
  166. visibleFull = visibleFull.without(item);
  167. }, this);
  168. }
  169. var obj = this.get('initPrefObject');
  170. obj.set('visible', visibleFull);
  171. obj.set('hidden', hiddenFull);
  172. },
  173. host_metrics_model: null,
  174. hdfs_model: null,
  175. mapreduce2_model: null,
  176. yarn_model: null,
  177. hbase_model: null,
  178. storm_model: null,
  179. flume_model: null,
  180. hawq_model: null,
  181. /**
  182. * List of visible widgets
  183. * @type {Ember.Enumerable}
  184. */
  185. visibleWidgets: [],
  186. /**
  187. * List of hidden widgets
  188. * @type {Ember.Enumerable}
  189. */
  190. hiddenWidgets: [], // widget child view will push object in this array if deleted
  191. /**
  192. * Submenu view for New Dashboard style
  193. * @type {Ember.View}
  194. * @class
  195. */
  196. plusButtonFilterView: Ember.View.extend({
  197. templateName: require('templates/main/dashboard/plus_button_filter'),
  198. hiddenWidgetsBinding: 'parentView.hiddenWidgets',
  199. visibleWidgetsBinding: 'parentView.visibleWidgets',
  200. valueBinding: '',
  201. widgetCheckbox: Em.Checkbox.extend({
  202. didInsertElement: function () {
  203. $('.checkbox').click(function (event) {
  204. event.stopPropagation();
  205. });
  206. }
  207. }),
  208. closeFilter: Em.K,
  209. applyFilter: function () {
  210. var self = this;
  211. var parent = this.get('parentView');
  212. var hiddenWidgets = this.get('hiddenWidgets');
  213. var checkedWidgets = hiddenWidgets.filterProperty('checked', true);
  214. if (App.get('testMode')) {
  215. var visibleWidgets = this.get('visibleWidgets');
  216. checkedWidgets.forEach(function (item) {
  217. var newObj = parent.widgetsMapper(item.id);
  218. visibleWidgets.pushObject(newObj);
  219. hiddenWidgets.removeObject(item);
  220. }, this);
  221. } else {
  222. //save in persist
  223. parent.getUserPref(parent.get('persistKey')).complete(function(){
  224. self.applyFilterComplete.apply(self);
  225. });
  226. }
  227. },
  228. applyFilterComplete: function () {
  229. var parent = this.get('parentView'),
  230. hiddenWidgets = this.get('hiddenWidgets'),
  231. oldValue = parent.get('currentPrefObject'),
  232. newValue = Em.Object.create({
  233. dashboardVersion: oldValue.dashboardVersion,
  234. visible: oldValue.visible,
  235. hidden: [],
  236. threshold: oldValue.threshold
  237. });
  238. hiddenWidgets.filterProperty('checked').forEach(function (item) {
  239. newValue.visible.push(item.id);
  240. hiddenWidgets.removeObject(item);
  241. }, this);
  242. hiddenWidgets.forEach(function (item) {
  243. newValue.hidden.push([item.id, item.displayName]);
  244. }, this);
  245. parent.postUserPref(parent.get('persistKey'), newValue);
  246. parent.translateToReal(newValue);
  247. }
  248. }),
  249. /**
  250. * Translate from Json value got from persist to real widgets view
  251. */
  252. translateToReal: function (value) {
  253. var version = value.dashboardVersion;
  254. var visible = value.visible;
  255. var hidden = value.hidden;
  256. var threshold = value.threshold;
  257. if (version == 'new') {
  258. var visibleWidgets = [];
  259. var hiddenWidgets = [];
  260. // re-construct visibleWidgets and hiddenWidgets
  261. for (var i = 0; i < visible.length; i++) {
  262. var id = visible[i];
  263. var widgetClass = this.widgetsMapper(id);
  264. //override with new threshold
  265. if (threshold[id].length > 0) {
  266. widgetClass.reopen({
  267. thresh1: threshold[id][0],
  268. thresh2: threshold[id][1]
  269. });
  270. }
  271. visibleWidgets.pushObject(widgetClass);
  272. }
  273. for (var j = 0; j < hidden.length; j++) {
  274. var title = hidden[j][1];
  275. hiddenWidgets.pushObject(Em.Object.create({displayName: title, id: hidden[j][0], checked: false}));
  276. }
  277. this.set('visibleWidgets', visibleWidgets);
  278. this.set('hiddenWidgets', hiddenWidgets);
  279. }
  280. },
  281. /**
  282. * Set visibility-status for widgets
  283. */
  284. setOnLoadVisibleWidgets: function () {
  285. var self = this;
  286. if (App.get('testMode')) {
  287. this.translateToReal(this.get('initPrefObject'));
  288. } else {
  289. // called when first load/refresh/jump back page
  290. this.getUserPref(this.get('persistKey')).complete(function () {
  291. self.setOnLoadVisibleWidgetsComplete.apply(self);
  292. });
  293. }
  294. },
  295. /**
  296. * complete load of visible widgets
  297. */
  298. setOnLoadVisibleWidgetsComplete: function () {
  299. var currentPrefObject = this.get('currentPrefObject') || this.getDBProperty(this.get('persistKey'));
  300. if (currentPrefObject) { // fit for no dashboard version
  301. if (!currentPrefObject.dashboardVersion) {
  302. currentPrefObject.dashboardVersion = 'new';
  303. this.postUserPref(this.get('persistKey'), currentPrefObject);
  304. this.setDBProperty(this.get('persistKey'), currentPrefObject);
  305. }
  306. this.set('currentPrefObject', this.checkServicesChange(currentPrefObject));
  307. this.translateToReal(this.get('currentPrefObject'));
  308. }
  309. else {
  310. // post persist then translate init object
  311. this.postUserPref(this.get('persistKey'), this.get('initPrefObject'));
  312. this.setDBProperty(this.get('persistKey'), this.get('initPrefObject'));
  313. this.translateToReal(this.get('initPrefObject'));
  314. }
  315. },
  316. /**
  317. * Remove widget from visible and hidden lists
  318. * @param {Object} value
  319. * @param {Object} widget
  320. * @returns {*}
  321. */
  322. removeWidget: function (value, widget) {
  323. value.visible = value.visible.without(widget);
  324. for (var j = 0; j < value.hidden.length; j++) {
  325. if (value.hidden[j][0] == widget) {
  326. value.hidden.splice(j, 1);
  327. }
  328. }
  329. return value;
  330. },
  331. /**
  332. * Check if widget is in visible or hidden list
  333. * @param {Object} value
  334. * @param {Object} widget
  335. * @returns {bool}
  336. */
  337. containsWidget: function (value, widget) {
  338. var flag = value.visible.contains(widget);
  339. for (var j = 0; j < value.hidden.length; j++) {
  340. if (!flag && value.hidden[j][0] == widget) {
  341. flag = true;
  342. break;
  343. }
  344. }
  345. return flag;
  346. },
  347. /**
  348. * check if stack has upgraded from HDP 1.0 to 2.0 OR add/delete services.
  349. * Update the value on server if true.
  350. * @param {Object} currentPrefObject
  351. * @return {Object}
  352. */
  353. checkServicesChange: function (currentPrefObject) {
  354. var toDelete = $.extend(true, {}, currentPrefObject);
  355. var toAdd = [];
  356. var serviceWidgetsMap = {
  357. hdfs_model: ['1', '2', '3', '4', '5', '10', '11'],
  358. host_metrics_model: ['6', '7', '8', '9'],
  359. hbase_model: ['12', '13', '14', '15', '16'],
  360. yarn_model: ['17', '18', '19', '20', '23'],
  361. storm_model: ['21'],
  362. flume_model: ['22'],
  363. hawq_model: ['24']
  364. };
  365. // check each service, find out the newly added service and already deleted service
  366. Em.keys(serviceWidgetsMap).forEach(function (modelName) {
  367. if (!Em.isNone(this.get(modelName))) {
  368. var ids = serviceWidgetsMap[modelName];
  369. var flag = this.containsWidget(toDelete, ids[0]);
  370. if (flag) {
  371. ids.forEach(function (item) {
  372. toDelete = this.removeWidget(toDelete, item);
  373. }, this);
  374. } else {
  375. toAdd = toAdd.concat(ids);
  376. }
  377. }
  378. }, this);
  379. var value = currentPrefObject;
  380. if (toDelete.visible.length || toDelete.hidden.length) {
  381. toDelete.visible.forEach(function (item) {
  382. value = this.removeWidget(value, item);
  383. }, this);
  384. toDelete.hidden.forEach(function (item) {
  385. value = this.removeWidget(value, item[0]);
  386. }, this);
  387. }
  388. if (toAdd.length) {
  389. value.visible = value.visible.concat(toAdd);
  390. var allThreshold = this.get('initPrefObject').threshold;
  391. // add new threshold OR override with default value
  392. toAdd.forEach(function (item) {
  393. value.threshold[item] = allThreshold[item];
  394. }, this);
  395. }
  396. return value;
  397. },
  398. /**
  399. * Get view for widget by widget's id
  400. * @param {string} id
  401. * @returns {Ember.View}
  402. */
  403. widgetsMapper: function (id) {
  404. return Em.get({
  405. '1': App.NameNodeHeapPieChartView,
  406. '2': App.NameNodeCapacityPieChartView,
  407. '3': App.NameNodeCpuPieChartView,
  408. '4': App.DataNodeUpView,
  409. '5': App.NameNodeRpcView,
  410. '6': App.ChartClusterMetricsMemoryWidgetView,
  411. '7': App.ChartClusterMetricsNetworkWidgetView,
  412. '8': App.ChartClusterMetricsCPUWidgetView,
  413. '9': App.ChartClusterMetricsLoadWidgetView,
  414. '10': App.NameNodeUptimeView,
  415. '11': App.HDFSLinksView,
  416. '12': App.HBaseLinksView,
  417. '13': App.HBaseMasterHeapPieChartView,
  418. '14': App.HBaseAverageLoadView,
  419. '15': App.HBaseRegionsInTransitionView,
  420. '16': App.HBaseMasterUptimeView,
  421. '17': App.ResourceManagerHeapPieChartView,
  422. '18': App.ResourceManagerUptimeView,
  423. '19': App.NodeManagersLiveView,
  424. '20': App.YARNMemoryPieChartView,
  425. '21': App.SuperVisorUpView,
  426. '22': App.FlumeAgentUpView,
  427. '23': App.YARNLinksView,
  428. '24': App.HawqSegmentUpView
  429. }, id);
  430. },
  431. /**
  432. * @type {Object|null}
  433. */
  434. currentPrefObject: null,
  435. /**
  436. * @type {Ember.Object}
  437. */
  438. initPrefObject: Em.Object.create({
  439. dashboardVersion: 'new',
  440. visible: [],
  441. hidden: [],
  442. threshold: {1: [80, 90], 2: [85, 95], 3: [90, 95], 4: [80, 90], 5: [1000, 3000], 6: [], 7: [], 8: [], 9: [], 10: [], 11: [], 12: [], 13: [70, 90], 14: [150, 250], 15: [3, 10], 16: [],
  443. 17: [70, 90], 18: [], 19: [50, 75], 20: [50, 75], 21: [85, 95], 22: [85, 95], 23: [], 24: [80, 90]} // id:[thresh1, thresh2]
  444. }),
  445. /**
  446. * Key-name to store data in Local Storage and Persist
  447. * @type {string}
  448. */
  449. persistKey: Em.computed.format('user-pref-{0}-dashboard', 'App.router.loginName'),
  450. getUserPrefSuccessCallback: function (response, request, data) {
  451. if (response) {
  452. var initPrefObject = this.get('initPrefObject');
  453. initPrefObject.get('threshold');
  454. for(var k in response.threshold) {
  455. if (response.threshold.hasOwnProperty(k)) {
  456. if (response.threshold[k].length === 0 && initPrefObject.get('threshold')[k] && initPrefObject.get('threshold')[k].length) {
  457. response.threshold[k] = initPrefObject.get('threshold')[k];
  458. }
  459. }
  460. }
  461. this.set('currentPrefObject', response);
  462. }
  463. },
  464. getUserPrefErrorCallback: function (request) {
  465. },
  466. /**
  467. * Reset widgets visibility-status
  468. */
  469. resetAllWidgets: function () {
  470. var self = this;
  471. App.showConfirmationPopup(function () {
  472. if (!App.get('testMode')) {
  473. self.postUserPref(self.get('persistKey'), self.get('initPrefObject'));
  474. self.setDBProperty(self.get('persistKey'), self.get('initPrefObject'));
  475. }
  476. self.setProperties({
  477. currentTimeRangeIndex: 0,
  478. customStartTime: null,
  479. customEndTime: null
  480. });
  481. self.translateToReal(self.get('initPrefObject'));
  482. });
  483. },
  484. showAlertsPopup: Em.K
  485. });