widgets.js 17 KB

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