mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
[search] - folder tests; search by ids (#95797)
[search] - folder tests; search by ids
This commit is contained in:
parent
af6ddad058
commit
ef29ce5b85
@ -31,16 +31,16 @@ type IndexResults struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ir IndexedResource) FromSearchHit(hit *search.DocumentMatch) IndexedResource {
|
func (ir IndexedResource) FromSearchHit(hit *search.DocumentMatch) IndexedResource {
|
||||||
ir.Uid = hit.Fields["Uid"].(string)
|
ir.Uid = fieldValue("Uid", hit)
|
||||||
ir.Kind = hit.Fields["Kind"].(string)
|
ir.Kind = fieldValue("Kind", hit)
|
||||||
ir.Name = hit.Fields["Name"].(string)
|
ir.Name = fieldValue("Name", hit)
|
||||||
ir.Namespace = hit.Fields["Namespace"].(string)
|
ir.Namespace = fieldValue("Namespace", hit)
|
||||||
ir.Group = hit.Fields["Group"].(string)
|
ir.Group = fieldValue("Group", hit)
|
||||||
ir.CreatedAt = hit.Fields["CreatedAt"].(string)
|
ir.CreatedAt = fieldValue("CreatedAt", hit)
|
||||||
ir.CreatedBy = hit.Fields["CreatedBy"].(string)
|
ir.CreatedBy = fieldValue("CreatedBy", hit)
|
||||||
ir.UpdatedAt = hit.Fields["UpdatedAt"].(string)
|
ir.UpdatedAt = fieldValue("UpdatedAt", hit)
|
||||||
ir.UpdatedBy = hit.Fields["UpdatedBy"].(string)
|
ir.UpdatedBy = fieldValue("UpdatedBy", hit)
|
||||||
ir.Title = hit.Fields["Title"].(string)
|
ir.Title = fieldValue("Title", hit)
|
||||||
|
|
||||||
// add indexed spec fields to search results
|
// add indexed spec fields to search results
|
||||||
specResult := map[string]any{}
|
specResult := map[string]any{}
|
||||||
@ -55,6 +55,13 @@ func (ir IndexedResource) FromSearchHit(hit *search.DocumentMatch) IndexedResour
|
|||||||
return ir
|
return ir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fieldValue(field string, hit *search.DocumentMatch) string {
|
||||||
|
if val, ok := hit.Fields[field]; ok {
|
||||||
|
return val.(string)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// NewIndexedResource creates a new IndexedResource from a raw resource.
|
// NewIndexedResource creates a new IndexedResource from a raw resource.
|
||||||
// rawResource is the raw json for the resource from unified storage.
|
// rawResource is the raw json for the resource from unified storage.
|
||||||
func NewIndexedResource(rawResource []byte) (*IndexedResource, error) {
|
func NewIndexedResource(rawResource []byte) (*IndexedResource, error) {
|
||||||
|
@ -32,6 +32,52 @@ func TestIndexDashboard(t *testing.T) {
|
|||||||
assertSearchCountEquals(t, index, "*", 1)
|
assertSearchCountEquals(t, index, "*", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIndexFolder(t *testing.T) {
|
||||||
|
data := readTestData(t, "folder-resource.json")
|
||||||
|
list := &ListResponse{Items: []*ResourceWrapper{{Value: data}}}
|
||||||
|
index := newTestIndex(t, 1)
|
||||||
|
|
||||||
|
err := index.writeBatch(testContext, list)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assertCountEquals(t, index, 1)
|
||||||
|
assertSearchCountEquals(t, index, "*", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchFolder(t *testing.T) {
|
||||||
|
dashboard := readTestData(t, "dashboard-resource.json")
|
||||||
|
folder := readTestData(t, "folder-resource.json")
|
||||||
|
list := &ListResponse{Items: []*ResourceWrapper{{Value: dashboard}, {Value: folder}}}
|
||||||
|
index := newTestIndex(t, 1)
|
||||||
|
|
||||||
|
err := index.writeBatch(testContext, list)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assertCountEquals(t, index, 2)
|
||||||
|
assertSearchCountEquals(t, index, "Kind:Folder", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookupNames(t *testing.T) {
|
||||||
|
records := 1000
|
||||||
|
folders, ids := simulateFolders(records)
|
||||||
|
list := &ListResponse{Items: []*ResourceWrapper{}}
|
||||||
|
for _, f := range folders {
|
||||||
|
list.Items = append(list.Items, &ResourceWrapper{Value: []byte(f)})
|
||||||
|
}
|
||||||
|
index := newTestIndex(t, 1)
|
||||||
|
|
||||||
|
err := index.writeBatch(testContext, list)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assertCountEquals(t, index, uint64(records))
|
||||||
|
query := ""
|
||||||
|
chunk := ids[:100] // query for n folders by id
|
||||||
|
for _, id := range chunk {
|
||||||
|
query += `"` + id + `" `
|
||||||
|
}
|
||||||
|
assertSearchCountEquals(t, index, query, int64(len(chunk)))
|
||||||
|
}
|
||||||
|
|
||||||
func TestIndexDashboardWithTags(t *testing.T) {
|
func TestIndexDashboardWithTags(t *testing.T) {
|
||||||
data := readTestData(t, "dashboard-tagged-resource.json")
|
data := readTestData(t, "dashboard-tagged-resource.json")
|
||||||
data2 := readTestData(t, "dashboard-tagged-resource2.json")
|
data2 := readTestData(t, "dashboard-tagged-resource2.json")
|
||||||
@ -143,8 +189,11 @@ func assertCountEquals(t *testing.T, index *Index, expected uint64) {
|
|||||||
|
|
||||||
func assertSearchCountEquals(t *testing.T, index *Index, search string, expected int64) {
|
func assertSearchCountEquals(t *testing.T, index *Index, search string, expected int64) {
|
||||||
req := &SearchRequest{Query: search, Tenant: testTenant, Limit: expected + 1, Offset: 0}
|
req := &SearchRequest{Query: search, Tenant: testTenant, Limit: expected + 1, Offset: 0}
|
||||||
|
start := time.Now()
|
||||||
results, err := index.Search(testContext, req)
|
results, err := index.Search(testContext, req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
fmt.Println("Search time:", elapsed)
|
||||||
assert.Equal(t, expected, int64(len(results.Values)))
|
assert.Equal(t, expected, int64(len(results.Values)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,3 +212,27 @@ func readTestData(t *testing.T, name string) []byte {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func simulateFolders(size int) ([]string, []string) {
|
||||||
|
folders := []string{}
|
||||||
|
ids := []string{}
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
id := "folder-" + strconv.Itoa(i)
|
||||||
|
folder := `{
|
||||||
|
"kind": "Folder",
|
||||||
|
"title": "test",
|
||||||
|
"metadata": {
|
||||||
|
"uid": "` + id + `",
|
||||||
|
"name": "folder-` + strconv.Itoa(i) + `",
|
||||||
|
"namespace": "default"
|
||||||
|
},
|
||||||
|
"spec": {
|
||||||
|
"title": "test",
|
||||||
|
"description": "test"
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
folders = append(folders, folder)
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
return folders, ids
|
||||||
|
}
|
||||||
|
19
pkg/storage/unified/resource/testdata/folder-resource.json
vendored
Normal file
19
pkg/storage/unified/resource/testdata/folder-resource.json
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"kind": "Folder",
|
||||||
|
"apiVersion": "folder.grafana.app/v0alpha1",
|
||||||
|
"metadata": {
|
||||||
|
"name": "ae2ntrqxefvnke",
|
||||||
|
"namespace": "default",
|
||||||
|
"uid": "aaaa-bbbb",
|
||||||
|
"creationTimestamp": "2024-11-01T19:42:22Z",
|
||||||
|
"annotations": {
|
||||||
|
"grafana.app/createdBy": "user:1",
|
||||||
|
"grafana.app/originName": "SQL",
|
||||||
|
"grafana.app/originPath": "15",
|
||||||
|
"grafana.app/originTimestamp": "2024-11-01T19:42:22Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"spec": {
|
||||||
|
"title": "test-us"
|
||||||
|
}
|
||||||
|
}
|
19
pkg/storage/unified/resource/testdata/folder-resource2.json
vendored
Normal file
19
pkg/storage/unified/resource/testdata/folder-resource2.json
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"kind": "Folder",
|
||||||
|
"apiVersion": "folder.grafana.app/v0alpha1",
|
||||||
|
"metadata": {
|
||||||
|
"name": "ae2ntrqxefvnke",
|
||||||
|
"namespace": "default",
|
||||||
|
"uid": "cccc-dddd",
|
||||||
|
"creationTimestamp": "2024-11-01T19:42:22Z",
|
||||||
|
"annotations": {
|
||||||
|
"grafana.app/createdBy": "user:1",
|
||||||
|
"grafana.app/originName": "SQL",
|
||||||
|
"grafana.app/originPath": "15",
|
||||||
|
"grafana.app/originTimestamp": "2024-11-01T19:42:22Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"spec": {
|
||||||
|
"title": "a folder"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user