tech: ran pretttier on all typescript files

This commit is contained in:
Torkel Ödegaard
2017-12-19 16:06:54 +01:00
parent b1621e1e8f
commit fadfc665e4
355 changed files with 20624 additions and 14931 deletions

View File

@@ -1,7 +1,7 @@
///<reference path="../../../headers/common.d.ts" />
export class CloudWatchConfigCtrl {
static templateUrl = 'partials/config.html';
static templateUrl = "partials/config.html";
current: any;
accessKeyExist = false;
@@ -9,8 +9,10 @@ export class CloudWatchConfigCtrl {
/** @ngInject */
constructor($scope) {
this.current.jsonData.timeField = this.current.jsonData.timeField || '@timestamp';
this.current.jsonData.authType = this.current.jsonData.authType || 'credentials';
this.current.jsonData.timeField =
this.current.jsonData.timeField || "@timestamp";
this.current.jsonData.authType =
this.current.jsonData.authType || "credentials";
this.accessKeyExist = this.current.secureJsonFields.accessKey;
this.secretKeyExist = this.current.secureJsonFields.secretKey;
@@ -25,18 +27,17 @@ export class CloudWatchConfigCtrl {
}
authTypes = [
{name: 'Access & secret key', value: 'keys'},
{name: 'Credentials file', value: 'credentials'},
{name: 'ARN', value: 'arn'},
{ name: "Access & secret key", value: "keys" },
{ name: "Credentials file", value: "credentials" },
{ name: "ARN", value: "arn" }
];
indexPatternTypes = [
{name: 'No pattern', value: undefined},
{name: 'Hourly', value: 'Hourly', example: '[logstash-]YYYY.MM.DD.HH'},
{name: 'Daily', value: 'Daily', example: '[logstash-]YYYY.MM.DD'},
{name: 'Weekly', value: 'Weekly', example: '[logstash-]GGGG.WW'},
{name: 'Monthly', value: 'Monthly', example: '[logstash-]YYYY.MM'},
{name: 'Yearly', value: 'Yearly', example: '[logstash-]YYYY'},
{ name: "No pattern", value: undefined },
{ name: "Hourly", value: "Hourly", example: "[logstash-]YYYY.MM.DD.HH" },
{ name: "Daily", value: "Daily", example: "[logstash-]YYYY.MM.DD" },
{ name: "Weekly", value: "Weekly", example: "[logstash-]GGGG.WW" },
{ name: "Monthly", value: "Monthly", example: "[logstash-]YYYY.MM" },
{ name: "Yearly", value: "Yearly", example: "[logstash-]YYYY" }
];
}

View File

@@ -1,3 +1,2 @@
declare var CloudWatchDatasource: any;
export default CloudWatchDatasource;

View File

@@ -1,17 +1,16 @@
import './query_parameter_ctrl';
import "./query_parameter_ctrl";
import CloudWatchDatasource from './datasource';
import {CloudWatchQueryCtrl} from './query_ctrl';
import {CloudWatchConfigCtrl} from './config_ctrl';
import CloudWatchDatasource from "./datasource";
import { CloudWatchQueryCtrl } from "./query_ctrl";
import { CloudWatchConfigCtrl } from "./config_ctrl";
class CloudWatchAnnotationsQueryCtrl {
static templateUrl = 'partials/annotations.editor.html';
static templateUrl = "partials/annotations.editor.html";
}
export {
CloudWatchDatasource as Datasource,
CloudWatchQueryCtrl as QueryCtrl,
CloudWatchConfigCtrl as ConfigCtrl,
CloudWatchAnnotationsQueryCtrl as AnnotationsQueryCtrl,
CloudWatchAnnotationsQueryCtrl as AnnotationsQueryCtrl
};

View File

@@ -1,16 +1,17 @@
///<reference path="../../../headers/common.d.ts" />
import './query_parameter_ctrl';
import {QueryCtrl} from 'app/plugins/sdk';
import "./query_parameter_ctrl";
import { QueryCtrl } from "app/plugins/sdk";
export class CloudWatchQueryCtrl extends QueryCtrl {
static templateUrl = 'partials/query.editor.html';
static templateUrl = "partials/query.editor.html";
aliasSyntax: string;
/** @ngInject **/
constructor($scope, $injector) {
super($scope, $injector);
this.aliasSyntax = '{{metric}} {{stat}} {{namespace}} {{region}} {{<dimension name>}}';
this.aliasSyntax =
"{{metric}} {{stat}} {{namespace}} {{region}} {{<dimension name>}}";
}
}

View File

@@ -1,46 +1,57 @@
import angular from 'angular';
import _ from 'lodash';
import angular from "angular";
import _ from "lodash";
export class CloudWatchQueryParameter {
constructor() {
return {
templateUrl: 'public/app/plugins/datasource/cloudwatch/partials/query.parameter.html',
controller: 'CloudWatchQueryParameterCtrl',
restrict: 'E',
templateUrl:
"public/app/plugins/datasource/cloudwatch/partials/query.parameter.html",
controller: "CloudWatchQueryParameterCtrl",
restrict: "E",
scope: {
target: "=",
datasource: "=",
onChange: "&",
onChange: "&"
}
};
}
}
export class CloudWatchQueryParameterCtrl {
/** @ngInject */
constructor($scope, templateSrv, uiSegmentSrv, datasourceSrv, $q) {
$scope.init = function() {
var target = $scope.target;
target.namespace = target.namespace || '';
target.metricName = target.metricName || '';
target.statistics = target.statistics || ['Average'];
target.namespace = target.namespace || "";
target.metricName = target.metricName || "";
target.statistics = target.statistics || ["Average"];
target.dimensions = target.dimensions || {};
target.period = target.period || '';
target.region = target.region || 'default';
target.period = target.period || "";
target.region = target.region || "default";
$scope.regionSegment = uiSegmentSrv.getSegmentForValue($scope.target.region, 'select region');
$scope.namespaceSegment = uiSegmentSrv.getSegmentForValue($scope.target.namespace, 'select namespace');
$scope.metricSegment = uiSegmentSrv.getSegmentForValue($scope.target.metricName, 'select metric');
$scope.regionSegment = uiSegmentSrv.getSegmentForValue(
$scope.target.region,
"select region"
);
$scope.namespaceSegment = uiSegmentSrv.getSegmentForValue(
$scope.target.namespace,
"select namespace"
);
$scope.metricSegment = uiSegmentSrv.getSegmentForValue(
$scope.target.metricName,
"select metric"
);
$scope.dimSegments = _.reduce($scope.target.dimensions, function(memo, value, key) {
memo.push(uiSegmentSrv.newKey(key));
memo.push(uiSegmentSrv.newOperator("="));
memo.push(uiSegmentSrv.newKeyValue(value));
return memo;
}, []);
$scope.dimSegments = _.reduce(
$scope.target.dimensions,
function(memo, value, key) {
memo.push(uiSegmentSrv.newKey(key));
memo.push(uiSegmentSrv.newOperator("="));
memo.push(uiSegmentSrv.newKeyValue(value));
return memo;
},
[]
);
$scope.statSegments = _.map($scope.target.statistics, function(stat) {
return uiSegmentSrv.getSegmentForValue(stat);
@@ -48,11 +59,17 @@ export class CloudWatchQueryParameterCtrl {
$scope.ensurePlusButton($scope.statSegments);
$scope.ensurePlusButton($scope.dimSegments);
$scope.removeDimSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove dimension --'});
$scope.removeStatSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove stat --'});
$scope.removeDimSegment = uiSegmentSrv.newSegment({
fake: true,
value: "-- remove dimension --"
});
$scope.removeStatSegment = uiSegmentSrv.newSegment({
fake: true,
value: "-- remove stat --"
});
if (_.isEmpty($scope.target.region)) {
$scope.target.region = 'default';
$scope.target.region = "default";
}
if (!$scope.onChange) {
@@ -61,25 +78,34 @@ export class CloudWatchQueryParameterCtrl {
};
$scope.getStatSegments = function() {
return $q.when(_.flatten([
angular.copy($scope.removeStatSegment),
_.map($scope.datasource.standardStatistics, function(s) {
return uiSegmentSrv.getSegmentForValue(s);
}),
uiSegmentSrv.getSegmentForValue('pNN.NN'),
]));
return $q.when(
_.flatten([
angular.copy($scope.removeStatSegment),
_.map($scope.datasource.standardStatistics, function(s) {
return uiSegmentSrv.getSegmentForValue(s);
}),
uiSegmentSrv.getSegmentForValue("pNN.NN")
])
);
};
$scope.statSegmentChanged = function(segment, index) {
if (segment.value === $scope.removeStatSegment.value) {
$scope.statSegments.splice(index, 1);
} else {
segment.type = 'value';
segment.type = "value";
}
$scope.target.statistics = _.reduce($scope.statSegments, function(memo, seg) {
if (!seg.fake) { memo.push(seg.value); } return memo;
}, []);
$scope.target.statistics = _.reduce(
$scope.statSegments,
function(memo, seg) {
if (!seg.fake) {
memo.push(seg.value);
}
return memo;
},
[]
);
$scope.ensurePlusButton($scope.statSegments);
$scope.onChange();
@@ -87,32 +113,45 @@ export class CloudWatchQueryParameterCtrl {
$scope.ensurePlusButton = function(segments) {
var count = segments.length;
var lastSegment = segments[Math.max(count-1, 0)];
var lastSegment = segments[Math.max(count - 1, 0)];
if (!lastSegment || lastSegment.type !== 'plus-button') {
if (!lastSegment || lastSegment.type !== "plus-button") {
segments.push(uiSegmentSrv.newPlusButton());
}
};
$scope.getDimSegments = function(segment, $index) {
if (segment.type === 'operator') { return $q.when([]); }
if (segment.type === "operator") {
return $q.when([]);
}
var target = $scope.target;
var query = $q.when([]);
if (segment.type === 'key' || segment.type === 'plus-button') {
query = $scope.datasource.getDimensionKeys($scope.target.namespace, $scope.target.region);
} else if (segment.type === 'value') {
var dimensionKey = $scope.dimSegments[$index-2].value;
query = $scope.datasource.getDimensionValues(target.region, target.namespace, target.metricName, dimensionKey, target.dimensions);
if (segment.type === "key" || segment.type === "plus-button") {
query = $scope.datasource.getDimensionKeys(
$scope.target.namespace,
$scope.target.region
);
} else if (segment.type === "value") {
var dimensionKey = $scope.dimSegments[$index - 2].value;
query = $scope.datasource.getDimensionValues(
target.region,
target.namespace,
target.metricName,
dimensionKey,
target.dimensions
);
}
return query.then($scope.transformToSegments(true)).then(function(results) {
if (segment.type === 'key') {
results.splice(0, 0, angular.copy($scope.removeDimSegment));
}
return results;
});
return query
.then($scope.transformToSegments(true))
.then(function(results) {
if (segment.type === "key") {
results.splice(0, 0, angular.copy($scope.removeDimSegment));
}
return results;
});
};
$scope.dimSegmentChanged = function(segment, index) {
@@ -120,11 +159,17 @@ export class CloudWatchQueryParameterCtrl {
if (segment.value === $scope.removeDimSegment.value) {
$scope.dimSegments.splice(index, 3);
} else if (segment.type === 'plus-button') {
$scope.dimSegments.push(uiSegmentSrv.newOperator('='));
$scope.dimSegments.push(uiSegmentSrv.newFake('select dimension value', 'value', 'query-segment-value'));
segment.type = 'key';
segment.cssClass = 'query-segment-key';
} else if (segment.type === "plus-button") {
$scope.dimSegments.push(uiSegmentSrv.newOperator("="));
$scope.dimSegments.push(
uiSegmentSrv.newFake(
"select dimension value",
"value",
"query-segment-value"
)
);
segment.type = "key";
segment.cssClass = "query-segment-key";
}
$scope.syncDimSegmentsWithModel();
@@ -148,22 +193,31 @@ export class CloudWatchQueryParameterCtrl {
};
$scope.getRegions = function() {
return $scope.datasource.metricFindQuery('regions()')
.then(function(results) {
results.unshift({ text: 'default'});
return results;
})
.then($scope.transformToSegments(true));
return $scope.datasource
.metricFindQuery("regions()")
.then(function(results) {
results.unshift({ text: "default" });
return results;
})
.then($scope.transformToSegments(true));
};
$scope.getNamespaces = function() {
return $scope.datasource.metricFindQuery('namespaces()')
.then($scope.transformToSegments(true));
return $scope.datasource
.metricFindQuery("namespaces()")
.then($scope.transformToSegments(true));
};
$scope.getMetrics = function() {
return $scope.datasource.metricFindQuery('metrics(' + $scope.target.namespace + ',' + $scope.target.region + ')')
.then($scope.transformToSegments(true));
return $scope.datasource
.metricFindQuery(
"metrics(" +
$scope.target.namespace +
"," +
$scope.target.region +
")"
)
.then($scope.transformToSegments(true));
};
$scope.regionChanged = function() {
@@ -184,12 +238,21 @@ export class CloudWatchQueryParameterCtrl {
$scope.transformToSegments = function(addTemplateVars) {
return function(results) {
var segments = _.map(results, function(segment) {
return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
return uiSegmentSrv.newSegment({
value: segment.text,
expandable: segment.expandable
});
});
if (addTemplateVars) {
_.each(templateSrv.variables, function(variable) {
segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
segments.unshift(
uiSegmentSrv.newSegment({
type: "template",
value: "$" + variable.name,
expandable: true
})
);
});
}
@@ -201,5 +264,9 @@ export class CloudWatchQueryParameterCtrl {
}
}
angular.module('grafana.controllers').directive('cloudwatchQueryParameter', CloudWatchQueryParameter);
angular.module('grafana.controllers').controller('CloudWatchQueryParameterCtrl', CloudWatchQueryParameterCtrl);
angular
.module("grafana.controllers")
.directive("cloudwatchQueryParameter", CloudWatchQueryParameter);
angular
.module("grafana.controllers")
.controller("CloudWatchQueryParameterCtrl", CloudWatchQueryParameterCtrl);

View File

@@ -1,43 +1,53 @@
import "../datasource";
import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common';
import helpers from 'test/specs/helpers';
import {
describe,
beforeEach,
it,
expect,
angularMocks
} from "test/lib/common";
import helpers from "test/specs/helpers";
import CloudWatchDatasource from "../datasource";
describe('CloudWatchDatasource', function() {
describe("CloudWatchDatasource", function() {
var ctx = new helpers.ServiceTestContext();
var instanceSettings = {
jsonData: {defaultRegion: 'us-east-1', access: 'proxy'},
jsonData: { defaultRegion: "us-east-1", access: "proxy" }
};
beforeEach(angularMocks.module('grafana.core'));
beforeEach(angularMocks.module('grafana.services'));
beforeEach(angularMocks.module('grafana.controllers'));
beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
beforeEach(angularMocks.module("grafana.core"));
beforeEach(angularMocks.module("grafana.services"));
beforeEach(angularMocks.module("grafana.controllers"));
beforeEach(ctx.providePhase(["templateSrv", "backendSrv"]));
beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
ctx.$q = $q;
ctx.$httpBackend = $httpBackend;
ctx.$rootScope = $rootScope;
ctx.ds = $injector.instantiate(CloudWatchDatasource, {instanceSettings: instanceSettings});
$httpBackend.when('GET', /\.html$/).respond('');
}));
beforeEach(
angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
ctx.$q = $q;
ctx.$httpBackend = $httpBackend;
ctx.$rootScope = $rootScope;
ctx.ds = $injector.instantiate(CloudWatchDatasource, {
instanceSettings: instanceSettings
});
$httpBackend.when("GET", /\.html$/).respond("");
})
);
describe('When performing CloudWatch query', function() {
describe("When performing CloudWatch query", function() {
var requestParams;
var query = {
range: { from: 'now-1h', to: 'now' },
range: { from: "now-1h", to: "now" },
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
region: "us-east-1",
namespace: "AWS/EC2",
metricName: "CPUUtilization",
dimensions: {
InstanceId: 'i-12345678'
InstanceId: "i-12345678"
},
statistics: ['Average'],
period: '300'
statistics: ["Average"],
period: "300"
}
]
};
@@ -46,18 +56,18 @@ describe('CloudWatchDatasource', function() {
timings: [null],
results: {
A: {
error: '',
refId: 'A',
error: "",
refId: "A",
series: [
{
name: 'CPUUtilization_Average',
name: "CPUUtilization_Average",
points: [
[1, 1483228800000],
[2, 1483229100000],
[5, 1483229700000],
[5, 1483229700000]
],
tags: {
InstanceId: 'i-12345678'
InstanceId: "i-12345678"
}
}
]
@@ -68,16 +78,16 @@ describe('CloudWatchDatasource', function() {
beforeEach(function() {
ctx.backendSrv.datasourceRequest = function(params) {
requestParams = params.data;
return ctx.$q.when({data: response});
return ctx.$q.when({ data: response });
};
});
it('should generate the correct query', function(done) {
it("should generate the correct query", function(done) {
ctx.ds.query(query).then(function() {
var params = requestParams.queries[0];
expect(params.namespace).to.be(query.targets[0].namespace);
expect(params.metricName).to.be(query.targets[0].metricName);
expect(params.dimensions['InstanceId']).to.be('i-12345678');
expect(params.dimensions["InstanceId"]).to.be("i-12345678");
expect(params.statistics).to.eql(query.targets[0].statistics);
expect(params.period).to.be(query.targets[0].period);
done();
@@ -85,149 +95,162 @@ describe('CloudWatchDatasource', function() {
ctx.$rootScope.$apply();
});
it('should generate the correct query with interval variable', function(done) {
it("should generate the correct query with interval variable", function(done) {
ctx.templateSrv.data = {
period: '10m'
period: "10m"
};
var query = {
range: { from: 'now-1h', to: 'now' },
range: { from: "now-1h", to: "now" },
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
region: "us-east-1",
namespace: "AWS/EC2",
metricName: "CPUUtilization",
dimensions: {
InstanceId: 'i-12345678'
InstanceId: "i-12345678"
},
statistics: ['Average'],
period: '[[period]]'
statistics: ["Average"],
period: "[[period]]"
}
]
};
ctx.ds.query(query).then(function() {
var params = requestParams.queries[0];
expect(params.period).to.be('600');
expect(params.period).to.be("600");
done();
});
ctx.$rootScope.$apply();
});
it('should return series list', function(done) {
it("should return series list", function(done) {
ctx.ds.query(query).then(function(result) {
expect(result.data[0].target).to.be(response.results.A.series[0].name);
expect(result.data[0].datapoints[0][0]).to.be(response.results.A.series[0].points[0][0]);
expect(result.data[0].datapoints[0][0]).to.be(
response.results.A.series[0].points[0][0]
);
done();
});
ctx.$rootScope.$apply();
});
it('should generate the correct targets by expanding template variables', function() {
it("should generate the correct targets by expanding template variables", function() {
var templateSrv = {
variables: [
{
name: 'instance_id',
name: "instance_id",
options: [
{ text: 'i-23456789', value: 'i-23456789', selected: false },
{ text: 'i-34567890', value: 'i-34567890', selected: true }
{ text: "i-23456789", value: "i-23456789", selected: false },
{ text: "i-34567890", value: "i-34567890", selected: true }
]
}
],
replace: function (target, scopedVars) {
if (target === '$instance_id' && scopedVars['instance_id']['text'] === 'i-34567890') {
return 'i-34567890';
replace: function(target, scopedVars) {
if (
target === "$instance_id" &&
scopedVars["instance_id"]["text"] === "i-34567890"
) {
return "i-34567890";
} else {
return '';
return "";
}
},
getVariableName: function (e) { return 'instance_id'; },
variableExists: function (e) { return true; },
containsVariable: function (str, variableName) { return str.indexOf('$' + variableName) !== -1; }
getVariableName: function(e) {
return "instance_id";
},
variableExists: function(e) {
return true;
},
containsVariable: function(str, variableName) {
return str.indexOf("$" + variableName) !== -1;
}
};
var targets = [
{
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
region: "us-east-1",
namespace: "AWS/EC2",
metricName: "CPUUtilization",
dimensions: {
InstanceId: '$instance_id'
InstanceId: "$instance_id"
},
statistics: ['Average'],
statistics: ["Average"],
period: 300
}
];
var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv);
expect(result[0].dimensions.InstanceId).to.be('i-34567890');
expect(result[0].dimensions.InstanceId).to.be("i-34567890");
});
});
describe('When query region is "default"', function () {
describe('When query region is "default"', function() {
it('should return the datasource region if empty or "default"', function() {
var defaultRegion = instanceSettings.jsonData.defaultRegion;
expect(ctx.ds.getActualRegion()).to.be(defaultRegion);
expect(ctx.ds.getActualRegion('')).to.be(defaultRegion);
expect(ctx.ds.getActualRegion("")).to.be(defaultRegion);
expect(ctx.ds.getActualRegion("default")).to.be(defaultRegion);
});
it('should return the specified region if specified', function() {
expect(ctx.ds.getActualRegion('some-fake-region-1')).to.be('some-fake-region-1');
it("should return the specified region if specified", function() {
expect(ctx.ds.getActualRegion("some-fake-region-1")).to.be(
"some-fake-region-1"
);
});
var requestParams;
beforeEach(function() {
ctx.ds.performTimeSeriesQuery = function(request) {
requestParams = request;
return ctx.$q.when({data: {}});
return ctx.$q.when({ data: {} });
};
});
it('should query for the datasource region if empty or "default"', function(done) {
var query = {
range: { from: 'now-1h', to: 'now' },
range: { from: "now-1h", to: "now" },
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
region: 'default',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
region: "default",
namespace: "AWS/EC2",
metricName: "CPUUtilization",
dimensions: {
InstanceId: 'i-12345678'
InstanceId: "i-12345678"
},
statistics: ['Average'],
statistics: ["Average"],
period: 300
}
]
};
ctx.ds.query(query).then(function(result) {
expect(requestParams.queries[0].region).to.be(instanceSettings.jsonData.defaultRegion);
expect(requestParams.queries[0].region).to.be(
instanceSettings.jsonData.defaultRegion
);
done();
});
ctx.$rootScope.$apply();
});
});
describe('When performing CloudWatch query for extended statistics', function() {
describe("When performing CloudWatch query for extended statistics", function() {
var query = {
range: { from: 'now-1h', to: 'now' },
range: { from: "now-1h", to: "now" },
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
region: 'us-east-1',
namespace: 'AWS/ApplicationELB',
metricName: 'TargetResponseTime',
region: "us-east-1",
namespace: "AWS/ApplicationELB",
metricName: "TargetResponseTime",
dimensions: {
LoadBalancer: 'lb',
TargetGroup: 'tg'
LoadBalancer: "lb",
TargetGroup: "tg"
},
statistics: ['p90.00'],
statistics: ["p90.00"],
period: 300
}
]
@@ -237,19 +260,19 @@ describe('CloudWatchDatasource', function() {
timings: [null],
results: {
A: {
error: '',
refId: 'A',
error: "",
refId: "A",
series: [
{
name: 'TargetResponseTime_p90.00',
name: "TargetResponseTime_p90.00",
points: [
[1, 1483228800000],
[2, 1483229100000],
[5, 1483229700000],
[5, 1483229700000]
],
tags: {
LoadBalancer: 'lb',
TargetGroup: 'tg'
LoadBalancer: "lb",
TargetGroup: "tg"
}
}
]
@@ -259,14 +282,16 @@ describe('CloudWatchDatasource', function() {
beforeEach(function() {
ctx.backendSrv.datasourceRequest = function(params) {
return ctx.$q.when({data: response});
return ctx.$q.when({ data: response });
};
});
it('should return series list', function(done) {
it("should return series list", function(done) {
ctx.ds.query(query).then(function(result) {
expect(result.data[0].target).to.be(response.results.A.series[0].name);
expect(result.data[0].datapoints[0][0]).to.be(response.results.A.series[0].points[0][0]);
expect(result.data[0].datapoints[0][0]).to.be(
response.results.A.series[0].points[0][0]
);
done();
});
ctx.$rootScope.$apply();
@@ -274,14 +299,14 @@ describe('CloudWatchDatasource', function() {
});
function describeMetricFindQuery(query, func) {
describe('metricFindQuery ' + query, () => {
describe("metricFindQuery " + query, () => {
let scenario: any = {};
scenario.setup = setupCallback => {
beforeEach(() => {
setupCallback();
ctx.backendSrv.datasourceRequest = args => {
scenario.request = args.data;
return ctx.$q.when({data: scenario.requestResponse});
return ctx.$q.when({ data: scenario.requestResponse });
};
ctx.ds.metricFindQuery(query).then(args => {
scenario.result = args;
@@ -294,190 +319,201 @@ describe('CloudWatchDatasource', function() {
});
}
describeMetricFindQuery('regions()', scenario => {
describeMetricFindQuery("regions()", scenario => {
scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [
{ rows: [['us-east-1', 'us-east-1']] }
]
tables: [{ rows: [["us-east-1", "us-east-1"]] }]
}
}
};
});
it('should call __GetRegions and return result', () => {
expect(scenario.result[0].text).to.contain('us-east-1');
expect(scenario.request.queries[0].type).to.be('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('regions');
it("should call __GetRegions and return result", () => {
expect(scenario.result[0].text).to.contain("us-east-1");
expect(scenario.request.queries[0].type).to.be("metricFindQuery");
expect(scenario.request.queries[0].subtype).to.be("regions");
});
});
describeMetricFindQuery('namespaces()', scenario => {
describeMetricFindQuery("namespaces()", scenario => {
scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [
{ rows: [['AWS/EC2', 'AWS/EC2']] }
]
tables: [{ rows: [["AWS/EC2", "AWS/EC2"]] }]
}
}
};
});
it('should call __GetNamespaces and return result', () => {
expect(scenario.result[0].text).to.contain('AWS/EC2');
expect(scenario.request.queries[0].type).to.be('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('namespaces');
it("should call __GetNamespaces and return result", () => {
expect(scenario.result[0].text).to.contain("AWS/EC2");
expect(scenario.request.queries[0].type).to.be("metricFindQuery");
expect(scenario.request.queries[0].subtype).to.be("namespaces");
});
});
describeMetricFindQuery('metrics(AWS/EC2)', scenario => {
describeMetricFindQuery("metrics(AWS/EC2)", scenario => {
scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [
{ rows: [['CPUUtilization', 'CPUUtilization']] }
]
tables: [{ rows: [["CPUUtilization", "CPUUtilization"]] }]
}
}
};
});
it('should call __GetMetrics and return result', () => {
expect(scenario.result[0].text).to.be('CPUUtilization');
expect(scenario.request.queries[0].type).to.be('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('metrics');
it("should call __GetMetrics and return result", () => {
expect(scenario.result[0].text).to.be("CPUUtilization");
expect(scenario.request.queries[0].type).to.be("metricFindQuery");
expect(scenario.request.queries[0].subtype).to.be("metrics");
});
});
describeMetricFindQuery('dimension_keys(AWS/EC2)', scenario => {
describeMetricFindQuery("dimension_keys(AWS/EC2)", scenario => {
scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [
{ rows: [['InstanceId', 'InstanceId']] }
]
tables: [{ rows: [["InstanceId", "InstanceId"]] }]
}
}
};
});
it('should call __GetDimensions and return result', () => {
expect(scenario.result[0].text).to.be('InstanceId');
expect(scenario.request.queries[0].type).to.be('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('dimension_keys');
it("should call __GetDimensions and return result", () => {
expect(scenario.result[0].text).to.be("InstanceId");
expect(scenario.request.queries[0].type).to.be("metricFindQuery");
expect(scenario.request.queries[0].subtype).to.be("dimension_keys");
});
});
describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [
{ rows: [['i-12345678', 'i-12345678']] }
]
describeMetricFindQuery(
"dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)",
scenario => {
scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [{ rows: [["i-12345678", "i-12345678"]] }]
}
}
}
};
});
};
});
it('should call __ListMetrics and return result', () => {
expect(scenario.result[0].text).to.contain('i-12345678');
expect(scenario.request.queries[0].type).to.be('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('dimension_values');
});
});
it("should call __ListMetrics and return result", () => {
expect(scenario.result[0].text).to.contain("i-12345678");
expect(scenario.request.queries[0].type).to.be("metricFindQuery");
expect(scenario.request.queries[0].subtype).to.be("dimension_values");
});
}
);
describeMetricFindQuery('dimension_values(default,AWS/EC2,CPUUtilization,InstanceId)', scenario => {
scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [
{ rows: [['i-12345678', 'i-12345678']] }
]
describeMetricFindQuery(
"dimension_values(default,AWS/EC2,CPUUtilization,InstanceId)",
scenario => {
scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [{ rows: [["i-12345678", "i-12345678"]] }]
}
}
}
};
});
};
});
it('should call __ListMetrics and return result', () => {
expect(scenario.result[0].text).to.contain('i-12345678');
expect(scenario.request.queries[0].type).to.be('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('dimension_values');
});
});
it("should call __ListMetrics and return result", () => {
expect(scenario.result[0].text).to.contain("i-12345678");
expect(scenario.request.queries[0].type).to.be("metricFindQuery");
expect(scenario.request.queries[0].subtype).to.be("dimension_values");
});
}
);
it('should caclculate the correct period', function () {
it("should caclculate the correct period", function() {
var hourSec = 60 * 60;
var daySec = hourSec * 24;
var start = 1483196400 * 1000;
var testData: any[] = [
[
{ period: 60, namespace: 'AWS/EC2' },
{ period: 60, namespace: "AWS/EC2" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(hourSec * 3), 60
hourSec * 3,
60
],
[
{ period: null, namespace: 'AWS/EC2' },
{ period: null, namespace: "AWS/EC2" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(hourSec * 3), 300
hourSec * 3,
300
],
[
{ period: 60, namespace: 'AWS/ELB' },
{ period: 60, namespace: "AWS/ELB" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(hourSec * 3), 60
hourSec * 3,
60
],
[
{ period: null, namespace: 'AWS/ELB' },
{ period: null, namespace: "AWS/ELB" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(hourSec * 3), 60
hourSec * 3,
60
],
[
{ period: 1, namespace: 'CustomMetricsNamespace' },
{ range: { from: new Date(start), to: new Date(start + (1440 - 1) * 1000) } },
(hourSec * 3 - 1), 1
{ period: 1, namespace: "CustomMetricsNamespace" },
{
range: {
from: new Date(start),
to: new Date(start + (1440 - 1) * 1000)
}
},
hourSec * 3 - 1,
1
],
[
{ period: 1, namespace: 'CustomMetricsNamespace' },
{ period: 1, namespace: "CustomMetricsNamespace" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(hourSec * 3 - 1), 60
hourSec * 3 - 1,
60
],
[
{ period: 60, namespace: 'CustomMetricsNamespace' },
{ period: 60, namespace: "CustomMetricsNamespace" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(hourSec * 3), 60
hourSec * 3,
60
],
[
{ period: null, namespace: 'CustomMetricsNamespace' },
{ period: null, namespace: "CustomMetricsNamespace" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(hourSec * 3 - 1), 60
hourSec * 3 - 1,
60
],
[
{ period: null, namespace: 'CustomMetricsNamespace' },
{ period: null, namespace: "CustomMetricsNamespace" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(hourSec * 3), 60
hourSec * 3,
60
],
[
{ period: null, namespace: 'CustomMetricsNamespace' },
{ period: null, namespace: "CustomMetricsNamespace" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(daySec * 15), 60
daySec * 15,
60
],
[
{ period: null, namespace: 'CustomMetricsNamespace' },
{ period: null, namespace: "CustomMetricsNamespace" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(daySec * 63), 300
daySec * 63,
300
],
[
{ period: null, namespace: 'CustomMetricsNamespace' },
{ period: null, namespace: "CustomMetricsNamespace" },
{ range: { from: new Date(start), to: new Date(start + 3600 * 1000) } },
(daySec * 455), 3600
daySec * 455,
3600
]
];
for (let t of testData) {
@@ -489,5 +525,4 @@ describe('CloudWatchDatasource', function() {
expect(actual).to.be(expected);
}
});
});