datasource.js 9.4 KB

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