Prechádzať zdrojové kódy

began work on support index time patterns

Torkel Ödegaard 10 rokov pred
rodič
commit
14cb2b0143

+ 1 - 3
public/app/features/org/partials/datasourceHttpConfig.html

@@ -23,9 +23,7 @@
 			Basic Auth
 		</li>
 		<li class="tight-form-item">
-			Enable&nbsp;
-			<input class="cr1" id="current.basicAuth" type="checkbox" ng-model="current.basicAuth" ng-checked="current.basicAuth">
-			<label for="current.basicAuth" class="cr1"></label>
+			<editor-checkbox text="Enable" model="current.basicAuth"></editor-checkbox>
 		</li>
 		<li class="tight-form-item" ng-if="current.basicAuth">
 			User

+ 11 - 10
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" };
+        }
       });
     };
 

+ 28 - 0
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;
+})

+ 21 - 5
public/app/plugins/datasource/elasticsearch/partials/config.html

@@ -1,18 +1,34 @@
 <div ng-include="httpConfigPartialSrc"></div>
-
 <br>
+
 <h5>Elastic search details</h5>
 
-<div class="tight-form last">
+<div class="tight-form">
 	<ul class="tight-form-list">
-		<li class="tight-form-item" style="width: 80px">
+		<li class="tight-form-item" style="width: 144px">
 			Index name
 		</li>
 		<li>
 			<input type="text" class="tight-form-input input-xlarge" ng-model='current.database' placeholder="" required></input>
 		</li>
+		<li class="tight-form-item">
+			Pattern
+		</li>
+		<li>
+			<select class="input-medium tight-form-input" ng-model="current.jsonData.interval"
+				ng-options="f.value as f.name for f in [{name: 'No pattern', value: undefined}, {name: 'Hourly', value: 'hourly'}, {name: 'Daily', value: 'daily'}]" ></select>
+		</li>
+	</ul>
+	<div class="clearfix"></div>
+</div>
+<div class="tight-form last">
+	<ul class="tight-form-list">
+		<li class="tight-form-item" style="width: 144px">
+			Time field name
+		</li>
+		<li>
+			<input type="text" class="tight-form-input input-xlarge" ng-model='current.jsonData.timeField' placeholder="" required ng-init="current.jsonData.timeField = current.jsonData.timeField || '@timestamp'"></input>
+		</li>
 	</ul>
 	<div class="clearfix"></div>
 </div>
-
-

+ 1 - 1
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);

+ 35 - 0
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']);
+        });
+      })
+
+    });
+
+  });
+
+});

+ 29 - 3
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() {

+ 1 - 0
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) {