Elasticsearch: Add time zone setting to Date Histogram aggregation (#40882)

* TimeZonePicker: Allow specifying internal timezones to display

* Elasticsearch: Add time zone setting to Date Histogram aggregation

* rename time_zone to timeZone

* Add tests

* fix typo

* Update packages/grafana-data/src/datetime/timezones.ts

Co-authored-by: Chris Cowan <chris@chriscowan.us>

Co-authored-by: Chris Cowan <chris@chriscowan.us>
This commit is contained in:
Giordano Ricci
2021-11-02 16:18:39 +00:00
committed by GitHub
co-authored by Chris Cowan
parent de83e5702c
commit 7b7c193551
10 changed files with 146 additions and 28 deletions
+1
View File
@@ -244,6 +244,7 @@ type DateHistogramAgg struct {
ExtendedBounds *ExtendedBounds `json:"extended_bounds"`
Format string `json:"format"`
Offset string `json:"offset,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
}
// FiltersAggregation represents a filters aggregation
@@ -264,6 +264,12 @@ func addDateHistogramAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg, timeFro
a.Missing = &missing
}
if timezone, err := bucketAgg.Settings.Get("timeZone").String(); err == nil {
if timezone != "utc" {
a.TimeZone = timezone
}
}
aggBuilder = b
})
@@ -402,6 +402,52 @@ func TestExecuteTimeSeriesQuery(t *testing.T) {
require.Equal(t, hAgg.Field, "@timestamp")
require.Equal(t, hAgg.Interval, "$__interval")
require.Equal(t, hAgg.MinDocCount, 2)
t.Run("Should not include time_zone when timeZone is utc", func(t *testing.T) {
c := newFakeClient("7.0.0")
_, err := executeTsdbQuery(c, `{
"timeField": "@timestamp",
"bucketAggs": [
{
"id": "2",
"type": "date_histogram",
"field": "@timestamp",
"settings": {
"timeZone": "utc"
}
}
],
"metrics": [{"type": "count", "id": "1" }]
}`, from, to, 15*time.Second)
require.NoError(t, err)
sr := c.multisearchRequests[0].Requests[0]
dateHistogram := sr.Aggs[0].Aggregation.Aggregation.(*es.DateHistogramAgg)
require.Empty(t, dateHistogram.TimeZone)
})
t.Run("Should include time_zone when timeZone is not utc", func(t *testing.T) {
c := newFakeClient("7.0.0")
_, err := executeTsdbQuery(c, `{
"timeField": "@timestamp",
"bucketAggs": [
{
"id": "2",
"type": "date_histogram",
"field": "@timestamp",
"settings": {
"timeZone": "America/Los_Angeles"
}
}
],
"metrics": [{"type": "count", "id": "1" }]
}`, from, to, 15*time.Second)
require.NoError(t, err)
sr := c.multisearchRequests[0].Requests[0]
deteHistogram := sr.Aggs[0].Aggregation.Aggregation.(*es.DateHistogramAgg)
require.Equal(t, deteHistogram.TimeZone, "America/Los_Angeles")
})
})
t.Run("With histogram agg", func(t *testing.T) {
@@ -1163,7 +1209,7 @@ func TestTimeSeriesQueryParser(t *testing.T) {
"timeField": "@timestamp",
"query": "@metric:cpu",
"alias": "{{@hostname}} {{metric}}",
"interval": "10m",
"interval": "10m",
"metrics": [
{
"field": "@value",