alert_definitions_mapper.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. var stringUtils = require('utils/string_utils');
  19. App.alertDefinitionsMapper = App.QuickDataMapper.create({
  20. model: App.AlertDefinition,
  21. reportModel: App.AlertReportDefinition,
  22. metricsSourceModel: App.AlertMetricsSourceDefinition,
  23. metricsUriModel: App.AlertMetricsUriDefinition,
  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. },
  42. portConfig: {
  43. default_port: 'AlertDefinition.source.default_port',
  44. port_uri: 'AlertDefinition.source.uri'
  45. },
  46. aggregateConfig: {
  47. alert_name: 'AlertDefinition.source.alert_name'
  48. },
  49. scriptConfig: {
  50. location: 'AlertDefinition.source.path'
  51. },
  52. serverConfig: {
  53. },
  54. uriConfig: {
  55. id: 'AlertDefinition.source.uri.id',
  56. http: 'AlertDefinition.source.uri.http',
  57. https: 'AlertDefinition.source.uri.https',
  58. https_property: 'AlertDefinition.source.uri.https_property',
  59. https_property_value: 'AlertDefinition.source.uri.https_property_value'
  60. },
  61. map: function (json) {
  62. console.time('App.alertDefinitionsMapper execution time');
  63. if (json && json.items) {
  64. var self = this,
  65. alertDefinitions = [],
  66. alertReportDefinitions = [],
  67. alertMetricsSourceDefinitions = [],
  68. alertMetricsUriDefinitions = [],
  69. alertGroupsMap = App.cache['previousAlertGroupsMap'],
  70. existingAlertDefinitions = App.AlertDefinition.find(),
  71. existingAlertDefinitionsMap = existingAlertDefinitions.toArray().toMapByProperty('id'),
  72. alertDefinitionsToDelete = existingAlertDefinitions.mapProperty('id'),
  73. rawSourceData = {};
  74. json.items.forEach(function (item) {
  75. var convertedReportDefinitions = [];
  76. var reporting = item.AlertDefinition.source.reporting;
  77. for (var report in reporting) {
  78. if (reporting.hasOwnProperty(report)) {
  79. if (report == "units") {
  80. convertedReportDefinitions.push({
  81. id: item.AlertDefinition.id + report,
  82. type: report,
  83. text: reporting[report]
  84. });
  85. } else {
  86. convertedReportDefinitions.push({
  87. id: item.AlertDefinition.id + report,
  88. type: report,
  89. text: reporting[report].text,
  90. value: reporting[report].value
  91. });
  92. }
  93. }
  94. }
  95. alertReportDefinitions = alertReportDefinitions.concat(convertedReportDefinitions);
  96. item.reporting = convertedReportDefinitions;
  97. rawSourceData[item.AlertDefinition.id] = item.AlertDefinition.source;
  98. item.AlertDefinition.description = item.AlertDefinition.description || '';
  99. var alertDefinition = this.parseIt(item, this.get('config'));
  100. if (alertGroupsMap[alertDefinition.id]) {
  101. alertDefinition.groups = alertGroupsMap[alertDefinition.id];
  102. }
  103. var oldAlertDefinition = existingAlertDefinitionsMap[alertDefinition.id];
  104. if (oldAlertDefinition) {
  105. // new values will be parsed in the another mapper, so for now just use old values
  106. alertDefinition.summary = oldAlertDefinition.get('summary');
  107. alertDefinition.last_triggered = oldAlertDefinition.get('lastTriggered');
  108. alertDefinition.last_triggered_raw = oldAlertDefinition.get('lastTriggeredRaw');
  109. }
  110. alertDefinitionsToDelete = alertDefinitionsToDelete.without(alertDefinition.id);
  111. // map properties dependent on Alert Definition type
  112. switch (item.AlertDefinition.source.type) {
  113. case 'PORT':
  114. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('portConfig'))));
  115. break;
  116. case 'METRIC':
  117. // map App.AlertMetricsSourceDefinition's
  118. var jmxMetric = item.AlertDefinition.source.jmx;
  119. var gangliaMetric = item.AlertDefinition.source.ganglia;
  120. if (jmxMetric) {
  121. alertDefinition.jmx_id = item.AlertDefinition.id + 'jmx';
  122. alertMetricsSourceDefinitions.push({
  123. id: alertDefinition.jmx_id,
  124. value: jmxMetric.value,
  125. property_list: jmxMetric.property_list
  126. });
  127. }
  128. if (gangliaMetric) {
  129. alertDefinition.ganglia_id = item.AlertDefinition.id + 'ganglia';
  130. alertMetricsSourceDefinitions.push({
  131. id: alertDefinition.ganglia_id,
  132. value: gangliaMetric.value,
  133. property_list: gangliaMetric.property_list
  134. });
  135. }
  136. // map App.AlertMetricsUriDefinition
  137. alertDefinition.uri_id = item.AlertDefinition.id + 'uri';
  138. item.AlertDefinition.source.uri.id = alertDefinition.uri_id;
  139. alertMetricsUriDefinitions.push(this.parseIt(item, this.get('uriConfig')));
  140. alertDefinitions.push(alertDefinition);
  141. break;
  142. case 'WEB':
  143. // map App.AlertMetricsUriDefinition
  144. alertDefinition.uri_id = item.AlertDefinition.id + 'uri';
  145. item.AlertDefinition.source.uri.id = alertDefinition.uri_id;
  146. alertMetricsUriDefinitions.push(this.parseIt(item, this.get('uriConfig')));
  147. alertDefinitions.push(alertDefinition);
  148. break;
  149. case 'AGGREGATE':
  150. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('aggregateConfig'))));
  151. break;
  152. case 'SCRIPT':
  153. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('scriptConfig'))));
  154. break;
  155. case 'SERVER':
  156. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('serverConfig'))));
  157. break;
  158. default:
  159. console.error('Incorrect Alert Definition type:', item.AlertDefinition);
  160. }
  161. }, this);
  162. alertDefinitionsToDelete.forEach(function(definitionId) {
  163. self.deleteRecord(existingAlertDefinitions.findProperty('id', definitionId));
  164. });
  165. // load all mapped data to model
  166. App.store.loadMany(this.get('reportModel'), alertReportDefinitions);
  167. App.store.loadMany(this.get('metricsSourceModel'), alertMetricsSourceDefinitions);
  168. this.setMetricsSourcePropertyLists(this.get('metricsSourceModel'), alertMetricsSourceDefinitions);
  169. App.store.loadMany(this.get('metricsUriModel'), alertMetricsUriDefinitions);
  170. // this loadMany takes too much time
  171. App.store.loadMany(this.get('model'), alertDefinitions);
  172. this.setAlertDefinitionsRawSourceData(rawSourceData);
  173. App.store.commit();
  174. }
  175. console.timeEnd('App.alertDefinitionsMapper execution time');
  176. },
  177. /**
  178. * set propertyList properties from <code>data</code> for records in <code>model</code>
  179. * @param model
  180. * @param data
  181. */
  182. setMetricsSourcePropertyLists: function (model, data) {
  183. var modelsMap = model.find().toArray().toMapByProperty('id');
  184. data.forEach(function (record) {
  185. var m = modelsMap[record.id];
  186. if (m) {
  187. m.set('propertyList', record.property_list);
  188. }
  189. });
  190. },
  191. /**
  192. * set rawSourceDate properties for <code>App.AlertDefinition</code> records
  193. * @param rawSourceData
  194. */
  195. setAlertDefinitionsRawSourceData: function (rawSourceData) {
  196. var allDefinitions = App.AlertDefinition.find();
  197. var allDefinitionsMap = allDefinitions.toArray().toMapByProperty('id');
  198. for (var alertDefinitionId in rawSourceData) {
  199. if (rawSourceData.hasOwnProperty(alertDefinitionId)) {
  200. var m = allDefinitionsMap[+alertDefinitionId];
  201. if (m) {
  202. m.set('rawSourceData', rawSourceData[alertDefinitionId]);
  203. }
  204. }
  205. }
  206. }
  207. });