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:
@@ -13,12 +13,12 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"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/org"
|
||||
"github.com/grafana/grafana/pkg/services/publicdashboards/database"
|
||||
publicDashboardModels "github.com/grafana/grafana/pkg/services/publicdashboards/models"
|
||||
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
||||
"github.com/grafana/grafana/pkg/services/search/model"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
||||
"github.com/grafana/grafana/pkg/services/star"
|
||||
@@ -407,7 +407,7 @@ func TestIntegrationDashboardDataAccess(t *testing.T) {
|
||||
|
||||
require.Equal(t, len(query.Result), 1)
|
||||
hit := query.Result[0]
|
||||
require.Equal(t, hit.Type, models.DashHitFolder)
|
||||
require.Equal(t, hit.Type, model.DashHitFolder)
|
||||
require.Equal(t, hit.URL, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.UID, savedFolder.Slug))
|
||||
require.Equal(t, hit.FolderTitle, "")
|
||||
})
|
||||
@@ -648,8 +648,8 @@ func TestIntegrationDashboard_SortingOptions(t *testing.T) {
|
||||
1: {dashboards.ActionDashboardsRead: []string{dashboards.ScopeDashboardsAll}},
|
||||
},
|
||||
},
|
||||
Sort: models.SortOption{
|
||||
Filter: []models.SortOptionFilter{
|
||||
Sort: model.SortOption{
|
||||
Filter: []model.SortOptionFilter{
|
||||
searchstore.TitleSorter{Descending: true},
|
||||
},
|
||||
},
|
||||
@@ -850,18 +850,18 @@ func testSearchDashboards(d *DashboardStore, query *dashboards.FindPersistedDash
|
||||
}
|
||||
|
||||
func makeQueryResult(query *dashboards.FindPersistedDashboardsQuery, res []dashboards.DashboardSearchProjection) {
|
||||
query.Result = make([]*models.Hit, 0)
|
||||
hits := make(map[int64]*models.Hit)
|
||||
query.Result = make([]*model.Hit, 0)
|
||||
hits := make(map[int64]*model.Hit)
|
||||
|
||||
for _, item := range res {
|
||||
hit, exists := hits[item.ID]
|
||||
if !exists {
|
||||
hitType := models.DashHitDB
|
||||
hitType := model.DashHitDB
|
||||
if item.IsFolder {
|
||||
hitType = models.DashHitFolder
|
||||
hitType = model.DashHitFolder
|
||||
}
|
||||
|
||||
hit = &models.Hit{
|
||||
hit = &model.Hit{
|
||||
ID: item.ID,
|
||||
UID: item.UID,
|
||||
Title: item.Title,
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/folder"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
"github.com/grafana/grafana/pkg/services/search/model"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
@@ -412,9 +413,9 @@ type FindPersistedDashboardsQuery struct {
|
||||
Limit int64
|
||||
Page int64
|
||||
Permission PermissionType
|
||||
Sort models.SortOption
|
||||
Sort model.SortOption
|
||||
|
||||
Filters []interface{}
|
||||
|
||||
Result models.HitList
|
||||
Result model.HitList
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/appcontext"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/alerting"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
@@ -18,6 +17,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/folder"
|
||||
"github.com/grafana/grafana/pkg/services/guardian"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/search/model"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
@@ -553,25 +553,25 @@ func (dr *DashboardServiceImpl) SearchDashboards(ctx context.Context, query *das
|
||||
return nil
|
||||
}
|
||||
|
||||
func getHitType(item dashboards.DashboardSearchProjection) models.HitType {
|
||||
var hitType models.HitType
|
||||
func getHitType(item dashboards.DashboardSearchProjection) model.HitType {
|
||||
var hitType model.HitType
|
||||
if item.IsFolder {
|
||||
hitType = models.DashHitFolder
|
||||
hitType = model.DashHitFolder
|
||||
} else {
|
||||
hitType = models.DashHitDB
|
||||
hitType = model.DashHitDB
|
||||
}
|
||||
|
||||
return hitType
|
||||
}
|
||||
|
||||
func makeQueryResult(query *dashboards.FindPersistedDashboardsQuery, res []dashboards.DashboardSearchProjection) {
|
||||
query.Result = make([]*models.Hit, 0)
|
||||
hits := make(map[int64]*models.Hit)
|
||||
query.Result = make([]*model.Hit, 0)
|
||||
hits := make(map[int64]*model.Hit)
|
||||
|
||||
for _, item := range res {
|
||||
hit, exists := hits[item.ID]
|
||||
if !exists {
|
||||
hit = &models.Hit{
|
||||
hit = &model.Hit{
|
||||
ID: item.ID,
|
||||
UID: item.UID,
|
||||
Title: item.Title,
|
||||
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"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/folder"
|
||||
"github.com/grafana/grafana/pkg/services/guardian"
|
||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/services/search/model"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
@@ -333,7 +333,7 @@ func (st DBstore) GetUserVisibleNamespaces(ctx context.Context, orgID int64, use
|
||||
Type: searchstore.TypeAlertFolder,
|
||||
Limit: -1,
|
||||
Permission: dashboards.PERMISSION_VIEW,
|
||||
Sort: models.SortOption{},
|
||||
Sort: model.SortOption{},
|
||||
Filters: []interface{}{
|
||||
searchstore.FolderWithAlertsFilter{},
|
||||
},
|
||||
|
||||
@@ -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