Annotations: Adds tags endpoint (#36199)
* Annotations: Adds tags endpoint * Chore: fixes sql statement * Refactor: adds count to the api * Chore: changes after PR comments * Refactor: changes after PR comments
This commit is contained in:
@@ -268,3 +268,52 @@ func (r *SQLAnnotationRepo) Delete(params *annotations.DeleteParams) error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (r *SQLAnnotationRepo) FindTags(query *annotations.TagsQuery) (annotations.FindTagsResult, error) {
|
||||
if query.Limit == 0 {
|
||||
query.Limit = 100
|
||||
}
|
||||
|
||||
var sql bytes.Buffer
|
||||
params := make([]interface{}, 0)
|
||||
tagKey := `tag.` + dialect.Quote("key")
|
||||
tagValue := `tag.` + dialect.Quote("value")
|
||||
|
||||
sql.WriteString(`
|
||||
SELECT
|
||||
` + tagKey + `,
|
||||
` + tagValue + `,
|
||||
count(*) as count
|
||||
FROM tag
|
||||
INNER JOIN annotation_tag ON tag.id = annotation_tag.tag_id
|
||||
`)
|
||||
|
||||
sql.WriteString(`WHERE EXISTS(SELECT 1 FROM annotation WHERE annotation.id = annotation_tag.annotation_id AND annotation.org_id = ?)`)
|
||||
params = append(params, query.OrgID)
|
||||
|
||||
sql.WriteString(` AND (` + tagKey + ` ` + dialect.LikeStr() + ` ? OR ` + tagValue + ` ` + dialect.LikeStr() + ` ?)`)
|
||||
params = append(params, `%`+query.Tag+`%`, `%`+query.Tag+`%`)
|
||||
|
||||
sql.WriteString(` GROUP BY ` + tagKey + `,` + tagValue)
|
||||
sql.WriteString(` ORDER BY ` + tagKey + `,` + tagValue)
|
||||
sql.WriteString(` ` + dialect.Limit(query.Limit))
|
||||
|
||||
var items []*annotations.Tags
|
||||
if err := x.SQL(sql.String(), params...).Find(&items); err != nil {
|
||||
return annotations.FindTagsResult{Tags: []*annotations.TagsDTO{}}, err
|
||||
}
|
||||
|
||||
tags := make([]*annotations.TagsDTO, 0)
|
||||
for _, item := range items {
|
||||
tag := item.Key
|
||||
if len(item.Value) > 0 {
|
||||
tag = item.Key + ":" + item.Value
|
||||
}
|
||||
tags = append(tags, &annotations.TagsDTO{
|
||||
Tag: tag,
|
||||
Count: item.Count,
|
||||
})
|
||||
}
|
||||
|
||||
return annotations.FindTagsResult{Tags: tags}, nil
|
||||
}
|
||||
|
||||
@@ -251,5 +251,47 @@ func TestAnnotations(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, items)
|
||||
})
|
||||
|
||||
t.Run("Should find tags by key", func(t *testing.T) {
|
||||
result, err := repo.FindTags(&annotations.TagsQuery{
|
||||
OrgID: 1,
|
||||
Tag: "server",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, result.Tags, 1)
|
||||
require.Equal(t, "server:server-1", result.Tags[0].Tag)
|
||||
require.Equal(t, int64(1), result.Tags[0].Count)
|
||||
})
|
||||
|
||||
t.Run("Should find tags by value", func(t *testing.T) {
|
||||
result, err := repo.FindTags(&annotations.TagsQuery{
|
||||
OrgID: 1,
|
||||
Tag: "outage",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, result.Tags, 2)
|
||||
require.Equal(t, "outage", result.Tags[0].Tag)
|
||||
require.Equal(t, "type:outage", result.Tags[1].Tag)
|
||||
require.Equal(t, int64(1), result.Tags[0].Count)
|
||||
require.Equal(t, int64(1), result.Tags[1].Count)
|
||||
})
|
||||
|
||||
t.Run("Should not find tags in other org", func(t *testing.T) {
|
||||
result, err := repo.FindTags(&annotations.TagsQuery{
|
||||
OrgID: 0,
|
||||
Tag: "server-1",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, result.Tags, 0)
|
||||
})
|
||||
|
||||
t.Run("Should not find tags that do not exist", func(t *testing.T) {
|
||||
result, err := repo.FindTags(&annotations.TagsQuery{
|
||||
OrgID: 0,
|
||||
Tag: "unknown:tag",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, result.Tags, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user