[search] - folder tests; search by ids (#95797)

[search] - folder tests; search by ids
This commit is contained in:
Scott Lepper 2024-11-05 09:34:54 -05:00 committed by GitHub
parent af6ddad058
commit ef29ce5b85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 128 additions and 10 deletions

View File

@ -31,16 +31,16 @@ type IndexResults struct {
}
func (ir IndexedResource) FromSearchHit(hit *search.DocumentMatch) IndexedResource {
ir.Uid = hit.Fields["Uid"].(string)
ir.Kind = hit.Fields["Kind"].(string)
ir.Name = hit.Fields["Name"].(string)
ir.Namespace = hit.Fields["Namespace"].(string)
ir.Group = hit.Fields["Group"].(string)
ir.CreatedAt = hit.Fields["CreatedAt"].(string)
ir.CreatedBy = hit.Fields["CreatedBy"].(string)
ir.UpdatedAt = hit.Fields["UpdatedAt"].(string)
ir.UpdatedBy = hit.Fields["UpdatedBy"].(string)
ir.Title = hit.Fields["Title"].(string)
ir.Uid = fieldValue("Uid", hit)
ir.Kind = fieldValue("Kind", hit)
ir.Name = fieldValue("Name", hit)
ir.Namespace = fieldValue("Namespace", hit)
ir.Group = fieldValue("Group", hit)
ir.CreatedAt = fieldValue("CreatedAt", hit)
ir.CreatedBy = fieldValue("CreatedBy", hit)
ir.UpdatedAt = fieldValue("UpdatedAt", hit)
ir.UpdatedBy = fieldValue("UpdatedBy", hit)
ir.Title = fieldValue("Title", hit)
// add indexed spec fields to search results
specResult := map[string]any{}
@ -55,6 +55,13 @@ func (ir IndexedResource) FromSearchHit(hit *search.DocumentMatch) IndexedResour
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.
// rawResource is the raw json for the resource from unified storage.
func NewIndexedResource(rawResource []byte) (*IndexedResource, error) {

View File

@ -32,6 +32,52 @@ func TestIndexDashboard(t *testing.T) {
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) {
data := readTestData(t, "dashboard-tagged-resource.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) {
req := &SearchRequest{Query: search, Tenant: testTenant, Limit: expected + 1, Offset: 0}
start := time.Now()
results, err := index.Search(testContext, req)
require.NoError(t, err)
elapsed := time.Since(start)
fmt.Println("Search time:", elapsed)
assert.Equal(t, expected, int64(len(results.Values)))
}
@ -163,3 +212,27 @@ func readTestData(t *testing.T, name string) []byte {
require.NoError(t, err)
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
}

View 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"
}
}

View 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"
}
}