mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Move search model from models package to search service (#62215)
* Chore: Move search model from models package to search service * Remove unused imports * Cleanup after merge
This commit is contained in:
62
pkg/services/search/model/model.go
Normal file
62
pkg/services/search/model/model.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
||||
)
|
||||
|
||||
type SortOption struct {
|
||||
Name string
|
||||
DisplayName string
|
||||
Description string
|
||||
Index int
|
||||
MetaName string
|
||||
Filter []SortOptionFilter
|
||||
}
|
||||
|
||||
type SortOptionFilter interface {
|
||||
searchstore.FilterOrderBy
|
||||
}
|
||||
|
||||
type HitType string
|
||||
|
||||
const (
|
||||
DashHitDB HitType = "dash-db"
|
||||
DashHitHome HitType = "dash-home"
|
||||
DashHitFolder HitType = "dash-folder"
|
||||
)
|
||||
|
||||
type Hit struct {
|
||||
ID int64 `json:"id"`
|
||||
UID string `json:"uid"`
|
||||
Title string `json:"title"`
|
||||
URI string `json:"uri"`
|
||||
URL string `json:"url"`
|
||||
Slug string `json:"slug"`
|
||||
Type HitType `json:"type"`
|
||||
Tags []string `json:"tags"`
|
||||
IsStarred bool `json:"isStarred"`
|
||||
FolderID int64 `json:"folderId,omitempty"`
|
||||
FolderUID string `json:"folderUid,omitempty"`
|
||||
FolderTitle string `json:"folderTitle,omitempty"`
|
||||
FolderURL string `json:"folderUrl,omitempty"`
|
||||
SortMeta int64 `json:"sortMeta"`
|
||||
SortMetaName string `json:"sortMetaName,omitempty"`
|
||||
}
|
||||
|
||||
type HitList []*Hit
|
||||
|
||||
func (s HitList) Len() int { return len(s) }
|
||||
func (s HitList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s HitList) Less(i, j int) bool {
|
||||
if s[i].Type == "dash-folder" && s[j].Type == "dash-db" {
|
||||
return true
|
||||
}
|
||||
|
||||
if s[i].Type == "dash-db" && s[j].Type == "dash-folder" {
|
||||
return false
|
||||
}
|
||||
|
||||
return strings.ToLower(s[i].Title) < strings.ToLower(s[j].Title)
|
||||
}
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"sort"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/search/model"
|
||||
"github.com/grafana/grafana/pkg/services/star"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
func ProvideService(cfg *setting.Cfg, sqlstore db.DB, starService star.Service, dashboardService dashboards.DashboardService) *SearchService {
|
||||
s := &SearchService{
|
||||
Cfg: cfg,
|
||||
sortOptions: map[string]models.SortOption{
|
||||
sortOptions: map[string]model.SortOption{
|
||||
SortAlphaAsc.Name: SortAlphaAsc,
|
||||
SortAlphaDesc.Name: SortAlphaDesc,
|
||||
},
|
||||
@@ -41,17 +41,17 @@ type Query struct {
|
||||
Permission dashboards.PermissionType
|
||||
Sort string
|
||||
|
||||
Result models.HitList
|
||||
Result model.HitList
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
SearchHandler(context.Context, *Query) error
|
||||
SortOptions() []models.SortOption
|
||||
SortOptions() []model.SortOption
|
||||
}
|
||||
|
||||
type SearchService struct {
|
||||
Cfg *setting.Cfg
|
||||
sortOptions map[string]models.SortOption
|
||||
sortOptions map[string]model.SortOption
|
||||
sqlstore db.DB
|
||||
starService star.Service
|
||||
dashboardService dashboards.DashboardService
|
||||
@@ -94,8 +94,8 @@ func (s *SearchService) SearchHandler(ctx context.Context, query *Query) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func sortedHits(unsorted models.HitList) models.HitList {
|
||||
hits := make(models.HitList, 0)
|
||||
func sortedHits(unsorted model.HitList) model.HitList {
|
||||
hits := make(model.HitList, 0)
|
||||
hits = append(hits, unsorted...)
|
||||
|
||||
sort.Sort(hits)
|
||||
@@ -107,7 +107,7 @@ func sortedHits(unsorted models.HitList) models.HitList {
|
||||
return hits
|
||||
}
|
||||
|
||||
func (s *SearchService) setStarredDashboards(ctx context.Context, userID int64, hits []*models.Hit) error {
|
||||
func (s *SearchService) setStarredDashboards(ctx context.Context, userID int64, hits []*model.Hit) error {
|
||||
query := star.GetUserStarsQuery{
|
||||
UserID: userID,
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db/dbtest"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/search/model"
|
||||
"github.com/grafana/grafana/pkg/services/star"
|
||||
"github.com/grafana/grafana/pkg/services/star/startest"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
@@ -24,12 +24,12 @@ func TestSearch_SortedResults(t *testing.T) {
|
||||
ds := dashboards.NewFakeDashboardService(t)
|
||||
ds.On("SearchDashboards", mock.Anything, mock.AnythingOfType("*dashboards.FindPersistedDashboardsQuery")).Run(func(args mock.Arguments) {
|
||||
q := args.Get(1).(*dashboards.FindPersistedDashboardsQuery)
|
||||
q.Result = models.HitList{
|
||||
&models.Hit{ID: 16, Title: "CCAA", Type: "dash-db", Tags: []string{"BB", "AA"}},
|
||||
&models.Hit{ID: 10, Title: "AABB", Type: "dash-db", Tags: []string{"CC", "AA"}},
|
||||
&models.Hit{ID: 15, Title: "BBAA", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
|
||||
&models.Hit{ID: 25, Title: "bbAAa", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
|
||||
&models.Hit{ID: 17, Title: "FOLDER", Type: "dash-folder"},
|
||||
q.Result = model.HitList{
|
||||
&model.Hit{ID: 16, Title: "CCAA", Type: "dash-db", Tags: []string{"BB", "AA"}},
|
||||
&model.Hit{ID: 10, Title: "AABB", Type: "dash-db", Tags: []string{"CC", "AA"}},
|
||||
&model.Hit{ID: 15, Title: "BBAA", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
|
||||
&model.Hit{ID: 25, Title: "bbAAa", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
|
||||
&model.Hit{ID: 17, Title: "FOLDER", Type: "dash-folder"},
|
||||
}
|
||||
}).Return(nil)
|
||||
us.ExpectedSignedInUser = &user.SignedInUser{IsGrafanaAdmin: true}
|
||||
|
||||
@@ -3,26 +3,26 @@ package search
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/search/model"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
||||
)
|
||||
|
||||
var (
|
||||
SortAlphaAsc = models.SortOption{
|
||||
SortAlphaAsc = model.SortOption{
|
||||
Name: "alpha-asc",
|
||||
DisplayName: "Alphabetically (A–Z)",
|
||||
Description: "Sort results in an alphabetically ascending order",
|
||||
Index: 0,
|
||||
Filter: []models.SortOptionFilter{
|
||||
Filter: []model.SortOptionFilter{
|
||||
searchstore.TitleSorter{},
|
||||
},
|
||||
}
|
||||
SortAlphaDesc = models.SortOption{
|
||||
SortAlphaDesc = model.SortOption{
|
||||
Name: "alpha-desc",
|
||||
DisplayName: "Alphabetically (Z–A)",
|
||||
Description: "Sort results in an alphabetically descending order",
|
||||
Index: 0,
|
||||
Filter: []models.SortOptionFilter{
|
||||
Filter: []model.SortOptionFilter{
|
||||
searchstore.TitleSorter{Descending: true},
|
||||
},
|
||||
}
|
||||
@@ -30,12 +30,12 @@ var (
|
||||
|
||||
// RegisterSortOption allows for hooking in more search options from
|
||||
// other services.
|
||||
func (s *SearchService) RegisterSortOption(option models.SortOption) {
|
||||
func (s *SearchService) RegisterSortOption(option model.SortOption) {
|
||||
s.sortOptions[option.Name] = option
|
||||
}
|
||||
|
||||
func (s *SearchService) SortOptions() []models.SortOption {
|
||||
opts := make([]models.SortOption, 0, len(s.sortOptions))
|
||||
func (s *SearchService) SortOptions() []model.SortOption {
|
||||
opts := make([]model.SortOption, 0, len(s.sortOptions))
|
||||
for _, o := range s.sortOptions {
|
||||
opts = append(opts, o)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user