alert_definitions_mapper.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. }
  109. alertDefinitionsToDelete = alertDefinitionsToDelete.without(alertDefinition.id);
  110. // map properties dependent on Alert Definition type
  111. switch (item.AlertDefinition.source.type) {
  112. case 'PORT':
  113. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('portConfig'))));
  114. break;
  115. case 'METRIC':
  116. // map App.AlertMetricsSourceDefinition's
  117. var jmxMetric = item.AlertDefinition.source.jmx;
  118. var gangliaMetric = item.AlertDefinition.source.ganglia;
  119. if (jmxMetric) {
  120. alertDefinition.jmx_id = item.AlertDefinition.id + 'jmx';
  121. alertMetricsSourceDefinitions.push({
  122. id: alertDefinition.jmx_id,
  123. value: jmxMetric.value,
  124. property_list: jmxMetric.property_list
  125. });
  126. }
  127. if (gangliaMetric) {
  128. alertDefinition.ganglia_id = item.AlertDefinition.id + 'ganglia';
  129. alertMetricsSourceDefinitions.push({
  130. id: alertDefinition.ganglia_id,
  131. value: gangliaMetric.value,
  132. property_list: gangliaMetric.property_list
  133. });
  134. }
  135. // map App.AlertMetricsUriDefinition
  136. alertDefinition.uri_id = item.AlertDefinition.id + 'uri';
  137. item.AlertDefinition.source.uri.id = alertDefinition.uri_id;
  138. alertMetricsUriDefinitions.push(this.parseIt(item, this.get('uriConfig')));
  139. alertDefinitions.push(alertDefinition);
  140. break;
  141. case 'WEB':
  142. // map App.AlertMetricsUriDefinition
  143. alertDefinition.uri_id = item.AlertDefinition.id + 'uri';
  144. item.AlertDefinition.source.uri.id = alertDefinition.uri_id;
  145. alertMetricsUriDefinitions.push(this.parseIt(item, this.get('uriConfig')));
  146. alertDefinitions.push(alertDefinition);
  147. break;
  148. case 'AGGREGATE':
  149. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('aggregateConfig'))));
  150. break;
  151. case 'SCRIPT':
  152. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('scriptConfig'))));
  153. break;
  154. case 'SERVER':
  155. alertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('serverConfig'))));
  156. break;
  157. default:
  158. console.error('Incorrect Alert Definition type:', item.AlertDefinition);
  159. }
  160. }, this);
  161. alertDefinitionsToDelete.forEach(function(definitionId) {
  162. self.deleteRecord(existingAlertDefinitions.findProperty('id', definitionId));
  163. });
  164. // load all mapped data to model
  165. App.store.loadMany(this.get('reportModel'), alertReportDefinitions);
  166. App.store.loadMany(this.get('metricsSourceModel'), alertMetricsSourceDefinitions);
  167. this.setMetricsSourcePropertyLists(this.get('metricsSourceModel'), alertMetricsSourceDefinitions);
  168. App.store.loadMany(this.get('metricsUriModel'), alertMetricsUriDefinitions);
  169. // this loadMany takes too much time
  170. App.store.loadMany(this.get('model'), alertDefinitions);
  171. this.setAlertDefinitionsRawSourceData(rawSourceData);
  172. App.store.commit();
  173. }
  174. console.timeEnd('App.alertDefinitionsMapper execution time');
  175. },
  176. /**
  177. * set propertyList properties from <code>data</code> for records in <code>model</code>
  178. * @param model
  179. * @param data
  180. */
  181. setMetricsSourcePropertyLists: function (model, data) {
  182. var modelsMap = model.find().toArray().toMapByProperty('id');
  183. data.forEach(function (record) {
  184. var m = modelsMap[record.id];
  185. if (m) {
  186. m.set('propertyList', record.property_list);
  187. }
  188. });
  189. },
  190. /**
  191. * set rawSourceDate properties for <code>App.AlertDefinition</code> records
  192. * @param rawSourceData
  193. */
  194. setAlertDefinitionsRawSourceData: function (rawSourceData) {
  195. var allDefinitions = App.AlertDefinition.find();
  196. var allDefinitionsMap = allDefinitions.toArray().toMapByProperty('id');
  197. for (var alertDefinitionId in rawSourceData) {
  198. if (rawSourceData.hasOwnProperty(alertDefinitionId)) {
  199. var m = allDefinitionsMap[+alertDefinitionId];
  200. if (m) {
  201. m.set('rawSourceData', rawSourceData[alertDefinitionId]);
  202. }
  203. }
  204. }
  205. }
  206. });