widgets.js 17 KB

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