Search: Replace search implementation (#23855)
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
@@ -217,11 +216,11 @@ type DashboardSearchProjection struct {
|
||||
}
|
||||
|
||||
func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSearchProjection, error) {
|
||||
sb := NewSearchBuilder(query.SignedInUser, query.Limit, query.Page, query.Permission).
|
||||
WithTags(query.Tags).
|
||||
WithDashboardIdsIn(query.DashboardIds)
|
||||
if query.SortBy == nil {
|
||||
query.SortBy = searchstore.TitleSorter{}
|
||||
}
|
||||
|
||||
sb2filters := []interface{}{
|
||||
filters := []interface{}{
|
||||
query.SortBy,
|
||||
permissions.DashboardPermissionFilter{
|
||||
OrgRole: query.SignedInUser.OrgRole,
|
||||
@@ -232,72 +231,55 @@ func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSear
|
||||
},
|
||||
}
|
||||
|
||||
if query.OrgId != 0 {
|
||||
filters = append(filters, searchstore.OrgFilter{OrgId: query.OrgId})
|
||||
} else if query.SignedInUser.OrgId != 0 {
|
||||
filters = append(filters, searchstore.OrgFilter{OrgId: query.SignedInUser.OrgId})
|
||||
}
|
||||
|
||||
if len(query.Tags) > 0 {
|
||||
sb2filters = append(sb2filters, searchstore.TagsFilter{Tags: query.Tags})
|
||||
filters = append(filters, searchstore.TagsFilter{Tags: query.Tags})
|
||||
}
|
||||
|
||||
if len(query.DashboardIds) > 0 {
|
||||
sb2filters = append(sb2filters, searchstore.DashboardFilter{IDs: query.DashboardIds})
|
||||
filters = append(filters, searchstore.DashboardFilter{IDs: query.DashboardIds})
|
||||
}
|
||||
|
||||
if query.IsStarred {
|
||||
sb.IsStarred()
|
||||
sb2filters = append(sb2filters, searchstore.StarredFilter{UserId: query.SignedInUser.UserId})
|
||||
filters = append(filters, searchstore.StarredFilter{UserId: query.SignedInUser.UserId})
|
||||
}
|
||||
|
||||
if len(query.Title) > 0 {
|
||||
sb.WithTitle(query.Title)
|
||||
sb2filters = append(sb2filters, searchstore.TitleFilter{Dialect: dialect, Title: query.Title})
|
||||
filters = append(filters, searchstore.TitleFilter{Dialect: dialect, Title: query.Title})
|
||||
}
|
||||
|
||||
if len(query.Type) > 0 {
|
||||
sb.WithType(query.Type)
|
||||
sb2filters = append(sb2filters, searchstore.TypeFilter{Dialect: dialect, Type: query.Type})
|
||||
filters = append(filters, searchstore.TypeFilter{Dialect: dialect, Type: query.Type})
|
||||
}
|
||||
|
||||
if len(query.FolderIds) > 0 {
|
||||
sb.WithFolderIds(query.FolderIds)
|
||||
sb2filters = append(sb2filters, searchstore.FolderFilter{IDs: query.FolderIds})
|
||||
filters = append(filters, searchstore.FolderFilter{IDs: query.FolderIds})
|
||||
}
|
||||
|
||||
var res []DashboardSearchProjection
|
||||
sb := &searchstore.Builder{Dialect: dialect, Filters: filters}
|
||||
|
||||
sql, params := sb.ToSql()
|
||||
limit := query.Limit
|
||||
if limit < 1 {
|
||||
limit = 1000
|
||||
}
|
||||
|
||||
page := query.Page
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
sql, params := sb.ToSql(limit, page)
|
||||
err := x.SQL(sql, params...).Find(&res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if query.FeatureSearch2 {
|
||||
var res2 []DashboardSearchProjection
|
||||
sb := &searchstore.Builder{Dialect: dialect, Filters: sb2filters}
|
||||
limit := query.Limit
|
||||
if limit < 1 {
|
||||
limit = 1000
|
||||
}
|
||||
|
||||
page := query.Page
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
shadowSql, params := sb.ToSql(limit, page)
|
||||
err = x.SQL(shadowSql, params...).Find(&res2)
|
||||
|
||||
equal := reflect.DeepEqual(res2, res)
|
||||
shadowSearchCounter.With(prometheus.Labels{
|
||||
"equal": strconv.FormatBool(equal),
|
||||
"error": strconv.FormatBool(err != nil),
|
||||
}).Inc()
|
||||
sqlog.Debug(
|
||||
"shadow search query result",
|
||||
"err", err,
|
||||
"equal", equal,
|
||||
"shadowQuery", strings.Replace(strings.Replace(shadowSql, "\n", " ", -1), "\t", " ", -1),
|
||||
"query", strings.Replace(strings.Replace(sql, "\n", " ", -1), "\t", " ", -1),
|
||||
)
|
||||
return res2, nil
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user