bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/models/alertKey.go (about) 1 package models 2 3 import ( 4 "fmt" 5 "strings" 6 7 "bosun.org/opentsdb" 8 ) 9 10 type AlertKey string 11 12 func ParseAlertKey(a string) (ak AlertKey, err error) { 13 ak = AlertKey(a) 14 defer func() { 15 e := recover() 16 if e != nil { 17 err = fmt.Errorf("%v", e) 18 } 19 }() 20 ak.Group() 21 return 22 } 23 24 func NewAlertKey(name string, group opentsdb.TagSet) AlertKey { 25 return AlertKey(name + group.String()) 26 } 27 28 func (a AlertKey) Name() string { 29 return strings.SplitN(string(a), "{", 2)[0] 30 } 31 32 // Group returns the tagset of this alert key. Will panic if a is not a valid 33 // AlertKey. OpenTSDB tag validation errors are ignored. 34 func (a AlertKey) Group() opentsdb.TagSet { 35 sp := strings.SplitN(string(a), "{", 2) 36 if len(sp) < 2 { 37 panic(fmt.Errorf("invalid alert key %s", a)) 38 } 39 s := sp[1] 40 s = s[:len(s)-1] 41 if s == "" { 42 return nil 43 } 44 g, err := opentsdb.ParseTags(s) 45 if g == nil && err != nil { 46 panic(err) 47 } 48 return g 49 } 50 51 type AlertKeys []AlertKey 52 53 func (a AlertKeys) Len() int { return len(a) } 54 func (a AlertKeys) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 55 func (a AlertKeys) Less(i, j int) bool { return a[i] < a[j] }