Skip to content

Commit

Permalink
Default to timeField if no field is specified in date histogram aggre…
Browse files Browse the repository at this point in the history
  • Loading branch information
fridgepoet authored Jul 14, 2023
1 parent 6718c37 commit 52e60e4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
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)
})
}

0 comments on commit 52e60e4

Please sign in to comment.