2017-12-19 09:06:54 -06:00
|
|
|
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";
|
|
|
|
|
|
|
|
describe("ElasticDatasource", function() {
|
2015-09-28 09:28:19 -05:00
|
|
|
var ctx = new helpers.ServiceTestContext();
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
beforeEach(angularMocks.module("grafana.core"));
|
|
|
|
beforeEach(angularMocks.module("grafana.services"));
|
|
|
|
beforeEach(ctx.providePhase(["templateSrv", "backendSrv", "timeSrv"]));
|
2016-03-28 13:44:32 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
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("");
|
|
|
|
})
|
|
|
|
);
|
2016-01-09 06:21:16 -06:00
|
|
|
|
|
|
|
function createDatasource(instanceSettings) {
|
|
|
|
instanceSettings.jsonData = instanceSettings.jsonData || {};
|
2017-12-19 09:06:54 -06:00
|
|
|
ctx.ds = ctx.$injector.instantiate(ElasticDatasource, {
|
|
|
|
instanceSettings: instanceSettings
|
|
|
|
});
|
2016-01-09 06:21:16 -06:00
|
|
|
}
|
2015-09-28 09:28:19 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("When testing datasource with index pattern", function() {
|
2015-09-28 09:28:19 -05:00
|
|
|
beforeEach(function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({
|
|
|
|
url: "http://es.com",
|
|
|
|
index: "[asd-]YYYY.MM.DD",
|
|
|
|
jsonData: { interval: "Daily", esVersion: "2" }
|
|
|
|
});
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should translate index pattern to current day", function() {
|
2015-09-28 09:28:19 -05:00
|
|
|
var requestOptions;
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
2017-12-19 09:06:54 -06:00
|
|
|
return ctx.$q.when({ data: {} });
|
2015-09-28 09:28:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
ctx.ds.testDatasource();
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
|
2015-10-22 15:58:31 -05:00
|
|
|
var today = moment.utc().format("YYYY.MM.DD");
|
2017-12-19 09:06:54 -06:00
|
|
|
expect(requestOptions.url).to.be(
|
|
|
|
"http://es.com/asd-" + today + "/_mapping"
|
|
|
|
);
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("When issueing metric query with interval pattern", function() {
|
2015-10-22 15:23:21 -05:00
|
|
|
var requestOptions, parts, header;
|
|
|
|
|
2015-09-28 09:28:19 -05:00
|
|
|
beforeEach(function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({
|
|
|
|
url: "http://es.com",
|
|
|
|
index: "[asd-]YYYY.MM.DD",
|
|
|
|
jsonData: { interval: "Daily", esVersion: "2" }
|
|
|
|
});
|
2015-09-28 09:28:19 -05:00
|
|
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
2017-12-19 09:06:54 -06:00
|
|
|
return ctx.$q.when({ data: { responses: [] } });
|
2015-09-28 09:28:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
ctx.ds.query({
|
|
|
|
range: {
|
2016-08-07 05:01:53 -05:00
|
|
|
from: moment.utc([2015, 4, 30, 10]),
|
|
|
|
to: moment.utc([2015, 5, 1, 10])
|
2015-09-28 09:28:19 -05:00
|
|
|
},
|
2017-12-19 09:06:54 -06:00
|
|
|
targets: [
|
|
|
|
{
|
|
|
|
bucketAggs: [],
|
|
|
|
metrics: [{ type: "raw_document" }],
|
|
|
|
query: "escape\\:test"
|
|
|
|
}
|
|
|
|
]
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
2015-10-22 15:23:21 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
parts = requestOptions.data.split("\n");
|
2015-10-22 15:23:21 -05:00
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
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"
|
|
|
|
]);
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
2015-10-22 15:23:21 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should json escape lucene query", function() {
|
2015-10-22 15:23:21 -05:00
|
|
|
var body = angular.fromJson(parts[1]);
|
2017-12-19 09:06:54 -06:00
|
|
|
expect(body.query.bool.filter[1].query_string.query).to.be(
|
|
|
|
"escape\\:test"
|
|
|
|
);
|
2015-10-22 15:23:21 -05:00
|
|
|
});
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|
2015-11-05 01:36:51 -06:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("When issueing document query", function() {
|
2015-11-05 01:36:51 -06:00
|
|
|
var requestOptions, parts, header;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({
|
|
|
|
url: "http://es.com",
|
|
|
|
index: "test",
|
|
|
|
jsonData: { esVersion: "2" }
|
|
|
|
});
|
2015-11-05 01:36:51 -06:00
|
|
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
2017-12-19 09:06:54 -06:00
|
|
|
return ctx.$q.when({ data: { responses: [] } });
|
2015-11-05 01:36:51 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
ctx.ds.query({
|
2017-12-19 09:06:54 -06:00
|
|
|
range: {
|
|
|
|
from: moment([2015, 4, 30, 10]),
|
|
|
|
to: moment([2015, 5, 1, 10])
|
|
|
|
},
|
|
|
|
targets: [
|
|
|
|
{
|
|
|
|
bucketAggs: [],
|
|
|
|
metrics: [{ type: "raw_document" }],
|
|
|
|
query: "test"
|
|
|
|
}
|
|
|
|
]
|
2015-11-05 01:36:51 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
2017-12-19 09:06:54 -06:00
|
|
|
parts = requestOptions.data.split("\n");
|
2015-11-05 01:36:51 -06:00
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should set search type to query_then_fetch", function() {
|
|
|
|
expect(header.search_type).to.eql("query_then_fetch");
|
2015-11-05 01:36:51 -06:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should set size", function() {
|
2015-11-05 01:36:51 -06:00
|
|
|
var body = angular.fromJson(parts[1]);
|
2015-11-05 02:56:19 -06:00
|
|
|
expect(body.size).to.be(500);
|
2015-11-05 01:36:51 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("When getting fields", function() {
|
2016-09-15 00:30:08 -05:00
|
|
|
beforeEach(function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({ url: "http://es.com", index: "metricbeat" });
|
2016-09-15 00:30:08 -05:00
|
|
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
2017-12-19 09:06:54 -06:00
|
|
|
return ctx.$q.when({
|
|
|
|
data: {
|
|
|
|
metricbeat: {
|
|
|
|
mappings: {
|
|
|
|
metricsets: {
|
|
|
|
_all: {},
|
|
|
|
properties: {
|
|
|
|
"@timestamp": { type: "date" },
|
|
|
|
beat: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
fields: { raw: { type: "keyword" } },
|
|
|
|
type: "string"
|
|
|
|
},
|
|
|
|
hostname: { type: "string" }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
system: {
|
|
|
|
properties: {
|
|
|
|
cpu: {
|
|
|
|
properties: {
|
|
|
|
system: { type: "float" },
|
|
|
|
user: { type: "float" }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
process: {
|
|
|
|
properties: {
|
|
|
|
cpu: {
|
|
|
|
properties: {
|
|
|
|
total: { type: "float" }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
name: { type: "string" }
|
|
|
|
}
|
2016-09-15 00:30:08 -05:00
|
|
|
}
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
2016-09-15 00:30:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-09-15 00:30:08 -05:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should return nested fields", function() {
|
|
|
|
ctx.ds
|
|
|
|
.getFields({
|
|
|
|
find: "fields",
|
|
|
|
query: "*"
|
|
|
|
})
|
|
|
|
.then(fieldObjects => {
|
|
|
|
var fields = _.map(fieldObjects, "text");
|
|
|
|
expect(fields).to.eql([
|
|
|
|
"@timestamp",
|
|
|
|
"beat.name.raw",
|
|
|
|
"beat.name",
|
|
|
|
"beat.hostname",
|
|
|
|
"system.cpu.system",
|
|
|
|
"system.cpu.user",
|
|
|
|
"system.process.cpu.total",
|
|
|
|
"system.process.name"
|
|
|
|
]);
|
|
|
|
});
|
2016-09-15 00:30:08 -05:00
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should return fields related to query type", function() {
|
|
|
|
ctx.ds
|
|
|
|
.getFields({
|
|
|
|
find: "fields",
|
|
|
|
query: "*",
|
|
|
|
type: "number"
|
|
|
|
})
|
|
|
|
.then(fieldObjects => {
|
|
|
|
var fields = _.map(fieldObjects, "text");
|
|
|
|
expect(fields).to.eql([
|
|
|
|
"system.cpu.system",
|
|
|
|
"system.cpu.user",
|
|
|
|
"system.process.cpu.total"
|
|
|
|
]);
|
|
|
|
});
|
2016-09-15 00:30:08 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
ctx.ds
|
|
|
|
.getFields({
|
|
|
|
find: "fields",
|
|
|
|
query: "*",
|
|
|
|
type: "date"
|
|
|
|
})
|
|
|
|
.then(fieldObjects => {
|
|
|
|
var fields = _.map(fieldObjects, "text");
|
|
|
|
expect(fields).to.eql(["@timestamp"]);
|
|
|
|
});
|
2016-09-15 00:30:08 -05:00
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
});
|
|
|
|
});
|
2016-09-19 13:10:45 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("When issuing aggregation query on es5.x", function() {
|
2016-06-19 17:40:16 -05:00
|
|
|
var requestOptions, parts, header;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({
|
|
|
|
url: "http://es.com",
|
|
|
|
index: "test",
|
|
|
|
jsonData: { esVersion: "5" }
|
|
|
|
});
|
2016-06-19 17:40:16 -05:00
|
|
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
2017-12-19 09:06:54 -06:00
|
|
|
return ctx.$q.when({ data: { responses: [] } });
|
2016-06-19 17:40:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
ctx.ds.query({
|
2017-12-19 09:06:54 -06:00
|
|
|
range: {
|
|
|
|
from: moment([2015, 4, 30, 10]),
|
|
|
|
to: moment([2015, 5, 1, 10])
|
|
|
|
},
|
|
|
|
targets: [
|
|
|
|
{
|
2016-06-19 17:40:16 -05:00
|
|
|
bucketAggs: [
|
2017-12-19 09:06:54 -06:00
|
|
|
{ type: "date_histogram", field: "@timestamp", id: "2" }
|
2016-06-19 17:40:16 -05:00
|
|
|
],
|
2017-12-19 09:06:54 -06:00
|
|
|
metrics: [{ type: "count" }],
|
|
|
|
query: "test"
|
|
|
|
}
|
|
|
|
]
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.$rootScope.$apply();
|
2017-12-19 09:06:54 -06:00
|
|
|
parts = requestOptions.data.split("\n");
|
2016-06-19 17:40:16 -05:00
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should not set search type to count", function() {
|
|
|
|
expect(header.search_type).to.not.eql("count");
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should set size to 0", function() {
|
2016-06-19 17:40:16 -05:00
|
|
|
var body = angular.fromJson(parts[1]);
|
|
|
|
expect(body.size).to.be(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
describe("When issuing metricFind query on es5.x", function() {
|
2017-07-12 00:43:12 -05:00
|
|
|
var requestOptions, parts, header, body, results;
|
2016-06-19 17:40:16 -05:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
createDatasource({
|
|
|
|
url: "http://es.com",
|
|
|
|
index: "test",
|
|
|
|
jsonData: { esVersion: "5" }
|
|
|
|
});
|
2016-06-19 17:40:16 -05:00
|
|
|
|
|
|
|
ctx.backendSrv.datasourceRequest = function(options) {
|
|
|
|
requestOptions = options;
|
|
|
|
return ctx.$q.when({
|
2017-07-12 00:43:12 -05:00
|
|
|
data: {
|
|
|
|
responses: [
|
|
|
|
{
|
|
|
|
aggregations: {
|
|
|
|
"1": {
|
|
|
|
buckets: [
|
2017-12-19 09:06:54 -06:00
|
|
|
{ doc_count: 1, key: "test" },
|
|
|
|
{
|
|
|
|
doc_count: 2,
|
|
|
|
key: "test2",
|
|
|
|
key_as_string: "test2_as_string"
|
|
|
|
}
|
2017-07-12 00:43:12 -05:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-07-12 00:43:12 -05:00
|
|
|
ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then(res => {
|
|
|
|
results = res;
|
|
|
|
});
|
|
|
|
|
2016-06-19 17:40:16 -05:00
|
|
|
ctx.$rootScope.$apply();
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
parts = requestOptions.data.split("\n");
|
2016-06-19 17:40:16 -05:00
|
|
|
header = angular.fromJson(parts[0]);
|
2016-07-26 15:05:49 -05:00
|
|
|
body = angular.fromJson(parts[1]);
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should get results", function() {
|
2017-07-12 00:43:12 -05:00
|
|
|
expect(results.length).to.eql(2);
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
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");
|
2017-07-12 00:43:12 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should not set search type to count", function() {
|
|
|
|
expect(header.search_type).to.not.eql("count");
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should set size to 0", function() {
|
2016-06-19 17:40:16 -05:00
|
|
|
expect(body.size).to.be(0);
|
|
|
|
});
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
it("should not set terms aggregation size to 0", function() {
|
|
|
|
expect(body["aggs"]["1"]["terms"].size).to.not.be(0);
|
2016-10-02 09:59:25 -05:00
|
|
|
});
|
2016-06-19 17:40:16 -05:00
|
|
|
});
|
2015-09-28 09:28:19 -05:00
|
|
|
});
|