search: use US searcher from dashboard list page (#95888)

search: wire up unified storage searcher
This commit is contained in:
Scott Lepper
2024-11-05 14:30:10 -05:00
committed by GitHub
parent 78c5fe61df
commit 6d8ee5fde0
4 changed files with 343 additions and 6 deletions
+72 -4
View File
@@ -2,7 +2,10 @@ package unifiedSearch
import (
"context"
"encoding/json"
"errors"
"strings"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
@@ -170,12 +173,77 @@ func (s *StandardSearchService) doSearchQuery(ctx context.Context, qry Query, _
return response
}
// TODO: implement this correctly
frame := data.NewFrame("results", data.NewField("value", nil, []string{}))
frame.Meta = &data.FrameMeta{Notices: []data.Notice{{Text: "TODO"}}}
fScore := data.NewFieldFromFieldType(data.FieldTypeFloat64, 0)
fUID := data.NewFieldFromFieldType(data.FieldTypeString, 0)
fKind := data.NewFieldFromFieldType(data.FieldTypeString, 0)
fName := data.NewFieldFromFieldType(data.FieldTypeString, 0)
fURL := data.NewFieldFromFieldType(data.FieldTypeString, 0)
fLocation := data.NewFieldFromFieldType(data.FieldTypeString, 0)
fTags := data.NewFieldFromFieldType(data.FieldTypeNullableJSON, 0)
fScore.Name = "score"
fUID.Name = "uid"
fKind.Name = "kind"
fName.Name = "name"
fLocation.Name = "location"
fURL.Name = "url"
fURL.Config = &data.FieldConfig{
Links: []data.DataLink{
{Title: "link", URL: "${__value.text}"},
},
}
fTags.Name = "tags"
frame := data.NewFrame("Query results", fKind, fUID, fName, fURL, fTags, fLocation)
frame.SetMeta(&data.FrameMeta{
Type: "search-results",
Custom: &customMeta{
Count: uint64(len(res.Items)),
},
})
for _, r := range res.Items {
frame.AppendRow(string(r.Value))
doc, err := getDoc(r.Value)
if err != nil {
s.logger.Error("Failed to parse doc", "error", err)
response.Error = err
return response
}
kind := strings.ToLower(doc.Kind)
frame.AppendRow(kind, doc.UID, doc.Spec.Title, "", nil, doc.FolderID)
}
response.Frames = append(response.Frames, frame)
return response
}
type customMeta struct {
Count uint64 `json:"count"`
MaxScore float64 `json:"max_score,omitempty"`
SortBy string `json:"sortBy,omitempty"`
}
type DashboardListDoc struct {
UID string `json:"Uid"`
Group string `json:"Group"`
Namespace string `json:"Namespace"`
Kind string `json:"Kind"`
Name string `json:"Name"`
CreatedAt time.Time `json:"CreatedAt"`
CreatedBy string `json:"CreatedBy"`
UpdatedAt time.Time `json:"UpdatedAt"`
UpdatedBy string `json:"UpdatedBy"`
FolderID string `json:"FolderId"`
Spec struct {
Title string `json:"title"`
} `json:"Spec"`
}
func getDoc(data []byte) (*DashboardListDoc, error) {
res := &DashboardListDoc{}
err := json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}