datasource.js 11 KB

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