datasource.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'moment',
  5. 'app/core/utils/datemath',
  6. 'app/core/utils/kbn',
  7. 'app/features/templating/variable',
  8. './annotation_query',
  9. ],
  10. function (angular, _, moment, dateMath, kbn, templatingVariable, CloudWatchAnnotationQuery) {
  11. 'use strict';
  12. /** @ngInject */
  13. function CloudWatchDatasource(instanceSettings, $q, backendSrv, templateSrv, timeSrv) {
  14. this.type = 'cloudwatch';
  15. this.name = instanceSettings.name;
  16. this.supportMetrics = true;
  17. this.proxyUrl = instanceSettings.url;
  18. this.defaultRegion = instanceSettings.jsonData.defaultRegion;
  19. this.instanceSettings = instanceSettings;
  20. this.standardStatistics = [
  21. 'Average',
  22. 'Maximum',
  23. 'Minimum',
  24. 'Sum',
  25. 'SampleCount'
  26. ];
  27. var self = this;
  28. this.query = function(options) {
  29. options = angular.copy(options);
  30. options.targets = this.expandTemplateVariable(options.targets, options.scopedVars, templateSrv);
  31. var queries = _.filter(options.targets, function (item) {
  32. return item.hide !== true &&
  33. !!item.region &&
  34. !!item.namespace &&
  35. !!item.metricName &&
  36. !_.isEmpty(item.statistics);
  37. }).map(function (item) {
  38. item.region = templateSrv.replace(item.region, options.scopedVars);
  39. item.namespace = templateSrv.replace(item.namespace, options.scopedVars);
  40. item.metricName = templateSrv.replace(item.metricName, options.scopedVars);
  41. var dimensions = {};
  42. _.each(item.dimensions, function (value, key) {
  43. dimensions[templateSrv.replace(key, options.scopedVars)] = templateSrv.replace(value, options.scopedVars);
  44. });
  45. item.dimensions = dimensions;
  46. item.period = self.getPeriod(item, options);
  47. return {
  48. refId: item.refId,
  49. intervalMs: options.intervalMs,
  50. maxDataPoints: options.maxDataPoints,
  51. datasourceId: self.instanceSettings.id,
  52. type: 'timeSeriesQuery',
  53. parameters: item
  54. };
  55. });
  56. // No valid targets, return the empty result to save a round trip.
  57. if (_.isEmpty(queries)) {
  58. var d = $q.defer();
  59. d.resolve({ data: [] });
  60. return d.promise;
  61. }
  62. var request = {
  63. from: options.rangeRaw.from,
  64. to: options.rangeRaw.to,
  65. queries: queries
  66. };
  67. return this.performTimeSeriesQuery(request);
  68. };
  69. this.getPeriod = function(target, options, now) {
  70. var start = this.convertToCloudWatchTime(options.range.from, false);
  71. var end = this.convertToCloudWatchTime(options.range.to, true);
  72. now = Math.round((now || Date.now()) / 1000);
  73. var period;
  74. var range = end - start;
  75. var hourSec = 60 * 60;
  76. var daySec = hourSec * 24;
  77. var periodUnit = 60;
  78. if (!target.period) {
  79. if (now - start <= (daySec * 15)) { // until 15 days ago
  80. if (target.namespace === 'AWS/EC2') {
  81. periodUnit = period = 300;
  82. } else {
  83. periodUnit = period = 60;
  84. }
  85. } else if (now - start <= (daySec * 63)) { // until 63 days ago
  86. periodUnit = period = 60 * 5;
  87. } else if (now - start <= (daySec * 455)) { // until 455 days ago
  88. periodUnit = period = 60 * 60;
  89. } else { // over 455 days, should return error, but try to long period
  90. periodUnit = period = 60 * 60;
  91. }
  92. } else {
  93. if (/^\d+$/.test(target.period)) {
  94. period = parseInt(target.period, 10);
  95. } else {
  96. period = kbn.interval_to_seconds(templateSrv.replace(target.period, options.scopedVars));
  97. }
  98. }
  99. if (period < 1) {
  100. period = 1;
  101. }
  102. if (range / period >= 1440) {
  103. period = Math.ceil(range / 1440 / periodUnit) * periodUnit;
  104. }
  105. return period;
  106. };
  107. this.performTimeSeriesQuery = function(request) {
  108. return backendSrv.post('/api/tsdb/query', request).then(function (res) {
  109. var data = [];
  110. if (res.results) {
  111. _.forEach(res.results, function (queryRes) {
  112. _.forEach(queryRes.series, function (series) {
  113. data.push({target: series.name, datapoints: series.points});
  114. });
  115. });
  116. }
  117. return {data: data};
  118. });
  119. };
  120. this.getRegions = function() {
  121. var range = timeSrv.timeRange();
  122. return backendSrv.post('/api/tsdb/query', {
  123. from: range.from,
  124. to: range.to,
  125. queries: [
  126. {
  127. refId: 'metricFindQuery',
  128. intervalMs: 1, // dummy
  129. maxDataPoints: 1, // dummy
  130. datasourceId: this.instanceSettings.id,
  131. type: 'metricFindQuery',
  132. subtype: 'regions'
  133. }
  134. ]
  135. });
  136. };
  137. this.getNamespaces = function() {
  138. return this.awsRequest({action: '__GetNamespaces'});
  139. };
  140. this.getMetrics = function(namespace, region) {
  141. return this.awsRequest({
  142. action: '__GetMetrics',
  143. region: region,
  144. parameters: {
  145. namespace: templateSrv.replace(namespace)
  146. }
  147. });
  148. };
  149. this.getDimensionKeys = function(namespace, region) {
  150. return this.awsRequest({
  151. action: '__GetDimensions',
  152. region: region,
  153. parameters: {
  154. namespace: templateSrv.replace(namespace)
  155. }
  156. });
  157. };
  158. this.getDimensionValues = function(region, namespace, metricName, dimensionKey, filterDimensions) {
  159. var request = {
  160. region: templateSrv.replace(region),
  161. action: 'ListMetrics',
  162. parameters: {
  163. namespace: templateSrv.replace(namespace),
  164. metricName: templateSrv.replace(metricName),
  165. dimensions: this.convertDimensionFormat(filterDimensions, {}),
  166. }
  167. };
  168. return this.awsRequest(request).then(function(result) {
  169. return _.chain(result.Metrics)
  170. .map('Dimensions')
  171. .flatten()
  172. .filter(function(dimension) {
  173. return dimension !== null && dimension.Name === dimensionKey;
  174. })
  175. .map('Value')
  176. .uniq()
  177. .sortBy()
  178. .map(function(value) {
  179. return {value: value, text: value};
  180. }).value();
  181. });
  182. };
  183. this.performEC2DescribeInstances = function(region, filters, instanceIds) {
  184. return this.awsRequest({
  185. region: region,
  186. action: 'DescribeInstances',
  187. parameters: { filters: filters, instanceIds: instanceIds }
  188. });
  189. };
  190. this.metricFindQuery = function(query) {
  191. var region;
  192. var namespace;
  193. var metricName;
  194. var transformSuggestDataFromTable = function(suggestData) {
  195. return _.map(suggestData.results['metricFindQuery'].tables[0].rows, function (v) {
  196. return {
  197. text: v[0],
  198. value: v[1]
  199. };
  200. });
  201. };
  202. var transformSuggestData = function(suggestData) {
  203. return _.map(suggestData, function(v) {
  204. return { text: v };
  205. });
  206. };
  207. var regionQuery = query.match(/^regions\(\)/);
  208. if (regionQuery) {
  209. return this.getRegions().then(function (r) { return transformSuggestDataFromTable(r); });
  210. }
  211. var namespaceQuery = query.match(/^namespaces\(\)/);
  212. if (namespaceQuery) {
  213. return this.getNamespaces();
  214. }
  215. var metricNameQuery = query.match(/^metrics\(([^\)]+?)(,\s?([^,]+?))?\)/);
  216. if (metricNameQuery) {
  217. return this.getMetrics(templateSrv.replace(metricNameQuery[1]), templateSrv.replace(metricNameQuery[3]));
  218. }
  219. var dimensionKeysQuery = query.match(/^dimension_keys\(([^\)]+?)(,\s?([^,]+?))?\)/);
  220. if (dimensionKeysQuery) {
  221. return this.getDimensionKeys(templateSrv.replace(dimensionKeysQuery[1]), templateSrv.replace(dimensionKeysQuery[3]));
  222. }
  223. var dimensionValuesQuery = query.match(/^dimension_values\(([^,]+?),\s?([^,]+?),\s?([^,]+?),\s?([^,]+?)\)/);
  224. if (dimensionValuesQuery) {
  225. region = templateSrv.replace(dimensionValuesQuery[1]);
  226. namespace = templateSrv.replace(dimensionValuesQuery[2]);
  227. metricName = templateSrv.replace(dimensionValuesQuery[3]);
  228. var dimensionKey = templateSrv.replace(dimensionValuesQuery[4]);
  229. return this.getDimensionValues(region, namespace, metricName, dimensionKey, {});
  230. }
  231. var ebsVolumeIdsQuery = query.match(/^ebs_volume_ids\(([^,]+?),\s?([^,]+?)\)/);
  232. if (ebsVolumeIdsQuery) {
  233. region = templateSrv.replace(ebsVolumeIdsQuery[1]);
  234. var instanceId = templateSrv.replace(ebsVolumeIdsQuery[2]);
  235. var instanceIds = [
  236. instanceId
  237. ];
  238. return this.performEC2DescribeInstances(region, [], instanceIds).then(function(result) {
  239. var volumeIds = _.map(result.Reservations[0].Instances[0].BlockDeviceMappings, function(mapping) {
  240. return mapping.Ebs.VolumeId;
  241. });
  242. return transformSuggestData(volumeIds);
  243. });
  244. }
  245. var ec2InstanceAttributeQuery = query.match(/^ec2_instance_attribute\(([^,]+?),\s?([^,]+?),\s?(.+?)\)/);
  246. if (ec2InstanceAttributeQuery) {
  247. region = templateSrv.replace(ec2InstanceAttributeQuery[1]);
  248. var filterJson = JSON.parse(templateSrv.replace(ec2InstanceAttributeQuery[3]));
  249. var filters = _.map(filterJson, function(values, name) {
  250. return {
  251. Name: name,
  252. Values: values
  253. };
  254. });
  255. var targetAttributeName = templateSrv.replace(ec2InstanceAttributeQuery[2]);
  256. return this.performEC2DescribeInstances(region, filters, null).then(function(result) {
  257. var attributes = _.chain(result.Reservations)
  258. .map(function(reservations) {
  259. return _.map(reservations.Instances, function(instance) {
  260. var tags = {};
  261. _.each(instance.Tags, function(tag) {
  262. tags[tag.Key] = tag.Value;
  263. });
  264. instance.Tags = tags;
  265. return instance;
  266. });
  267. })
  268. .map(function(instances) {
  269. return _.map(instances, targetAttributeName);
  270. })
  271. .flatten().uniq().sortBy().value();
  272. return transformSuggestData(attributes);
  273. });
  274. }
  275. return $q.when([]);
  276. };
  277. this.performDescribeAlarms = function(region, actionPrefix, alarmNamePrefix, alarmNames, stateValue) {
  278. return this.awsRequest({
  279. region: region,
  280. action: 'DescribeAlarms',
  281. parameters: { actionPrefix: actionPrefix, alarmNamePrefix: alarmNamePrefix, alarmNames: alarmNames, stateValue: stateValue }
  282. });
  283. };
  284. this.performDescribeAlarmsForMetric = function(region, namespace, metricName, dimensions, statistic, period) {
  285. var s = _.includes(self.standardStatistics, statistic) ? statistic : '';
  286. var es = _.includes(self.standardStatistics, statistic) ? '' : statistic;
  287. return this.awsRequest({
  288. region: region,
  289. action: 'DescribeAlarmsForMetric',
  290. parameters: {
  291. namespace: namespace,
  292. metricName: metricName,
  293. dimensions: dimensions,
  294. statistic: s,
  295. extendedStatistic: es,
  296. period: period
  297. }
  298. });
  299. };
  300. this.performDescribeAlarmHistory = function(region, alarmName, startDate, endDate) {
  301. return this.awsRequest({
  302. region: region,
  303. action: 'DescribeAlarmHistory',
  304. parameters: { alarmName: alarmName, startDate: startDate, endDate: endDate }
  305. });
  306. };
  307. this.annotationQuery = function(options) {
  308. var annotationQuery = new CloudWatchAnnotationQuery(this, options.annotation, $q, templateSrv);
  309. return annotationQuery.process(options.range.from, options.range.to);
  310. };
  311. this.testDatasource = function() {
  312. /* use billing metrics for test */
  313. var region = this.defaultRegion;
  314. var namespace = 'AWS/Billing';
  315. var metricName = 'EstimatedCharges';
  316. var dimensions = {};
  317. return this.getDimensionValues(region, namespace, metricName, 'ServiceName', dimensions).then(function () {
  318. return { status: 'success', message: 'Data source is working' };
  319. });
  320. };
  321. this.awsRequest = function(data) {
  322. var options = {
  323. method: 'POST',
  324. url: this.proxyUrl,
  325. data: data
  326. };
  327. return backendSrv.datasourceRequest(options).then(function(result) {
  328. return result.data;
  329. });
  330. };
  331. this.getDefaultRegion = function() {
  332. return this.defaultRegion;
  333. };
  334. this.getExpandedVariables = function(target, dimensionKey, variable, templateSrv) {
  335. /* if the all checkbox is marked we should add all values to the targets */
  336. var allSelected = _.find(variable.options, {'selected': true, 'text': 'All'});
  337. return _.chain(variable.options)
  338. .filter(function(v) {
  339. if (allSelected) {
  340. return v.text !== 'All';
  341. } else {
  342. return v.selected;
  343. }
  344. })
  345. .map(function(v) {
  346. var t = angular.copy(target);
  347. var scopedVar = {};
  348. scopedVar[variable.name] = v;
  349. t.dimensions[dimensionKey] = templateSrv.replace(t.dimensions[dimensionKey], scopedVar);
  350. return t;
  351. }).value();
  352. };
  353. this.expandTemplateVariable = function(targets, scopedVars, templateSrv) {
  354. var self = this;
  355. return _.chain(targets)
  356. .map(function(target) {
  357. var dimensionKey = _.findKey(target.dimensions, function(v) {
  358. return templateSrv.variableExists(v) && !_.has(scopedVars, templateSrv.getVariableName(v));
  359. });
  360. if (dimensionKey) {
  361. var multiVariable = _.find(templateSrv.variables, function(variable) {
  362. return templatingVariable.containsVariable(target.dimensions[dimensionKey], variable.name) && variable.multi;
  363. });
  364. var variable = _.find(templateSrv.variables, function(variable) {
  365. return templatingVariable.containsVariable(target.dimensions[dimensionKey], variable.name);
  366. });
  367. return self.getExpandedVariables(target, dimensionKey, multiVariable || variable, templateSrv);
  368. } else {
  369. return [target];
  370. }
  371. }).flatten().value();
  372. };
  373. this.convertToCloudWatchTime = function(date, roundUp) {
  374. if (_.isString(date)) {
  375. date = dateMath.parse(date, roundUp);
  376. }
  377. return Math.round(date.valueOf() / 1000);
  378. };
  379. this.convertDimensionFormat = function(dimensions, scopedVars) {
  380. return _.map(dimensions, function(value, key) {
  381. return {
  382. Name: templateSrv.replace(key, scopedVars),
  383. Value: templateSrv.replace(value, scopedVars)
  384. };
  385. });
  386. };
  387. }
  388. return CloudWatchDatasource;
  389. });