wizard_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. App.WidgetWizardController = App.WizardController.extend({
  20. name: 'widgetWizardController',
  21. totalSteps: 3,
  22. /**
  23. * Used for hiding back button in wizard
  24. */
  25. hideBackButton: true,
  26. content: Em.Object.create({
  27. controllerName: 'widgetWizardController',
  28. widgetService: null,
  29. widgetType: "",
  30. isMetricsLoaded: false,
  31. /**
  32. * @type {Object}
  33. * @default null
  34. */
  35. layout: null,
  36. /**
  37. * Example:
  38. * {
  39. * "display_unit": "%",
  40. * "warning_threshold": 70,
  41. * "error_threshold": 90
  42. * }
  43. */
  44. widgetProperties: {},
  45. /**
  46. * Example:
  47. * [{
  48. * widget_id: "metrics/rpc/closeRegion_num_ops",
  49. * name: "rpc.rpc.closeRegion_num_ops",
  50. * pointInTime: true,
  51. * temporal: true,
  52. * category: "default"
  53. * serviceName: "HBASE"
  54. * componentName: "HBASE_CLIENT"
  55. * type: "GANGLIA"//or JMX
  56. * level: "COMPONENT"//or HOSTCOMPONENT
  57. * }]
  58. * @type {Array}
  59. */
  60. allMetrics: [],
  61. /**
  62. * Example:
  63. * [{
  64. * "name": "regionserver.Server.percentFilesLocal",
  65. * "serviceName": "HBASE",
  66. * "componentName": "HBASE_REGIONSERVER"
  67. * }]
  68. */
  69. widgetMetrics: [],
  70. /**
  71. * Example:
  72. * [{
  73. * "name": "Files Local",
  74. * "value": "${regionserver.Server.percentFilesLocal}"
  75. * }]
  76. */
  77. widgetValues: [],
  78. widgetName: "",
  79. widgetDescription: "",
  80. widgetAuthor: function () {
  81. return App.router.get('loginName');
  82. }.property('App.router.loginName'),
  83. widgetScope: null
  84. }),
  85. loadMap: {
  86. '1': [
  87. {
  88. type: 'sync',
  89. callback: function () {
  90. this.load('widgetService');
  91. this.load('widgetType');
  92. this.load('layout', true);
  93. }
  94. }
  95. ],
  96. '2': [
  97. {
  98. type: 'sync',
  99. callback: function () {
  100. this.load('widgetProperties', true);
  101. this.load('widgetValues', true);
  102. this.load('widgetMetrics', true);
  103. this.load('expressions', true);
  104. this.load('dataSets', true);
  105. this.load('templateValue', true);
  106. }
  107. },
  108. {
  109. type: 'async',
  110. callback: function () {
  111. return this.loadAllMetrics();
  112. }
  113. }
  114. ]
  115. },
  116. /**
  117. * set current step
  118. * @param {string} currentStep
  119. * @param {boolean} completed
  120. * @param {boolean} skipStateSave
  121. */
  122. setCurrentStep: function (currentStep, completed, skipStateSave) {
  123. this._super(currentStep, completed);
  124. if (App.get('testMode') || skipStateSave) {
  125. return;
  126. }
  127. this.saveClusterStatus('WIDGET_DEPLOY');
  128. },
  129. setStepsEnable: function () {
  130. for (var i = 1; i <= this.get('totalSteps'); i++) {
  131. var step = this.get('isStepDisabled').findProperty('step', i);
  132. if (i <= this.get('currentStep') && App.get('router.clusterController.isLoaded')) {
  133. step.set('value', false);
  134. } else {
  135. step.set('value', i != this.get('currentStep'));
  136. }
  137. }
  138. }.observes('currentStep', 'App.router.clusterController.isLoaded'),
  139. /**
  140. * save status of the cluster.
  141. * @param {object} clusterStatus
  142. */
  143. saveClusterStatus: function (clusterStatus) {
  144. App.clusterStatus.setClusterStatus({
  145. clusterState: clusterStatus,
  146. wizardControllerName: this.get('name'),
  147. localdb: App.db.data
  148. });
  149. },
  150. /**
  151. * save wizard properties to controller and localStorage
  152. * @param {string} name
  153. * @param value
  154. */
  155. save: function (name, value) {
  156. this.set('content.' + name, value);
  157. this._super(name);
  158. },
  159. /**
  160. * load widget metrics
  161. * on resolve deferred return array of widget metrics
  162. * @returns {$.Deferred}
  163. */
  164. loadAllMetrics: function () {
  165. var allMetrics = this.getDBProperty('allMetrics');
  166. var self = this;
  167. this.set("content.isMetricsLoaded", false);
  168. var dfd = $.Deferred();
  169. if (allMetrics.length === 0) {
  170. this.loadAllMetricsFromServer(function () {
  171. self.set("content.isMetricsLoaded", true);
  172. dfd.resolve(self.get('content.allMetrics'));
  173. });
  174. } else {
  175. this.set("content.isMetricsLoaded", true);
  176. this.set('content.allMetrics', allMetrics);
  177. dfd.resolve(allMetrics);
  178. }
  179. return dfd.promise();
  180. },
  181. /**
  182. * load metrics from server
  183. * @param {function} callback
  184. * @returns {$.ajax}
  185. */
  186. loadAllMetricsFromServer: function (callback) {
  187. return App.ajax.send({
  188. name: 'widgets.wizard.metrics.get',
  189. sender: this,
  190. data: {
  191. stackVersionURL: App.get('stackVersionURL'),
  192. serviceNames: App.Service.find().filter(function (item) {
  193. return App.StackService.find(item.get('id')).get('isServiceWithWidgets');
  194. }).mapProperty('serviceName').join(',')
  195. },
  196. callback: callback,
  197. success: 'loadAllMetricsFromServerCallback'
  198. });
  199. },
  200. /**
  201. *
  202. * @param {object} json
  203. */
  204. loadAllMetricsFromServerCallback: function (json) {
  205. var self = this;
  206. var result = [];
  207. var metrics = {};
  208. var slaveComponents = App.StackServiceComponent.find().filterProperty('isSlave').mapProperty('componentName');
  209. if (json) {
  210. json.items.forEach(function (service) {
  211. var data = service.artifacts[0].artifact_data[service.StackServices.service_name];
  212. for (var componentName in data) {
  213. var isSlave = slaveComponents.contains(componentName);
  214. for (var level in data[componentName]) {
  215. var metricTypes = data[componentName][level]; //Ganglia or JMX
  216. metricTypes.forEach(function (_metricType) {
  217. metrics = _metricType['metrics']['default'];
  218. var type = _metricType["type"].toUpperCase();
  219. if (!((type === 'JMX' && level.toUpperCase() === 'COMPONENT') || (isSlave && level.toUpperCase() === 'HOSTCOMPONENT'))) {
  220. for (var widgetId in metrics) {
  221. var _metrics = metrics[widgetId];
  222. var metricObj = {
  223. widget_id: widgetId,
  224. point_in_time: _metrics.pointInTime,
  225. temporal: _metrics.temporal,
  226. name: _metrics.name,
  227. level: level.toUpperCase(),
  228. type: type,
  229. component_name: componentName,
  230. service_name: service.StackServices.service_name
  231. };
  232. result.push(metricObj);
  233. if (metricObj.level === 'HOSTCOMPONENT') {
  234. self.insertHostComponentCriteria(metricObj);
  235. }
  236. }
  237. }
  238. }, this);
  239. }
  240. }
  241. }, this);
  242. }
  243. if (!!App.YARNService.find("YARN")) {
  244. result = this.substitueQueueMetrics(result);
  245. }
  246. this.save('allMetrics', result);
  247. },
  248. /**
  249. * @name substitueQueueMetrics
  250. * @param metrics
  251. * @return {Array} array of metric objects with regex substituted with actual metrics names
  252. */
  253. substitueQueueMetrics: function (metrics) {
  254. var result = [];
  255. var queuePaths = App.YARNService.find("YARN").get("allQueueNames");
  256. var queueNames = [];
  257. var queueMetricName;
  258. queuePaths.forEach(function (_queuePath) {
  259. var queueName = _queuePath.replace(/\//g, ".");
  260. queueNames.push(queueName);
  261. }, this);
  262. var regexpForAMS = new RegExp("^yarn.QueueMetrics.Queue=\\(\\.\\+\\).*$");
  263. var regexpForJMX = new RegExp("\\(\\.\\+\\)", "g");
  264. var replaceRegexForMetricName = regexpForJMX;
  265. var replaceRegexForMetricPath = new RegExp("\\$\\d\\..*\\)(?=\\/)", "g");
  266. metrics.forEach(function (_metric) {
  267. var isAMSQueueMetric = regexpForAMS.test(_metric.name);
  268. var isJMXQueueMetrics = regexpForJMX.test(_metric.name);
  269. if ((_metric.type === 'GANGLIA' && isAMSQueueMetric) || (_metric.type === 'JMX' && isJMXQueueMetrics)) {
  270. queuePaths.forEach(function (_queuePath) {
  271. queueMetricName = '';
  272. if (_metric.type === 'GANGLIA') {
  273. queueMetricName = _queuePath.replace(/\//g, ".");
  274. } else if (_metric.type === 'JMX') {
  275. _queuePath.split("/").forEach(function(_metricName, index){
  276. queueMetricName = queueMetricName + ',q' + index + '=' + _metricName;
  277. }, this);
  278. }
  279. var metricName = _metric.name.replace(replaceRegexForMetricName, queueMetricName);
  280. var newMetricPath = _metric.widget_id.replace(replaceRegexForMetricPath, _queuePath);
  281. var newQueueMetric = $.extend(true, {}, _metric, {name: metricName, widget_id: newMetricPath});
  282. result.pushObject(newQueueMetric);
  283. }, this);
  284. } else {
  285. result.pushObject(_metric);
  286. }
  287. }, this);
  288. return result;
  289. },
  290. /**
  291. *
  292. * @param metricObj {Object}
  293. */
  294. insertHostComponentCriteria: function (metricObj) {
  295. switch (metricObj.component_name) {
  296. case 'NAMENODE':
  297. metricObj.host_component_criteria = 'host_components/metrics/dfs/FSNamesystem/HAState=active';
  298. break;
  299. case 'RESOURCEMANAGER':
  300. metricObj.host_component_criteria = 'host_components/HostRoles/ha_state=ACTIVE';
  301. break;
  302. default:
  303. }
  304. },
  305. /**
  306. * post widget definition to server
  307. * @returns {$.ajax}
  308. */
  309. postWidgetDefinition: function (data) {
  310. return App.ajax.send({
  311. name: 'widgets.wizard.add',
  312. sender: this,
  313. data: {
  314. data: data
  315. },
  316. success: 'postWidgetDefinitionSuccessCallback'
  317. });
  318. },
  319. /**
  320. * assign created widget to active layout if it present
  321. * @param data
  322. */
  323. postWidgetDefinitionSuccessCallback: function (data) {
  324. if (Em.isNone(this.get('content.layout'))) return;
  325. var widgets = this.get('content.layout.widgets').map(function(item){
  326. return Em.Object.create({id: item});
  327. });
  328. widgets.pushObject(Em.Object.create({
  329. id: data.resources[0].WidgetInfo.id
  330. }));
  331. var mainServiceInfoSummaryController = App.router.get('mainServiceInfoSummaryController');
  332. mainServiceInfoSummaryController.saveWidgetLayout(widgets, Em.Object.create(this.get('content.layout'))).done(function() {
  333. mainServiceInfoSummaryController.updateActiveLayout();
  334. });
  335. },
  336. /**
  337. * Remove all loaded data.
  338. * Created as copy for App.router.clearAllSteps
  339. */
  340. clearAllSteps: function () {
  341. this.clearInstallOptions();
  342. // clear temporary information stored during the install
  343. this.set('content.cluster', this.getCluster());
  344. },
  345. clearTasksData: function () {
  346. this.saveTasksStatuses(undefined);
  347. this.saveRequestIds(undefined);
  348. this.saveTasksRequestIds(undefined);
  349. },
  350. cancel: function () {
  351. var self = this;
  352. var step3Controller = App.router.get('widgetWizardStep3Controller');
  353. var isLastStep = parseInt(self.get('currentStep')) === self.get('totalSteps');
  354. return App.ModalPopup.show({
  355. header: Em.I18n.t('common.warning'),
  356. body: Em.I18n.t('dashboard.widgets.wizard.onClose.popup.body'),
  357. primary: isLastStep ? Em.I18n.t('common.save') : null,
  358. secondary: Em.I18n.t('dashboard.widgets.wizard.onClose.popup.discardAndExit'),
  359. third: Em.I18n.t('common.cancel'),
  360. disablePrimary: function () {
  361. return !(isLastStep && !step3Controller.get('isSubmitDisabled'));
  362. }.property(),
  363. onPrimary: function () {
  364. App.router.send('complete', step3Controller.collectWidgetData());
  365. this.onSecondary();
  366. },
  367. onSecondary: function () {
  368. this.hide();
  369. self.finishWizard();
  370. },
  371. onThird: function () {
  372. this.hide();
  373. }
  374. });
  375. },
  376. /**
  377. * finish wizard
  378. */
  379. finishWizard: function () {
  380. var service = App.Service.find(this.get('content.widgetService'));
  381. this.finish();
  382. this.get('popup').hide();
  383. App.router.transitionTo('main.services.service.summary', service);
  384. if (!App.get('testMode')) {
  385. App.clusterStatus.setClusterStatus({
  386. clusterName: App.router.getClusterName(),
  387. clusterState: 'DEFAULT',
  388. localdb: App.db.data
  389. });
  390. }
  391. },
  392. /**
  393. * Clear all temporary data
  394. */
  395. finish: function () {
  396. this.setCurrentStep('1', false, true);
  397. this.save('widgetType', '');
  398. this.resetDbNamespace();
  399. App.get('router.updateController').updateAll();
  400. }
  401. });