| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-04-20 22:20:45 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2016-03-14 18:59:51 +08:00
										 |  |  | 	"strconv" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2019-02-24 06:35:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/metrics" | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2022-05-17 21:51:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/accesscontrol" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards" | 
					
						
							| 
									
										
										
										
											2015-06-05 14:15:38 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/search" | 
					
						
							| 
									
										
										
										
											2022-05-17 21:51:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 01:46:38 +08:00
										 |  |  | func (hs *HTTPServer) Search(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2015-02-07 23:12:29 +08:00
										 |  |  | 	query := c.Query("query") | 
					
						
							| 
									
										
										
										
											2015-06-02 16:24:20 +08:00
										 |  |  | 	tags := c.QueryStrings("tag") | 
					
						
							| 
									
										
										
										
											2015-02-04 18:35:59 +08:00
										 |  |  | 	starred := c.Query("starred") | 
					
						
							| 
									
										
										
										
											2019-04-17 19:07:50 +08:00
										 |  |  | 	limit := c.QueryInt64("limit") | 
					
						
							|  |  |  | 	page := c.QueryInt64("page") | 
					
						
							| 
									
										
										
										
											2017-05-25 00:28:13 +08:00
										 |  |  | 	dashboardType := c.Query("type") | 
					
						
							| 
									
										
										
										
											2020-04-20 22:20:45 +08:00
										 |  |  | 	sort := c.Query("sort") | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	permission := models.PERMISSION_VIEW | 
					
						
							| 
									
										
										
										
											2015-02-05 18:10:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 19:07:50 +08:00
										 |  |  | 	if limit > 5000 { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(422, "Limit is above maximum allowed (5000), use page parameter to access hits beyond limit", nil) | 
					
						
							| 
									
										
										
										
											2015-02-05 18:10:56 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-02-04 18:35:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-09 00:11:01 +08:00
										 |  |  | 	if c.Query("permission") == "Edit" { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		permission = models.PERMISSION_EDIT | 
					
						
							| 
									
										
										
										
											2018-02-09 00:11:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	dbIDs := make([]int64, 0) | 
					
						
							| 
									
										
										
										
											2016-03-05 19:26:21 +08:00
										 |  |  | 	for _, id := range c.QueryStrings("dashboardIds") { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		dashboardID, err := strconv.ParseInt(id, 10, 64) | 
					
						
							| 
									
										
										
										
											2016-03-05 19:26:21 +08:00
										 |  |  | 		if err == nil { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 			dbIDs = append(dbIDs, dashboardID) | 
					
						
							| 
									
										
										
										
											2016-03-05 19:26:21 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	folderIDs := make([]int64, 0) | 
					
						
							| 
									
										
										
										
											2017-11-20 19:47:03 +08:00
										 |  |  | 	for _, id := range c.QueryStrings("folderIds") { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		folderID, err := strconv.ParseInt(id, 10, 64) | 
					
						
							| 
									
										
										
										
											2017-11-20 19:47:03 +08:00
										 |  |  | 		if err == nil { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 			folderIDs = append(folderIDs, folderID) | 
					
						
							| 
									
										
										
										
											2017-11-20 19:47:03 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 16:45:53 +08:00
										 |  |  | 	searchQuery := search.Query{ | 
					
						
							| 
									
										
										
										
											2016-03-05 19:26:21 +08:00
										 |  |  | 		Title:        query, | 
					
						
							|  |  |  | 		Tags:         tags, | 
					
						
							| 
									
										
										
										
											2017-06-17 08:33:53 +08:00
										 |  |  | 		SignedInUser: c.SignedInUser, | 
					
						
							| 
									
										
										
										
											2016-03-05 19:26:21 +08:00
										 |  |  | 		Limit:        limit, | 
					
						
							| 
									
										
										
										
											2019-04-17 19:07:50 +08:00
										 |  |  | 		Page:         page, | 
					
						
							| 
									
										
										
										
											2016-03-05 19:26:21 +08:00
										 |  |  | 		IsStarred:    starred == "true", | 
					
						
							|  |  |  | 		OrgId:        c.OrgId, | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		DashboardIds: dbIDs, | 
					
						
							| 
									
										
										
										
											2017-05-25 00:28:13 +08:00
										 |  |  | 		Type:         dashboardType, | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		FolderIds:    folderIDs, | 
					
						
							| 
									
										
										
										
											2018-02-09 00:11:01 +08:00
										 |  |  | 		Permission:   permission, | 
					
						
							| 
									
										
										
										
											2020-04-20 22:20:45 +08:00
										 |  |  | 		Sort:         sort, | 
					
						
							| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 01:46:38 +08:00
										 |  |  | 	err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery) | 
					
						
							| 
									
										
										
										
											2015-05-13 16:45:53 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Search failed", err) | 
					
						
							| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-17 21:51:44 +08:00
										 |  |  | 	defer c.TimeRequest(metrics.MApiDashboardSearch) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !c.QueryBool("accesscontrol") { | 
					
						
							|  |  |  | 		return response.JSON(http.StatusOK, searchQuery.Result) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return hs.searchHitsWithMetadata(c, searchQuery.Result) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) searchHitsWithMetadata(c *models.ReqContext, hits models.HitList) response.Response { | 
					
						
							|  |  |  | 	folderUIDs := make(map[string]bool) | 
					
						
							|  |  |  | 	dashboardUIDs := make(map[string]bool) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, hit := range hits { | 
					
						
							|  |  |  | 		if hit.Type == models.DashHitFolder { | 
					
						
							|  |  |  | 			folderUIDs[hit.UID] = true | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			dashboardUIDs[hit.UID] = true | 
					
						
							|  |  |  | 			folderUIDs[hit.FolderUID] = true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	folderMeta := hs.getMultiAccessControlMetadata(c, c.OrgId, dashboards.ScopeFoldersPrefix, folderUIDs) | 
					
						
							|  |  |  | 	dashboardMeta := hs.getMultiAccessControlMetadata(c, c.OrgId, dashboards.ScopeDashboardsPrefix, dashboardUIDs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// search hit with access control metadata attached
 | 
					
						
							|  |  |  | 	type hitWithMeta struct { | 
					
						
							|  |  |  | 		*models.Hit | 
					
						
							|  |  |  | 		AccessControl accesscontrol.Metadata `json:"accessControl,omitempty"` | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	hitsWithMeta := make([]hitWithMeta, 0, len(hits)) | 
					
						
							|  |  |  | 	for _, hit := range hits { | 
					
						
							|  |  |  | 		var meta accesscontrol.Metadata | 
					
						
							|  |  |  | 		if hit.Type == models.DashHitFolder { | 
					
						
							|  |  |  | 			meta = folderMeta[hit.UID] | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			meta = accesscontrol.MergeMeta("dashboards", dashboardMeta[hit.UID], folderMeta[hit.FolderUID]) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		hitsWithMeta = append(hitsWithMeta, hitWithMeta{hit, meta}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, hitsWithMeta) | 
					
						
							| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-20 22:20:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) ListSortOptions(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2020-04-20 22:20:45 +08:00
										 |  |  | 	opts := hs.SearchService.SortOptions() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res := []util.DynMap{} | 
					
						
							|  |  |  | 	for _, o := range opts { | 
					
						
							|  |  |  | 		res = append(res, util.DynMap{ | 
					
						
							|  |  |  | 			"name":        o.Name, | 
					
						
							|  |  |  | 			"displayName": o.DisplayName, | 
					
						
							|  |  |  | 			"description": o.Description, | 
					
						
							| 
									
										
										
										
											2021-02-11 15:49:16 +08:00
										 |  |  | 			"meta":        o.MetaName, | 
					
						
							| 
									
										
										
										
											2020-04-20 22:20:45 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2020-04-20 22:20:45 +08:00
										 |  |  | 		"sortOptions": res, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |