Commit 14cb2b01 by Torkel Ödegaard

began work on support index time patterns

parent b24c5392
......@@ -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
......
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" };
}
});
};
......
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;
})
<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>
......@@ -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);
......
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']);
});
})
});
});
});
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() {
......
......@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment