datasource.js 14 KB

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