diff --git a/public/app/plugins/datasource/cloudwatch/datasource.js b/public/app/plugins/datasource/cloudwatch/datasource.js index e01bdce6096..aeee1e59bf8 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.js +++ b/public/app/plugins/datasource/cloudwatch/datasource.js @@ -209,6 +209,74 @@ function (angular, _) { return $q.when([]); }; + CloudWatchDatasource.prototype.performDescribeAlarmsForMetric = function(region, namespace, metricName, dimensions, statistic, period) { + return this.awsRequest({ + region: region, + action: 'DescribeAlarmsForMetric', + parameters: { namespace: namespace, metricName: metricName, dimensions: dimensions, statistic: statistic, period: period } + }); + }; + + CloudWatchDatasource.prototype.performDescribeAlarmHistory = function(region, alarmName, startDate, endDate) { + return this.awsRequest({ + region: region, + action: 'DescribeAlarmHistory', + parameters: { alarmName: alarmName, startDate: startDate, endDate: endDate } + }); + }; + + CloudWatchDatasource.prototype.annotationQuery = function(annotation, range) { + var region = templateSrv.replace(annotation.region); + var namespace = templateSrv.replace(annotation.namespace); + var metricName = templateSrv.replace(annotation.metricName); + var dimensionPart = templateSrv.replace(annotation.dimensions); + var statistic = templateSrv.replace(annotation.statistic) || ''; + var period = annotation.period || '300'; + + if (!region || !namespace || !metricName) { return $q.when([]); } + + var dimensions = {}; + if (!_.isEmpty(dimensionPart)) { + _.each(dimensionPart.split(','), function(v) { + var t = v.split('='); + if (t.length !== 2) { + throw new Error('Invalid query format'); + } + dimensions[t[0]] = t[1]; + }); + dimensions = convertDimensionFormat(dimensions); + } + period = parseInt(period, 10); + + var d = $q.defer(); + var self = this; + this.performDescribeAlarmsForMetric(region, namespace, metricName, dimensions, statistic, period).then(function(alarms) { + var eventList = []; + + var start = convertToCloudWatchTime(range.from); + var end = convertToCloudWatchTime(range.to); + _.each(alarms.MetricAlarms, function(alarm) { + self.performDescribeAlarmHistory(region, alarm.AlarmName, start, end).then(function(history) { + _.each(history.AlarmHistoryItems, function(h) { + var event = { + annotation: annotation, + time: Date.parse(h.Timestamp), + title: h.AlarmName, + tags: [h.HistoryItemType], + text: h.HistorySummary + }; + + eventList.push(event); + }); + + d.resolve(eventList); + }); + }); + }); + + return d.promise; + }; + CloudWatchDatasource.prototype.testDatasource = function() { /* use billing metrics for test */ var region = this.defaultRegion; diff --git a/public/app/plugins/datasource/cloudwatch/directives.js b/public/app/plugins/datasource/cloudwatch/directives.js index c758aa34e5c..22a98e036e4 100644 --- a/public/app/plugins/datasource/cloudwatch/directives.js +++ b/public/app/plugins/datasource/cloudwatch/directives.js @@ -10,4 +10,8 @@ function (angular) { return {controller: 'CloudWatchQueryCtrl', templateUrl: 'app/plugins/datasource/cloudwatch/partials/query.editor.html'}; }); + module.directive('annotationsQueryEditorCloudwatch', function() { + return {templateUrl: 'app/plugins/datasource/cloudwatch/partials/annotations.editor.html'}; + }); + }); diff --git a/public/app/plugins/datasource/cloudwatch/partials/annotations.editor.html b/public/app/plugins/datasource/cloudwatch/partials/annotations.editor.html new file mode 100644 index 00000000000..1db6eb704b3 --- /dev/null +++ b/public/app/plugins/datasource/cloudwatch/partials/annotations.editor.html @@ -0,0 +1,38 @@ +