datasource.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. function transformSuggestDataFromTable(suggestData) {
  121. return _.map(suggestData.results['metricFindQuery'].tables[0].rows, function (v) {
  122. return {
  123. text: v[0],
  124. value: v[1]
  125. };
  126. });
  127. }
  128. this.getRegions = function () {
  129. var range = timeSrv.timeRange();
  130. return backendSrv.post('/api/tsdb/query', {
  131. from: range.from,
  132. to: range.to,
  133. queries: [
  134. {
  135. refId: 'metricFindQuery',
  136. intervalMs: 1, // dummy
  137. maxDataPoints: 1, // dummy
  138. datasourceId: this.instanceSettings.id,
  139. type: 'metricFindQuery',
  140. subtype: 'regions'
  141. }
  142. ]
  143. }).then(function (r) { return transformSuggestDataFromTable(r); });
  144. };
  145. this.getNamespaces = function() {
  146. var range = timeSrv.timeRange();
  147. return backendSrv.post('/api/tsdb/query', {
  148. from: range.from,
  149. to: range.to,
  150. queries: [
  151. {
  152. refId: 'metricFindQuery',
  153. intervalMs: 1, // dummy
  154. maxDataPoints: 1, // dummy
  155. datasourceId: this.instanceSettings.id,
  156. type: 'metricFindQuery',
  157. subtype: 'namespaces'
  158. }
  159. ]
  160. }).then(function (r) { return transformSuggestDataFromTable(r); });
  161. };
  162. this.getMetrics = function (namespace, region) {
  163. var range = timeSrv.timeRange();
  164. return backendSrv.post('/api/tsdb/query', {
  165. from: range.from,
  166. to: range.to,
  167. queries: [
  168. {
  169. refId: 'metricFindQuery',
  170. intervalMs: 1, // dummy
  171. maxDataPoints: 1, // dummy
  172. datasourceId: this.instanceSettings.id,
  173. type: 'metricFindQuery',
  174. subtype: 'metrics',
  175. parameters: {
  176. region: region,
  177. namespace: templateSrv.replace(namespace)
  178. }
  179. }
  180. ]
  181. }).then(function (r) { return transformSuggestDataFromTable(r); });
  182. };
  183. this.getDimensionKeys = function(namespace, region) {
  184. var range = timeSrv.timeRange();
  185. return backendSrv.post('/api/tsdb/query', {
  186. from: range.from,
  187. to: range.to,
  188. queries: [
  189. {
  190. refId: 'metricFindQuery',
  191. intervalMs: 1, // dummy
  192. maxDataPoints: 1, // dummy
  193. datasourceId: this.instanceSettings.id,
  194. type: 'metricFindQuery',
  195. subtype: 'dimension_keys',
  196. parameters: {
  197. region: region,
  198. namespace: templateSrv.replace(namespace)
  199. }
  200. }
  201. ]
  202. }).then(function (r) { return transformSuggestDataFromTable(r); });
  203. };
  204. this.getDimensionValues = function(region, namespace, metricName, dimensionKey, filterDimensions) {
  205. var request = {
  206. region: templateSrv.replace(region),
  207. action: 'ListMetrics',
  208. parameters: {
  209. namespace: templateSrv.replace(namespace),
  210. metricName: templateSrv.replace(metricName),
  211. dimensions: this.convertDimensionFormat(filterDimensions, {}),
  212. }
  213. };
  214. return this.awsRequest(request).then(function(result) {
  215. return _.chain(result.Metrics)
  216. .map('Dimensions')
  217. .flatten()
  218. .filter(function(dimension) {
  219. return dimension !== null && dimension.Name === dimensionKey;
  220. })
  221. .map('Value')
  222. .uniq()
  223. .sortBy()
  224. .map(function(value) {
  225. return {value: value, text: value};
  226. }).value();
  227. });
  228. };
  229. this.getEbsVolumeIds = function(region, instanceId) {
  230. var range = timeSrv.timeRange();
  231. return backendSrv.post('/api/tsdb/query', {
  232. from: range.from,
  233. to: range.to,
  234. queries: [
  235. {
  236. refId: 'metricFindQuery',
  237. intervalMs: 1, // dummy
  238. maxDataPoints: 1, // dummy
  239. datasourceId: this.instanceSettings.id,
  240. type: 'metricFindQuery',
  241. subtype: 'ebs_volume_ids',
  242. parameters: {
  243. region: region,
  244. instanceId: instanceId
  245. }
  246. }
  247. ]
  248. }).then(function (r) { return transformSuggestDataFromTable(r); });
  249. };
  250. this.performEC2DescribeInstances = function(region, filters, instanceIds) {
  251. return this.awsRequest({
  252. region: region,
  253. action: 'DescribeInstances',
  254. parameters: { filters: filters, instanceIds: instanceIds }
  255. });
  256. };
  257. this.metricFindQuery = function(query) {
  258. var region;
  259. var namespace;
  260. var metricName;
  261. var transformSuggestData = function(suggestData) {
  262. return _.map(suggestData, function(v) {
  263. return { text: v };
  264. });
  265. };
  266. var regionQuery = query.match(/^regions\(\)/);
  267. if (regionQuery) {
  268. return this.getRegions();
  269. }
  270. var namespaceQuery = query.match(/^namespaces\(\)/);
  271. if (namespaceQuery) {
  272. return this.getNamespaces();
  273. }
  274. var metricNameQuery = query.match(/^metrics\(([^\)]+?)(,\s?([^,]+?))?\)/);
  275. if (metricNameQuery) {
  276. return this.getMetrics(templateSrv.replace(metricNameQuery[1]), templateSrv.replace(metricNameQuery[3]));
  277. }
  278. var dimensionKeysQuery = query.match(/^dimension_keys\(([^\)]+?)(,\s?([^,]+?))?\)/);
  279. if (dimensionKeysQuery) {
  280. return this.getDimensionKeys(templateSrv.replace(dimensionKeysQuery[1]), templateSrv.replace(dimensionKeysQuery[3]));
  281. }
  282. var dimensionValuesQuery = query.match(/^dimension_values\(([^,]+?),\s?([^,]+?),\s?([^,]+?),\s?([^,]+?)\)/);
  283. if (dimensionValuesQuery) {
  284. region = templateSrv.replace(dimensionValuesQuery[1]);
  285. namespace = templateSrv.replace(dimensionValuesQuery[2]);
  286. metricName = templateSrv.replace(dimensionValuesQuery[3]);
  287. var dimensionKey = templateSrv.replace(dimensionValuesQuery[4]);
  288. return this.getDimensionValues(region, namespace, metricName, dimensionKey, {});
  289. }
  290. var ebsVolumeIdsQuery = query.match(/^ebs_volume_ids\(([^,]+?),\s?([^,]+?)\)/);
  291. if (ebsVolumeIdsQuery) {
  292. region = templateSrv.replace(ebsVolumeIdsQuery[1]);
  293. var instanceId = templateSrv.replace(ebsVolumeIdsQuery[2]);
  294. return this.getEbsVolumeIds(region, instanceId);
  295. }
  296. var ec2InstanceAttributeQuery = query.match(/^ec2_instance_attribute\(([^,]+?),\s?([^,]+?),\s?(.+?)\)/);
  297. if (ec2InstanceAttributeQuery) {
  298. region = templateSrv.replace(ec2InstanceAttributeQuery[1]);
  299. var filterJson = JSON.parse(templateSrv.replace(ec2InstanceAttributeQuery[3]));
  300. var filters = _.map(filterJson, function(values, name) {
  301. return {
  302. Name: name,
  303. Values: values
  304. };
  305. });
  306. var targetAttributeName = templateSrv.replace(ec2InstanceAttributeQuery[2]);
  307. return this.performEC2DescribeInstances(region, filters, null).then(function(result) {
  308. var attributes = _.chain(result.Reservations)
  309. .map(function(reservations) {
  310. return _.map(reservations.Instances, function(instance) {
  311. var tags = {};
  312. _.each(instance.Tags, function(tag) {
  313. tags[tag.Key] = tag.Value;
  314. });
  315. instance.Tags = tags;
  316. return instance;
  317. });
  318. })
  319. .map(function(instances) {
  320. return _.map(instances, targetAttributeName);
  321. })
  322. .flatten().uniq().sortBy().value();
  323. return transformSuggestData(attributes);
  324. });
  325. }
  326. return $q.when([]);
  327. };
  328. this.performDescribeAlarms = function(region, actionPrefix, alarmNamePrefix, alarmNames, stateValue) {
  329. return this.awsRequest({
  330. region: region,
  331. action: 'DescribeAlarms',
  332. parameters: { actionPrefix: actionPrefix, alarmNamePrefix: alarmNamePrefix, alarmNames: alarmNames, stateValue: stateValue }
  333. });
  334. };
  335. this.performDescribeAlarmsForMetric = function(region, namespace, metricName, dimensions, statistic, period) {
  336. var s = _.includes(self.standardStatistics, statistic) ? statistic : '';
  337. var es = _.includes(self.standardStatistics, statistic) ? '' : statistic;
  338. return this.awsRequest({
  339. region: region,
  340. action: 'DescribeAlarmsForMetric',
  341. parameters: {
  342. namespace: namespace,
  343. metricName: metricName,
  344. dimensions: dimensions,
  345. statistic: s,
  346. extendedStatistic: es,
  347. period: period
  348. }
  349. });
  350. };
  351. this.performDescribeAlarmHistory = function(region, alarmName, startDate, endDate) {
  352. return this.awsRequest({
  353. region: region,
  354. action: 'DescribeAlarmHistory',
  355. parameters: { alarmName: alarmName, startDate: startDate, endDate: endDate }
  356. });
  357. };
  358. this.annotationQuery = function(options) {
  359. var annotationQuery = new CloudWatchAnnotationQuery(this, options.annotation, $q, templateSrv);
  360. return annotationQuery.process(options.range.from, options.range.to);
  361. };
  362. this.testDatasource = function() {
  363. /* use billing metrics for test */
  364. var region = this.defaultRegion;
  365. var namespace = 'AWS/Billing';
  366. var metricName = 'EstimatedCharges';
  367. var dimensions = {};
  368. return this.getDimensionValues(region, namespace, metricName, 'ServiceName', dimensions).then(function () {
  369. return { status: 'success', message: 'Data source is working' };
  370. });
  371. };
  372. this.awsRequest = function(data) {
  373. var options = {
  374. method: 'POST',
  375. url: this.proxyUrl,
  376. data: data
  377. };
  378. return backendSrv.datasourceRequest(options).then(function(result) {
  379. return result.data;
  380. });
  381. };
  382. this.getDefaultRegion = function() {
  383. return this.defaultRegion;
  384. };
  385. this.getExpandedVariables = function(target, dimensionKey, variable, templateSrv) {
  386. /* if the all checkbox is marked we should add all values to the targets */
  387. var allSelected = _.find(variable.options, {'selected': true, 'text': 'All'});
  388. return _.chain(variable.options)
  389. .filter(function(v) {
  390. if (allSelected) {
  391. return v.text !== 'All';
  392. } else {
  393. return v.selected;
  394. }
  395. })
  396. .map(function(v) {
  397. var t = angular.copy(target);
  398. var scopedVar = {};
  399. scopedVar[variable.name] = v;
  400. t.dimensions[dimensionKey] = templateSrv.replace(t.dimensions[dimensionKey], scopedVar);
  401. return t;
  402. }).value();
  403. };
  404. this.expandTemplateVariable = function(targets, scopedVars, templateSrv) {
  405. var self = this;
  406. return _.chain(targets)
  407. .map(function(target) {
  408. var dimensionKey = _.findKey(target.dimensions, function(v) {
  409. return templateSrv.variableExists(v) && !_.has(scopedVars, templateSrv.getVariableName(v));
  410. });
  411. if (dimensionKey) {
  412. var multiVariable = _.find(templateSrv.variables, function(variable) {
  413. return templatingVariable.containsVariable(target.dimensions[dimensionKey], variable.name) && variable.multi;
  414. });
  415. var variable = _.find(templateSrv.variables, function(variable) {
  416. return templatingVariable.containsVariable(target.dimensions[dimensionKey], variable.name);
  417. });
  418. return self.getExpandedVariables(target, dimensionKey, multiVariable || variable, templateSrv);
  419. } else {
  420. return [target];
  421. }
  422. }).flatten().value();
  423. };
  424. this.convertToCloudWatchTime = function(date, roundUp) {
  425. if (_.isString(date)) {
  426. date = dateMath.parse(date, roundUp);
  427. }
  428. return Math.round(date.valueOf() / 1000);
  429. };
  430. this.convertDimensionFormat = function(dimensions, scopedVars) {
  431. return _.map(dimensions, function(value, key) {
  432. return {
  433. Name: templateSrv.replace(key, scopedVars),
  434. Value: templateSrv.replace(value, scopedVars)
  435. };
  436. });
  437. };
  438. }
  439. return CloudWatchDatasource;
  440. });