Commit f1cb8f2f by Torkel Ödegaard

Merge branch 'keep_es_version' of https://github.com/replay/grafana into replay-keep_es_version

parents 7ec2c092 a30ceefa
...@@ -13,7 +13,7 @@ function (angular, _, config) { ...@@ -13,7 +13,7 @@ function (angular, _, config) {
$scope.httpConfigPartialSrc = 'app/features/org/partials/datasourceHttpConfig.html'; $scope.httpConfigPartialSrc = 'app/features/org/partials/datasourceHttpConfig.html';
var defaults = {name: '', type: 'graphite', url: '', access: 'proxy' }; var defaults = {name: '', type: 'graphite', url: '', access: 'proxy', jsonData: {'elasticsearchVersion': 2} };
$scope.indexPatternTypes = [ $scope.indexPatternTypes = [
{name: 'No pattern', value: undefined}, {name: 'No pattern', value: undefined},
...@@ -24,6 +24,12 @@ function (angular, _, config) { ...@@ -24,6 +24,12 @@ function (angular, _, config) {
{name: 'Yearly', value: 'Yearly', example: '[logstash-]YYYY'}, {name: 'Yearly', value: 'Yearly', example: '[logstash-]YYYY'},
]; ];
$scope.elasticsearchVersions = [
{name: '0.x', value: 0},
{name: '1.x', value: 1},
{name: '2.x', value: 2},
];
$scope.init = function() { $scope.init = function() {
$scope.isNew = true; $scope.isNew = true;
$scope.datasources = []; $scope.datasources = [];
......
...@@ -23,9 +23,11 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes ...@@ -23,9 +23,11 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes
this.name = datasource.name; this.name = datasource.name;
this.index = datasource.index; this.index = datasource.index;
this.timeField = datasource.jsonData.timeField; this.timeField = datasource.jsonData.timeField;
this.elasticsearchVersion = datasource.jsonData.elasticsearchVersion;
this.indexPattern = new IndexPattern(datasource.index, datasource.jsonData.interval); this.indexPattern = new IndexPattern(datasource.index, datasource.jsonData.interval);
this.queryBuilder = new ElasticQueryBuilder({ this.queryBuilder = new ElasticQueryBuilder({
timeField: this.timeField timeField: this.timeField,
elasticsearchVersion: this.elasticsearchVersion
}); });
} }
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
</ul> </ul>
<div class="clearfix"></div> <div class="clearfix"></div>
</div> </div>
<div class="tight-form last"> <div class="tight-form">
<ul class="tight-form-list"> <ul class="tight-form-list">
<li class="tight-form-item" style="width: 144px"> <li class="tight-form-item" style="width: 144px">
Time field name Time field name
...@@ -31,3 +31,14 @@ ...@@ -31,3 +31,14 @@
</ul> </ul>
<div class="clearfix"></div> <div class="clearfix"></div>
</div> </div>
<div class="tight-form last">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 144px">
Elasticsearch version
</li>
<li>
<select class="input-medium tight-form-input" ng-model="current.jsonData.elasticsearchVersion" ng-options="f.value as f.name for f in elasticsearchVersions"></select>
</li>
</ul>
<div class="clearfix"></div>
</div>
...@@ -5,11 +5,18 @@ function () { ...@@ -5,11 +5,18 @@ function () {
function ElasticQueryBuilder(options) { function ElasticQueryBuilder(options) {
this.timeField = options.timeField; this.timeField = options.timeField;
this.elasticsearchVersion = options.elasticsearchVersion;
} }
ElasticQueryBuilder.prototype.getRangeFilter = function() { ElasticQueryBuilder.prototype.getRangeFilter = function() {
var filter = {}; var filter = {};
filter[this.timeField] = {"gte": "$timeFrom", "lte": "$timeTo"}; filter[this.timeField] = {"gte": "$timeFrom", "lte": "$timeTo"};
// elastic search versions 2.x require the time format to be specified
if (this.elasticsearchVersion >= 2) {
filter[this.timeField]["format"] = "epoch_millis";
}
return filter; return filter;
}; };
...@@ -130,6 +137,10 @@ function () { ...@@ -130,6 +137,10 @@ function () {
"min_doc_count": 0, "min_doc_count": 0,
"extended_bounds": { "min": "$timeFrom", "max": "$timeTo" } "extended_bounds": { "min": "$timeFrom", "max": "$timeTo" }
}; };
// elastic search versions 2.x require the time format to be specified
if (this.elasticsearchVersion >= 2) {
esAgg["date_histogram"]["format"] = "epoch_millis";
}
break; break;
} }
case 'filters': { case 'filters': {
......
...@@ -36,6 +36,39 @@ describe('ElasticQueryBuilder', function() { ...@@ -36,6 +36,39 @@ describe('ElasticQueryBuilder', function() {
expect(query.aggs["2"].aggs["3"].date_histogram.field).to.be("@timestamp"); expect(query.aggs["2"].aggs["3"].date_histogram.field).to.be("@timestamp");
}); });
it('with es1.x and es2.x date histogram queries check time format', function() {
var builder_2x = new ElasticQueryBuilder({
timeField: '@timestamp',
elasticsearchVersion: 2
});
var query_params = {
metrics: [],
bucketAggs: [
{type: 'date_histogram', field: '@timestamp', id: '1'}
],
};
// format should not be specified in 1.x queries
expect("format" in builder.build(query_params)["aggs"]["1"]["date_histogram"]).to.be(false);
// 2.x query should specify format to be "epoch_millis"
expect(builder_2x.build(query_params)["aggs"]["1"]["date_histogram"]["format"]).to.be("epoch_millis");
});
it('with es1.x and es2.x range filter check time format', function() {
var builder_2x = new ElasticQueryBuilder({
timeField: '@timestamp',
elasticsearchVersion: 2
});
// format should not be specified in 1.x queries
expect("format" in builder.getRangeFilter()["@timestamp"]).to.be(false);
// 2.x query should specify format to be "epoch_millis"
expect(builder_2x.getRangeFilter()["@timestamp"]["format"]).to.be("epoch_millis");
});
it('with select field', function() { it('with select field', function() {
var query = builder.build({ var query = builder.build({
metrics: [{type: 'avg', field: '@value', id: '1'}], metrics: [{type: 'avg', field: '@value', id: '1'}],
......
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