alert_definitions_mapper.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. label: 'AlertDefinition.label',
  28. service_id: 'AlertDefinition.service_name',
  29. component_name: 'AlertDefinition.component_name',
  30. enabled: 'AlertDefinition.enabled',
  31. scope: 'AlertDefinition.scope',
  32. interval: 'AlertDefinition.interval',
  33. type: 'AlertDefinition.source.type',
  34. reporting_key: 'reporting',
  35. reporting_type: 'array',
  36. reporting: {
  37. item: 'id'
  38. }
  39. },
  40. portConfig: {
  41. default_port: 'AlertDefinition.source.default_port',
  42. uri: 'AlertDefinition.source.uri'
  43. },
  44. aggregateConfig: {
  45. alert_name: 'AlertDefinition.source.alert_name'
  46. },
  47. scriptConfig: {
  48. location: 'AlertDefinition.source.path'
  49. },
  50. uriConfig: {
  51. id: 'AlertDefinition.source.uri.id',
  52. http: 'AlertDefinition.source.uri.http',
  53. https: 'AlertDefinition.source.uri.https',
  54. https_property: 'AlertDefinition.source.uri.https_property',
  55. https_property_value: 'AlertDefinition.source.uri.https_property_value'
  56. },
  57. map: function (json) {
  58. if (json && json.items) {
  59. var portAlertDefinitions = [];
  60. var metricsAlertDefinitions = [];
  61. var webAlertDefinitions = [];
  62. var aggregateAlertDefinitions = [];
  63. var scriptAlertDefinitions = [];
  64. var alertReportDefinitions = [];
  65. var alertMetricsSourceDefinitions = [];
  66. var alertMetricsUriDefinitions = [];
  67. json.items.forEach(function (item) {
  68. var convertedReportDefinitions = [];
  69. var reporting = item.AlertDefinition.source.reporting;
  70. for (var report in reporting) {
  71. if (reporting.hasOwnProperty(report)) {
  72. convertedReportDefinitions.push({
  73. id: item.AlertDefinition.id + report,
  74. type: report,
  75. text: reporting[report].text,
  76. value: reporting[report].value
  77. });
  78. }
  79. }
  80. alertReportDefinitions = alertReportDefinitions.concat(convertedReportDefinitions);
  81. item.reporting = convertedReportDefinitions;
  82. var alertDefinition = this.parseIt(item, this.get('config'));
  83. // map properties dependent on Alert Definition type
  84. switch (item.AlertDefinition.source.type) {
  85. case 'PORT':
  86. portAlertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('portConfig'))));
  87. break;
  88. case 'METRIC':
  89. // map App.AlertMetricsSourceDefinition's
  90. var jmxMetric = item.AlertDefinition.source.jmx;
  91. var gangliaMetric = item.AlertDefinition.source.ganglia;
  92. if (jmxMetric) {
  93. alertReportDefinitions.jmx_id = item.AlertDefinition.id + 'jmx';
  94. alertMetricsSourceDefinitions.push({
  95. id: item.AlertDefinition.id + 'jmx',
  96. value: jmxMetric.value
  97. });
  98. }
  99. if (gangliaMetric) {
  100. alertReportDefinitions.ganglia_id = item.AlertDefinition.id + 'ganglia';
  101. alertMetricsSourceDefinitions.push({
  102. id: alertReportDefinitions.ganglia_id,
  103. value: gangliaMetric.value
  104. });
  105. }
  106. // map App.AlertMetricsUriDefinition
  107. alertDefinition.uri_id = item.AlertDefinition.id + 'uri';
  108. item.AlertDefinition.source.uri.id = alertDefinition.uri_id;
  109. alertMetricsUriDefinitions.push(this.parseIt(item, this.get('uriConfig')));
  110. metricsAlertDefinitions.push(alertDefinition);
  111. break;
  112. case 'WEB':
  113. // map App.AlertMetricsUriDefinition
  114. alertDefinition.uri_id = item.AlertDefinition.id + 'uri';
  115. item.AlertDefinition.source.uri.id = alertDefinition.uri_id;
  116. alertMetricsUriDefinitions.push(this.parseIt(item, this.get('uriConfig')));
  117. webAlertDefinitions.push(alertDefinition);
  118. break;
  119. case 'AGGREGATE':
  120. aggregateAlertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('aggregateConfig'))));
  121. break;
  122. case 'SCRIPT':
  123. scriptAlertDefinitions.push($.extend(alertDefinition, this.parseIt(item, this.get('scriptConfig'))));
  124. break;
  125. default:
  126. console.error('Incorrect Alert Definition type:', item.AlertDefinition);
  127. }
  128. }, this);
  129. // load all mapped data to model
  130. App.store.loadMany(this.get('reportModel'), alertReportDefinitions);
  131. App.store.loadMany(this.get('metricsSourceModel'), alertMetricsSourceDefinitions);
  132. this.setMetricsSourcePropertyLists(this.get('metricsSourceModel'), alertMetricsSourceDefinitions);
  133. App.store.loadMany(this.get('metricsUriModel'), alertMetricsUriDefinitions);
  134. App.store.loadMany(App.PortAlertDefinition, portAlertDefinitions);
  135. App.store.loadMany(App.MetricsAlertDefinition, metricsAlertDefinitions);
  136. App.store.loadMany(App.WebAlertDefinition, webAlertDefinitions);
  137. App.store.loadMany(App.AggregateAlertDefinition, aggregateAlertDefinitions);
  138. App.store.loadMany(App.ScriptAlertDefinition, scriptAlertDefinitions);
  139. }
  140. },
  141. /**
  142. * set propertyList properties from <code>data</code> for records in <code>model</code>
  143. * @param model
  144. * @param data
  145. */
  146. setMetricsSourcePropertyLists: function (model, data) {
  147. data.forEach(function (record) {
  148. model.find().findProperty('id', record.id).set('propertyList', record.property_list);
  149. });
  150. }
  151. });