grafana/pkg/services/search/service.go
Arve Knudsen 78596a6756
Migrate to Wire for dependency injection (#32289)
Fixes #30144

Co-authored-by: dsotirakis <sotirakis.dim@gmail.com>
Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
Co-authored-by: Ida Furjesova <ida.furjesova@grafana.com>
Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
Co-authored-by: Leon Sorokin <leeoniya@gmail.com>
Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com>
Co-authored-by: spinillos <selenepinillos@gmail.com>
Co-authored-by: Karl Persson <kalle.persson@grafana.com>
Co-authored-by: Leonard Gram <leo@xlson.com>
2021-08-25 15:11:22 +02:00

134 lines
2.5 KiB
Go

package search
import (
"sort"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
)
func ProvideService(cfg *setting.Cfg, bus bus.Bus) *SearchService {
s := &SearchService{
Cfg: cfg,
Bus: bus,
sortOptions: map[string]SortOption{
SortAlphaAsc.Name: SortAlphaAsc,
SortAlphaDesc.Name: SortAlphaDesc,
},
}
s.Bus.AddHandler(s.searchHandler)
return s
}
type Query struct {
Title string
Tags []string
OrgId int64
SignedInUser *models.SignedInUser
Limit int64
Page int64
IsStarred bool
Type string
DashboardIds []int64
FolderIds []int64
Permission models.PermissionType
Sort string
Result HitList
}
type FindPersistedDashboardsQuery struct {
Title string
OrgId int64
SignedInUser *models.SignedInUser
IsStarred bool
DashboardIds []int64
Type string
FolderIds []int64
Tags []string
Limit int64
Page int64
Permission models.PermissionType
Sort SortOption
Filters []interface{}
Result HitList
}
type SearchService struct {
Bus bus.Bus
Cfg *setting.Cfg
sortOptions map[string]SortOption
}
func (s *SearchService) searchHandler(query *Query) error {
dashboardQuery := FindPersistedDashboardsQuery{
Title: query.Title,
SignedInUser: query.SignedInUser,
IsStarred: query.IsStarred,
DashboardIds: query.DashboardIds,
Type: query.Type,
FolderIds: query.FolderIds,
Tags: query.Tags,
Limit: query.Limit,
Page: query.Page,
Permission: query.Permission,
}
if sortOpt, exists := s.sortOptions[query.Sort]; exists {
dashboardQuery.Sort = sortOpt
}
if err := bus.Dispatch(&dashboardQuery); err != nil {
return err
}
hits := dashboardQuery.Result
if query.Sort == "" {
hits = sortedHits(hits)
}
if err := setStarredDashboards(query.SignedInUser.UserId, hits); err != nil {
return err
}
query.Result = hits
return nil
}
func sortedHits(unsorted HitList) HitList {
hits := make(HitList, 0)
hits = append(hits, unsorted...)
sort.Sort(hits)
for _, hit := range hits {
sort.Strings(hit.Tags)
}
return hits
}
func setStarredDashboards(userID int64, hits []*Hit) error {
query := models.GetUserStarsQuery{
UserId: userID,
}
if err := bus.Dispatch(&query); err != nil {
return err
}
for _, dashboard := range hits {
if _, ok := query.Result[dashboard.ID]; ok {
dashboard.IsStarred = true
}
}
return nil
}