definition_configs_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. App.MainAlertDefinitionConfigsController = Em.Controller.extend({
  19. name: 'mainAlertDefinitionConfigsController',
  20. /**
  21. * All configurable properties of alert definition
  22. * @type {Array}
  23. */
  24. configs: [],
  25. /**
  26. * Define whether configs are editable
  27. * binds to property populated in template
  28. * @type {Boolean}
  29. */
  30. canEdit: true,
  31. /**
  32. * Array of displayNames of all services
  33. * is used for "Service" config options
  34. * @type {Array}
  35. */
  36. allServices: function () {
  37. return App.Service.find().mapProperty('displayName');
  38. }.property(),
  39. /**
  40. * Array of all aggregate-alerts names
  41. * @type {Array}
  42. */
  43. aggregateAlertNames: function () {
  44. return App.AggregateAlertDefinition.find().mapProperty('name');
  45. }.property(),
  46. /**
  47. * Compute thresholds from value using <code>content.reporting</code>
  48. * @type {Number|Null}
  49. */
  50. thresholdsFrom: function () {
  51. var warning = this.get('content.reporting').findProperty('type', 'warning');
  52. if (warning && warning.get('value')) {
  53. return warning.get('value');
  54. } else {
  55. return null;
  56. }
  57. }.property('content.reporting.@each.value'),
  58. /**
  59. * Compute thresholds to value using <code>content.reporting</code>
  60. * @type {Number|Null}
  61. */
  62. thresholdsTo: function () {
  63. var critical = this.get('content.reporting').findProperty('type', 'critical');
  64. if (critical && critical.get('value')) {
  65. return critical.get('value');
  66. } else {
  67. return null;
  68. }
  69. }.property('content.reporting.@each.value'),
  70. /**
  71. * Change options of "Component", after changing value of "Service" config
  72. */
  73. onServiceSelect: function () {
  74. var serviceProperty = this.get('configs').findProperty('label', 'Service');
  75. if (serviceProperty) {
  76. var componentsProperty = this.get('configs').findProperty('label', 'Component');
  77. componentsProperty.set('options', ['No component'].concat(App.HostComponent.find().filterProperty('service.displayName', serviceProperty.get('value')).mapProperty('displayName').uniq()));
  78. }
  79. }.observes('configs.@each.value'),
  80. /**
  81. * Render array of configs for appropriate alert definition type
  82. */
  83. renderConfigs: function () {
  84. var alertDefinition = this.get('content');
  85. var configs = [];
  86. switch (alertDefinition.get('type')) {
  87. case 'PORT':
  88. configs = this.renderPortConfigs();
  89. break;
  90. case 'METRIC':
  91. configs = this.renderMetricConfigs();
  92. break;
  93. case 'WEB':
  94. configs = this.renderWebConfigs();
  95. break;
  96. case 'SCRIPT':
  97. configs = this.renderScriptConfigs();
  98. break;
  99. case 'AGGREGATE':
  100. configs = this.renderAggregateConfigs();
  101. break;
  102. default:
  103. console.error('Incorrect Alert Definition Type: ', alertDefinition.get('type'));
  104. }
  105. configs.setEach('isDisabled', !this.get('canEdit'));
  106. this.set('configs', configs);
  107. },
  108. /**
  109. * Render config properties for port-type alert definition
  110. * @returns {Array}
  111. */
  112. renderPortConfigs: function () {
  113. var alertDefinition = this.get('content');
  114. return [
  115. App.AlertConfigProperties.AlertName.create({
  116. value: alertDefinition.get('name')
  117. }),
  118. App.AlertConfigProperties.Service.create({
  119. options: this.get('allServices'),
  120. value: alertDefinition.get('service.displayName')
  121. }),
  122. App.AlertConfigProperties.Component.create({
  123. options: this.get('allComponents'),
  124. value: alertDefinition.get('componentName') ? App.format.role(alertDefinition.get('componentName')) : 'No component'
  125. }),
  126. App.AlertConfigProperties.Scope.create({
  127. value: alertDefinition.get('scope').toLowerCase().capitalize()
  128. }),
  129. App.AlertConfigProperties.Description.create({
  130. value: alertDefinition.get('description')
  131. }),
  132. App.AlertConfigProperties.Interval.create({
  133. value: alertDefinition.get('interval')
  134. }),
  135. App.AlertConfigProperties.Thresholds.create({
  136. value: this.get('thresholdsFrom') + '-' + this.get('thresholdsTo'),
  137. from: this.get('thresholdsFrom'),
  138. to: this.get('thresholdsTo')
  139. }),
  140. App.AlertConfigProperties.URI.create({
  141. value: alertDefinition.get('uri')
  142. }),
  143. App.AlertConfigProperties.DefaultPort.create({
  144. value: alertDefinition.get('defaultPort')
  145. })
  146. ];
  147. },
  148. /**
  149. * Render config properties for metric-type alert definition
  150. * @returns {Array}
  151. */
  152. renderMetricConfigs: function () {
  153. var alertDefinition = this.get('content');
  154. return [
  155. App.AlertConfigProperties.AlertName.create({
  156. value: alertDefinition.get('name')
  157. }),
  158. App.AlertConfigProperties.Service.create({
  159. options: this.get('allServices'),
  160. value: alertDefinition.get('service.displayName')
  161. }),
  162. App.AlertConfigProperties.Component.create({
  163. options: this.get('allComponents'),
  164. value: alertDefinition.get('componentName') ? App.format.role(alertDefinition.get('componentName')) : 'No component'
  165. }),
  166. App.AlertConfigProperties.Scope.create({
  167. value: alertDefinition.get('scope').toLowerCase().capitalize()
  168. }),
  169. App.AlertConfigProperties.Description.create({
  170. value: alertDefinition.get('description')
  171. }),
  172. App.AlertConfigProperties.Interval.create({
  173. value: alertDefinition.get('interval')
  174. }),
  175. App.AlertConfigProperties.Thresholds.create({
  176. value: this.get('thresholdsFrom') + '-' + this.get('thresholdsTo'),
  177. from: this.get('thresholdsFrom'),
  178. to: this.get('thresholdsTo')
  179. }),
  180. App.AlertConfigProperties.URIExtended.create({
  181. value: JSON.stringify({
  182. http: alertDefinition.get('uri.http'),
  183. https: alertDefinition.get('uri.https'),
  184. https_property: alertDefinition.get('uri.httpsProperty'),
  185. https_property_value: alertDefinition.get('uri.httpsPropertyValue')
  186. })
  187. }),
  188. App.AlertConfigProperties.Metrics.create({
  189. value: alertDefinition.get('jmx.propertyList') ? alertDefinition.get('jmx.propertyList').join(',\n') : alertDefinition.get('ganglia.propertyList').join(',\n'),
  190. isJMXMetric: !!alertDefinition.get('jmx.propertyList')
  191. }),
  192. App.AlertConfigProperties.FormatString.create({
  193. value: alertDefinition.get('jmx.value') ? alertDefinition.get('jmx.value') : alertDefinition.get('ganglia.value'),
  194. isJMXMetric: !!alertDefinition.get('jmx.value')
  195. })
  196. ];
  197. },
  198. /**
  199. * Render config properties for web-type alert definition
  200. * @returns {Array}
  201. */
  202. renderWebConfigs: function () {
  203. var alertDefinition = this.get('content');
  204. return [
  205. App.AlertConfigProperties.AlertName.create({
  206. value: alertDefinition.get('name')
  207. }),
  208. App.AlertConfigProperties.Service.create({
  209. options: this.get('allServices'),
  210. value: alertDefinition.get('service.displayName')
  211. }),
  212. App.AlertConfigProperties.Component.create({
  213. options: this.get('allComponents'),
  214. value: alertDefinition.get('componentName') ? App.format.role(alertDefinition.get('componentName')) : 'No component'
  215. }),
  216. App.AlertConfigProperties.Scope.create({
  217. value: alertDefinition.get('scope').toLowerCase().capitalize()
  218. }),
  219. App.AlertConfigProperties.Description.create({
  220. value: alertDefinition.get('description')
  221. }),
  222. App.AlertConfigProperties.Interval.create({
  223. value: alertDefinition.get('interval')
  224. }),
  225. App.AlertConfigProperties.Thresholds.create({
  226. value: this.get('thresholdsFrom') + '-' + this.get('thresholdsTo'),
  227. from: this.get('thresholdsFrom'),
  228. to: this.get('thresholdsTo')
  229. }),
  230. App.AlertConfigProperties.URIExtended.create({
  231. value: JSON.stringify({
  232. http: alertDefinition.get('uri.http'),
  233. https: alertDefinition.get('uri.https'),
  234. https_property: alertDefinition.get('uri.httpsProperty'),
  235. https_property_value: alertDefinition.get('uri.httpsPropertyValue')
  236. })
  237. })
  238. ];
  239. },
  240. /**
  241. * Render config properties for script-type alert definition
  242. * @returns {Array}
  243. */
  244. renderScriptConfigs: function () {
  245. var alertDefinition = this.get('content');
  246. return [
  247. App.AlertConfigProperties.AlertName.create({
  248. value: alertDefinition.get('name')
  249. }),
  250. App.AlertConfigProperties.Service.create({
  251. options: this.get('allServices'),
  252. value: alertDefinition.get('service.displayName')
  253. }),
  254. App.AlertConfigProperties.Component.create({
  255. options: this.get('allComponents'),
  256. value: alertDefinition.get('componentName') ? App.format.role(alertDefinition.get('componentName')) : 'No component'
  257. }),
  258. App.AlertConfigProperties.Scope.create({
  259. value: alertDefinition.get('scope').toLowerCase().capitalize()
  260. }),
  261. App.AlertConfigProperties.Description.create({
  262. value: alertDefinition.get('description')
  263. }),
  264. App.AlertConfigProperties.Interval.create({
  265. value: alertDefinition.get('interval')
  266. }),
  267. App.AlertConfigProperties.Thresholds.create({
  268. value: this.get('thresholdsFrom') + '-' + this.get('thresholdsTo'),
  269. from: this.get('thresholdsFrom'),
  270. to: this.get('thresholdsTo')
  271. }),
  272. App.AlertConfigProperties.Path.create({
  273. value: alertDefinition.get('location')
  274. })
  275. ];
  276. },
  277. /**
  278. * Render config properties for aggregate-type alert definition
  279. * @returns {Array}
  280. */
  281. renderAggregateConfigs: function () {
  282. var alertDefinition = this.get('content');
  283. return [
  284. App.AlertConfigProperties.AlertNameSelected.create({
  285. value: alertDefinition.get('name'),
  286. options: this.get('aggregateAlertNames')
  287. }),
  288. App.AlertConfigProperties.Description.create({
  289. value: alertDefinition.get('description')
  290. })
  291. ];
  292. },
  293. /**
  294. * Edit configs button handler
  295. */
  296. editConfigs: function () {
  297. this.get('configs').forEach(function (property) {
  298. property.set('previousValue', property.get('value'));
  299. });
  300. this.get('configs').setEach('isDisabled', false);
  301. this.set('canEdit', true);
  302. },
  303. /**
  304. * Cancel edit configs button handler
  305. */
  306. cancelEditConfigs: function () {
  307. this.get('configs').forEach(function (property) {
  308. property.set('value', property.get('previousValue'));
  309. });
  310. this.get('configs').setEach('isDisabled', true);
  311. this.set('canEdit', false);
  312. },
  313. /**
  314. * Save edit configs button handler
  315. */
  316. saveConfigs: function () {
  317. this.get('configs').setEach('isDisabled', true);
  318. this.set('canEdit', false);
  319. var propertiesToUpdate = this.getPropertiesToUpdate();
  320. App.ajax.send({
  321. name: 'alerts.update_alert_definition',
  322. sender: this,
  323. data: {
  324. id: this.get('content.id'),
  325. data: propertiesToUpdate
  326. }
  327. });
  328. },
  329. /**
  330. * Create object with new values to put it on server
  331. * @returns {Object}
  332. */
  333. getPropertiesToUpdate: function () {
  334. var propertiesToUpdate = {};
  335. this.get('configs').filterProperty('wasChanged').forEach(function (property) {
  336. var apiProperties = property.get('apiProperty');
  337. var apiFormattedValues = property.get('apiFormattedValue');
  338. if (!Em.isArray(property.get('apiProperty'))) {
  339. apiProperties = [property.get('apiProperty')];
  340. apiFormattedValues = [property.get('apiFormattedValue')];
  341. }
  342. apiProperties.forEach(function (apiProperty, i) {
  343. if (apiProperty.contains('source.')) {
  344. if (!propertiesToUpdate['AlertDefinition/source']) {
  345. propertiesToUpdate['AlertDefinition/source'] = this.get('content.rawSourceData');
  346. }
  347. var sourcePath = propertiesToUpdate['AlertDefinition/source'];
  348. apiProperty.replace('source.', '').split('.').forEach(function (path, index, array) {
  349. // check if it is last path
  350. if (array.length - index === 1) {
  351. sourcePath[path] = apiFormattedValues[i];
  352. } else {
  353. sourcePath = sourcePath[path];
  354. }
  355. });
  356. } else {
  357. propertiesToUpdate['AlertDefinition/' + apiProperty] = apiFormattedValues[i];
  358. }
  359. }, this);
  360. }, this);
  361. return propertiesToUpdate;
  362. }
  363. });