SQL Expressions / Alerting: Do not allow duplicates (#103394)
This commit is contained in:
@@ -3,6 +3,8 @@ package expr
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/errutil"
|
||||
)
|
||||
@@ -93,3 +95,31 @@ func makeUnexpectedNodeTypeError(refID, nodeType string) error {
|
||||
|
||||
return UnexpectedNodeTypeError.Build(data)
|
||||
}
|
||||
|
||||
var DuplicateStringColumnError = errutil.NewBase(
|
||||
errutil.StatusBadRequest, "sse.duplicateStringColumns").MustTemplate(
|
||||
"your SQL query returned {{ .Public.count }} rows with duplicate values across the string columns, which is not allowed for alerting. Examples: ({{ .Public.examples }}). Hint: use GROUP BY or aggregation (e.g. MAX(), AVG()) to return one row per unique combination.",
|
||||
errutil.WithPublic("SQL query returned duplicate combinations of string column values. Use GROUP BY or aggregation to return one row per combination."),
|
||||
)
|
||||
|
||||
func makeDuplicateStringColumnError(examples []string) error {
|
||||
const limit = 5
|
||||
sort.Strings(examples)
|
||||
exampleStr := strings.Join(truncateExamples(examples, limit), ", ")
|
||||
|
||||
return DuplicateStringColumnError.Build(errutil.TemplateData{
|
||||
Public: map[string]any{
|
||||
"examples": exampleStr,
|
||||
"count": len(examples),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func truncateExamples(examples []string, limit int) []string {
|
||||
if len(examples) <= limit {
|
||||
return examples
|
||||
}
|
||||
truncated := examples[:limit]
|
||||
truncated = append(truncated, fmt.Sprintf("... and %d more", len(examples)-limit))
|
||||
return truncated
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user