LibraryPanels: Replace folderID with folderUID (#56414)
* user essentials mob! 🔱 lastFile:pkg/services/libraryelements/writers.go * user essentials mob! 🔱 lastFile:pkg/services/libraryelements/writers.go * user essentials mob! 🔱 lastFile:pkg/services/libraryelements/writers.go * user essentials mob! 🔱 lastFile:pkg/services/libraryelements/writers.go * user essentials mob! 🔱 lastFile:pkg/services/libraryelements/database.go * user essentials mob! 🔱 lastFile:pkg/services/libraryelements/writers.go * user essentials mob! 🔱 lastFile:pkg/services/libraryelements/writers.go * user essentials mob! 🔱 * support filterFolderUIDs in the frontend * move common logic to a variable * fixed FolderLibraryPanelsPage and improved unit test * fix backend lint error * fix formatting error Co-authored-by: Joao Silva <joao.silva@grafana.com> Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com> Co-authored-by: eledobleefe <laura.fernandez@grafana.com> Co-authored-by: joshhunt <josh@trtr.co>
This commit is contained in:
co-authored by
Joao Silva
Ashley Harrison
eledobleefe
joshhunt
parent
5a99aff7b8
commit
932429a545
@@ -133,14 +133,15 @@ func (l *LibraryElementService) getHandler(c *models.ReqContext) response.Respon
|
||||
// 500: internalServerError
|
||||
func (l *LibraryElementService) getAllHandler(c *models.ReqContext) response.Response {
|
||||
query := searchLibraryElementsQuery{
|
||||
perPage: c.QueryInt("perPage"),
|
||||
page: c.QueryInt("page"),
|
||||
searchString: c.Query("searchString"),
|
||||
sortDirection: c.Query("sortDirection"),
|
||||
kind: c.QueryInt("kind"),
|
||||
typeFilter: c.Query("typeFilter"),
|
||||
excludeUID: c.Query("excludeUid"),
|
||||
folderFilter: c.Query("folderFilter"),
|
||||
perPage: c.QueryInt("perPage"),
|
||||
page: c.QueryInt("page"),
|
||||
searchString: c.Query("searchString"),
|
||||
sortDirection: c.Query("sortDirection"),
|
||||
kind: c.QueryInt("kind"),
|
||||
typeFilter: c.Query("typeFilter"),
|
||||
excludeUID: c.Query("excludeUid"),
|
||||
folderFilter: c.Query("folderFilter"),
|
||||
folderFilterUIDs: c.Query("folderFilterUIDs"),
|
||||
}
|
||||
elementsResult, err := l.getAllLibraryElements(c.Req.Context(), c.SignedInUser, query)
|
||||
if err != nil {
|
||||
|
||||
@@ -409,6 +409,7 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
|
||||
var libraryElements []LibraryElement
|
||||
countBuilder := db.SQLBuilder{}
|
||||
countBuilder.Write("SELECT * FROM library_element AS le")
|
||||
countBuilder.Write(" INNER JOIN dashboard AS dashboard on le.folder_id = dashboard.id")
|
||||
countBuilder.Write(` WHERE le.org_id=?`, signedInUser.OrgID)
|
||||
writeKindSQL(query, &countBuilder)
|
||||
writeSearchStringSQL(query, l.SQLStore, &countBuilder)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/guardian"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
@@ -14,6 +15,10 @@ func isGeneralFolder(folderID int64) bool {
|
||||
return folderID == 0
|
||||
}
|
||||
|
||||
func isUIDGeneralFolder(folderUID string) bool {
|
||||
return folderUID == accesscontrol.GeneralFolderUID
|
||||
}
|
||||
|
||||
func (l *LibraryElementService) requireSupportedElementKind(kindAsInt int64) error {
|
||||
kind := models.LibraryElementKind(kindAsInt)
|
||||
switch kind {
|
||||
|
||||
@@ -207,14 +207,15 @@ type PatchLibraryElementCommand struct {
|
||||
|
||||
// searchLibraryElementsQuery is the query used for searching for Elements
|
||||
type searchLibraryElementsQuery struct {
|
||||
perPage int
|
||||
page int
|
||||
searchString string
|
||||
sortDirection string
|
||||
kind int
|
||||
typeFilter string
|
||||
excludeUID string
|
||||
folderFilter string
|
||||
perPage int
|
||||
page int
|
||||
searchString string
|
||||
sortDirection string
|
||||
kind int
|
||||
typeFilter string
|
||||
excludeUID string
|
||||
folderFilter string
|
||||
folderFilterUIDs string
|
||||
}
|
||||
|
||||
// LibraryElementResponse is a response struct for LibraryElementDTO.
|
||||
|
||||
@@ -2,6 +2,7 @@ package libraryelements
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -72,41 +73,58 @@ func writeExcludeSQL(query searchLibraryElementsQuery, builder *db.SQLBuilder) {
|
||||
type FolderFilter struct {
|
||||
includeGeneralFolder bool
|
||||
folderIDs []string
|
||||
folderUIDs []string
|
||||
parseError error
|
||||
}
|
||||
|
||||
func parseFolderFilter(query searchLibraryElementsQuery) FolderFilter {
|
||||
folderIDs := make([]string, 0)
|
||||
if len(strings.TrimSpace(query.folderFilter)) == 0 {
|
||||
return FolderFilter{
|
||||
includeGeneralFolder: true,
|
||||
folderIDs: folderIDs,
|
||||
parseError: nil,
|
||||
}
|
||||
}
|
||||
folderUIDs := make([]string, 0)
|
||||
hasFolderFilter := len(strings.TrimSpace(query.folderFilter)) > 0
|
||||
hasFolderFilterUID := len(strings.TrimSpace(query.folderFilterUIDs)) > 0
|
||||
|
||||
includeGeneralFolder := false
|
||||
folderIDs = strings.Split(query.folderFilter, ",")
|
||||
for _, filter := range folderIDs {
|
||||
folderID, err := strconv.ParseInt(filter, 10, 64)
|
||||
if err != nil {
|
||||
return FolderFilter{
|
||||
includeGeneralFolder: false,
|
||||
folderIDs: folderIDs,
|
||||
parseError: err,
|
||||
}
|
||||
}
|
||||
if isGeneralFolder(folderID) {
|
||||
includeGeneralFolder = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return FolderFilter{
|
||||
includeGeneralFolder: includeGeneralFolder,
|
||||
result := FolderFilter{
|
||||
includeGeneralFolder: true,
|
||||
folderIDs: folderIDs,
|
||||
folderUIDs: folderUIDs,
|
||||
parseError: nil,
|
||||
}
|
||||
|
||||
if hasFolderFilter && hasFolderFilterUID {
|
||||
result.parseError = errors.New("cannot pass both folderFilter and folderFilterUIDs")
|
||||
return result
|
||||
}
|
||||
|
||||
if hasFolderFilter {
|
||||
result.includeGeneralFolder = false
|
||||
folderIDs = strings.Split(query.folderFilter, ",")
|
||||
result.folderIDs = folderIDs
|
||||
for _, filter := range folderIDs {
|
||||
folderID, err := strconv.ParseInt(filter, 10, 64)
|
||||
if err != nil {
|
||||
result.parseError = err
|
||||
}
|
||||
if isGeneralFolder(folderID) {
|
||||
result.includeGeneralFolder = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if hasFolderFilterUID {
|
||||
result.includeGeneralFolder = false
|
||||
folderUIDs = strings.Split(query.folderFilterUIDs, ",")
|
||||
result.folderUIDs = folderUIDs
|
||||
|
||||
for _, folderUID := range folderUIDs {
|
||||
if isUIDGeneralFolder(folderUID) {
|
||||
result.includeGeneralFolder = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (f *FolderFilter) writeFolderFilterSQL(includeGeneral bool, builder *db.SQLBuilder) error {
|
||||
@@ -127,5 +145,17 @@ func (f *FolderFilter) writeFolderFilterSQL(includeGeneral bool, builder *db.SQL
|
||||
builder.Write(sql.String(), params...)
|
||||
}
|
||||
|
||||
paramsUIDs := make([]interface{}, 0)
|
||||
for _, folderUID := range f.folderUIDs {
|
||||
if !includeGeneral && isUIDGeneralFolder(folderUID) {
|
||||
continue
|
||||
}
|
||||
paramsUIDs = append(paramsUIDs, folderUID)
|
||||
}
|
||||
if len(paramsUIDs) > 0 {
|
||||
sql.WriteString(` AND dashboard.uid IN (?` + strings.Repeat(",?", len(paramsUIDs)-1) + ")")
|
||||
builder.Write(sql.String(), paramsUIDs...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user