mirror of
https://github.com/prymitive/karma
synced 2026-05-05 03:16:51 +00:00
fix(backend): reduce allocations in autocomplete
This commit is contained in:
committed by
Łukasz Mierzwa
parent
c9c9f079ae
commit
e5de49ee04
@@ -271,7 +271,7 @@ func (am *Alertmanager) pullAlerts(version string, si *intern.Interner) error {
|
||||
|
||||
dedupedGroups := make([]models.AlertGroup, 0, len(uniqueGroups))
|
||||
colors := models.LabelsColorMap{}
|
||||
autocompleteMap := map[string]models.Autocomplete{}
|
||||
autocompleteMap := map[string]*models.Autocomplete{}
|
||||
|
||||
log.Info().
|
||||
Str("alertmanager", am.Name).
|
||||
@@ -336,7 +336,8 @@ func (am *Alertmanager) pullAlerts(version string, si *intern.Interner) error {
|
||||
}
|
||||
|
||||
for _, hint := range filters.BuildAutocomplete(alerts) {
|
||||
autocompleteMap[hint.Value] = hint
|
||||
hint := hint
|
||||
autocompleteMap[hint.Value] = &hint
|
||||
}
|
||||
|
||||
sort.Sort(&alerts)
|
||||
@@ -354,7 +355,7 @@ func (am *Alertmanager) pullAlerts(version string, si *intern.Interner) error {
|
||||
Msg("Merging autocomplete hints")
|
||||
autocomplete := make([]models.Autocomplete, 0, len(autocompleteMap))
|
||||
for _, hint := range autocompleteMap {
|
||||
autocomplete = append(autocomplete, hint)
|
||||
autocomplete = append(autocomplete, *hint)
|
||||
}
|
||||
|
||||
knownLabels := make([]string, 0, len(knownLabelsMap))
|
||||
|
||||
@@ -18,17 +18,11 @@ func makeAC(value string, tokens []string) models.Autocomplete {
|
||||
// BuildAutocomplete takes an alert object and generates list of autocomplete
|
||||
// strings for it
|
||||
func BuildAutocomplete(alerts []models.Alert) []models.Autocomplete {
|
||||
acHints := map[string]models.Autocomplete{}
|
||||
acHints := []models.Autocomplete{}
|
||||
for _, filterConfig := range AllFilters {
|
||||
if filterConfig.Autocomplete != nil {
|
||||
for _, hint := range filterConfig.Autocomplete(filterConfig.Label, filterConfig.SupportedOperators, alerts) {
|
||||
acHints[hint.Value] = hint
|
||||
}
|
||||
acHints = append(acHints, filterConfig.Autocomplete(filterConfig.Label, filterConfig.SupportedOperators, alerts)...)
|
||||
}
|
||||
}
|
||||
acHintsSlice := make([]models.Autocomplete, 0, len(acHints))
|
||||
for _, hint := range acHints {
|
||||
acHintsSlice = append(acHintsSlice, hint)
|
||||
}
|
||||
return acHintsSlice
|
||||
return acHints
|
||||
}
|
||||
|
||||
@@ -39,14 +39,14 @@ func newAlertmanagerInstanceFilter() FilterT {
|
||||
}
|
||||
|
||||
func alertmanagerInstanceAutocomplete(name string, operators []string, alerts []models.Alert) []models.Autocomplete {
|
||||
tokens := map[string]models.Autocomplete{}
|
||||
tokens := map[string]*models.Autocomplete{}
|
||||
for _, alert := range alerts {
|
||||
for _, am := range alert.Alertmanager {
|
||||
for _, operator := range operators {
|
||||
switch operator {
|
||||
case equalOperator, notEqualOperator:
|
||||
token := fmt.Sprintf("%s%s%s", name, operator, am.Name)
|
||||
tokens[token] = makeAC(
|
||||
hint := makeAC(
|
||||
token,
|
||||
[]string{
|
||||
name,
|
||||
@@ -54,13 +54,14 @@ func alertmanagerInstanceAutocomplete(name string, operators []string, alerts []
|
||||
name + operator,
|
||||
},
|
||||
)
|
||||
tokens[token] = &hint
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
acData := make([]models.Autocomplete, 0, len(tokens))
|
||||
for _, token := range tokens {
|
||||
acData = append(acData, token)
|
||||
acData = append(acData, *token)
|
||||
}
|
||||
return acData
|
||||
}
|
||||
|
||||
@@ -39,14 +39,14 @@ func newAlertmanagerClusterFilter() FilterT {
|
||||
}
|
||||
|
||||
func alertmanagerClusterAutocomplete(name string, operators []string, alerts []models.Alert) []models.Autocomplete {
|
||||
tokens := map[string]models.Autocomplete{}
|
||||
tokens := map[string]*models.Autocomplete{}
|
||||
for _, alert := range alerts {
|
||||
for _, am := range alert.Alertmanager {
|
||||
for _, operator := range operators {
|
||||
switch operator {
|
||||
case equalOperator, notEqualOperator:
|
||||
token := fmt.Sprintf("%s%s%s", name, operator, am.Cluster)
|
||||
tokens[token] = makeAC(
|
||||
hint := makeAC(
|
||||
token,
|
||||
[]string{
|
||||
name,
|
||||
@@ -54,13 +54,14 @@ func alertmanagerClusterAutocomplete(name string, operators []string, alerts []m
|
||||
name + operator,
|
||||
},
|
||||
)
|
||||
tokens[token] = &hint
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
acData := make([]models.Autocomplete, 0, len(tokens))
|
||||
for _, token := range tokens {
|
||||
acData = append(acData, token)
|
||||
acData = append(acData, *token)
|
||||
}
|
||||
return acData
|
||||
}
|
||||
|
||||
@@ -61,20 +61,21 @@ func newInhibitedFilter() FilterT {
|
||||
}
|
||||
|
||||
func inhibitedAutocomplete(name string, _ []string, _ []models.Alert) []models.Autocomplete {
|
||||
tokens := map[string]models.Autocomplete{}
|
||||
tokens := map[string]*models.Autocomplete{}
|
||||
|
||||
for _, val := range []string{trueValue, falseValue} {
|
||||
token := fmt.Sprintf("%s%s%s", name, equalOperator, val)
|
||||
tokens[token] = makeAC(token, []string{
|
||||
hint := makeAC(token, []string{
|
||||
name,
|
||||
strings.TrimPrefix(name, "@"),
|
||||
fmt.Sprintf("%s%s", name, equalOperator),
|
||||
val,
|
||||
})
|
||||
tokens[token] = &hint
|
||||
}
|
||||
acData := make([]models.Autocomplete, 0, len(tokens))
|
||||
for _, token := range tokens {
|
||||
acData = append(acData, token)
|
||||
acData = append(acData, *token)
|
||||
}
|
||||
return acData
|
||||
}
|
||||
|
||||
@@ -49,25 +49,26 @@ func newInhibitedByFilter() FilterT {
|
||||
}
|
||||
|
||||
func inhibitedByAutocomplete(name string, operators []string, alerts []models.Alert) []models.Autocomplete {
|
||||
tokens := map[string]models.Autocomplete{}
|
||||
tokens := map[string]*models.Autocomplete{}
|
||||
for _, alert := range alerts {
|
||||
for _, am := range alert.Alertmanager {
|
||||
for _, silenceID := range am.InhibitedBy {
|
||||
for _, operator := range operators {
|
||||
token := fmt.Sprintf("%s%s%s", name, operator, silenceID)
|
||||
tokens[token] = makeAC(token, []string{
|
||||
hint := makeAC(token, []string{
|
||||
name,
|
||||
strings.TrimPrefix(name, "@"),
|
||||
fmt.Sprintf("%s%s", name, operator),
|
||||
silenceID,
|
||||
})
|
||||
tokens[token] = &hint
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
acData := make([]models.Autocomplete, 0, len(tokens))
|
||||
for _, token := range tokens {
|
||||
acData = append(acData, token)
|
||||
acData = append(acData, *token)
|
||||
}
|
||||
return acData
|
||||
}
|
||||
|
||||
@@ -30,14 +30,14 @@ func newLabelFilter() FilterT {
|
||||
}
|
||||
|
||||
func labelAutocomplete(_ string, operators []string, alerts []models.Alert) []models.Autocomplete {
|
||||
tokens := map[string]models.Autocomplete{}
|
||||
tokens := map[string]*models.Autocomplete{}
|
||||
for _, alert := range alerts {
|
||||
for _, l := range alert.Labels {
|
||||
for _, operator := range operators {
|
||||
switch operator {
|
||||
case equalOperator, notEqualOperator:
|
||||
token := fmt.Sprintf("%s%s%s", l.Name, operator, l.Value)
|
||||
tokens[token] = makeAC(
|
||||
hint := makeAC(
|
||||
token,
|
||||
[]string{
|
||||
l.Name,
|
||||
@@ -45,12 +45,13 @@ func labelAutocomplete(_ string, operators []string, alerts []models.Alert) []mo
|
||||
l.Value,
|
||||
},
|
||||
)
|
||||
tokens[token] = &hint
|
||||
case regexpOperator, negativeRegexOperator:
|
||||
substrings := strings.Split(l.Value, " ")
|
||||
if len(substrings) > 1 {
|
||||
for _, substring := range substrings {
|
||||
token := fmt.Sprintf("%s%s%s", l.Name, operator, substring)
|
||||
tokens[token] = makeAC(
|
||||
hint := makeAC(
|
||||
token,
|
||||
[]string{
|
||||
l.Name,
|
||||
@@ -59,12 +60,13 @@ func labelAutocomplete(_ string, operators []string, alerts []models.Alert) []mo
|
||||
substring,
|
||||
},
|
||||
)
|
||||
tokens[token] = &hint
|
||||
}
|
||||
}
|
||||
case moreThanOperator, lessThanOperator:
|
||||
if _, err := strconv.Atoi(l.Value); err == nil {
|
||||
token := fmt.Sprintf("%s%s%s", l.Name, operator, l.Value)
|
||||
tokens[token] = makeAC(
|
||||
hint := makeAC(
|
||||
token,
|
||||
[]string{
|
||||
l.Name,
|
||||
@@ -72,6 +74,7 @@ func labelAutocomplete(_ string, operators []string, alerts []models.Alert) []mo
|
||||
l.Value,
|
||||
},
|
||||
)
|
||||
tokens[token] = &hint
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,7 +82,7 @@ func labelAutocomplete(_ string, operators []string, alerts []models.Alert) []mo
|
||||
}
|
||||
acData := make([]models.Autocomplete, 0, len(tokens))
|
||||
for _, token := range tokens {
|
||||
acData = append(acData, token)
|
||||
acData = append(acData, *token)
|
||||
}
|
||||
return acData
|
||||
}
|
||||
|
||||
@@ -29,14 +29,14 @@ func newreceiverFilter() FilterT {
|
||||
}
|
||||
|
||||
func receiverAutocomplete(name string, operators []string, alerts []models.Alert) []models.Autocomplete {
|
||||
tokens := map[string]models.Autocomplete{}
|
||||
tokens := map[string]*models.Autocomplete{}
|
||||
for _, alert := range alerts {
|
||||
if alert.Receiver != "" {
|
||||
for _, operator := range operators {
|
||||
switch operator {
|
||||
case equalOperator, notEqualOperator:
|
||||
token := fmt.Sprintf("%s%s%s", name, operator, alert.Receiver)
|
||||
tokens[token] = makeAC(
|
||||
hint := makeAC(
|
||||
token,
|
||||
[]string{
|
||||
name,
|
||||
@@ -44,12 +44,13 @@ func receiverAutocomplete(name string, operators []string, alerts []models.Alert
|
||||
name + operator,
|
||||
},
|
||||
)
|
||||
tokens[token] = &hint
|
||||
case regexpOperator, negativeRegexOperator:
|
||||
substrings := strings.Split(alert.Receiver, " ")
|
||||
if len(substrings) > 1 {
|
||||
for _, substring := range substrings {
|
||||
token := fmt.Sprintf("%s%s%s", name, operator, substring)
|
||||
tokens[token] = makeAC(
|
||||
hint := makeAC(
|
||||
token,
|
||||
[]string{
|
||||
name,
|
||||
@@ -58,6 +59,7 @@ func receiverAutocomplete(name string, operators []string, alerts []models.Alert
|
||||
substring,
|
||||
},
|
||||
)
|
||||
tokens[token] = &hint
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,7 +68,7 @@ func receiverAutocomplete(name string, operators []string, alerts []models.Alert
|
||||
}
|
||||
acData := make([]models.Autocomplete, 0, len(tokens))
|
||||
for _, token := range tokens {
|
||||
acData = append(acData, token)
|
||||
acData = append(acData, *token)
|
||||
}
|
||||
return acData
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ func newSilenceAuthorFilter() FilterT {
|
||||
}
|
||||
|
||||
func silenceAuthorAutocomplete(name string, operators []string, alerts []models.Alert) []models.Autocomplete {
|
||||
tokens := map[string]models.Autocomplete{}
|
||||
tokens := map[string]*models.Autocomplete{}
|
||||
for _, alert := range alerts {
|
||||
for _, am := range alert.Alertmanager {
|
||||
for _, silenceID := range am.SilencedBy {
|
||||
@@ -64,12 +64,13 @@ func silenceAuthorAutocomplete(name string, operators []string, alerts []models.
|
||||
if found {
|
||||
for _, operator := range operators {
|
||||
token := fmt.Sprintf("%s%s%s", name, operator, silence.CreatedBy)
|
||||
tokens[token] = makeAC(token, []string{
|
||||
hint := makeAC(token, []string{
|
||||
name,
|
||||
strings.TrimPrefix(name, "@"),
|
||||
fmt.Sprintf("%s%s", name, operator),
|
||||
silence.CreatedBy,
|
||||
})
|
||||
tokens[token] = &hint
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,7 +79,7 @@ func silenceAuthorAutocomplete(name string, operators []string, alerts []models.
|
||||
}
|
||||
acData := make([]models.Autocomplete, 0, len(tokens))
|
||||
for _, token := range tokens {
|
||||
acData = append(acData, token)
|
||||
acData = append(acData, *token)
|
||||
}
|
||||
return acData
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ func newSilenceTicketFilter() FilterT {
|
||||
}
|
||||
|
||||
func silenceTicketIDAutocomplete(name string, operators []string, alerts []models.Alert) []models.Autocomplete {
|
||||
tokens := map[string]models.Autocomplete{}
|
||||
tokens := map[string]*models.Autocomplete{}
|
||||
for _, alert := range alerts {
|
||||
for _, am := range alert.Alertmanager {
|
||||
for _, silenceID := range am.SilencedBy {
|
||||
@@ -64,12 +64,13 @@ func silenceTicketIDAutocomplete(name string, operators []string, alerts []model
|
||||
if found && silence.TicketID != "" {
|
||||
for _, operator := range operators {
|
||||
token := fmt.Sprintf("%s%s%s", name, operator, silence.TicketID)
|
||||
tokens[token] = makeAC(token, []string{
|
||||
hint := makeAC(token, []string{
|
||||
name,
|
||||
strings.TrimPrefix(name, "@"),
|
||||
fmt.Sprintf("%s%s", name, operator),
|
||||
silence.TicketID,
|
||||
})
|
||||
tokens[token] = &hint
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,7 +79,7 @@ func silenceTicketIDAutocomplete(name string, operators []string, alerts []model
|
||||
}
|
||||
acData := make([]models.Autocomplete, 0, len(tokens))
|
||||
for _, token := range tokens {
|
||||
acData = append(acData, token)
|
||||
acData = append(acData, *token)
|
||||
}
|
||||
return acData
|
||||
}
|
||||
|
||||
@@ -49,25 +49,26 @@ func newsilenceIDFilter() FilterT {
|
||||
}
|
||||
|
||||
func silenceIDAutocomplete(name string, operators []string, alerts []models.Alert) []models.Autocomplete {
|
||||
tokens := map[string]models.Autocomplete{}
|
||||
tokens := map[string]*models.Autocomplete{}
|
||||
for _, alert := range alerts {
|
||||
for _, am := range alert.Alertmanager {
|
||||
for _, silenceID := range am.SilencedBy {
|
||||
for _, operator := range operators {
|
||||
token := fmt.Sprintf("%s%s%s", name, operator, silenceID)
|
||||
tokens[token] = makeAC(token, []string{
|
||||
hint := makeAC(token, []string{
|
||||
name,
|
||||
strings.TrimPrefix(name, "@"),
|
||||
fmt.Sprintf("%s%s", name, operator),
|
||||
silenceID,
|
||||
})
|
||||
tokens[token] = &hint
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
acData := make([]models.Autocomplete, 0, len(tokens))
|
||||
for _, token := range tokens {
|
||||
acData = append(acData, token)
|
||||
acData = append(acData, *token)
|
||||
}
|
||||
return acData
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user