Files
grafana/pkg/tsdb/elasticsearch/data_query_validator.go
T
Andrew Hackmann 956ab05148 Elasticsearch: Raw query editor for DSL (#114066)
* init

* it works! but what a mess

* nil ptr bug

* split up client.go

* split up search_request.go

* split up data_query.go

* split up response_parser

* fix merge

* update handling request

* raw dsl agg parser

* change rawQuery to rawDSLQuery

* agg parser works but needs work

* clean up agg parser

* fix bugs with raw dsl parsers

* feature toggle

* fix tests

* editor type selector

* editor type added

* add fix builder vs code by not using same query field

* clean up

* fix lint

* pretty

* editor type selection should be behind ft

* adam's feedback

* prettier
2025-12-15 19:11:05 +00:00

41 lines
1.3 KiB
Go

package elasticsearch
import (
"fmt"
)
// isQueryWithError validates the query and returns an error if invalid
func isQueryWithError(query *Query) error {
// Skip validation for raw DSL queries because no easy way to see it is valid without just running it
if query.EditorType != nil && *query.EditorType == "code" && query.RawDSLQuery != "" {
return nil
}
if len(query.BucketAggs) == 0 {
// If no aggregations, only document and logs queries are valid
if len(query.Metrics) == 0 || (!isLogsQuery(query) && !isDocumentQuery(query)) {
return fmt.Errorf("invalid query, missing metrics and aggregations")
}
}
return nil
}
// isLogsQuery checks if the query is a logs query
func isLogsQuery(query *Query) bool {
return len(query.Metrics) > 0 && query.Metrics[0].Type == logsType
}
// isDocumentQuery checks if the query is a document query (raw_data or raw_document)
func isDocumentQuery(query *Query) bool {
return isRawDataQuery(query) || isRawDocumentQuery(query)
}
// isRawDataQuery checks if the query is a raw_data query
func isRawDataQuery(query *Query) bool {
return len(query.Metrics) > 0 && query.Metrics[0].Type == rawDataType
}
// isRawDocumentQuery checks if the query is a raw_document query
func isRawDocumentQuery(query *Query) bool {
return len(query.Metrics) > 0 && query.Metrics[0].Type == rawDocumentType
}