datasource.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'moment',
  5. 'app/core/utils/datemath',
  6. './annotation_query',
  7. ],
  8. function (angular, _, moment, dateMath, CloudWatchAnnotationQuery) {
  9. 'use strict';
  10. /** @ngInject */
  11. function CloudWatchDatasource(instanceSettings, $q, backendSrv, templateSrv) {
  12. this.type = 'cloudwatch';
  13. this.name = instanceSettings.name;
  14. this.supportMetrics = true;
  15. this.proxyUrl = instanceSettings.url;
  16. this.defaultRegion = instanceSettings.jsonData.defaultRegion;
  17. var self = this;
  18. this.query = function(options) {
  19. var start = self.convertToCloudWatchTime(options.range.from, false);
  20. var end = self.convertToCloudWatchTime(options.range.to, true);
  21. var queries = [];
  22. options = angular.copy(options);
  23. _.each(options.targets, _.bind(function(target) {
  24. if (target.hide || !target.namespace || !target.metricName || _.isEmpty(target.statistics)) {
  25. return;
  26. }
  27. var query = {};
  28. query.region = templateSrv.replace(target.region, options.scopedVars);
  29. query.namespace = templateSrv.replace(target.namespace, options.scopedVars);
  30. query.metricName = templateSrv.replace(target.metricName, options.scopedVars);
  31. query.dimensions = self.convertDimensionFormat(target.dimensions, options.scopedVars);
  32. query.statistics = target.statistics;
  33. var range = end - start;
  34. query.period = parseInt(target.period, 10) || (query.namespace === 'AWS/EC2' ? 300 : 60);
  35. if (range / query.period >= 1440) {
  36. query.period = Math.ceil(range / 1440 / 60) * 60;
  37. }
  38. target.period = query.period;
  39. queries.push(query);
  40. }, this));
  41. // No valid targets, return the empty result to save a round trip.
  42. if (_.isEmpty(queries)) {
  43. var d = $q.defer();
  44. d.resolve({ data: [] });
  45. return d.promise;
  46. }
  47. var allQueryPromise = _.map(queries, function(query) {
  48. return this.performTimeSeriesQuery(query, start, end);
  49. }, this);
  50. return $q.all(allQueryPromise).then(function(allResponse) {
  51. var result = [];
  52. _.each(allResponse, function(response, index) {
  53. var metrics = transformMetricData(response, options.targets[index]);
  54. result = result.concat(metrics);
  55. });
  56. return { data: result };
  57. });
  58. };
  59. this.performTimeSeriesQuery = function(query, start, end) {
  60. return this.awsRequest({
  61. region: query.region,
  62. action: 'GetMetricStatistics',
  63. parameters: {
  64. namespace: query.namespace,
  65. metricName: query.metricName,
  66. dimensions: query.dimensions,
  67. statistics: query.statistics,
  68. startTime: start,
  69. endTime: end,
  70. period: query.period
  71. }
  72. });
  73. };
  74. this.getRegions = function() {
  75. return this.awsRequest({action: '__GetRegions'});
  76. };
  77. this.getNamespaces = function() {
  78. return this.awsRequest({action: '__GetNamespaces'});
  79. };
  80. this.getMetrics = function(namespace) {
  81. return this.awsRequest({
  82. action: '__GetMetrics',
  83. parameters: {
  84. namespace: templateSrv.replace(namespace)
  85. }
  86. });
  87. };
  88. this.getDimensionKeys = function(namespace) {
  89. return this.awsRequest({
  90. action: '__GetDimensions',
  91. parameters: {
  92. namespace: templateSrv.replace(namespace)
  93. }
  94. });
  95. };
  96. this.getDimensionValues = function(region, namespace, metricName, dimensionKey, filterDimensions) {
  97. var request = {
  98. region: templateSrv.replace(region),
  99. action: 'ListMetrics',
  100. parameters: {
  101. namespace: templateSrv.replace(namespace),
  102. metricName: templateSrv.replace(metricName),
  103. dimensions: this.convertDimensionFormat(filterDimensions, {}),
  104. }
  105. };
  106. return this.awsRequest(request).then(function(result) {
  107. return _.chain(result.Metrics)
  108. .pluck('Dimensions')
  109. .flatten()
  110. .filter(function(dimension) {
  111. return dimension !== null && dimension.Name === dimensionKey;
  112. })
  113. .pluck('Value')
  114. .uniq()
  115. .sortBy()
  116. .map(function(value) {
  117. return {value: value, text: value};
  118. }).value();
  119. });
  120. };
  121. this.performEC2DescribeInstances = function(region, filters, instanceIds) {
  122. return this.awsRequest({
  123. region: region,
  124. action: 'DescribeInstances',
  125. parameters: { filter: filters, instanceIds: instanceIds }
  126. });
  127. };
  128. this.metricFindQuery = function(query) {
  129. var region;
  130. var namespace;
  131. var metricName;
  132. var transformSuggestData = function(suggestData) {
  133. return _.map(suggestData, function(v) {
  134. return { text: v };
  135. });
  136. };
  137. var regionQuery = query.match(/^regions\(\)/);
  138. if (regionQuery) {
  139. return this.getRegions();
  140. }
  141. var namespaceQuery = query.match(/^namespaces\(\)/);
  142. if (namespaceQuery) {
  143. return this.getNamespaces();
  144. }
  145. var metricNameQuery = query.match(/^metrics\(([^\)]+?)\)/);
  146. if (metricNameQuery) {
  147. return this.getMetrics(metricNameQuery[1]);
  148. }
  149. var dimensionKeysQuery = query.match(/^dimension_keys\(([^\)]+?)\)/);
  150. if (dimensionKeysQuery) {
  151. return this.getDimensionKeys(dimensionKeysQuery[1]);
  152. }
  153. var dimensionValuesQuery = query.match(/^dimension_values\(([^,]+?),\s?([^,]+?),\s?([^,]+?),\s?([^,]+?)\)/);
  154. if (dimensionValuesQuery) {
  155. region = templateSrv.replace(dimensionValuesQuery[1]);
  156. namespace = templateSrv.replace(dimensionValuesQuery[2]);
  157. metricName = templateSrv.replace(dimensionValuesQuery[3]);
  158. var dimensionKey = templateSrv.replace(dimensionValuesQuery[4]);
  159. return this.getDimensionValues(region, namespace, metricName, dimensionKey, {});
  160. }
  161. var ebsVolumeIdsQuery = query.match(/^ebs_volume_ids\(([^,]+?),\s?([^,]+?)\)/);
  162. if (ebsVolumeIdsQuery) {
  163. region = templateSrv.replace(ebsVolumeIdsQuery[1]);
  164. var instanceId = templateSrv.replace(ebsVolumeIdsQuery[2]);
  165. var instanceIds = [
  166. instanceId
  167. ];
  168. return this.performEC2DescribeInstances(region, [], instanceIds).then(function(result) {
  169. var volumeIds = _.map(result.Reservations[0].Instances[0].BlockDeviceMappings, function(mapping) {
  170. return mapping.Ebs.VolumeId;
  171. });
  172. return transformSuggestData(volumeIds);
  173. });
  174. }
  175. return $q.when([]);
  176. };
  177. this.performDescribeAlarms = function(region, actionPrefix, alarmNamePrefix, alarmNames, stateValue) {
  178. return this.awsRequest({
  179. region: region,
  180. action: 'DescribeAlarms',
  181. parameters: { actionPrefix: actionPrefix, alarmNamePrefix: alarmNamePrefix, alarmNames: alarmNames, stateValue: stateValue }
  182. });
  183. };
  184. this.performDescribeAlarmsForMetric = function(region, namespace, metricName, dimensions, statistic, period) {
  185. return this.awsRequest({
  186. region: region,
  187. action: 'DescribeAlarmsForMetric',
  188. parameters: { namespace: namespace, metricName: metricName, dimensions: dimensions, statistic: statistic, period: period }
  189. });
  190. };
  191. this.performDescribeAlarmHistory = function(region, alarmName, startDate, endDate) {
  192. return this.awsRequest({
  193. region: region,
  194. action: 'DescribeAlarmHistory',
  195. parameters: { alarmName: alarmName, startDate: startDate, endDate: endDate }
  196. });
  197. };
  198. this.annotationQuery = function(options) {
  199. var annotationQuery = new CloudWatchAnnotationQuery(this, options.annotation, $q, templateSrv);
  200. return annotationQuery.process(options.range.from, options.range.to);
  201. };
  202. this.testDatasource = function() {
  203. /* use billing metrics for test */
  204. var region = this.defaultRegion;
  205. var namespace = 'AWS/Billing';
  206. var metricName = 'EstimatedCharges';
  207. var dimensions = {};
  208. return this.getDimensionValues(region, namespace, metricName, 'ServiceName', dimensions).then(function () {
  209. return { status: 'success', message: 'Data source is working', title: 'Success' };
  210. });
  211. };
  212. this.awsRequest = function(data) {
  213. var options = {
  214. method: 'POST',
  215. url: this.proxyUrl,
  216. data: data
  217. };
  218. return backendSrv.datasourceRequest(options).then(function(result) {
  219. return result.data;
  220. });
  221. };
  222. this.getDefaultRegion = function() {
  223. return this.defaultRegion;
  224. };
  225. function transformMetricData(md, options) {
  226. var aliasRegex = /\{\{(.+?)\}\}/g;
  227. var aliasPattern = options.alias || '{{metric}}_{{stat}}';
  228. var aliasData = {
  229. region: templateSrv.replace(options.region),
  230. namespace: templateSrv.replace(options.namespace),
  231. metric: templateSrv.replace(options.metricName),
  232. };
  233. _.extend(aliasData, options.dimensions);
  234. var periodMs = options.period * 1000;
  235. return _.map(options.statistics, function(stat) {
  236. var dps = [];
  237. var lastTimestamp = null;
  238. _.chain(md.Datapoints)
  239. .sortBy(function(dp) {
  240. return dp.Timestamp;
  241. })
  242. .each(function(dp) {
  243. var timestamp = new Date(dp.Timestamp).getTime();
  244. if (lastTimestamp && (timestamp - lastTimestamp) > periodMs) {
  245. dps.push([null, lastTimestamp + periodMs]);
  246. }
  247. lastTimestamp = timestamp;
  248. dps.push([dp[stat], timestamp]);
  249. });
  250. aliasData.stat = stat;
  251. var seriesName = aliasPattern.replace(aliasRegex, function(match, g1) {
  252. if (aliasData[g1]) {
  253. return aliasData[g1];
  254. }
  255. return g1;
  256. });
  257. return {target: seriesName, datapoints: dps};
  258. });
  259. }
  260. this.convertToCloudWatchTime = function(date, roundUp) {
  261. if (_.isString(date)) {
  262. date = dateMath.parse(date, roundUp);
  263. }
  264. return Math.round(date.valueOf() / 1000);
  265. };
  266. this.convertDimensionFormat = function(dimensions, scopedVars) {
  267. return _.map(dimensions, function(value, key) {
  268. return {
  269. Name: templateSrv.replace(key, scopedVars),
  270. Value: templateSrv.replace(value, scopedVars)
  271. };
  272. });
  273. };
  274. }
  275. return {
  276. CloudWatchDatasource: CloudWatchDatasource
  277. };
  278. });