datasource.js 11 KB

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