chore(api): store cluster as a dict

This will allow silences to be grouped by cluster rather than by alertmanager instance
This commit is contained in:
Łukasz Mierzwa
2018-11-30 23:14:59 +00:00
parent d2d1bce747
commit 8b5862d44b
4 changed files with 50 additions and 7 deletions

View File

@@ -2,11 +2,13 @@ package main
import (
"sort"
"strings"
"github.com/prymitive/karma/internal/alertmanager"
"github.com/prymitive/karma/internal/filters"
"github.com/prymitive/karma/internal/models"
"github.com/prymitive/karma/internal/slices"
log "github.com/sirupsen/logrus"
)
func getFiltersFromQuery(filterStrings []string) ([]filters.FilterT, bool) {
@@ -41,7 +43,11 @@ func getUpstreams() models.AlertmanagerAPISummary {
for _, upstream := range upstreams {
members := upstream.ClusterMemberNames()
sort.Strings(members)
key := strings.Join(members[:], "\n")
key, err := slices.StringSliceToSHA1(members)
if err != nil {
log.Errorf("slices.StringSliceToSHA1 error: %s", err)
continue
}
if _, found := clusters[key]; !found {
clusters[key] = members
}
@@ -63,10 +69,7 @@ func getUpstreams() models.AlertmanagerAPISummary {
summary.Counters.Failed++
}
}
for _, cluster := range clusters {
summary.Clusters = append(summary.Clusters, cluster)
}
summary.Clusters = clusters
return summary
}

View File

@@ -47,5 +47,5 @@ type AlertmanagerAPICounters struct {
type AlertmanagerAPISummary struct {
Counters AlertmanagerAPICounters `json:"counters"`
Instances []AlertmanagerAPIStatus `json:"instances"`
Clusters [][]string `json:"clusters"`
Clusters map[string][]string `json:"clusters"`
}

View File

@@ -1,5 +1,10 @@
package slices
import (
"crypto/sha1"
"fmt"
)
// BoolInSlice returns true if given bool is found in a slice of bools
func BoolInSlice(boolArray []bool, value bool) bool {
for _, s := range boolArray {
@@ -19,3 +24,19 @@ func StringInSlice(stringArray []string, value string) bool {
}
return false
}
// StringSliceToSHA1 returns a SHA1 hash computed from a slice of strings
func StringSliceToSHA1(stringArray []string) (string, error) {
h := sha1.New()
for _, s := range stringArray {
_, err := h.Write([]byte(s))
if err != nil {
return "", err
}
_, err = h.Write([]byte("\n"))
if err != nil {
return "", err
}
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}

View File

@@ -111,3 +111,22 @@ func TestBoolInSlice(t *testing.T) {
}
}
}
func TestStringSliceToSHA1(t *testing.T) {
s, err := slices.StringSliceToSHA1([]string{"a", "b", "c"})
if err != nil {
t.Errorf("StringSliceToSHA1() returned error: %s", err)
}
if s == "" {
t.Errorf("StringSliceToSHA1() returned empty string")
}
}
func BenchmarkStringSliceToSHA1(b *testing.B) {
for _, stringSliceTest := range stringSliceTests {
_, err := slices.StringSliceToSHA1(stringSliceTest.array)
if err != nil {
b.Errorf("StringSliceToSHA1() returned error: %s", err)
}
}
}