Merge remote-tracking branch 'upstream/master' into postgres-query-builder

This commit is contained in:
Sven Klemm
2018-07-04 18:59:23 +02:00
9 changed files with 126 additions and 121 deletions
+5 -4
View File
@@ -8,6 +8,9 @@ aliases:
- &filter-not-release
tags:
ignore: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
- &filter-only-master
branches:
only: master
version: 2
@@ -242,7 +245,7 @@ workflows:
- build-all:
filters: *filter-not-release
- build-enterprise:
filters: *filter-not-release
filters: *filter-only-master
- codespell:
filters: *filter-not-release
- gometalinter:
@@ -277,9 +280,7 @@ workflows:
- mysql-integration-test
- postgres-integration-test
- build-enterprise
filters:
branches:
only: master
filters: *filter-only-master
release:
jobs:
+15 -15
View File
@@ -36,11 +36,10 @@ HTTP/1.1 200
Content-Type: application/json
{
"DEFAULT":
{
"app_mode":"production"},
"analytics":
{
"DEFAULT": {
"app_mode":"production"
},
"analytics": {
"google_analytics_ua_id":"",
"reporting_enabled":"false"
},
@@ -195,15 +194,16 @@ HTTP/1.1 200
Content-Type: application/json
{
"user_count":2,
"org_count":1,
"dashboard_count":4,
"db_snapshot_count":2,
"db_tag_count":6,
"data_source_count":1,
"playlist_count":1,
"starred_db_count":2,
"grafana_admin_count":2
"users":2,
"orgs":1,
"dashboards":4,
"snapshots":2,
"tags":6,
"datasources":1,
"playlists":1,
"stars":2,
"alerts":2,
"activeUsers":1
}
```
@@ -340,4 +340,4 @@ HTTP/1.1 200
Content-Type: application/json
{state: "new state", message: "alerts pause/un paused", "alertsAffected": 100}
```
```
+4 -4
View File
@@ -50,7 +50,7 @@ func TestAlertRuleExtraction(t *testing.T) {
So(err, ShouldBeNil)
Convey("Extractor should not modify the original json", func() {
dashJson, err := simplejson.NewJson([]byte(json))
dashJson, err := simplejson.NewJson(json)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
@@ -79,7 +79,7 @@ func TestAlertRuleExtraction(t *testing.T) {
Convey("Parsing and validating dashboard containing graphite alerts", func() {
dashJson, err := simplejson.NewJson([]byte(json))
dashJson, err := simplejson.NewJson(json)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
@@ -143,7 +143,7 @@ func TestAlertRuleExtraction(t *testing.T) {
panelWithoutId, err := ioutil.ReadFile("./test-data/panels-missing-id.json")
So(err, ShouldBeNil)
dashJson, err := simplejson.NewJson([]byte(panelWithoutId))
dashJson, err := simplejson.NewJson(panelWithoutId)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
extractor := NewDashAlertExtractor(dash, 1)
@@ -159,7 +159,7 @@ func TestAlertRuleExtraction(t *testing.T) {
panelWithIdZero, err := ioutil.ReadFile("./test-data/panel-with-id-0.json")
So(err, ShouldBeNil)
dashJson, err := simplejson.NewJson([]byte(panelWithIdZero))
dashJson, err := simplejson.NewJson(panelWithIdZero)
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
extractor := NewDashAlertExtractor(dash, 1)
@@ -32,7 +32,7 @@ func TestSearchRequest(t *testing.T) {
Convey("When marshal to JSON should generate correct json", func() {
body, err := json.Marshal(sr)
So(err, ShouldBeNil)
json, err := simplejson.NewJson([]byte(body))
json, err := simplejson.NewJson(body)
So(err, ShouldBeNil)
So(json.Get("size").MustInt(500), ShouldEqual, 0)
So(json.Get("sort").Interface(), ShouldBeNil)
@@ -81,7 +81,7 @@ func TestSearchRequest(t *testing.T) {
Convey("When marshal to JSON should generate correct json", func() {
body, err := json.Marshal(sr)
So(err, ShouldBeNil)
json, err := simplejson.NewJson([]byte(body))
json, err := simplejson.NewJson(body)
So(err, ShouldBeNil)
So(json.Get("size").MustInt(0), ShouldEqual, 200)
@@ -124,7 +124,7 @@ func TestSearchRequest(t *testing.T) {
Convey("When marshal to JSON should generate correct json", func() {
body, err := json.Marshal(sr)
So(err, ShouldBeNil)
json, err := simplejson.NewJson([]byte(body))
json, err := simplejson.NewJson(body)
So(err, ShouldBeNil)
scriptFields, err := json.Get("script_fields").Map()
@@ -163,7 +163,7 @@ func TestSearchRequest(t *testing.T) {
Convey("When marshal to JSON should generate correct json", func() {
body, err := json.Marshal(sr)
So(err, ShouldBeNil)
json, err := simplejson.NewJson([]byte(body))
json, err := simplejson.NewJson(body)
So(err, ShouldBeNil)
So(json.Get("aggs").MustMap(), ShouldHaveLength, 2)
@@ -200,7 +200,7 @@ func TestSearchRequest(t *testing.T) {
Convey("When marshal to JSON should generate correct json", func() {
body, err := json.Marshal(sr)
So(err, ShouldBeNil)
json, err := simplejson.NewJson([]byte(body))
json, err := simplejson.NewJson(body)
So(err, ShouldBeNil)
So(json.Get("aggs").MustMap(), ShouldHaveLength, 1)
@@ -251,7 +251,7 @@ func TestSearchRequest(t *testing.T) {
Convey("When marshal to JSON should generate correct json", func() {
body, err := json.Marshal(sr)
So(err, ShouldBeNil)
json, err := simplejson.NewJson([]byte(body))
json, err := simplejson.NewJson(body)
So(err, ShouldBeNil)
topAggOne := json.GetPath("aggs", "1")
@@ -300,7 +300,7 @@ func TestSearchRequest(t *testing.T) {
Convey("When marshal to JSON should generate correct json", func() {
body, err := json.Marshal(sr)
So(err, ShouldBeNil)
json, err := simplejson.NewJson([]byte(body))
json, err := simplejson.NewJson(body)
So(err, ShouldBeNil)
topAgg := json.GetPath("aggs", "1")
@@ -364,7 +364,7 @@ func TestSearchRequest(t *testing.T) {
Convey("When marshal to JSON should generate correct json", func() {
body, err := json.Marshal(sr)
So(err, ShouldBeNil)
json, err := simplejson.NewJson([]byte(body))
json, err := simplejson.NewJson(body)
So(err, ShouldBeNil)
termsAgg := json.GetPath("aggs", "1")
@@ -419,7 +419,7 @@ func TestSearchRequest(t *testing.T) {
Convey("When marshal to JSON should generate correct json", func() {
body, err := json.Marshal(sr)
So(err, ShouldBeNil)
json, err := simplejson.NewJson([]byte(body))
json, err := simplejson.NewJson(body)
So(err, ShouldBeNil)
scriptFields, err := json.Get("script_fields").Map()
+4 -4
View File
@@ -531,7 +531,7 @@ func TestMSSQL(t *testing.T) {
So(queryResult.Error, ShouldBeNil)
So(len(queryResult.Series), ShouldEqual, 1)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3)
})
Convey("When doing a metric query using epoch (float32 nullable) as time column and value column (float32 nullable) should return metric with time in milliseconds", func() {
@@ -553,7 +553,7 @@ func TestMSSQL(t *testing.T) {
So(queryResult.Error, ShouldBeNil)
So(len(queryResult.Series), ShouldEqual, 1)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3)
})
Convey("When doing a metric query grouping by time and select metric column should return correct series", func() {
@@ -930,7 +930,7 @@ func TestMSSQL(t *testing.T) {
columns := queryResult.Tables[0].Rows[0]
//Should be in milliseconds
So(columns[0].(int64), ShouldEqual, int64(dt.Unix()*1000))
So(columns[0].(int64), ShouldEqual, dt.Unix()*1000)
})
Convey("When doing an annotation query with a time column in epoch second format (int) should return ms", func() {
@@ -960,7 +960,7 @@ func TestMSSQL(t *testing.T) {
columns := queryResult.Tables[0].Rows[0]
//Should be in milliseconds
So(columns[0].(int64), ShouldEqual, int64(dt.Unix()*1000))
So(columns[0].(int64), ShouldEqual, dt.Unix()*1000)
})
Convey("When doing an annotation query with a time column in epoch millisecond format should return ms", func() {
+5 -5
View File
@@ -132,8 +132,8 @@ func TestMySQL(t *testing.T) {
So(column[7].(float64), ShouldEqual, 1.11)
So(column[8].(float64), ShouldEqual, 2.22)
So(*column[9].(*float32), ShouldEqual, 3.33)
So(column[10].(time.Time), ShouldHappenWithin, time.Duration(10*time.Second), time.Now())
So(column[11].(time.Time), ShouldHappenWithin, time.Duration(10*time.Second), time.Now())
So(column[10].(time.Time), ShouldHappenWithin, 10*time.Second, time.Now())
So(column[11].(time.Time), ShouldHappenWithin, 10*time.Second, time.Now())
So(column[12].(string), ShouldEqual, "11:11:11")
So(column[13].(int64), ShouldEqual, 2018)
So(*column[14].(*[]byte), ShouldHaveSameTypeAs, []byte{1})
@@ -578,7 +578,7 @@ func TestMySQL(t *testing.T) {
So(queryResult.Error, ShouldBeNil)
So(len(queryResult.Series), ShouldEqual, 1)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3)
})
Convey("When doing a metric query using epoch (float32 nullable) as time column and value column (float32 nullable) should return metric with time in milliseconds", func() {
@@ -600,7 +600,7 @@ func TestMySQL(t *testing.T) {
So(queryResult.Error, ShouldBeNil)
So(len(queryResult.Series), ShouldEqual, 1)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3)
})
Convey("When doing a metric query grouping by time and select metric column should return correct series", func() {
@@ -817,7 +817,7 @@ func TestMySQL(t *testing.T) {
columns := queryResult.Tables[0].Rows[0]
//Should be in milliseconds
So(columns[0].(int64), ShouldEqual, int64(dt.Unix()*1000))
So(columns[0].(int64), ShouldEqual, dt.Unix()*1000)
})
Convey("When doing an annotation query with a time column in epoch millisecond format should return ms", func() {
+4 -4
View File
@@ -512,7 +512,7 @@ func TestPostgres(t *testing.T) {
So(queryResult.Error, ShouldBeNil)
So(len(queryResult.Series), ShouldEqual, 1)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3)
})
Convey("When doing a metric query using epoch (float32 nullable) as time column and value column (float32 nullable) should return metric with time in milliseconds", func() {
@@ -534,7 +534,7 @@ func TestPostgres(t *testing.T) {
So(queryResult.Error, ShouldBeNil)
So(len(queryResult.Series), ShouldEqual, 1)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float64(float32(tInitial.Unix())))*1e3)
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(float32(tInitial.Unix()))*1e3)
})
Convey("When doing a metric query grouping by time and select metric column should return correct series", func() {
@@ -721,7 +721,7 @@ func TestPostgres(t *testing.T) {
columns := queryResult.Tables[0].Rows[0]
//Should be in milliseconds
So(columns[0].(int64), ShouldEqual, int64(dt.Unix()*1000))
So(columns[0].(int64), ShouldEqual, dt.Unix()*1000)
})
Convey("When doing an annotation query with a time column in epoch second format (int) should return ms", func() {
@@ -751,7 +751,7 @@ func TestPostgres(t *testing.T) {
columns := queryResult.Tables[0].Rows[0]
//Should be in milliseconds
So(columns[0].(int64), ShouldEqual, int64(dt.Unix()*1000))
So(columns[0].(int64), ShouldEqual, dt.Unix()*1000)
})
Convey("When doing an annotation query with a time column in epoch millisecond format should return ms", func() {
@@ -1,32 +1,46 @@
import _ from 'lodash';
import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common';
import moment from 'moment';
import angular from 'angular';
import helpers from 'test/specs/helpers';
import { ElasticDatasource } from '../datasource';
import * as dateMath from 'app/core/utils/datemath';
describe('ElasticDatasource', function() {
var ctx = new helpers.ServiceTestContext();
let backendSrv = {
datasourceRequest: jest.fn(),
};
beforeEach(angularMocks.module('grafana.core'));
beforeEach(angularMocks.module('grafana.services'));
beforeEach(ctx.providePhase(['templateSrv', 'backendSrv', 'timeSrv']));
let $rootScope = {
$on: jest.fn(),
appEvent: jest.fn(),
};
beforeEach(
angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
ctx.$q = $q;
ctx.$httpBackend = $httpBackend;
ctx.$rootScope = $rootScope;
ctx.$injector = $injector;
$httpBackend.when('GET', /\.html$/).respond('');
})
);
let templateSrv = {
replace: jest.fn(text => text),
getAdhocFilters: jest.fn(() => []),
};
let timeSrv = {
time: { from: 'now-1h', to: 'now' },
timeRange: jest.fn(() => {
return {
from: dateMath.parse(this.time.from, false),
to: dateMath.parse(this.time.to, true),
};
}),
setTime: jest.fn(time => {
this.time = time;
}),
};
let ctx = <any>{
$rootScope,
backendSrv,
};
function createDatasource(instanceSettings) {
instanceSettings.jsonData = instanceSettings.jsonData || {};
ctx.ds = ctx.$injector.instantiate(ElasticDatasource, {
instanceSettings: instanceSettings,
});
ctx.ds = new ElasticDatasource(instanceSettings, {}, backendSrv, templateSrv, timeSrv);
}
describe('When testing datasource with index pattern', function() {
@@ -40,33 +54,32 @@ describe('ElasticDatasource', function() {
it('should translate index pattern to current day', function() {
var requestOptions;
ctx.backendSrv.datasourceRequest = function(options) {
ctx.backendSrv.datasourceRequest = jest.fn(options => {
requestOptions = options;
return ctx.$q.when({ data: {} });
};
return Promise.resolve({ data: {} });
});
ctx.ds.testDatasource();
ctx.$rootScope.$apply();
var today = moment.utc().format('YYYY.MM.DD');
expect(requestOptions.url).to.be('http://es.com/asd-' + today + '/_mapping');
expect(requestOptions.url).toBe('http://es.com/asd-' + today + '/_mapping');
});
});
describe('When issuing metric query with interval pattern', function() {
var requestOptions, parts, header;
beforeEach(function() {
beforeEach(() => {
createDatasource({
url: 'http://es.com',
index: '[asd-]YYYY.MM.DD',
jsonData: { interval: 'Daily', esVersion: '2' },
});
ctx.backendSrv.datasourceRequest = function(options) {
ctx.backendSrv.datasourceRequest = jest.fn(options => {
requestOptions = options;
return ctx.$q.when({ data: { responses: [] } });
};
return Promise.resolve({ data: { responses: [] } });
});
ctx.ds.query({
range: {
@@ -82,19 +95,17 @@ describe('ElasticDatasource', function() {
],
});
ctx.$rootScope.$apply();
parts = requestOptions.data.split('\n');
header = angular.fromJson(parts[0]);
});
it('should translate index pattern to current day', function() {
expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
expect(header.index).toEqual(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
});
it('should json escape lucene query', function() {
var body = angular.fromJson(parts[1]);
expect(body.query.bool.filter[1].query_string.query).to.be('escape\\:test');
expect(body.query.bool.filter[1].query_string.query).toBe('escape\\:test');
});
});
@@ -108,10 +119,10 @@ describe('ElasticDatasource', function() {
jsonData: { esVersion: '2' },
});
ctx.backendSrv.datasourceRequest = function(options) {
ctx.backendSrv.datasourceRequest = jest.fn(options => {
requestOptions = options;
return ctx.$q.when({ data: { responses: [] } });
};
return Promise.resolve({ data: { responses: [] } });
});
ctx.ds.query({
range: {
@@ -127,27 +138,26 @@ describe('ElasticDatasource', function() {
],
});
ctx.$rootScope.$apply();
parts = requestOptions.data.split('\n');
header = angular.fromJson(parts[0]);
});
it('should set search type to query_then_fetch', function() {
expect(header.search_type).to.eql('query_then_fetch');
expect(header.search_type).toEqual('query_then_fetch');
});
it('should set size', function() {
var body = angular.fromJson(parts[1]);
expect(body.size).to.be(500);
expect(body.size).toBe(500);
});
});
describe('When getting fields', function() {
beforeEach(function() {
beforeEach(() => {
createDatasource({ url: 'http://es.com', index: 'metricbeat' });
ctx.backendSrv.datasourceRequest = function(options) {
return ctx.$q.when({
ctx.backendSrv.datasourceRequest = jest.fn(options => {
return Promise.resolve({
data: {
metricbeat: {
mappings: {
@@ -190,7 +200,7 @@ describe('ElasticDatasource', function() {
},
},
});
};
});
});
it('should return nested fields', function() {
@@ -201,7 +211,7 @@ describe('ElasticDatasource', function() {
})
.then(fieldObjects => {
var fields = _.map(fieldObjects, 'text');
expect(fields).to.eql([
expect(fields).toEqual([
'@timestamp',
'beat.name.raw',
'beat.name',
@@ -212,7 +222,6 @@ describe('ElasticDatasource', function() {
'system.process.name',
]);
});
ctx.$rootScope.$apply();
});
it('should return fields related to query type', function() {
@@ -224,7 +233,7 @@ describe('ElasticDatasource', function() {
})
.then(fieldObjects => {
var fields = _.map(fieldObjects, 'text');
expect(fields).to.eql(['system.cpu.system', 'system.cpu.user', 'system.process.cpu.total']);
expect(fields).toEqual(['system.cpu.system', 'system.cpu.user', 'system.process.cpu.total']);
});
ctx.ds
@@ -235,10 +244,8 @@ describe('ElasticDatasource', function() {
})
.then(fieldObjects => {
var fields = _.map(fieldObjects, 'text');
expect(fields).to.eql(['@timestamp']);
expect(fields).toEqual(['@timestamp']);
});
ctx.$rootScope.$apply();
});
});
@@ -252,10 +259,10 @@ describe('ElasticDatasource', function() {
jsonData: { esVersion: '5' },
});
ctx.backendSrv.datasourceRequest = function(options) {
ctx.backendSrv.datasourceRequest = jest.fn(options => {
requestOptions = options;
return ctx.$q.when({ data: { responses: [] } });
};
return Promise.resolve({ data: { responses: [] } });
});
ctx.ds.query({
range: {
@@ -271,34 +278,33 @@ describe('ElasticDatasource', function() {
],
});
ctx.$rootScope.$apply();
parts = requestOptions.data.split('\n');
header = angular.fromJson(parts[0]);
});
it('should not set search type to count', function() {
expect(header.search_type).to.not.eql('count');
expect(header.search_type).not.toEqual('count');
});
it('should set size to 0', function() {
var body = angular.fromJson(parts[1]);
expect(body.size).to.be(0);
expect(body.size).toBe(0);
});
});
describe('When issuing metricFind query on es5.x', function() {
var requestOptions, parts, header, body, results;
beforeEach(function() {
beforeEach(() => {
createDatasource({
url: 'http://es.com',
index: 'test',
jsonData: { esVersion: '5' },
});
ctx.backendSrv.datasourceRequest = function(options) {
ctx.backendSrv.datasourceRequest = jest.fn(options => {
requestOptions = options;
return ctx.$q.when({
return Promise.resolve({
data: {
responses: [
{
@@ -318,38 +324,36 @@ describe('ElasticDatasource', function() {
],
},
});
};
});
ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then(res => {
results = res;
});
ctx.$rootScope.$apply();
parts = requestOptions.data.split('\n');
header = angular.fromJson(parts[0]);
body = angular.fromJson(parts[1]);
});
it('should get results', function() {
expect(results.length).to.eql(2);
it('should get results', () => {
expect(results.length).toEqual(2);
});
it('should use key or key_as_string', function() {
expect(results[0].text).to.eql('test');
expect(results[1].text).to.eql('test2_as_string');
it('should use key or key_as_string', () => {
expect(results[0].text).toEqual('test');
expect(results[1].text).toEqual('test2_as_string');
});
it('should not set search type to count', function() {
expect(header.search_type).to.not.eql('count');
it('should not set search type to count', () => {
expect(header.search_type).not.toEqual('count');
});
it('should set size to 0', function() {
expect(body.size).to.be(0);
it('should set size to 0', () => {
expect(body.size).toBe(0);
});
it('should not set terms aggregation size to 0', function() {
expect(body['aggs']['1']['terms'].size).to.not.be(0);
it('should not set terms aggregation size to 0', () => {
expect(body['aggs']['1']['terms'].size).not.toBe(0);
});
});
});
+6 -6
View File
@@ -196,12 +196,12 @@ export function TemplateSrvStub() {
}
var allDeps = {
ContextSrvStub: ContextSrvStub,
TemplateSrvStub: TemplateSrvStub,
TimeSrvStub: TimeSrvStub,
ControllerTestContext: ControllerTestContext,
ServiceTestContext: ServiceTestContext,
DashboardViewStateStub: DashboardViewStateStub,
ContextSrvStub,
TemplateSrvStub,
TimeSrvStub,
ControllerTestContext,
ServiceTestContext,
DashboardViewStateStub,
};
// for legacy