alert_definitions_mapper.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. portModel: App.PortAlertDefinition,
  25. metricsModel: App.MetricsAlertDefinition,
  26. webModel: App.WebAlertDefinition,
  27. aggregateModel: App.AggregateAlertDefinition,
  28. scriptModel: App.ScriptAlertDefinition,
  29. config: {
  30. id: 'AlertDefinition.id',
  31. name: 'AlertDefinition.name',
  32. description: 'AlertDefinition.description',
  33. label: 'AlertDefinition.label',
  34. service_id: 'AlertDefinition.service_name',
  35. service_name: 'AlertDefinition.service_name',
  36. component_name: 'AlertDefinition.component_name',
  37. enabled: 'AlertDefinition.enabled',
  38. scope: 'AlertDefinition.scope',
  39. interval: 'AlertDefinition.interval',
  40. type: 'AlertDefinition.source.type',
  41. reporting_key: 'reporting',
  42. reporting_type: 'array',
  43. reporting: {
  44. item: 'id'
  45. }
  46. },
  47. portConfig: {
  48. default_port: 'AlertDefinition.source.default_port',
  49. uri: 'AlertDefinition.source.uri'
  50. },
  51. aggregateConfig: {
  52. alert_name: 'AlertDefinition.source.alert_name'
  53. },
  54. scriptConfig: {
  55. location: 'AlertDefinition.source.path'
  56. },
  57. uriConfig: {
  58. id: 'AlertDefinition.source.uri.id',
  59. http: 'AlertDefinition.source.uri.http',
  60. https: 'AlertDefinition.source.uri.https',
  61. https_property: 'AlertDefinition.source.uri.https_property',
  62. https_property_value: 'AlertDefinition.source.uri.https_property_value'
  63. },
  64. map: function (json) {
  65. if (json && json.items) {
  66. var self = this,
  67. portAlertDefinitions = [],
  68. metricsAlertDefinitions = [],
  69. webAlertDefinitions = [],
  70. aggregateAlertDefinitions = [],
  71. scriptAlertDefinitions = [],
  72. alertReportDefinitions = [],
  73. alertMetricsSourceDefinitions = [],
  74. alertMetricsUriDefinitions = [],
  75. alertGroupsMap = App.cache['previousAlertGroupsMap'],
  76. alertDefinitions = App.AlertDefinition.getAllDefinitions(),
  77. alertDefinitionsToDelete = alertDefinitions.mapProperty('id'),
  78. rawSourceData = {};
  79. json.items.forEach(function (item) {
  80. var convertedReportDefinitions = [];
  81. var reporting = item.AlertDefinition.source.reporting;
  82. for (var report in reporting) {
  83. if (reporting.hasOwnProperty(report)) {
  84. convertedReportDefinitions.push({
  85. id: item.AlertDefinition.id + report,
  86. type: report,
  87. text: reporting[report].text,
  88. value: reporting[report].value
  89. });
  90. }
  91. }
  92. alertReportDefinitions = alertReportDefinitions.concat(convertedReportDefinitions);
  93. item.reporting = convertedReportDefinitions;
  94. rawSourceData[item.AlertDefinition.id] = item.AlertDefinition.source;
  95. item.AlertDefinition.description = item.AlertDefinition.description || '';
  96. var alertDefinition = this.parseIt(item, this.get('config'));
  97. if (alertGroupsMap[alertDefinition.id]) {
  98. alertDefinition.groups = alertGroupsMap[alertDefinition.id];
  99. }
  100. var oldAlertDefinition = alertDefinitions.findProperty('id', alertDefinition.id);
  101. if (oldAlertDefinition) {
  102. // new values will be parsed in the another mapper, so for now just use old values
  103. alertDefinition.summary = oldAlertDefinition.get('summary');
  104. alertDefinition.last_triggered = oldAlertDefinition.get('lastTriggered');
  105. }
  106. alertDefinitionsToDelete = alertDefinitionsToDelete.without(alertDefinition.id);
  107. // map properties dependent on Alert Definition type
  108. switch (item.AlertDefinition.source.type) {
  109. case 'PORT':
  110. portAlertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('portConfig'))));
  111. break;
  112. case 'METRIC':
  113. // map App.AlertMetricsSourceDefinition's
  114. var jmxMetric = item.AlertDefinition.source.jmx;
  115. var gangliaMetric = item.AlertDefinition.source.ganglia;
  116. if (jmxMetric) {
  117. alertDefinition.jmx_id = item.AlertDefinition.id + 'jmx';
  118. alertMetricsSourceDefinitions.push({
  119. id: alertDefinition.jmx_id,
  120. value: jmxMetric.value,
  121. property_list: jmxMetric.property_list
  122. });
  123. }
  124. if (gangliaMetric) {
  125. alertDefinition.ganglia_id = item.AlertDefinition.id + 'ganglia';
  126. alertMetricsSourceDefinitions.push({
  127. id: alertDefinition.ganglia_id,
  128. value: gangliaMetric.value,
  129. property_list: gangliaMetric.property_list
  130. });
  131. }
  132. // map App.AlertMetricsUriDefinition
  133. alertDefinition.uri_id = item.AlertDefinition.id + 'uri';
  134. item.AlertDefinition.source.uri.id = alertDefinition.uri_id;
  135. alertMetricsUriDefinitions.push(this.parseIt(item, this.get('uriConfig')));
  136. metricsAlertDefinitions.push(alertDefinition);
  137. break;
  138. case 'WEB':
  139. // map App.AlertMetricsUriDefinition
  140. alertDefinition.uri_id = item.AlertDefinition.id + 'uri';
  141. item.AlertDefinition.source.uri.id = alertDefinition.uri_id;
  142. alertMetricsUriDefinitions.push(this.parseIt(item, this.get('uriConfig')));
  143. webAlertDefinitions.push(alertDefinition);
  144. break;
  145. case 'AGGREGATE':
  146. aggregateAlertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('aggregateConfig'))));
  147. break;
  148. case 'SCRIPT':
  149. scriptAlertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('scriptConfig'))));
  150. break;
  151. default:
  152. console.error('Incorrect Alert Definition type:', item.AlertDefinition);
  153. }
  154. }, this);
  155. alertDefinitionsToDelete.forEach(function(definitionId) {
  156. self.deleteRecord(alertDefinitions.findProperty('id', definitionId));
  157. });
  158. // load all mapped data to model
  159. App.store.loadMany(this.get('reportModel'), alertReportDefinitions);
  160. App.store.loadMany(this.get('metricsSourceModel'), alertMetricsSourceDefinitions);
  161. this.setMetricsSourcePropertyLists(this.get('metricsSourceModel'), alertMetricsSourceDefinitions);
  162. App.store.loadMany(this.get('metricsUriModel'), alertMetricsUriDefinitions);
  163. App.store.loadMany(this.get('portModel'), portAlertDefinitions);
  164. App.store.loadMany(this.get('metricsModel'), metricsAlertDefinitions);
  165. App.store.loadMany(this.get('webModel'), webAlertDefinitions);
  166. App.store.loadMany(this.get('aggregateModel'), aggregateAlertDefinitions);
  167. App.store.loadMany(this.get('scriptModel'), scriptAlertDefinitions);
  168. this.setAlertDefinitionsRawSourceData(rawSourceData);
  169. if (App.router.get('mainAlertDefinitionsController')) {
  170. App.router.set('mainAlertDefinitionsController.mapperTimestamp', (new Date()).getTime());
  171. }
  172. App.store.commit();
  173. }
  174. },
  175. /**
  176. * set propertyList properties from <code>data</code> for records in <code>model</code>
  177. * @param model
  178. * @param data
  179. */
  180. setMetricsSourcePropertyLists: function (model, data) {
  181. data.forEach(function (record) {
  182. model.find().findProperty('id', record.id).set('propertyList', record.property_list);
  183. });
  184. },
  185. /**
  186. * set rawSourceDate properties for <code>App.AlertDefinition</code> records
  187. * @param rawSourceData
  188. */
  189. setAlertDefinitionsRawSourceData: function (rawSourceData) {
  190. var allDefinitions = App.AlertDefinition.getAllDefinitions();
  191. for (var alertDefinitionId in rawSourceData) {
  192. if (rawSourceData.hasOwnProperty(alertDefinitionId)) {
  193. allDefinitions.findProperty('id', +alertDefinitionId).set('rawSourceData', rawSourceData[alertDefinitionId]);
  194. }
  195. }
  196. }
  197. });