datasource.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. options.targets = this.expandTemplateVariable(options.targets);
  24. _.each(options.targets, 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 = self.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. }.bind(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. }.bind(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], options.scopedVars);
  55. result = result.concat(metrics);
  56. });
  57. return {data: result};
  58. });
  59. };
  60. this.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. this.getRegions = function() {
  76. return this.awsRequest({action: '__GetRegions'});
  77. };
  78. this.getNamespaces = function() {
  79. return this.awsRequest({action: '__GetNamespaces'});
  80. };
  81. this.getMetrics = function(namespace, region) {
  82. return this.awsRequest({
  83. action: '__GetMetrics',
  84. region: region,
  85. parameters: {
  86. namespace: templateSrv.replace(namespace)
  87. }
  88. });
  89. };
  90. this.getDimensionKeys = function(namespace, region) {
  91. return this.awsRequest({
  92. action: '__GetDimensions',
  93. region: region,
  94. parameters: {
  95. namespace: templateSrv.replace(namespace)
  96. }
  97. });
  98. };
  99. this.getDimensionValues = function(region, namespace, metricName, dimensionKey, filterDimensions) {
  100. var request = {
  101. region: templateSrv.replace(region),
  102. action: 'ListMetrics',
  103. parameters: {
  104. namespace: templateSrv.replace(namespace),
  105. metricName: templateSrv.replace(metricName),
  106. dimensions: this.convertDimensionFormat(filterDimensions, {}),
  107. }
  108. };
  109. return this.awsRequest(request).then(function(result) {
  110. return _.chain(result.Metrics)
  111. .map('Dimensions')
  112. .flatten()
  113. .filter(function(dimension) {
  114. return dimension !== null && dimension.Name === dimensionKey;
  115. })
  116. .map('Value')
  117. .uniq()
  118. .sortBy()
  119. .map(function(value) {
  120. return {value: value, text: value};
  121. }).value();
  122. });
  123. };
  124. this.performEC2DescribeInstances = function(region, filters, instanceIds) {
  125. return this.awsRequest({
  126. region: region,
  127. action: 'DescribeInstances',
  128. parameters: { filters: filters, instanceIds: instanceIds }
  129. });
  130. };
  131. this.metricFindQuery = function(query) {
  132. var region;
  133. var namespace;
  134. var metricName;
  135. var transformSuggestData = function(suggestData) {
  136. return _.map(suggestData, function(v) {
  137. return { text: v };
  138. });
  139. };
  140. var regionQuery = query.match(/^regions\(\)/);
  141. if (regionQuery) {
  142. return this.getRegions();
  143. }
  144. var namespaceQuery = query.match(/^namespaces\(\)/);
  145. if (namespaceQuery) {
  146. return this.getNamespaces();
  147. }
  148. var metricNameQuery = query.match(/^metrics\(([^\)]+?)(,\s?([^,]+?))?\)/);
  149. if (metricNameQuery) {
  150. return this.getMetrics(metricNameQuery[1], metricNameQuery[3]);
  151. }
  152. var dimensionKeysQuery = query.match(/^dimension_keys\(([^\)]+?)(,\s?([^,]+?))?\)/);
  153. if (dimensionKeysQuery) {
  154. return this.getDimensionKeys(dimensionKeysQuery[1], dimensionKeysQuery[3]);
  155. }
  156. var dimensionValuesQuery = query.match(/^dimension_values\(([^,]+?),\s?([^,]+?),\s?([^,]+?),\s?([^,]+?)\)/);
  157. if (dimensionValuesQuery) {
  158. region = templateSrv.replace(dimensionValuesQuery[1]);
  159. namespace = templateSrv.replace(dimensionValuesQuery[2]);
  160. metricName = templateSrv.replace(dimensionValuesQuery[3]);
  161. var dimensionKey = templateSrv.replace(dimensionValuesQuery[4]);
  162. return this.getDimensionValues(region, namespace, metricName, dimensionKey, {});
  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. var ec2InstanceAttributeQuery = query.match(/^ec2_instance_attribute\(([^,]+?),\s?([^,]+?),\s?(.+?)\)/);
  179. if (ec2InstanceAttributeQuery) {
  180. region = templateSrv.replace(ec2InstanceAttributeQuery[1]);
  181. var filterJson = JSON.parse(templateSrv.replace(ec2InstanceAttributeQuery[3]));
  182. var filters = _.map(filterJson, function(values, name) {
  183. return {
  184. Name: name,
  185. Values: values
  186. };
  187. });
  188. var targetAttributeName = templateSrv.replace(ec2InstanceAttributeQuery[2]);
  189. return this.performEC2DescribeInstances(region, filters, null).then(function(result) {
  190. var attributes = _.chain(result.Reservations)
  191. .map(function(reservations) {
  192. return _.map(reservations.Instances, targetAttributeName);
  193. })
  194. .flatten().uniq().sortBy().value();
  195. return transformSuggestData(attributes);
  196. });
  197. }
  198. return $q.when([]);
  199. };
  200. this.performDescribeAlarms = function(region, actionPrefix, alarmNamePrefix, alarmNames, stateValue) {
  201. return this.awsRequest({
  202. region: region,
  203. action: 'DescribeAlarms',
  204. parameters: { actionPrefix: actionPrefix, alarmNamePrefix: alarmNamePrefix, alarmNames: alarmNames, stateValue: stateValue }
  205. });
  206. };
  207. this.performDescribeAlarmsForMetric = function(region, namespace, metricName, dimensions, statistic, period) {
  208. return this.awsRequest({
  209. region: region,
  210. action: 'DescribeAlarmsForMetric',
  211. parameters: { namespace: namespace, metricName: metricName, dimensions: dimensions, statistic: statistic, period: period }
  212. });
  213. };
  214. this.performDescribeAlarmHistory = function(region, alarmName, startDate, endDate) {
  215. return this.awsRequest({
  216. region: region,
  217. action: 'DescribeAlarmHistory',
  218. parameters: { alarmName: alarmName, startDate: startDate, endDate: endDate }
  219. });
  220. };
  221. this.annotationQuery = function(options) {
  222. var annotationQuery = new CloudWatchAnnotationQuery(this, options.annotation, $q, templateSrv);
  223. return annotationQuery.process(options.range.from, options.range.to);
  224. };
  225. this.testDatasource = function() {
  226. /* use billing metrics for test */
  227. var region = this.defaultRegion;
  228. var namespace = 'AWS/Billing';
  229. var metricName = 'EstimatedCharges';
  230. var dimensions = {};
  231. return this.getDimensionValues(region, namespace, metricName, 'ServiceName', dimensions).then(function () {
  232. return { status: 'success', message: 'Data source is working', title: 'Success' };
  233. });
  234. };
  235. this.awsRequest = function(data) {
  236. var options = {
  237. method: 'POST',
  238. url: this.proxyUrl,
  239. data: data
  240. };
  241. return backendSrv.datasourceRequest(options).then(function(result) {
  242. return result.data;
  243. });
  244. };
  245. this.getDefaultRegion = function() {
  246. return this.defaultRegion;
  247. };
  248. function transformMetricData(md, options, scopedVars) {
  249. var aliasRegex = /\{\{(.+?)\}\}/g;
  250. var aliasPattern = options.alias || '{{metric}}_{{stat}}';
  251. var aliasData = {
  252. region: templateSrv.replace(options.region, scopedVars),
  253. namespace: templateSrv.replace(options.namespace, scopedVars),
  254. metric: templateSrv.replace(options.metricName, scopedVars),
  255. };
  256. var aliasDimensions = {};
  257. _.each(_.keys(options.dimensions), function(origKey) {
  258. var key = templateSrv.replace(origKey, scopedVars);
  259. var value = templateSrv.replace(options.dimensions[origKey], scopedVars);
  260. aliasDimensions[key] = value;
  261. });
  262. _.extend(aliasData, aliasDimensions);
  263. var periodMs = options.period * 1000;
  264. return _.map(options.statistics, function(stat) {
  265. var dps = [];
  266. var lastTimestamp = null;
  267. _.chain(md.Datapoints)
  268. .sortBy(function(dp) {
  269. return dp.Timestamp;
  270. })
  271. .each(function(dp) {
  272. var timestamp = new Date(dp.Timestamp).getTime();
  273. if (lastTimestamp && (timestamp - lastTimestamp) > periodMs) {
  274. dps.push([null, lastTimestamp + periodMs]);
  275. }
  276. lastTimestamp = timestamp;
  277. dps.push([dp[stat], timestamp]);
  278. })
  279. .value();
  280. aliasData.stat = stat;
  281. var seriesName = aliasPattern.replace(aliasRegex, function(match, g1) {
  282. if (aliasData[g1]) {
  283. return aliasData[g1];
  284. }
  285. return g1;
  286. });
  287. return {target: seriesName, datapoints: dps};
  288. });
  289. }
  290. this.expandTemplateVariable = function(targets) {
  291. return _.chain(targets)
  292. .map(function(target) {
  293. var dimensionKey = null;
  294. var variableName = null;
  295. _.each(target.dimensions, function(v, k) {
  296. if (templateSrv.variableExists(v)) {
  297. dimensionKey = k;
  298. variableName = v;
  299. }
  300. });
  301. if (dimensionKey) {
  302. var variable = _.find(templateSrv.variables, function(variable) {
  303. return templateSrv.containsVariable(variableName, variable.name);
  304. });
  305. return _.chain(variable.options)
  306. .filter(function(v) {
  307. return v.selected;
  308. })
  309. .map(function(v) {
  310. var t = angular.copy(target);
  311. t.dimensions[dimensionKey] = v.value;
  312. return t;
  313. }).value();
  314. } else {
  315. return [target];
  316. }
  317. }).flatten().value();
  318. };
  319. this.convertToCloudWatchTime = function(date, roundUp) {
  320. if (_.isString(date)) {
  321. date = dateMath.parse(date, roundUp);
  322. }
  323. return Math.round(date.valueOf() / 1000);
  324. };
  325. this.convertDimensionFormat = function(dimensions, scopedVars) {
  326. return _.map(dimensions, function(value, key) {
  327. return {
  328. Name: templateSrv.replace(key, scopedVars),
  329. Value: templateSrv.replace(value, scopedVars)
  330. };
  331. });
  332. };
  333. }
  334. return {
  335. CloudWatchDatasource: CloudWatchDatasource
  336. };
  337. });