mirror of
https://github.com/grafana/grafana.git
synced 2025-09-22 12:13:15 +08:00
elasticsearch: always use fixed_interval (#50297)
This commit is contained in:
@ -145,7 +145,7 @@ func TestClient_ExecuteMultisearch(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
||||||
|
|
||||||
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString())
|
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "fixed_interval").MustString())
|
||||||
|
|
||||||
assert.Equal(t, 200, res.Status)
|
assert.Equal(t, 200, res.Status)
|
||||||
require.Len(t, res.Responses, 1)
|
require.Len(t, res.Responses, 1)
|
||||||
@ -197,7 +197,7 @@ func TestClient_ExecuteMultisearch(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
||||||
|
|
||||||
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString())
|
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "fixed_interval").MustString())
|
||||||
|
|
||||||
assert.Equal(t, 200, res.Status)
|
assert.Equal(t, 200, res.Status)
|
||||||
require.Len(t, res.Responses, 1)
|
require.Len(t, res.Responses, 1)
|
||||||
@ -252,7 +252,7 @@ func TestClient_ExecuteMultisearch(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
||||||
|
|
||||||
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString())
|
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "fixed_interval").MustString())
|
||||||
|
|
||||||
assert.Equal(t, 200, res.Status)
|
assert.Equal(t, 200, res.Status)
|
||||||
require.Len(t, res.Responses, 1)
|
require.Len(t, res.Responses, 1)
|
||||||
@ -308,7 +308,7 @@ func TestClient_ExecuteMultisearch(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
||||||
|
|
||||||
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString())
|
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "fixed_interval").MustString())
|
||||||
|
|
||||||
assert.Equal(t, 200, res.Status)
|
assert.Equal(t, 200, res.Status)
|
||||||
require.Len(t, res.Responses, 1)
|
require.Len(t, res.Responses, 1)
|
||||||
@ -321,7 +321,7 @@ func createMultisearchForTest(t *testing.T, c Client) (*MultiSearchRequest, erro
|
|||||||
msb := c.MultiSearch()
|
msb := c.MultiSearch()
|
||||||
s := msb.Search(intervalv2.Interval{Value: 15 * time.Second, Text: "15s"})
|
s := msb.Search(intervalv2.Interval{Value: 15 * time.Second, Text: "15s"})
|
||||||
s.Agg().DateHistogram("2", "@timestamp", func(a *DateHistogramAgg, ab AggBuilder) {
|
s.Agg().DateHistogram("2", "@timestamp", func(a *DateHistogramAgg, ab AggBuilder) {
|
||||||
a.Interval = "$__interval"
|
a.FixedInterval = "$__interval"
|
||||||
|
|
||||||
ab.Metric("1", "avg", "@hostname", func(a *MetricAggregation) {
|
ab.Metric("1", "avg", "@hostname", func(a *MetricAggregation) {
|
||||||
a.Settings["script"] = "$__interval_ms*@hostname"
|
a.Settings["script"] = "$__interval_ms*@hostname"
|
||||||
|
@ -238,7 +238,6 @@ type HistogramAgg struct {
|
|||||||
// DateHistogramAgg represents a date histogram aggregation
|
// DateHistogramAgg represents a date histogram aggregation
|
||||||
type DateHistogramAgg struct {
|
type DateHistogramAgg struct {
|
||||||
Field string `json:"field"`
|
Field string `json:"field"`
|
||||||
Interval string `json:"interval,omitempty"`
|
|
||||||
FixedInterval string `json:"fixed_interval,omitempty"`
|
FixedInterval string `json:"fixed_interval,omitempty"`
|
||||||
MinDocCount int `json:"min_doc_count"`
|
MinDocCount int `json:"min_doc_count"`
|
||||||
Missing *string `json:"missing,omitempty"`
|
Missing *string `json:"missing,omitempty"`
|
||||||
|
@ -342,11 +342,6 @@ func (b *aggBuilderImpl) DateHistogram(key, field string, fn func(a *DateHistogr
|
|||||||
fn(innerAgg, builder)
|
fn(innerAgg, builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.version.Major() >= 8 {
|
|
||||||
innerAgg.FixedInterval = innerAgg.Interval
|
|
||||||
innerAgg.Interval = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
b.aggDefs = append(b.aggDefs, aggDef)
|
b.aggDefs = append(b.aggDefs, aggDef)
|
||||||
|
|
||||||
return b
|
return b
|
||||||
|
@ -245,13 +245,13 @@ func (bucketAgg BucketAgg) generateSettingsForDSL() map[string]interface{} {
|
|||||||
|
|
||||||
func addDateHistogramAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg, timeFrom, timeTo int64) es.AggBuilder {
|
func addDateHistogramAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg, timeFrom, timeTo int64) es.AggBuilder {
|
||||||
aggBuilder.DateHistogram(bucketAgg.ID, bucketAgg.Field, func(a *es.DateHistogramAgg, b es.AggBuilder) {
|
aggBuilder.DateHistogram(bucketAgg.ID, bucketAgg.Field, func(a *es.DateHistogramAgg, b es.AggBuilder) {
|
||||||
a.Interval = bucketAgg.Settings.Get("interval").MustString("auto")
|
a.FixedInterval = bucketAgg.Settings.Get("interval").MustString("auto")
|
||||||
a.MinDocCount = bucketAgg.Settings.Get("min_doc_count").MustInt(0)
|
a.MinDocCount = bucketAgg.Settings.Get("min_doc_count").MustInt(0)
|
||||||
a.ExtendedBounds = &es.ExtendedBounds{Min: timeFrom, Max: timeTo}
|
a.ExtendedBounds = &es.ExtendedBounds{Min: timeFrom, Max: timeTo}
|
||||||
a.Format = bucketAgg.Settings.Get("format").MustString(es.DateFormatEpochMS)
|
a.Format = bucketAgg.Settings.Get("format").MustString(es.DateFormatEpochMS)
|
||||||
|
|
||||||
if a.Interval == "auto" {
|
if a.FixedInterval == "auto" {
|
||||||
a.Interval = "$__interval"
|
a.FixedInterval = "$__interval"
|
||||||
}
|
}
|
||||||
|
|
||||||
if offset, err := bucketAgg.Settings.Get("offset").String(); err == nil {
|
if offset, err := bucketAgg.Settings.Get("offset").String(); err == nil {
|
||||||
|
@ -399,7 +399,7 @@ func TestExecuteTimeSeriesQuery(t *testing.T) {
|
|||||||
require.Equal(t, firstLevel.Aggregation.Type, "date_histogram")
|
require.Equal(t, firstLevel.Aggregation.Type, "date_histogram")
|
||||||
hAgg := firstLevel.Aggregation.Aggregation.(*es.DateHistogramAgg)
|
hAgg := firstLevel.Aggregation.Aggregation.(*es.DateHistogramAgg)
|
||||||
require.Equal(t, hAgg.Field, "@timestamp")
|
require.Equal(t, hAgg.Field, "@timestamp")
|
||||||
require.Equal(t, hAgg.Interval, "$__interval")
|
require.Equal(t, hAgg.FixedInterval, "$__interval")
|
||||||
require.Equal(t, hAgg.MinDocCount, 2)
|
require.Equal(t, hAgg.MinDocCount, 2)
|
||||||
|
|
||||||
t.Run("Should not include time_zone when timeZone is utc", func(t *testing.T) {
|
t.Run("Should not include time_zone when timeZone is utc", func(t *testing.T) {
|
||||||
@ -1050,35 +1050,8 @@ func TestSettingsCasting(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("interval parameter", func(t *testing.T) {
|
t.Run("interval parameter", func(t *testing.T) {
|
||||||
t.Run("Uses interval with ES < 8.0.0", func(t *testing.T) {
|
t.Run("Uses fixed_interval", func(t *testing.T) {
|
||||||
c := newFakeClient("7.7.0")
|
c := newFakeClient("7.10.0")
|
||||||
_, err := executeTsdbQuery(c, `{
|
|
||||||
"timeField": "@timestamp",
|
|
||||||
"bucketAggs": [
|
|
||||||
{
|
|
||||||
"type": "date_histogram",
|
|
||||||
"field": "@timestamp",
|
|
||||||
"id": "2",
|
|
||||||
"settings": {
|
|
||||||
"interval": "1d"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"metrics": [
|
|
||||||
{ "id": "1", "type": "average", "field": "@value" }
|
|
||||||
]
|
|
||||||
}`, from, to, 15*time.Second)
|
|
||||||
assert.Nil(t, err)
|
|
||||||
sr := c.multisearchRequests[0].Requests[0]
|
|
||||||
|
|
||||||
dateHistogramAgg := sr.Aggs[0].Aggregation.Aggregation.(*es.DateHistogramAgg)
|
|
||||||
|
|
||||||
assert.Zero(t, dateHistogramAgg.FixedInterval)
|
|
||||||
assert.NotZero(t, dateHistogramAgg.Interval)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Uses fixed_interval with ES >= 8.0.0", func(t *testing.T) {
|
|
||||||
c := newFakeClient("8.0.0")
|
|
||||||
_, err := executeTsdbQuery(c, `{
|
_, err := executeTsdbQuery(c, `{
|
||||||
"timeField": "@timestamp",
|
"timeField": "@timestamp",
|
||||||
"bucketAggs": [
|
"bucketAggs": [
|
||||||
@ -1101,7 +1074,6 @@ func TestSettingsCasting(t *testing.T) {
|
|||||||
dateHistogramAgg := sr.Aggs[0].Aggregation.Aggregation.(*es.DateHistogramAgg)
|
dateHistogramAgg := sr.Aggs[0].Aggregation.Aggregation.(*es.DateHistogramAgg)
|
||||||
|
|
||||||
assert.NotZero(t, dateHistogramAgg.FixedInterval)
|
assert.NotZero(t, dateHistogramAgg.FixedInterval)
|
||||||
assert.Zero(t, dateHistogramAgg.Interval)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -112,13 +112,7 @@ export class ElasticQueryBuilder {
|
|||||||
|
|
||||||
const interval = settings.interval === 'auto' ? '$__interval' : settings.interval;
|
const interval = settings.interval === 'auto' ? '$__interval' : settings.interval;
|
||||||
|
|
||||||
if (gte(this.esVersion, '8.0.0')) {
|
esAgg.fixed_interval = interval;
|
||||||
// The deprecation was actually introduced in 7.0.0, we might want to use that instead of the removal date,
|
|
||||||
// but it woudl be a breaking change on our side.
|
|
||||||
esAgg.fixed_interval = interval;
|
|
||||||
} else {
|
|
||||||
esAgg.interval = interval;
|
|
||||||
}
|
|
||||||
|
|
||||||
return esAgg;
|
return esAgg;
|
||||||
}
|
}
|
||||||
|
@ -763,16 +763,12 @@ describe('ElasticQueryBuilder', () => {
|
|||||||
extended_bounds: { max: '$timeTo', min: '$timeFrom' },
|
extended_bounds: { max: '$timeTo', min: '$timeFrom' },
|
||||||
field: '@timestamp',
|
field: '@timestamp',
|
||||||
format: 'epoch_millis',
|
format: 'epoch_millis',
|
||||||
interval: '$__interval',
|
fixed_interval: '$__interval',
|
||||||
min_doc_count: 0,
|
min_doc_count: 0,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (gte(builder.esVersion, '8.0.0')) {
|
|
||||||
expectedAggs['1'].date_histogram.fixed_interval = expectedAggs['1'].date_histogram.interval;
|
|
||||||
delete expectedAggs['1'].date_histogram.interval;
|
|
||||||
}
|
|
||||||
expect(query.aggs).toMatchObject(expectedAggs);
|
expect(query.aggs).toMatchObject(expectedAggs);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -959,7 +955,7 @@ describe('ElasticQueryBuilder', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('interval parameter', () => {
|
describe('interval parameter', () => {
|
||||||
it('should use interval if Elasticsearch version <8.0.0', () => {
|
it('should use fixed_interval', () => {
|
||||||
const query = builder77.build({
|
const query = builder77.build({
|
||||||
refId: 'A',
|
refId: 'A',
|
||||||
metrics: [{ type: 'count', id: '1' }],
|
metrics: [{ type: 'count', id: '1' }],
|
||||||
@ -974,8 +970,7 @@ describe('ElasticQueryBuilder', () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(query.aggs['2'].date_histogram.interval).toBe('1d');
|
expect(query.aggs['2'].date_histogram.fixed_interval).toBe('1d');
|
||||||
expect(query.aggs['2'].date_histogram.fixed_interval).toBeUndefined();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user