Chore: StoreSplit tag service (#55453)
* move tag service outside * fix dashboard * fix test * lint * fix linter * remove spew
This commit is contained in:
+4
-3
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/tag"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
@@ -115,15 +116,15 @@ func (a *Alert) ContainsUpdates(other *Alert) bool {
|
||||
return result
|
||||
}
|
||||
|
||||
func (a *Alert) GetTagsFromSettings() []*Tag {
|
||||
tags := []*Tag{}
|
||||
func (a *Alert) GetTagsFromSettings() []*tag.Tag {
|
||||
tags := []*tag.Tag{}
|
||||
if a.Settings != nil {
|
||||
if data, ok := a.Settings.CheckGet("alertRuleTags"); ok {
|
||||
for tagNameString, tagValue := range data.MustMap() {
|
||||
// MustMap() already guarantees the return of a `map[string]interface{}`.
|
||||
// Therefore we only need to verify that tagValue is a String.
|
||||
tagValueString := simplejson.NewFromAny(tagValue).MustString()
|
||||
tags = append(tags, &Tag{Key: tagNameString, Value: tagValueString})
|
||||
tags = append(tags, &tag.Tag{Key: tagNameString, Value: tagValueString})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/tag"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -51,7 +52,7 @@ func TestAlert_GetTagsFromSettings(t *testing.T) {
|
||||
Message: "Message",
|
||||
}
|
||||
|
||||
expectedTags := []*Tag{
|
||||
expectedTags := []*tag.Tag{
|
||||
{Id: 0, Key: "foo", Value: "bar"},
|
||||
{Id: 0, Key: "waldo", Value: "fred"},
|
||||
{Id: 0, Key: "tagMap", Value: ""},
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Tag struct {
|
||||
Id int64
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
|
||||
func ParseTagPairs(tagPairs []string) (tags []*Tag) {
|
||||
if tagPairs == nil {
|
||||
return []*Tag{}
|
||||
}
|
||||
|
||||
for _, tagPair := range tagPairs {
|
||||
var tag Tag
|
||||
|
||||
if strings.Contains(tagPair, ":") {
|
||||
keyValue := strings.Split(tagPair, ":")
|
||||
tag.Key = strings.Trim(keyValue[0], " ")
|
||||
tag.Value = strings.Trim(keyValue[1], " ")
|
||||
} else {
|
||||
tag.Key = strings.Trim(tagPair, " ")
|
||||
}
|
||||
|
||||
if tag.Key == "" || ContainsTag(tags, &tag) {
|
||||
continue
|
||||
}
|
||||
|
||||
tags = append(tags, &tag)
|
||||
}
|
||||
|
||||
return tags
|
||||
}
|
||||
|
||||
func ContainsTag(existingTags []*Tag, tag *Tag) bool {
|
||||
for _, t := range existingTags {
|
||||
if t.Key == tag.Key && t.Value == tag.Value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func JoinTagPairs(tags []*Tag) []string {
|
||||
tagPairs := []string{}
|
||||
|
||||
for _, tag := range tags {
|
||||
if tag.Value != "" {
|
||||
tagPairs = append(tagPairs, tag.Key+":"+tag.Value)
|
||||
} else {
|
||||
tagPairs = append(tagPairs, tag.Key)
|
||||
}
|
||||
}
|
||||
|
||||
return tagPairs
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParseTagPairs(t *testing.T) {
|
||||
t.Run("Can parse one empty tag", func(t *testing.T) {
|
||||
tags := ParseTagPairs([]string{""})
|
||||
require.Empty(t, tags)
|
||||
})
|
||||
|
||||
t.Run("Can parse valid tags", func(t *testing.T) {
|
||||
tags := ParseTagPairs([]string{"outage", "type:outage", "error"})
|
||||
require.Len(t, tags, 3)
|
||||
assert.Equal(t, "outage", tags[0].Key)
|
||||
assert.Empty(t, tags[0].Value)
|
||||
assert.Equal(t, "type", tags[1].Key)
|
||||
assert.Equal(t, "outage", tags[1].Value)
|
||||
assert.Equal(t, "error", tags[2].Key)
|
||||
assert.Empty(t, tags[2].Value)
|
||||
})
|
||||
|
||||
t.Run("Can parse tags with spaces", func(t *testing.T) {
|
||||
tags := ParseTagPairs([]string{" outage ", " type : outage ", "error "})
|
||||
require.Len(t, tags, 3)
|
||||
assert.Equal(t, "outage", tags[0].Key)
|
||||
assert.Empty(t, tags[0].Value)
|
||||
assert.Equal(t, "type", tags[1].Key)
|
||||
assert.Equal(t, "outage", tags[1].Value)
|
||||
assert.Equal(t, "error", tags[2].Key)
|
||||
assert.Empty(t, tags[2].Value)
|
||||
})
|
||||
|
||||
t.Run("Can parse empty tags", func(t *testing.T) {
|
||||
tags := ParseTagPairs([]string{" outage ", "", "", ":", "type : outage ", "error ", "", ""})
|
||||
require.Len(t, tags, 3)
|
||||
assert.Equal(t, "outage", tags[0].Key)
|
||||
assert.Empty(t, tags[0].Value)
|
||||
assert.Equal(t, "type", tags[1].Key)
|
||||
assert.Equal(t, "outage", tags[1].Value)
|
||||
assert.Equal(t, "error", tags[2].Key)
|
||||
assert.Empty(t, tags[2].Value)
|
||||
})
|
||||
|
||||
t.Run("Can parse tags with extra colons", func(t *testing.T) {
|
||||
tags := ParseTagPairs([]string{" outage", "type : outage:outage2 :outage3 ", "error :"})
|
||||
require.Len(t, tags, 3)
|
||||
assert.Equal(t, "outage", tags[0].Key)
|
||||
assert.Empty(t, tags[0].Value)
|
||||
assert.Equal(t, "type", tags[1].Key)
|
||||
assert.Equal(t, "outage", tags[1].Value)
|
||||
assert.Equal(t, "error", tags[2].Key)
|
||||
assert.Empty(t, tags[2].Value)
|
||||
})
|
||||
|
||||
t.Run("Can parse tags that contains key and values with spaces", func(t *testing.T) {
|
||||
tags := ParseTagPairs([]string{" outage 1", "type 1: outage 1 ", "has error "})
|
||||
require.Len(t, tags, 3)
|
||||
assert.Equal(t, "outage 1", tags[0].Key)
|
||||
assert.Empty(t, tags[0].Value)
|
||||
assert.Equal(t, "type 1", tags[1].Key)
|
||||
assert.Equal(t, "outage 1", tags[1].Value)
|
||||
assert.Equal(t, "has error", tags[2].Key)
|
||||
assert.Empty(t, tags[2].Value)
|
||||
})
|
||||
|
||||
t.Run("Can filter out duplicate tags", func(t *testing.T) {
|
||||
tags := ParseTagPairs([]string{"test", "test", "key:val1", "key:val2"})
|
||||
require.Len(t, tags, 3)
|
||||
assert.Equal(t, "test", tags[0].Key)
|
||||
assert.Empty(t, tags[0].Value)
|
||||
assert.Equal(t, "key", tags[1].Key)
|
||||
assert.Equal(t, "val1", tags[1].Value)
|
||||
assert.Equal(t, "key", tags[2].Key)
|
||||
assert.Equal(t, "val2", tags[2].Value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestJoinTagPairs(t *testing.T) {
|
||||
t.Run("Can join tag pairs", func(t *testing.T) {
|
||||
tagPairs := []*Tag{
|
||||
{Key: "key1", Value: "val1"},
|
||||
{Key: "key2", Value: ""},
|
||||
{Key: "key3"},
|
||||
}
|
||||
tags := JoinTagPairs(tagPairs)
|
||||
require.Len(t, tags, 3)
|
||||
assert.Equal(t, "key1:val1", tags[0])
|
||||
assert.Equal(t, "key2", tags[1])
|
||||
assert.Equal(t, "key3", tags[2])
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user