datasource.js 14 KB

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