datasource.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'moment',
  5. './query_ctrl',
  6. './directives',
  7. ],
  8. function (angular, _) {
  9. 'use strict';
  10. var module = angular.module('grafana.services');
  11. module.factory('CloudWatchDatasource', function($q, backendSrv, templateSrv) {
  12. function CloudWatchDatasource(datasource) {
  13. this.type = 'cloudwatch';
  14. this.name = datasource.name;
  15. this.supportMetrics = true;
  16. this.proxyUrl = datasource.url;
  17. this.defaultRegion = datasource.jsonData.defaultRegion;
  18. }
  19. CloudWatchDatasource.prototype.query = function(options) {
  20. var start = convertToCloudWatchTime(options.range.from);
  21. var end = convertToCloudWatchTime(options.range.to);
  22. var queries = [];
  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 = convertDimensionFormat(target.dimensions);
  32. query.statistics = getActivatedStatistics(target.statistics);
  33. query.period = parseInt(target.period, 10);
  34. var range = end - start;
  35. // CloudWatch limit datapoints up to 1440
  36. if (range / query.period >= 1440) {
  37. query.period = Math.floor(range / 1440 / 60) * 60;
  38. }
  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. CloudWatchDatasource.prototype.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. CloudWatchDatasource.prototype.getRegions = function() {
  75. return this.awsRequest({action: '__GetRegions'});
  76. };
  77. CloudWatchDatasource.prototype.getNamespaces = function() {
  78. return this.awsRequest({action: '__GetNamespaces'});
  79. };
  80. CloudWatchDatasource.prototype.getMetrics = function(namespace) {
  81. return this.awsRequest({
  82. action: '__GetMetrics',
  83. parameters: {
  84. namespace: templateSrv.replace(namespace)
  85. }
  86. });
  87. };
  88. CloudWatchDatasource.prototype.getDimensionKeys = function(namespace) {
  89. namespace = templateSrv.replace(namespace);
  90. return $q.when(this.supportedDimensions[namespace] || []);
  91. };
  92. CloudWatchDatasource.prototype.getDimensionValues = function(region, namespace, metricName, dimensions) {
  93. var request = {
  94. region: templateSrv.replace(region),
  95. action: 'ListMetrics',
  96. parameters: {
  97. namespace: templateSrv.replace(namespace),
  98. metricName: templateSrv.replace(metricName),
  99. dimensions: convertDimensionFormat(dimensions),
  100. }
  101. };
  102. return this.awsRequest(request).then(function(result) {
  103. return _.chain(result.Metrics).map(function(metric) {
  104. return _.pluck(metric.Dimensions, 'Value');
  105. }).flatten().uniq().sortBy(function(name) {
  106. return name;
  107. }).value();
  108. });
  109. };
  110. CloudWatchDatasource.prototype.performEC2DescribeInstances = function(region, filters, instanceIds) {
  111. return this.awsRequest({
  112. region: region,
  113. action: 'DescribeInstances',
  114. parameters: {
  115. filter: filters,
  116. instanceIds: instanceIds
  117. }
  118. });
  119. };
  120. CloudWatchDatasource.prototype.metricFindQuery = function(query) {
  121. var region;
  122. var namespace;
  123. var metricName;
  124. var transformSuggestData = function(suggestData) {
  125. return _.map(suggestData, function(v) {
  126. return { text: v };
  127. });
  128. };
  129. var regionQuery = query.match(/^regions\(\)/);
  130. if (regionQuery) {
  131. return this.getRegions();
  132. }
  133. var namespaceQuery = query.match(/^namespaces\(\)/);
  134. if (namespaceQuery) {
  135. return this.getNamespaces();
  136. }
  137. var metricNameQuery = query.match(/^metrics\(([^\)]+?)\)/);
  138. if (metricNameQuery) {
  139. return this.getMetrics(metricNameQuery[1]);
  140. }
  141. var dimensionKeysQuery = query.match(/^dimension_keys\(([^\)]+?)\)/);
  142. if (dimensionKeysQuery) {
  143. namespace = templateSrv.replace(dimensionKeysQuery[1]);
  144. return this.getDimensionKeys(namespace).then(transformSuggestData);
  145. }
  146. var dimensionValuesQuery = query.match(/^dimension_values\(([^,]+?),\s?([^,]+?),\s?([^,]+?)(,\s?([^)]*))?\)/);
  147. if (dimensionValuesQuery) {
  148. region = templateSrv.replace(dimensionValuesQuery[1]);
  149. namespace = templateSrv.replace(dimensionValuesQuery[2]);
  150. metricName = templateSrv.replace(dimensionValuesQuery[3]);
  151. var dimensionPart = templateSrv.replace(dimensionValuesQuery[5]);
  152. var dimensions = {};
  153. if (!_.isEmpty(dimensionPart)) {
  154. _.each(dimensionPart.split(','), function(v) {
  155. var t = v.split('=');
  156. if (t.length !== 2) {
  157. throw new Error('Invalid query format');
  158. }
  159. dimensions[t[0]] = t[1];
  160. });
  161. }
  162. return this.getDimensionValues(region, namespace, metricName, dimensions).then(transformSuggestData);
  163. }
  164. var ebsVolumeIdsQuery = query.match(/^ebs_volume_ids\(([^,]+?),\s?([^,]+?)\)/);
  165. if (ebsVolumeIdsQuery) {
  166. region = templateSrv.replace(ebsVolumeIdsQuery[1]);
  167. var instanceId = templateSrv.replace(ebsVolumeIdsQuery[2]);
  168. var instanceIds = [
  169. instanceId
  170. ];
  171. return this.performEC2DescribeInstances(region, [], instanceIds).then(function(result) {
  172. var volumeIds = _.map(result.Reservations[0].Instances[0].BlockDeviceMappings, function(mapping) {
  173. return mapping.EBS.VolumeID;
  174. });
  175. return transformSuggestData(volumeIds);
  176. });
  177. }
  178. return $q.when([]);
  179. };
  180. CloudWatchDatasource.prototype.testDatasource = function() {
  181. /* use billing metrics for test */
  182. var region = 'us-east-1';
  183. var namespace = 'AWS/Billing';
  184. var metricName = 'EstimatedCharges';
  185. var dimensions = {};
  186. return this.performSuggestDimensionValues(region, namespace, metricName, dimensions).then(function () {
  187. return { status: 'success', message: 'Data source is working', title: 'Success' };
  188. });
  189. };
  190. CloudWatchDatasource.prototype.awsRequest = function(data) {
  191. var options = {
  192. method: 'POST',
  193. url: this.proxyUrl,
  194. data: data
  195. };
  196. return backendSrv.datasourceRequest(options).then(function(result) {
  197. return result.data;
  198. });
  199. };
  200. CloudWatchDatasource.prototype.getDefaultRegion = function() {
  201. return this.defaultRegion;
  202. };
  203. function transformMetricData(md, options) {
  204. var result = [];
  205. var dimensionPart = templateSrv.replace(JSON.stringify(options.dimensions));
  206. _.each(getActivatedStatistics(options.statistics), function(s) {
  207. var originalSettings = _.templateSettings;
  208. _.templateSettings = {
  209. interpolate: /\{\{(.+?)\}\}/g
  210. };
  211. var template = _.template(options.legendFormat);
  212. var metricLabel;
  213. if (_.isEmpty(options.legendFormat)) {
  214. metricLabel = md.Label + '_' + s + dimensionPart;
  215. } else {
  216. var d = convertDimensionFormat(options.dimensions);
  217. metricLabel = template({
  218. Region: templateSrv.replace(options.region),
  219. Namespace: templateSrv.replace(options.namespace),
  220. MetricName: templateSrv.replace(options.metricName),
  221. Dimensions: d,
  222. Statistics: s
  223. });
  224. }
  225. _.templateSettings = originalSettings;
  226. var dps = _.map(md.Datapoints, function(value) {
  227. return [value[s], new Date(value.Timestamp).getTime()];
  228. });
  229. dps = _.sortBy(dps, function(dp) { return dp[1]; });
  230. result.push({ target: metricLabel, datapoints: dps });
  231. });
  232. return result;
  233. }
  234. function getActivatedStatistics(statistics) {
  235. var activatedStatistics = [];
  236. _.each(statistics, function(v, k) {
  237. if (v) {
  238. activatedStatistics.push(k);
  239. }
  240. });
  241. return activatedStatistics;
  242. }
  243. function convertToCloudWatchTime(date) {
  244. return Math.round(date.valueOf() / 1000);
  245. }
  246. function convertDimensionFormat(dimensions) {
  247. return _.map(_.keys(dimensions), function(key) {
  248. return {
  249. Name: templateSrv.replace(key),
  250. Value: templateSrv.replace(dimensions[key])
  251. };
  252. });
  253. }
  254. return CloudWatchDatasource;
  255. });
  256. });