| 
									
										
										
										
											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" | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 18:20:04 +08:00
										 |  |  |  | 	"github.com/grafana/grafana/pkg/services/org" | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  |  | 	contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | 
					
						
							| 
									
										
										
										
											2023-11-22 21:20:22 +08:00
										 |  |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards/dashboardaccess" | 
					
						
							| 
									
										
										
										
											2015-06-05 14:15:38 +08:00
										 |  |  |  | 	"github.com/grafana/grafana/pkg/services/search" | 
					
						
							| 
									
										
										
										
											2023-01-30 22:17:53 +08:00
										 |  |  |  | 	"github.com/grafana/grafana/pkg/services/search/model" | 
					
						
							| 
									
										
										
										
											2022-05-17 21:51:44 +08:00
										 |  |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | // swagger:route GET /search search search
 | 
					
						
							|  |  |  |  | //
 | 
					
						
							|  |  |  |  | // Responses:
 | 
					
						
							|  |  |  |  | // 200: searchResponse
 | 
					
						
							|  |  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  |  | // 422: unprocessableEntityError
 | 
					
						
							|  |  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  |  | func (hs *HTTPServer) Search(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2024-08-06 09:17:39 +08:00
										 |  |  |  | 	c, span := hs.injectSpan(c, "api.Search") | 
					
						
							|  |  |  |  | 	defer span.End() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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") | 
					
						
							| 
									
										
										
										
											2024-05-17 01:36:26 +08:00
										 |  |  |  | 	deleted := c.Query("deleted") | 
					
						
							| 
									
										
										
										
											2023-11-22 21:20:22 +08:00
										 |  |  |  | 	permission := dashboardaccess.PERMISSION_VIEW | 
					
						
							| 
									
										
										
										
											2015-02-05 18:10:56 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 18:20:04 +08:00
										 |  |  |  | 	if deleted == "true" && c.SignedInUser.GetOrgRole() != org.RoleAdmin { | 
					
						
							|  |  |  |  | 		return response.Error(http.StatusUnauthorized, "Unauthorized", nil) | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 19:07:50 +08:00
										 |  |  |  | 	if limit > 5000 { | 
					
						
							| 
									
										
										
										
											2024-02-28 00:39:51 +08:00
										 |  |  |  | 		return response.Error(http.StatusUnprocessableEntity, "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" { | 
					
						
							| 
									
										
										
										
											2023-11-22 21:20:22 +08:00
										 |  |  |  | 		permission = dashboardaccess.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
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 10:28:14 +08:00
										 |  |  |  | 	dbUIDs := c.QueryStrings("dashboardUIDs") | 
					
						
							|  |  |  |  | 	if len(dbUIDs) == 0 { | 
					
						
							|  |  |  |  | 		// To keep it for now backward compatible for grafana 9
 | 
					
						
							|  |  |  |  | 		dbUIDs = c.QueryStrings("dashboardUID") | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-06-03 03:56:01 +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) | 
					
						
							| 
									
										
										
										
											2024-01-24 19:39:11 +08:00
										 |  |  |  | 			metrics.MFolderIDsAPICount.WithLabelValues(metrics.Search).Inc() | 
					
						
							| 
									
										
										
										
											2017-11-20 19:47:03 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 17:43:47 +08:00
										 |  |  |  | 	folderUIDs := c.QueryStrings("folderUIDs") | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	bothDashboardIds := len(dbIDs) > 0 && len(dbUIDs) > 0 | 
					
						
							|  |  |  |  | 	bothFolderIds := len(folderIDs) > 0 && len(folderUIDs) > 0 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if bothDashboardIds || bothFolderIds { | 
					
						
							| 
									
										
										
										
											2024-02-28 00:39:51 +08:00
										 |  |  |  | 		return response.Error(http.StatusBadRequest, "search supports UIDs or IDs, not both", nil) | 
					
						
							| 
									
										
										
										
											2022-06-03 03:56:01 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 16:45:53 +08:00
										 |  |  |  | 	searchQuery := search.Query{ | 
					
						
							| 
									
										
										
										
											2022-06-03 03:56:01 +08:00
										 |  |  |  | 		Title:         query, | 
					
						
							|  |  |  |  | 		Tags:          tags, | 
					
						
							|  |  |  |  | 		SignedInUser:  c.SignedInUser, | 
					
						
							|  |  |  |  | 		Limit:         limit, | 
					
						
							|  |  |  |  | 		Page:          page, | 
					
						
							|  |  |  |  | 		IsStarred:     starred == "true", | 
					
						
							| 
									
										
										
										
											2024-05-17 01:36:26 +08:00
										 |  |  |  | 		IsDeleted:     deleted == "true", | 
					
						
							| 
									
										
										
										
											2023-10-06 17:34:36 +08:00
										 |  |  |  | 		OrgId:         c.SignedInUser.GetOrgID(), | 
					
						
							| 
									
										
										
										
											2022-06-03 03:56:01 +08:00
										 |  |  |  | 		DashboardIds:  dbIDs, | 
					
						
							|  |  |  |  | 		DashboardUIDs: dbUIDs, | 
					
						
							|  |  |  |  | 		Type:          dashboardType, | 
					
						
							| 
									
										
										
										
											2023-11-07 22:51:44 +08:00
										 |  |  |  | 		FolderIds:     folderIDs, // nolint:staticcheck
 | 
					
						
							| 
									
										
										
										
											2023-08-04 17:43:47 +08:00
										 |  |  |  | 		FolderUIDs:    folderUIDs, | 
					
						
							| 
									
										
										
										
											2022-06-03 03:56:01 +08:00
										 |  |  |  | 		Permission:    permission, | 
					
						
							|  |  |  |  | 		Sort:          sort, | 
					
						
							| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-30 17:28:12 +08:00
										 |  |  |  | 	hits, err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery) | 
					
						
							| 
									
										
										
										
											2015-05-13 16:45:53 +08:00
										 |  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-02-28 00:39:51 +08:00
										 |  |  |  | 		return response.Error(http.StatusInternalServerError, "Search failed", err) | 
					
						
							| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-17 21:51:44 +08:00
										 |  |  |  | 	defer c.TimeRequest(metrics.MApiDashboardSearch) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-21 22:05:11 +08:00
										 |  |  |  | 	return response.JSON(http.StatusOK, hits) | 
					
						
							| 
									
										
										
										
											2015-01-08 16:00:00 +08:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-20 22:20:45 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | // swagger:route GET /search/sorting search listSortOptions
 | 
					
						
							|  |  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-09-12 15:40:35 +08:00
										 |  |  |  | // List search sorting options.
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | //
 | 
					
						
							|  |  |  |  | // Responses:
 | 
					
						
							|  |  |  |  | // 200: listSortOptionsResponse
 | 
					
						
							|  |  |  |  | // 401: unauthorisedError
 | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  |  | func (hs *HTTPServer) ListSortOptions(c *contextmodel.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, | 
					
						
							|  |  |  |  | 	}) | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | // swagger:parameters search
 | 
					
						
							|  |  |  |  | type SearchParams struct { | 
					
						
							|  |  |  |  | 	// Search Query
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	Query string `json:"query"` | 
					
						
							|  |  |  |  | 	// List of tags to search for
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	// type: array
 | 
					
						
							|  |  |  |  | 	// collectionFormat: multi
 | 
					
						
							|  |  |  |  | 	Tag []string `json:"tag"` | 
					
						
							|  |  |  |  | 	// Type to search for, dash-folder or dash-db
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	// Description:
 | 
					
						
							|  |  |  |  | 	// * `dash-folder` - Search for folder
 | 
					
						
							|  |  |  |  | 	// * `dash-db` - Seatch for dashboard
 | 
					
						
							|  |  |  |  | 	// Enum: dash-folder,dash-db
 | 
					
						
							|  |  |  |  | 	Type string `json:"type"` | 
					
						
							|  |  |  |  | 	// List of dashboard id’s to search for
 | 
					
						
							| 
									
										
										
										
											2023-08-04 17:43:47 +08:00
										 |  |  |  | 	// This is deprecated: users should use the `dashboardUIDs` query parameter instead
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							| 
									
										
										
										
											2023-08-04 17:43:47 +08:00
										 |  |  |  | 	// deprecated: true
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | 	DashboardIds []int64 `json:"dashboardIds"` | 
					
						
							|  |  |  |  | 	// List of dashboard uid’s to search for
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	DashboardUIDs []string `json:"dashboardUIDs"` | 
					
						
							|  |  |  |  | 	// List of folder id’s to search in for dashboards
 | 
					
						
							| 
									
										
										
										
											2023-08-04 17:43:47 +08:00
										 |  |  |  | 	// If it's `0` then it will query for the top level folders
 | 
					
						
							|  |  |  |  | 	// This is deprecated: users should use the `folderUIDs` query parameter instead
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							| 
									
										
										
										
											2023-08-04 17:43:47 +08:00
										 |  |  |  | 	// deprecated: true
 | 
					
						
							| 
									
										
										
										
											2023-11-16 22:57:04 +08:00
										 |  |  |  | 	//
 | 
					
						
							|  |  |  |  | 	// Deprecated: use FolderUIDs instead
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | 	FolderIds []int64 `json:"folderIds"` | 
					
						
							| 
									
										
										
										
											2023-08-04 17:43:47 +08:00
										 |  |  |  | 	// List of folder UID’s to search in for dashboards
 | 
					
						
							|  |  |  |  | 	// If it's an empty string then it will query for the top level folders
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	FolderUIDs []string `json:"folderUIDs"` | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | 	// Flag indicating if only starred Dashboards should be returned
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	Starred bool `json:"starred"` | 
					
						
							|  |  |  |  | 	// Limit the number of returned results (max 5000)
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	Limit int64 `json:"limit"` | 
					
						
							|  |  |  |  | 	// Use this parameter to access hits beyond limit. Numbering starts at 1. limit param acts as page size. Only available in Grafana v6.2+.
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	Page int64 `json:"page"` | 
					
						
							|  |  |  |  | 	// Set to `Edit` to return dashboards/folders that the user can edit
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	// default:View
 | 
					
						
							|  |  |  |  | 	// Enum: Edit,View
 | 
					
						
							|  |  |  |  | 	Permission string `json:"permission"` | 
					
						
							|  |  |  |  | 	// Sort method; for listing all the possible sort methods use the search sorting endpoint.
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	// default: alpha-asc
 | 
					
						
							|  |  |  |  | 	// Enum: alpha-asc,alpha-desc
 | 
					
						
							|  |  |  |  | 	Sort string `json:"sort"` | 
					
						
							| 
									
										
										
										
											2024-05-17 01:36:26 +08:00
										 |  |  |  | 	// Flag indicating if only soft deleted Dashboards should be returned
 | 
					
						
							|  |  |  |  | 	// in:query
 | 
					
						
							|  |  |  |  | 	// required: false
 | 
					
						
							|  |  |  |  | 	Deleted bool `json:"deleted"` | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // swagger:response searchResponse
 | 
					
						
							|  |  |  |  | type SearchResponse struct { | 
					
						
							|  |  |  |  | 	// in: body
 | 
					
						
							| 
									
										
										
										
											2023-01-30 22:17:53 +08:00
										 |  |  |  | 	Body model.HitList `json:"body"` | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // swagger:response listSortOptionsResponse
 | 
					
						
							|  |  |  |  | type ListSortOptionsResponse struct { | 
					
						
							|  |  |  |  | 	// in: body
 | 
					
						
							|  |  |  |  | 	Body struct { | 
					
						
							|  |  |  |  | 		Name        string `json:"name"` | 
					
						
							|  |  |  |  | 		DisplayName string `json:"displayName"` | 
					
						
							|  |  |  |  | 		Description string `json:"description"` | 
					
						
							|  |  |  |  | 		Meta        string `json:"meta"` | 
					
						
							|  |  |  |  | 	} `json:"body"` | 
					
						
							|  |  |  |  | } |