mirror of
https://github.com/grafana/grafana.git
synced 2025-01-16 19:52:33 -06:00
18e93c7077
* Chore: Remove bus * remove unused const
123 lines
2.5 KiB
Go
123 lines
2.5 KiB
Go
package search
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
"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, sqlstore *sqlstore.SQLStore) *SearchService {
|
|
s := &SearchService{
|
|
Cfg: cfg,
|
|
Bus: bus,
|
|
sortOptions: map[string]models.SortOption{
|
|
SortAlphaAsc.Name: SortAlphaAsc,
|
|
SortAlphaDesc.Name: SortAlphaDesc,
|
|
},
|
|
sqlstore: sqlstore,
|
|
}
|
|
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 models.HitList
|
|
}
|
|
|
|
type Service interface {
|
|
SearchHandler(context.Context, *Query) error
|
|
SortOptions() []models.SortOption
|
|
}
|
|
|
|
type SearchService struct {
|
|
Bus bus.Bus
|
|
Cfg *setting.Cfg
|
|
sortOptions map[string]models.SortOption
|
|
sqlstore sqlstore.Store
|
|
}
|
|
|
|
func (s *SearchService) SearchHandler(ctx context.Context, query *Query) error {
|
|
dashboardQuery := models.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 := s.sqlstore.SearchDashboards(ctx, &dashboardQuery); err != nil {
|
|
return err
|
|
}
|
|
|
|
hits := dashboardQuery.Result
|
|
if query.Sort == "" {
|
|
hits = sortedHits(hits)
|
|
}
|
|
|
|
if err := s.setStarredDashboards(ctx, query.SignedInUser.UserId, hits); err != nil {
|
|
return err
|
|
}
|
|
|
|
query.Result = hits
|
|
|
|
return nil
|
|
}
|
|
|
|
func sortedHits(unsorted models.HitList) models.HitList {
|
|
hits := make(models.HitList, 0)
|
|
hits = append(hits, unsorted...)
|
|
|
|
sort.Sort(hits)
|
|
|
|
for _, hit := range hits {
|
|
sort.Strings(hit.Tags)
|
|
}
|
|
|
|
return hits
|
|
}
|
|
|
|
func (s *SearchService) setStarredDashboards(ctx context.Context, userID int64, hits []*models.Hit) error {
|
|
query := models.GetUserStarsQuery{
|
|
UserId: userID,
|
|
}
|
|
|
|
err := s.sqlstore.GetUserStars(ctx, &query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
iuserstars := query.Result
|
|
for _, dashboard := range hits {
|
|
if _, ok := iuserstars[dashboard.ID]; ok {
|
|
dashboard.IsStarred = true
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|