Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to timeField if no field is specified in date histogram aggregation #215

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions pkg/opensearch/lucene_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func (h *luceneHandler) processQuery(q *Query) error {
b := h.ms.Search(interval)
b.Size(0)
filters := b.Query().Bool().Filter()
filters.AddDateRangeFilter(h.client.GetTimeField(), es.DateFormatEpochMS, toMs, fromMs)
defaultTimeField := h.client.GetTimeField()
filters.AddDateRangeFilter(defaultTimeField, es.DateFormatEpochMS, toMs, fromMs)

if q.RawQuery != "" {
filters.AddQueryStringFilter(q.RawQuery, true)
Expand All @@ -59,9 +60,9 @@ func (h *luceneHandler) processQuery(q *Query) error {

switch {
case q.Metrics[0].Type == rawDataType:
processRawDataQuery(q, b, h.client.GetTimeField())
processRawDataQuery(q, b, defaultTimeField)
default:
processTimeSeriesQuery(q, b, fromMs, toMs)
processTimeSeriesQuery(q, b, fromMs, toMs, defaultTimeField)
}

return nil
Expand All @@ -83,7 +84,7 @@ func processRawDataQuery(q *Query, b *es.SearchRequestBuilder, defaultTimeField
b.Size(size)
}

func processTimeSeriesQuery(q *Query, b *es.SearchRequestBuilder, fromMs int64, toMs int64) {
func processTimeSeriesQuery(q *Query, b *es.SearchRequestBuilder, fromMs int64, toMs int64, defaultTimeField string) {
metric := q.Metrics[0]
b.Size(metric.Settings.Get("size").MustInt(500))
aggBuilder := b.Agg()
Expand All @@ -92,7 +93,7 @@ func processTimeSeriesQuery(q *Query, b *es.SearchRequestBuilder, fromMs int64,
for _, bucketAgg := range q.BucketAggs {
switch bucketAgg.Type {
case dateHistType:
aggBuilder = addDateHistogramAgg(aggBuilder, bucketAgg, fromMs, toMs)
aggBuilder = addDateHistogramAgg(aggBuilder, bucketAgg, fromMs, toMs, defaultTimeField)
case histogramType:
aggBuilder = addHistogramAgg(aggBuilder, bucketAgg)
case filtersType:
Expand Down Expand Up @@ -204,8 +205,13 @@ func (h *luceneHandler) executeQueries() (*backend.QueryDataResponse, error) {
return rp.getTimeSeries(h.client.GetTimeField())
}

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) {
func addDateHistogramAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg, timeFrom, timeTo int64, timeField string) es.AggBuilder {
// If no field is specified, use the time field
field := bucketAgg.Field
if field == "" {
field = timeField
}
aggBuilder.DateHistogram(bucketAgg.ID, field, func(a *es.DateHistogramAgg, b es.AggBuilder) {
a.Interval = bucketAgg.Settings.Get("interval").MustString("auto")
a.MinDocCount = bucketAgg.Settings.Get("min_doc_count").MustInt(0)
a.ExtendedBounds = &es.ExtendedBounds{Min: timeFrom, Max: timeTo}
Expand Down
36 changes: 36 additions & 0 deletions pkg/opensearch/time_series_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,3 +1031,39 @@ func TestTimeSeriesQueryParser(t *testing.T) {
})
})
}

func Test_Field_property(t *testing.T) {
from := time.Date(2018, 5, 15, 17, 50, 0, 0, time.UTC)
to := time.Date(2018, 5, 15, 17, 55, 0, 0, time.UTC)
t.Run("Should use timeField from datasource when not specified", func(t *testing.T) {
c := newFakeClient(es.Elasticsearch, "2.0.0")
_, err := executeTsdbQuery(c, `{
"timeField": "@timestamp",
"metrics": [{ "type": "count", "id": "1" }],
"bucketAggs": [
{ "type": "date_histogram", "id": "2", "settings": { "min_doc_count": "1" } }
]
}`, from, to, 15*time.Second)
assert.Nil(t, err)

sr := c.multisearchRequests[0].Requests[0]
dateHistogramAgg := sr.Aggs[0].Aggregation.Aggregation.(*es.DateHistogramAgg)
assert.Equal(t, "@timestamp", dateHistogramAgg.Field)
})

t.Run("Should use field from bucket agg when specified", func(t *testing.T) {
c := newFakeClient(es.Elasticsearch, "2.0.0")
_, err := executeTsdbQuery(c, `{
"timeField": "@timestamp",
"metrics": [{ "type": "count", "id": "1" }],
"bucketAggs": [
{ "type": "date_histogram", "id": "2", "field": "some_other_field", "settings": { "min_doc_count": "1" } }
]
}`, from, to, 15*time.Second)

assert.Nil(t, err)
sr := c.multisearchRequests[0].Requests[0]
dateHistogramAgg := sr.Aggs[0].Aggregation.Aggregation.(*es.DateHistogramAgg)
assert.Equal(t, "some_other_field", dateHistogramAgg.Field)
})
}
Loading