mirror of
https://github.com/prymitive/karma
synced 2026-05-05 03:16:51 +00:00
Import code from internal repository (#1)
Import code from internal repository
This commit is contained in:
24
transform/autocomplete.go
Normal file
24
transform/autocomplete.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"github.com/cloudflare/unsee/filters"
|
||||
"github.com/cloudflare/unsee/models"
|
||||
)
|
||||
|
||||
// BuildAutocomplete takes an alert object and generates list of autocomplete
|
||||
// strings for it
|
||||
func BuildAutocomplete(alerts []models.UnseeAlert) []models.UnseeAutocomplete {
|
||||
acHints := map[string]models.UnseeAutocomplete{}
|
||||
for _, filterConfig := range filters.AllFilters {
|
||||
if filterConfig.Autocomplete != nil {
|
||||
for _, hint := range filterConfig.Autocomplete(filterConfig.Label, filterConfig.SupportedOperators, alerts) {
|
||||
acHints[hint.Value] = hint
|
||||
}
|
||||
}
|
||||
}
|
||||
acHintsSlice := []models.UnseeAutocomplete{}
|
||||
for _, hint := range acHints {
|
||||
acHintsSlice = append(acHintsSlice, hint)
|
||||
}
|
||||
return acHintsSlice
|
||||
}
|
||||
71
transform/colors.go
Normal file
71
transform/colors.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"io"
|
||||
"math/rand"
|
||||
"github.com/cloudflare/unsee/config"
|
||||
"github.com/cloudflare/unsee/models"
|
||||
|
||||
"github.com/hansrodtang/randomcolor"
|
||||
)
|
||||
|
||||
func labelToSeed(key string, val string) int64 {
|
||||
h := sha1.New()
|
||||
io.WriteString(h, key)
|
||||
io.WriteString(h, val)
|
||||
var seed int64
|
||||
for _, i := range h.Sum(nil) {
|
||||
seed += int64(i)
|
||||
}
|
||||
return seed
|
||||
}
|
||||
|
||||
// ColorLabel update UnseeColorMap object with a color object generated
|
||||
// from label key and value passed here
|
||||
// It's used to generate unique colors for configured labels
|
||||
func ColorLabel(colorStore models.UnseeColorMap, key string, val string) {
|
||||
if stringInSlice(config.Config.ColorLabels, key) == true {
|
||||
if _, found := colorStore[key]; !found {
|
||||
colorStore[key] = make(map[string]models.UnseeLabelColor)
|
||||
}
|
||||
if _, found := colorStore[key][val]; !found {
|
||||
rand.Seed(labelToSeed(key, val))
|
||||
color := randomcolor.New(randomcolor.Random, randomcolor.LIGHT)
|
||||
red, green, blue, alpha := color.RGBA()
|
||||
bc := models.UnseeColor{
|
||||
Red: uint8(red >> 8),
|
||||
Green: uint8(green >> 8),
|
||||
Blue: uint8(blue >> 8),
|
||||
Alpha: uint8(alpha >> 8),
|
||||
}
|
||||
// check if color is bright or dark and pick the right background
|
||||
// uses https://www.w3.org/WAI/ER/WD-AERT/#color-contrast method
|
||||
var brightness int32
|
||||
brightness = ((int32(bc.Red) * 299) + (int32(bc.Green) * 587) + (int32(bc.Blue) * 114)) / 1000
|
||||
var fc models.UnseeColor
|
||||
if brightness <= 125 {
|
||||
// background color is dark, use white font
|
||||
fc = models.UnseeColor{
|
||||
Red: 255,
|
||||
Green: 255,
|
||||
Blue: 255,
|
||||
Alpha: 255,
|
||||
}
|
||||
} else {
|
||||
// background color is bright, use dark font
|
||||
fc = models.UnseeColor{
|
||||
Red: 44,
|
||||
Green: 62,
|
||||
Blue: 80,
|
||||
Alpha: 255,
|
||||
}
|
||||
}
|
||||
|
||||
colorStore[key][val] = models.UnseeLabelColor{
|
||||
Font: fc,
|
||||
Background: bc,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
48
transform/jira.go
Normal file
48
transform/jira.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"github.com/cloudflare/unsee/models"
|
||||
)
|
||||
|
||||
type jiraDetectRule struct {
|
||||
Regexp *regexp.Regexp
|
||||
URL string
|
||||
}
|
||||
|
||||
var jiraDetectRules = []jiraDetectRule{}
|
||||
|
||||
// ParseRules will parse and validate list of JIRA detection rules provided
|
||||
// from config, valid rules will be stored for future use in DetectJIRAs() calls
|
||||
func ParseRules(rules []string) {
|
||||
for _, s := range rules {
|
||||
ss := strings.SplitN(s, "@", 2)
|
||||
re := ss[0]
|
||||
url := ss[1]
|
||||
if re == "" || url == "" {
|
||||
log.Fatalf("Invalid JIRA rule '%s', regexp part is '%s', url is '%s'", s, re, url)
|
||||
}
|
||||
jdr := jiraDetectRule{
|
||||
Regexp: regexp.MustCompile(re),
|
||||
URL: url,
|
||||
}
|
||||
jiraDetectRules = append(jiraDetectRules, jdr)
|
||||
}
|
||||
}
|
||||
|
||||
// DetectJIRAs will try to find JIRA links in AlertManager silence objects
|
||||
// using regexp rules from configuration that were parsed and populated
|
||||
// by ParseRules call
|
||||
func DetectJIRAs(silence *models.AlertManagerSilence) (jiraID, jiraLink string) {
|
||||
for _, jdr := range jiraDetectRules {
|
||||
jiraID := jdr.Regexp.FindString(silence.Comment)
|
||||
if jiraID != "" {
|
||||
jiraLink := fmt.Sprintf("%s/browse/%s", jdr.URL, jiraID)
|
||||
return jiraID, jiraLink
|
||||
}
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
21
transform/links.go
Normal file
21
transform/links.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package transform
|
||||
|
||||
import "github.com/asaskevich/govalidator"
|
||||
|
||||
// DetectLinks takes alert annotation dict and returns two dicts:
|
||||
// first with regular annotations
|
||||
// secondd with annotations where values are URLs
|
||||
func DetectLinks(sourceAnnotations map[string]string) (map[string]string, map[string]string) {
|
||||
links := make(map[string]string)
|
||||
annotations := make(map[string]string)
|
||||
|
||||
for k, v := range sourceAnnotations {
|
||||
if govalidator.IsURL(v) {
|
||||
links[k] = v
|
||||
} else {
|
||||
annotations[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return annotations, links
|
||||
}
|
||||
10
transform/slices.go
Normal file
10
transform/slices.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package transform
|
||||
|
||||
func stringInSlice(stringArray []string, value string) bool {
|
||||
for _, s := range stringArray {
|
||||
if s == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
20
transform/strip.go
Normal file
20
transform/strip.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// StripLables allows filtering out some labels from alerts
|
||||
// it takes the list of label keys to ignore and alert label map
|
||||
// it will return label map without labels found on the ignore list
|
||||
func StripLables(ignoredLabels []string, sourceLabels map[string]string) map[string]string {
|
||||
labels := map[string]string{}
|
||||
for label, value := range sourceLabels {
|
||||
if !stringInSlice(ignoredLabels, label) {
|
||||
// strip leading and trailung space in label value
|
||||
// this is to normalize values in case space is added by AlertManager rules
|
||||
labels[label] = strings.TrimSpace(value)
|
||||
}
|
||||
}
|
||||
return labels
|
||||
}
|
||||
Reference in New Issue
Block a user