widgets.js 17 KB

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