From 14cb2b0143174e1a92a2835ed6f47fb894de915f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sun, 6 Sep 2015 12:58:53 +0200 Subject: [PATCH] began work on support index time patterns --- .../org/partials/datasourceHttpConfig.html | 4 +-- .../datasource/elasticsearch/datasource.js | 21 +++++------ .../datasource/elasticsearch/indexPattern.js | 28 +++++++++++++++ .../elasticsearch/partials/config.html | 26 +++++++++++--- .../datasource/elasticsearch/queryCtrl.js | 2 +- .../specs/elasticsearch-indexPattern-specs.js | 35 +++++++++++++++++++ public/test/specs/elasticsearch-specs.js | 32 +++++++++++++++-- public/test/test-main.js | 1 + 8 files changed, 127 insertions(+), 22 deletions(-) create mode 100644 public/app/plugins/datasource/elasticsearch/indexPattern.js create mode 100644 public/test/specs/elasticsearch-indexPattern-specs.js diff --git a/public/app/features/org/partials/datasourceHttpConfig.html b/public/app/features/org/partials/datasourceHttpConfig.html index 94c208d8370..87785519620 100644 --- a/public/app/features/org/partials/datasourceHttpConfig.html +++ b/public/app/features/org/partials/datasourceHttpConfig.html @@ -23,9 +23,7 @@ Basic Auth
  • - Enable  - - +
  • User diff --git a/public/app/plugins/datasource/elasticsearch/datasource.js b/public/app/plugins/datasource/elasticsearch/datasource.js index a134ea264fe..db9ec0f413e 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.js +++ b/public/app/plugins/datasource/elasticsearch/datasource.js @@ -1,14 +1,13 @@ define([ 'angular', 'lodash', - 'config', - 'kbn', 'moment', './queryBuilder', + './indexPattern', './queryCtrl', './directives' ], -function (angular, _, config, kbn, moment, ElasticQueryBuilder) { +function (angular, _, moment, ElasticQueryBuilder, IndexPattern) { 'use strict'; var module = angular.module('grafana.services'); @@ -21,10 +20,7 @@ function (angular, _, config, kbn, moment, ElasticQueryBuilder) { this.url = datasource.url; this.name = datasource.name; this.index = datasource.index; - this.searchMaxResults = config.search.max_results || 20; - - this.saveTemp = _.isUndefined(datasource.save_temp) ? true : datasource.save_temp; - this.saveTempTTL = _.isUndefined(datasource.save_temp_ttl) ? '30d' : datasource.save_temp_ttl; + this.indexPattern = new IndexPattern(datasource.index, datasource.jsonData.interval) } ElasticDatasource.prototype._request = function(method, url, index, data) { @@ -45,7 +41,7 @@ function (angular, _, config, kbn, moment, ElasticQueryBuilder) { }; ElasticDatasource.prototype._get = function(url) { - return this._request('GET', url, this.index) + return this._request('GET', url, this.indexPattern.getIndexForToday()) .then(function(results) { return results.data; }); @@ -128,9 +124,14 @@ function (angular, _, config, kbn, moment, ElasticQueryBuilder) { }; ElasticDatasource.prototype.testDatasource = function() { - var query = JSON.stringify(); - return this._post('/_search?search_type=count', query).then(function() { + return this._get('/_stats').then(function() { return { status: "success", message: "Data source is working", title: "Success" }; + }, function(err) { + if (err.data && err.data.error) { + return { status: "error", message: err.data.error, title: "Error" }; + } else { + return { status: "error", message: err.status, title: "Error" }; + } }); }; diff --git a/public/app/plugins/datasource/elasticsearch/indexPattern.js b/public/app/plugins/datasource/elasticsearch/indexPattern.js new file mode 100644 index 00000000000..b1e24f62c97 --- /dev/null +++ b/public/app/plugins/datasource/elasticsearch/indexPattern.js @@ -0,0 +1,28 @@ +define([ + 'lodash', + 'moment', +], +function (_, moment) { + 'use strict'; + + function IndexPattern(pattern, interval) { + this.pattern = pattern; + this.interval = interval; + }; + + IndexPattern.prototype.getIndexForToday = function() { + if (this.interval) { + return moment().format(this.pattern); + } else { + return this.pattern; + } + }; + + + IndexPattern.prototype.getIndexList = function(from, to) { + + }; + + + return IndexPattern; +}) diff --git a/public/app/plugins/datasource/elasticsearch/partials/config.html b/public/app/plugins/datasource/elasticsearch/partials/config.html index d520053e13c..c38663a04af 100644 --- a/public/app/plugins/datasource/elasticsearch/partials/config.html +++ b/public/app/plugins/datasource/elasticsearch/partials/config.html @@ -1,18 +1,34 @@
    -
    +
    Elastic search details
    -
    +
      -
    • +
    • Index name
    • +
    • + Pattern +
    • +
    • + +
    • +
    +
    +
    +
    +
      +
    • + Time field name +
    • +
    • + +
    - - diff --git a/public/app/plugins/datasource/elasticsearch/queryCtrl.js b/public/app/plugins/datasource/elasticsearch/queryCtrl.js index 6d50534018a..cc15a5d12a4 100644 --- a/public/app/plugins/datasource/elasticsearch/queryCtrl.js +++ b/public/app/plugins/datasource/elasticsearch/queryCtrl.js @@ -16,7 +16,7 @@ function (angular, _, ElasticQueryBuilder) { target.timeField = target.timeField || '@timestamp'; target.metrics = target.metrics || [{ type: 'count', id: '1' }]; - target.bucketAggs = target.bucketAggs || [{ type: 'date_histogram', field: '@timestmap', id: '2'}]; + target.bucketAggs = target.bucketAggs || [{ type: 'date_histogram', field: '@timestamp', id: '2'}]; $scope.queryBuilder = new ElasticQueryBuilder(target); $scope.rawQueryOld = angular.toJson($scope.queryBuilder.build($scope.target), true); diff --git a/public/test/specs/elasticsearch-indexPattern-specs.js b/public/test/specs/elasticsearch-indexPattern-specs.js new file mode 100644 index 00000000000..c42eac4f64f --- /dev/null +++ b/public/test/specs/elasticsearch-indexPattern-specs.js @@ -0,0 +1,35 @@ +define([ + 'moment', + 'plugins/datasource/elasticsearch/indexPattern' +], function(moment, IndexPattern) { + 'use strict'; + + describe('IndexPattern', function() { + + describe('when getting index for today', function() { + it('should return correct index name', function() { + var pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'daily'); + var expected = 'asd-' + moment().format('YYYY.MM.DD'); + + expect(pattern.getIndexForToday()).to.be(expected); + }); + }); + + describe('when getting index list for time range', function() { + + describe('daily', function() { + + it('should return correct index list', function() { + var pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'daily'); + var from = new Date(2015, 4, 29); + var to = new Date(2015, 5, 1); + + expect(pattern.getIndexList(from, to)).to.be(['asd', 'asd2']); + }); + }) + + }); + + }); + +}); diff --git a/public/test/specs/elasticsearch-specs.js b/public/test/specs/elasticsearch-specs.js index 5c3c9049721..0d8f8645ce6 100644 --- a/public/test/specs/elasticsearch-specs.js +++ b/public/test/specs/elasticsearch-specs.js @@ -1,18 +1,44 @@ define([ 'helpers', + 'moment', 'plugins/datasource/elasticsearch/datasource', 'aws-sdk', -], function(helpers) { +], function(helpers, moment) { 'use strict'; describe('ElasticDatasource', function() { var ctx = new helpers.ServiceTestContext(); beforeEach(module('grafana.services')); - beforeEach(ctx.providePhase(['templateSrv'])); + beforeEach(ctx.providePhase(['templateSrv', 'backendSrv'])); beforeEach(ctx.createService('ElasticDatasource')); beforeEach(function() { - ctx.ds = new ctx.service({}); + ctx.ds = new ctx.service({jsonData: {}}); + }); + + describe('When testing datasource with index pattern', function() { + beforeEach(function(){ + ctx.ds = new ctx.service({ + url: 'http://es.com', + index: '[asd-]YYYY.MM.DD', + jsonData: { interval: 'daily' } + }); + }) + + it('should translate index pattern to current day', function() { + var requestOptions; + ctx.backendSrv.datasourceRequest = function(options) { + requestOptions = options; + return ctx.$q.when({}); + }; + + ctx.ds.testDatasource(); + ctx.$rootScope.$apply(); + + var today = moment().format("YYYY.MM.DD"); + expect(requestOptions.url).to.be("http://es.com/asd-" + today + '/_stats'); + }); + }); describe('When processing es response', function() { diff --git a/public/test/test-main.js b/public/test/test-main.js index b71a7a14c9e..ff5e64ab09b 100644 --- a/public/test/test-main.js +++ b/public/test/test-main.js @@ -155,6 +155,7 @@ require([ 'specs/elasticsearch-specs', 'specs/elasticsearch-querybuilder-specs', 'specs/elasticsearch-queryctrl-specs', + 'specs/elasticsearch-indexPattern-specs', ]; var pluginSpecs = (config.plugins.specs || []).map(function (spec) {