Search: Support multiple order filters (#24230)
This commit is contained in:
@@ -216,12 +216,7 @@ type DashboardSearchProjection struct {
|
||||
}
|
||||
|
||||
func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSearchProjection, error) {
|
||||
if query.SortBy == nil {
|
||||
query.SortBy = searchstore.TitleSorter{}
|
||||
}
|
||||
|
||||
filters := []interface{}{
|
||||
query.SortBy,
|
||||
permissions.DashboardPermissionFilter{
|
||||
OrgRole: query.SignedInUser.OrgRole,
|
||||
OrgId: query.SignedInUser.OrgId,
|
||||
@@ -231,6 +226,8 @@ func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSear
|
||||
},
|
||||
}
|
||||
|
||||
filters = append(filters, query.Filters...)
|
||||
|
||||
if query.OrgId != 0 {
|
||||
filters = append(filters, searchstore.OrgFilter{OrgId: query.OrgId})
|
||||
} else if query.SignedInUser.OrgId != 0 {
|
||||
|
||||
@@ -9,9 +9,13 @@ import (
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/search"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDashboardDataAccess(t *testing.T) {
|
||||
@@ -401,6 +405,33 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestDashboard_SortingOptions(t *testing.T) {
|
||||
// insertTestDashboard uses GoConvey's assertions. Workaround.
|
||||
Convey("test with multiple sorting options", t, func() {
|
||||
InitTestDB(t)
|
||||
dashB := insertTestDashboard("Beta", 1, 0, false)
|
||||
dashA := insertTestDashboard("Alfa", 1, 0, false)
|
||||
|
||||
assert.NotZero(t, dashA.Id)
|
||||
assert.Less(t, dashB.Id, dashA.Id)
|
||||
|
||||
q := &search.FindPersistedDashboardsQuery{
|
||||
SignedInUser: &models.SignedInUser{OrgId: 1, UserId: 1, OrgRole: models.ROLE_ADMIN},
|
||||
// adding two sorting options (silly no-op example, but it'll complicate the query)
|
||||
Filters: []interface{}{
|
||||
searchstore.TitleSorter{},
|
||||
searchstore.TitleSorter{Descending: true},
|
||||
},
|
||||
}
|
||||
dashboards, err := findDashboards(q)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, dashboards, 2)
|
||||
assert.Equal(t, dashA.Id, dashboards[0].Id)
|
||||
assert.Equal(t, dashB.Id, dashboards[1].Id)
|
||||
})
|
||||
}
|
||||
|
||||
func insertTestDashboard(title string, orgId int64, folderId int64, isFolder bool, tags ...interface{}) *models.Dashboard {
|
||||
cmd := models.SaveDashboardCommand{
|
||||
OrgId: orgId,
|
||||
|
||||
@@ -113,13 +113,14 @@ func (b *Builder) applyFilters() (ordering string) {
|
||||
b.params = append(b.params, groupParams...)
|
||||
}
|
||||
|
||||
if len(orders) > 0 {
|
||||
orderBy := fmt.Sprintf(" ORDER BY %s", strings.Join(orders, ", "))
|
||||
b.sql.WriteString(orderBy)
|
||||
|
||||
order := strings.Join(orderJoins, "")
|
||||
order += orderBy
|
||||
return order
|
||||
if len(orders) < 1 {
|
||||
orders = append(orders, TitleSorter{}.OrderBy())
|
||||
}
|
||||
return " ORDER BY dashboard.id"
|
||||
|
||||
orderBy := fmt.Sprintf(" ORDER BY %s", strings.Join(orders, ", "))
|
||||
b.sql.WriteString(orderBy)
|
||||
|
||||
order := strings.Join(orderJoins, "")
|
||||
order += orderBy
|
||||
return order
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user