Annotations: Use dashboard uids instead of dashboard ids (#106676)

This commit is contained in:
Stephanie Hingtgen
2025-06-13 13:59:24 -05:00
committed by GitHub
parent 47f3073ab8
commit a8886ad5ec
20 changed files with 410 additions and 228 deletions
@@ -78,14 +78,15 @@ func TestIntegrationAnnotations(t *testing.T) {
var err error
annotation := &annotations.Item{
OrgID: 1,
UserID: 1,
DashboardID: dashboard.ID,
Text: "hello",
Type: "alert",
Epoch: 10,
Tags: []string{"outage", "error", "type:outage", "server:server-1"},
Data: simplejson.NewFromAny(map[string]any{"data1": "I am a cool data", "data2": "I am another cool data"}),
OrgID: 1,
UserID: 1,
DashboardID: dashboard.ID, // nolint: staticcheck
DashboardUID: dashboard.UID,
Text: "hello",
Type: "alert",
Epoch: 10,
Tags: []string{"outage", "error", "type:outage", "server:server-1"},
Data: simplejson.NewFromAny(map[string]any{"data1": "I am a cool data", "data2": "I am another cool data"}),
}
err = store.Add(context.Background(), annotation)
require.NoError(t, err)
@@ -93,14 +94,15 @@ func TestIntegrationAnnotations(t *testing.T) {
assert.Equal(t, annotation.Epoch, annotation.EpochEnd)
annotation2 := &annotations.Item{
OrgID: 1,
UserID: 1,
DashboardID: dashboard2.ID,
Text: "hello",
Type: "alert",
Epoch: 21, // Should swap epoch & epochEnd
EpochEnd: 20,
Tags: []string{"outage", "type:outage", "server:server-1", "error"},
OrgID: 1,
UserID: 1,
DashboardID: dashboard2.ID, // nolint: staticcheck
DashboardUID: dashboard2.UID,
Text: "hello",
Type: "alert",
Epoch: 21, // Should swap epoch & epochEnd
EpochEnd: 20,
Tags: []string{"outage", "type:outage", "server:server-1", "error"},
}
err = store.Add(context.Background(), annotation2)
require.NoError(t, err)
@@ -135,7 +137,31 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Can query for annotation by dashboard id", func(t *testing.T) {
items, err := store.Get(context.Background(), annotations.ItemQuery{
OrgID: 1,
DashboardID: dashboard.ID,
DashboardID: dashboard.ID, // nolint: staticcheck
From: 0,
To: 15,
SignedInUser: testUser,
}, &annotation_ac.AccessResources{
Dashboards: map[string]int64{
dashboard.UID: dashboard.ID,
},
CanAccessDashAnnotations: true,
})
require.NoError(t, err)
assert.Len(t, items, 1)
assert.Equal(t, []string{"outage", "error", "type:outage", "server:server-1"}, items[0].Tags)
assert.GreaterOrEqual(t, items[0].Created, int64(0))
assert.GreaterOrEqual(t, items[0].Updated, int64(0))
assert.Equal(t, items[0].Updated, items[0].Created)
})
t.Run("Can query for annotation by dashboard uid", func(t *testing.T) {
items, err := store.Get(context.Background(), annotations.ItemQuery{
OrgID: 1,
DashboardUID: dashboard.UID,
From: 0,
To: 15,
SignedInUser: testUser,
@@ -234,12 +260,13 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Should not find any when item is outside time range", func(t *testing.T) {
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{"foo": 1},
Dashboards: map[string]int64{dashboard.UID: 1},
CanAccessDashAnnotations: true,
}
items, err := store.Get(context.Background(), annotations.ItemQuery{
OrgID: 1,
DashboardID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: dashboard.UID,
From: 12,
To: 15,
SignedInUser: testUser,
@@ -250,12 +277,13 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Should not find one when tag filter does not match", func(t *testing.T) {
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{"foo": 1},
Dashboards: map[string]int64{dashboard.UID: 1},
CanAccessDashAnnotations: true,
}
items, err := store.Get(context.Background(), annotations.ItemQuery{
OrgID: 1,
DashboardID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: dashboard.UID,
From: 1,
To: 15,
Tags: []string{"asd"},
@@ -267,12 +295,13 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Should not find one when type filter does not match", func(t *testing.T) {
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{"foo": 1},
Dashboards: map[string]int64{dashboard.UID: 1},
CanAccessDashAnnotations: true,
}
items, err := store.Get(context.Background(), annotations.ItemQuery{
OrgID: 1,
DashboardID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: dashboard.UID,
From: 1,
To: 15,
Type: "alert",
@@ -284,12 +313,13 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Should find one when all tag filters does match", func(t *testing.T) {
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{"foo": 1},
Dashboards: map[string]int64{dashboard.UID: 1},
CanAccessDashAnnotations: true,
}
items, err := store.Get(context.Background(), annotations.ItemQuery{
OrgID: 1,
DashboardID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: dashboard.UID,
From: 1,
To: 15, // this will exclude the second test annotation
Tags: []string{"outage", "error"},
@@ -315,12 +345,13 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Should find one when all key value tag filters does match", func(t *testing.T) {
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{"foo": 1},
Dashboards: map[string]int64{dashboard.UID: 1},
CanAccessDashAnnotations: true,
}
items, err := store.Get(context.Background(), annotations.ItemQuery{
OrgID: 1,
DashboardID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: dashboard.UID,
From: 1,
To: 15,
Tags: []string{"type:outage", "server:server-1"},
@@ -333,13 +364,14 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Can update annotation and remove all tags", func(t *testing.T) {
query := annotations.ItemQuery{
OrgID: 1,
DashboardID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: dashboard.UID,
From: 0,
To: 15,
SignedInUser: testUser,
}
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{"foo": 1},
Dashboards: map[string]int64{dashboard.UID: 1},
CanAccessDashAnnotations: true,
}
items, err := store.Get(context.Background(), query, accRes)
@@ -368,13 +400,14 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Can update annotation with new tags", func(t *testing.T) {
query := annotations.ItemQuery{
OrgID: 1,
DashboardID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: dashboard.UID,
From: 0,
To: 15,
SignedInUser: testUser,
}
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{"foo": 1},
Dashboards: map[string]int64{dashboard.UID: 1},
CanAccessDashAnnotations: true,
}
items, err := store.Get(context.Background(), query, accRes)
@@ -401,13 +434,14 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Can update annotation with additional tags", func(t *testing.T) {
query := annotations.ItemQuery{
OrgID: 1,
DashboardID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: dashboard.UID,
From: 0,
To: 15,
SignedInUser: testUser,
}
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{"foo": 1},
Dashboards: map[string]int64{dashboard.UID: 1},
CanAccessDashAnnotations: true,
}
items, err := store.Get(context.Background(), query, accRes)
@@ -434,13 +468,14 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Can update annotations with data", func(t *testing.T) {
query := annotations.ItemQuery{
OrgID: 1,
DashboardID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: dashboard.UID,
From: 0,
To: 15,
SignedInUser: testUser,
}
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{"foo": 1},
Dashboards: map[string]int64{dashboard.UID: 1},
CanAccessDashAnnotations: true,
}
items, err := store.Get(context.Background(), query, accRes)
@@ -470,13 +505,14 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Can delete annotation", func(t *testing.T) {
query := annotations.ItemQuery{
OrgID: 1,
DashboardID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: dashboard.UID,
From: 0,
To: 15,
SignedInUser: testUser,
}
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{"foo": 1},
Dashboards: map[string]int64{dashboard.UID: 1},
CanAccessDashAnnotations: true,
}
items, err := store.Get(context.Background(), query, accRes)
@@ -493,14 +529,15 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Can delete annotation using dashboard id and panel id", func(t *testing.T) {
annotation3 := &annotations.Item{
OrgID: 1,
UserID: 1,
DashboardID: dashboard2.ID,
Text: "toBeDeletedWithPanelId",
Type: "alert",
Epoch: 11,
Tags: []string{"test"},
PanelID: 20,
OrgID: 1,
UserID: 1,
DashboardID: dashboard2.ID, // nolint: staticcheck
DashboardUID: dashboard2.UID,
Text: "toBeDeletedWithPanelId",
Type: "alert",
Epoch: 11,
Tags: []string{"test"},
PanelID: 20,
}
err = store.Add(context.Background(), annotation3)
require.NoError(t, err)
@@ -520,9 +557,46 @@ func TestIntegrationAnnotations(t *testing.T) {
items, err := store.Get(context.Background(), query, accRes)
require.NoError(t, err)
dashboardId := items[0].DashboardID
panelId := items[0].PanelID
err = store.Delete(context.Background(), &annotations.DeleteParams{DashboardID: dashboardId, PanelID: panelId, OrgID: 1})
// nolint:staticcheck
err = store.Delete(context.Background(), &annotations.DeleteParams{DashboardID: items[0].DashboardID, PanelID: items[0].PanelID, OrgID: 1})
require.NoError(t, err)
items, err = store.Get(context.Background(), query, accRes)
require.NoError(t, err)
assert.Empty(t, items)
})
t.Run("Can delete annotation using dashboard uid and panel id", func(t *testing.T) {
annotation3 := &annotations.Item{
OrgID: 1,
UserID: 1,
DashboardUID: dashboard2.UID,
Text: "toBeDeletedWithPanelId",
Type: "alert",
Epoch: 11,
Tags: []string{"test"},
PanelID: 20,
}
err = store.Add(context.Background(), annotation3)
require.NoError(t, err)
accRes := &annotation_ac.AccessResources{
Dashboards: map[string]int64{
dashboard2.UID: dashboard2.ID,
},
CanAccessDashAnnotations: true,
}
query := annotations.ItemQuery{
OrgID: 1,
AnnotationID: annotation3.ID,
SignedInUser: testUser,
}
items, err := store.Get(context.Background(), query, accRes)
require.NoError(t, err)
// nolint:staticcheck
err = store.Delete(context.Background(), &annotations.DeleteParams{DashboardUID: *items[0].DashboardUID, PanelID: items[0].PanelID, OrgID: 1})
require.NoError(t, err)
items, err = store.Get(context.Background(), query, accRes)
@@ -635,15 +709,16 @@ func benchmarkFindTags(b *testing.B, numAnnotations int) {
require.NoError(b, err)
annotationWithTheTag := annotations.Item{
ID: int64(numAnnotations) + 1,
OrgID: 1,
UserID: 1,
DashboardID: int64(1),
Text: "hello",
Type: "alert",
Epoch: 10,
Tags: []string{"outage", "error", "type:outage", "server:server-1"},
Data: simplejson.NewFromAny(map[string]any{"data1": "I am a cool data", "data2": "I am another cool data"}),
ID: int64(numAnnotations) + 1,
OrgID: 1,
UserID: 1,
DashboardID: 1, // nolint: staticcheck
DashboardUID: "uid",
Text: "hello",
Type: "alert",
Epoch: 10,
Tags: []string{"outage", "error", "type:outage", "server:server-1"},
Data: simplejson.NewFromAny(map[string]any{"data1": "I am a cool data", "data2": "I am another cool data"}),
}
err = store.Add(context.Background(), &annotationWithTheTag)
require.NoError(b, err)