wizard_controller.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. /**
  31. * @type {number}
  32. * @default null
  33. */
  34. layoutId: null,
  35. /**
  36. * Example:
  37. * {
  38. * "display_unit": "%",
  39. * "warning_threshold": 70,
  40. * "error_threshold": 90
  41. * }
  42. */
  43. widgetProperties: {},
  44. /**
  45. * Example:
  46. * [{
  47. * widget_id: "metrics/rpc/closeRegion_num_ops",
  48. * name: "rpc.rpc.closeRegion_num_ops",
  49. * pointInTime: true,
  50. * temporal: true,
  51. * category: "default"
  52. * serviceName: "HBASE"
  53. * componentName: "HBASE_CLIENT"
  54. * type: "GANGLIA"//or JMX
  55. * level: "COMPONENT"//or HOSTCOMPONENT
  56. * }]
  57. * @type {Array}
  58. */
  59. allMetrics: [],
  60. /**
  61. * Example:
  62. * [{
  63. * "name": "regionserver.Server.percentFilesLocal",
  64. * "serviceName": "HBASE",
  65. * "componentName": "HBASE_REGIONSERVER"
  66. * }]
  67. */
  68. widgetMetrics: [],
  69. /**
  70. * Example:
  71. * [{
  72. * "name": "Files Local",
  73. * "value": "${regionserver.Server.percentFilesLocal}"
  74. * }]
  75. */
  76. widgetValues: [],
  77. widgetName: "",
  78. widgetDescription: "",
  79. widgetAuthor: function () {
  80. return App.router.get('loginName');
  81. }.property('App.router.loginName'),
  82. widgetScope: null
  83. }),
  84. loadMap: {
  85. '1': [
  86. {
  87. type: 'sync',
  88. callback: function () {
  89. this.load('widgetService');
  90. this.load('widgetType');
  91. }
  92. }
  93. ],
  94. '2': [
  95. {
  96. type: 'sync',
  97. callback: function () {
  98. this.load('widgetProperties', true);
  99. this.load('widgetValues', true);
  100. this.load('widgetMetrics', true);
  101. this.load('expressions', true);
  102. this.load('dataSets', true);
  103. this.load('templateValue', true);
  104. }
  105. },
  106. {
  107. type: 'async',
  108. callback: function () {
  109. return this.loadAllMetrics();
  110. }
  111. }
  112. ]
  113. },
  114. /**
  115. * set current step
  116. * @param {string} currentStep
  117. * @param {boolean} completed
  118. * @param {boolean} skipStateSave
  119. */
  120. setCurrentStep: function (currentStep, completed, skipStateSave) {
  121. this._super(currentStep, completed);
  122. if (App.get('testMode') || skipStateSave) {
  123. return;
  124. }
  125. this.saveClusterStatus('WIDGET_DEPLOY');
  126. },
  127. setStepsEnable: function () {
  128. for (var i = 1; i <= this.get('totalSteps'); i++) {
  129. var step = this.get('isStepDisabled').findProperty('step', i);
  130. if (i <= this.get('currentStep') && App.get('router.clusterController.isLoaded')) {
  131. step.set('value', false);
  132. } else {
  133. step.set('value', i != this.get('currentStep'));
  134. }
  135. }
  136. }.observes('currentStep', 'App.router.clusterController.isLoaded'),
  137. /**
  138. * save status of the cluster.
  139. * @param {object} clusterStatus
  140. */
  141. saveClusterStatus: function (clusterStatus) {
  142. App.clusterStatus.setClusterStatus({
  143. clusterState: clusterStatus,
  144. wizardControllerName: 'widgetWizardController',
  145. localdb: App.db.data
  146. });
  147. },
  148. /**
  149. * save wizard properties to controller and localStorage
  150. * @param {string} name
  151. * @param value
  152. */
  153. save: function (name, value) {
  154. this.set('content.' + name, value);
  155. this._super(name);
  156. },
  157. /**
  158. * load widget metrics
  159. * on resolve deferred return array of widget metrics
  160. * @returns {$.Deferred}
  161. */
  162. loadAllMetrics: function () {
  163. var widgetMetrics = this.getDBProperty('allMetrics');
  164. var self = this;
  165. var dfd = $.Deferred();
  166. if (widgetMetrics.length === 0) {
  167. this.loadAllMetricsFromServer(function () {
  168. dfd.resolve(self.get('content.allMetrics'));
  169. });
  170. } else {
  171. this.set('content.allMetrics', widgetMetrics);
  172. dfd.resolve(widgetMetrics);
  173. }
  174. return dfd.promise();
  175. },
  176. /**
  177. * load metrics from server
  178. * @param {function} callback
  179. * @returns {$.ajax}
  180. */
  181. loadAllMetricsFromServer: function (callback) {
  182. return App.ajax.send({
  183. name: 'widgets.wizard.metrics.get',
  184. sender: this,
  185. data: {
  186. stackVersionURL: App.get('stackVersionURL'),
  187. serviceNames: App.Service.find().filter(function (item) {
  188. return App.StackService.find(item.get('id')).get('isServiceWithWidgets');
  189. }).mapProperty('serviceName').join(',')
  190. },
  191. callback: callback,
  192. success: 'loadAllMetricsFromServerCallback'
  193. });
  194. },
  195. /**
  196. *
  197. * @param {object} json
  198. */
  199. loadAllMetricsFromServerCallback: function (json) {
  200. var self = this;
  201. var result = [];
  202. var metrics = {};
  203. if (json) {
  204. json.items.forEach(function (service) {
  205. var data = service.artifacts[0].artifact_data[service.StackServices.service_name];
  206. for (var componentName in data) {
  207. for (var level in data[componentName]) {
  208. metrics = data[componentName][level][0]['metrics']['default'];
  209. for (var widgetId in metrics) {
  210. var metricObj = {
  211. widget_id: widgetId,
  212. point_in_time: metrics[widgetId].pointInTime,
  213. temporal: metrics[widgetId].temporal,
  214. name: metrics[widgetId].name,
  215. level: level.toUpperCase(),
  216. type: data[componentName][level][0]["type"].toUpperCase(),
  217. component_name: componentName,
  218. service_name: service.StackServices.service_name
  219. };
  220. result.push(metricObj);
  221. if (metricObj.level === 'HOSTCOMPONENT') {
  222. self.insertHostComponentCriteria(metricObj);
  223. }
  224. }
  225. }
  226. }
  227. }, this);
  228. }
  229. this.save('allMetrics', result);
  230. },
  231. /**
  232. *
  233. * @param metricObj {Object}
  234. */
  235. insertHostComponentCriteria: function (metricObj) {
  236. switch (metricObj.component_name) {
  237. case 'NAMENODE':
  238. metricObj.host_component_criteria = 'host_components/metrics/dfs/FSNamesystem/HAState=active';
  239. break;
  240. case 'RESOURCEMANAGER':
  241. metricObj.host_component_criteria = 'host_components/HostRoles/ha_state=ACTIVE';
  242. break;
  243. default:
  244. }
  245. },
  246. /**
  247. * post widget definition to server
  248. * @returns {$.ajax}
  249. */
  250. postWidgetDefinition: function (data) {
  251. //TODO remove setting diplay_name once API supports it
  252. data.WidgetInfo.display_name = data.WidgetInfo.widget_name;
  253. return App.ajax.send({
  254. name: 'widgets.wizard.add',
  255. sender: this,
  256. data: {
  257. data: data
  258. },
  259. success: 'postWidgetDefinitionSuccessCallback'
  260. });
  261. },
  262. /**
  263. * assign created widget to active layout if it present
  264. * @param data
  265. */
  266. postWidgetDefinitionSuccessCallback: function (data) {
  267. if (Em.isNone(this.get('content.layoutId'))) return;
  268. var widgets = App.WidgetLayout.find(this.get('content.layoutId')).get('widgets').toArray();
  269. widgets.pushObject(Em.Object.create({
  270. id: data.resources[0].WidgetInfo.id
  271. }));
  272. App.router.get('mainServiceInfoSummaryController').saveWidgetLayout(widgets);
  273. },
  274. /**
  275. * Remove all loaded data.
  276. * Created as copy for App.router.clearAllSteps
  277. */
  278. clearAllSteps: function () {
  279. this.clearInstallOptions();
  280. // clear temporary information stored during the install
  281. this.set('content.cluster', this.getCluster());
  282. },
  283. clearTasksData: function () {
  284. this.saveTasksStatuses(undefined);
  285. this.saveRequestIds(undefined);
  286. this.saveTasksRequestIds(undefined);
  287. },
  288. /**
  289. * Clear all temporary data
  290. */
  291. finish: function () {
  292. this.setCurrentStep('1', false, true);
  293. this.save('widgetType', '');
  294. this.resetDbNamespace();
  295. App.get('router.updateController').updateAll();
  296. }
  297. });