alert_definitions_mapper.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. App.alertDefinitionsMapper = App.QuickDataMapper.create({
  19. model: App.AlertDefinition,
  20. reportModel: App.AlertReportDefinition,
  21. metricsSourceModel: App.AlertMetricsSourceDefinition,
  22. metricsUriModel: App.AlertMetricsUriDefinition,
  23. parameterModel: App.AlertDefinitionParameter,
  24. config: {
  25. id: 'AlertDefinition.id',
  26. name: 'AlertDefinition.name',
  27. description: 'AlertDefinition.description',
  28. label: 'AlertDefinition.label',
  29. service_id: 'AlertDefinition.service_name',
  30. service_name: 'AlertDefinition.service_name',
  31. component_name: 'AlertDefinition.component_name',
  32. enabled: 'AlertDefinition.enabled',
  33. scope: 'AlertDefinition.scope',
  34. interval: 'AlertDefinition.interval',
  35. type: 'AlertDefinition.source.type',
  36. reporting_key: 'reporting',
  37. reporting_type: 'array',
  38. reporting: {
  39. item: 'id'
  40. },
  41. parameters_key: 'parameters',
  42. parameters_type: 'array',
  43. parameters: {
  44. item: 'id'
  45. }
  46. },
  47. portConfig: {
  48. default_port: 'AlertDefinition.source.default_port',
  49. port_uri: 'AlertDefinition.source.uri'
  50. },
  51. aggregateConfig: {
  52. alert_name: 'AlertDefinition.source.alert_name'
  53. },
  54. scriptConfig: {
  55. location: 'AlertDefinition.source.path'
  56. },
  57. serverConfig: {
  58. },
  59. uriConfig: {
  60. id: 'AlertDefinition.source.uri.id',
  61. http: 'AlertDefinition.source.uri.http',
  62. https: 'AlertDefinition.source.uri.https',
  63. https_property: 'AlertDefinition.source.uri.https_property',
  64. https_property_value: 'AlertDefinition.source.uri.https_property_value',
  65. connection_timeout: 'AlertDefinition.source.uri.connection_timeout'
  66. },
  67. map: function (json) {
  68. console.time('App.alertDefinitionsMapper execution time');
  69. if (json && json.items) {
  70. var self = this,
  71. parameters = [],
  72. alertDefinitions = [],
  73. alertReportDefinitions = [],
  74. alertMetricsSourceDefinitions = [],
  75. alertMetricsUriDefinitions = [],
  76. alertGroupsMap = App.cache.previousAlertGroupsMap,
  77. existingAlertDefinitions = App.AlertDefinition.find(),
  78. existingAlertDefinitionsMap = existingAlertDefinitions.toArray().toMapByProperty('id'),
  79. alertDefinitionsToDelete = existingAlertDefinitions.mapProperty('id'),
  80. rawSourceData = {};
  81. json.items.forEach(function (item) {
  82. var convertedReportDefinitions = [];
  83. var reporting = item.AlertDefinition.source.reporting;
  84. for (var report in reporting) {
  85. if (reporting.hasOwnProperty(report)) {
  86. if (report === "units") {
  87. convertedReportDefinitions.push({
  88. id: item.AlertDefinition.id + report,
  89. type: report,
  90. text: reporting[report]
  91. });
  92. } else {
  93. convertedReportDefinitions.push({
  94. id: item.AlertDefinition.id + report,
  95. type: report,
  96. text: reporting[report].text,
  97. value: reporting[report].value
  98. });
  99. }
  100. }
  101. }
  102. var convertedParameters = [];
  103. var sourceParameters = item.AlertDefinition.source.parameters;
  104. if (Array.isArray(sourceParameters)) {
  105. sourceParameters.forEach(function (parameter) {
  106. convertedParameters.push({
  107. id: item.AlertDefinition.id + parameter.name,
  108. name: parameter.name,
  109. display_name: parameter.display_name,
  110. units: parameter.units,
  111. value: parameter.value,
  112. description: parameter.description,
  113. type: parameter.type,
  114. threshold: parameter.threshold
  115. });
  116. });
  117. }
  118. alertReportDefinitions = alertReportDefinitions.concat(convertedReportDefinitions);
  119. parameters = parameters.concat(convertedParameters);
  120. item.reporting = convertedReportDefinitions;
  121. item.parameters = convertedParameters;
  122. rawSourceData[item.AlertDefinition.id] = item.AlertDefinition.source;
  123. item.AlertDefinition.description = item.AlertDefinition.description || '';
  124. var alertDefinition = this.parseIt(item, this.get('config'));
  125. if (alertGroupsMap[alertDefinition.id]) {
  126. alertDefinition.groups = alertGroupsMap[alertDefinition.id];
  127. }
  128. var oldAlertDefinition = existingAlertDefinitionsMap[alertDefinition.id];
  129. if (oldAlertDefinition) {
  130. // new values will be parsed in the another mapper, so for now just use old values
  131. alertDefinition.summary = oldAlertDefinition.get('summary');
  132. alertDefinition.last_triggered = oldAlertDefinition.get('lastTriggered');
  133. alertDefinition.last_triggered_raw = oldAlertDefinition.get('lastTriggeredRaw');
  134. }
  135. alertDefinitionsToDelete = alertDefinitionsToDelete.without(alertDefinition.id);
  136. // map properties dependent on Alert Definition type
  137. switch (item.AlertDefinition.source.type) {
  138. case 'PORT':
  139. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('portConfig'))));
  140. break;
  141. case 'METRIC':
  142. // map App.AlertMetricsSourceDefinition's
  143. var jmxMetric = item.AlertDefinition.source.jmx;
  144. var gangliaMetric = item.AlertDefinition.source.ganglia;
  145. if (jmxMetric) {
  146. alertDefinition.jmx_id = item.AlertDefinition.id + 'jmx';
  147. alertMetricsSourceDefinitions.push({
  148. id: alertDefinition.jmx_id,
  149. value: jmxMetric.value,
  150. property_list: jmxMetric.property_list
  151. });
  152. }
  153. if (gangliaMetric) {
  154. alertDefinition.ganglia_id = item.AlertDefinition.id + 'ganglia';
  155. alertMetricsSourceDefinitions.push({
  156. id: alertDefinition.ganglia_id,
  157. value: gangliaMetric.value,
  158. property_list: gangliaMetric.property_list
  159. });
  160. }
  161. // map App.AlertMetricsUriDefinition
  162. alertDefinition.uri_id = item.AlertDefinition.id + 'uri';
  163. item.AlertDefinition.source.uri.id = alertDefinition.uri_id;
  164. alertMetricsUriDefinitions.push(this.parseIt(item, this.get('uriConfig')));
  165. alertDefinitions.push(alertDefinition);
  166. break;
  167. case 'WEB':
  168. // map App.AlertMetricsUriDefinition
  169. alertDefinition.uri_id = item.AlertDefinition.id + 'uri';
  170. item.AlertDefinition.source.uri.id = alertDefinition.uri_id;
  171. alertMetricsUriDefinitions.push(this.parseIt(item, this.get('uriConfig')));
  172. alertDefinitions.push(alertDefinition);
  173. break;
  174. case 'AGGREGATE':
  175. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('aggregateConfig'))));
  176. break;
  177. case 'SCRIPT':
  178. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('scriptConfig'))));
  179. break;
  180. case 'SERVER':
  181. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('serverConfig'))));
  182. break;
  183. case 'RECOVERY':
  184. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('uriConfig'))));
  185. break;
  186. default:
  187. console.error('Incorrect Alert Definition type:', item.AlertDefinition);
  188. }
  189. }, this);
  190. alertDefinitionsToDelete.forEach(function(definitionId) {
  191. self.deleteRecord(existingAlertDefinitions.findProperty('id', definitionId));
  192. });
  193. // load all mapped data to model
  194. App.store.loadMany(this.get('reportModel'), alertReportDefinitions);
  195. App.store.loadMany(this.get('parameterModel'), parameters);
  196. App.store.loadMany(this.get('metricsSourceModel'), alertMetricsSourceDefinitions);
  197. this.setMetricsSourcePropertyLists(this.get('metricsSourceModel'), alertMetricsSourceDefinitions);
  198. App.store.loadMany(this.get('metricsUriModel'), alertMetricsUriDefinitions);
  199. // this loadMany takes too much time
  200. App.store.loadMany(this.get('model'), alertDefinitions);
  201. this.setAlertDefinitionsRawSourceData(rawSourceData);
  202. App.store.commit();
  203. }
  204. console.timeEnd('App.alertDefinitionsMapper execution time');
  205. },
  206. /**
  207. * set propertyList properties from <code>data</code> for records in <code>model</code>
  208. * @param model
  209. * @param data
  210. */
  211. setMetricsSourcePropertyLists: function (model, data) {
  212. var modelsMap = model.find().toArray().toMapByProperty('id');
  213. data.forEach(function (record) {
  214. var m = modelsMap[record.id];
  215. if (m) {
  216. m.set('propertyList', record.property_list);
  217. }
  218. });
  219. },
  220. /**
  221. * set rawSourceDate properties for <code>App.AlertDefinition</code> records
  222. * @param rawSourceData
  223. */
  224. setAlertDefinitionsRawSourceData: function (rawSourceData) {
  225. var allDefinitions = App.AlertDefinition.find();
  226. var allDefinitionsMap = allDefinitions.toArray().toMapByProperty('id');
  227. for (var alertDefinitionId in rawSourceData) {
  228. if (rawSourceData.hasOwnProperty(alertDefinitionId)) {
  229. var m = allDefinitionsMap[+alertDefinitionId];
  230. if (m) {
  231. m.set('rawSourceData', rawSourceData[alertDefinitionId]);
  232. }
  233. }
  234. }
  235. }
  236. });