Alerting: Fix alert migration RefID generation (#35667)

* Alerting: Fix alert migration RefID generation

if the alert has more than 26 conditions

* Remove TODO comment

* Make similar change under pkg/expr
This commit is contained in:
Sofia Papagiannaki 2021-06-14 22:13:45 +03:00 committed by GitHub
parent 7f882eea05
commit c997f646cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/util"
)
// DashboardAlertConditions turns dashboard alerting conditions into server side expression queries and a
@ -279,10 +280,6 @@ const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// getNewRefID finds first capital letter in the alphabet not in use
// to use for a new RefID. It errors if it runs out of letters.
//
// TODO: Research if there is a limit. If so enforce is by
// number of queries not letters. If no limit generate more types
// of refIDs.
func getNewRefID(refIDs map[string][]int) (string, error) {
for _, r := range alpha {
sR := string(r)
@ -291,7 +288,14 @@ func getNewRefID(refIDs map[string][]int) (string, error) {
}
return sR, nil
}
return "", fmt.Errorf("ran out of letters when creating expression")
for i := 0; i < 20; i++ {
sR := util.GenerateShortUID()
if _, ok := refIDs[sR]; ok {
continue
}
return sR, nil
}
return "", fmt.Errorf("failed to generate unique RefID")
}
// getRelativeDuration turns the alerting durations for dashboard conditions

View File

@ -6,6 +6,8 @@ import (
"sort"
"strings"
"time"
"github.com/grafana/grafana/pkg/util"
)
func transConditions(set dashAlertSettings, orgID int64, dsUIDMap dsUIDLookup) (*condition, error) {
@ -211,10 +213,6 @@ const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// getNewRefID finds first capital letter in the alphabet not in use
// to use for a new RefID. It errors if it runs out of letters.
//
// TODO: Research if there is a limit. If so enforce is by
// number of queries not letters. If no limit generate more types
// of refIDs.
func getNewRefID(refIDs map[string][]int) (string, error) {
for _, r := range alpha {
sR := string(r)
@ -223,7 +221,14 @@ func getNewRefID(refIDs map[string][]int) (string, error) {
}
return sR, nil
}
return "", fmt.Errorf("ran out of letters when creating expression")
for i := 0; i < 20; i++ {
sR := util.GenerateShortUID()
if _, ok := refIDs[sR]; ok {
continue
}
return sR, nil
}
return "", fmt.Errorf("failed to generate unique RefID")
}
// getRelativeDuration turns the alerting durations for dashboard conditions