Query history: Add swagger definitions (#50498)

* Query history: Add swagger definitions

* Update

* Remove changes in generated files

* Generate api files

* Add query history into api docs

* Add examples for commands

* Add missing parameters

* Generate api-spec and api-merged

* Fix linting

* Fix showing of example of queries

* Revert "Fix showing of example of queries"

This reverts commit b1eb073fbe.

* Update

* Update
This commit is contained in:
Ivana Huckova
2022-06-13 09:47:40 +02:00
committed by GitHub
parent 088a1880d8
commit 0ddb3b7521
9 changed files with 1211 additions and 56 deletions
+179
View File
@@ -0,0 +1,179 @@
package definitions
import (
"github.com/grafana/grafana/pkg/services/queryhistory"
)
// swagger:route GET /query-history query_history searchQueries
//
// Query history search.
//
// Returns a list of queries in the query history that matches the search criteria.
// Query history search supports pagination. Use the `limit` parameter to control the maximum number of queries returned; the default limit is 100.
// You can also use the `page` query parameter to fetch queries from any page other than the first one.
//
// Responses:
// 200: getQueryHistorySearchResponse
// 401: unauthorisedError
// 500: internalServerError
// swagger:route POST /query-history query_history createQuery
//
// Add query to query history.
//
// Adds new query to query history.
//
// Responses:
// 200: getQueryHistoryResponse
// 400: badRequestError
// 401: unauthorisedError
// 500: internalServerError
// swagger:route POST /query-history/star/{query_history_uid} query_history starQuery
//
// Add star to query in query history.
//
// Adds star to query in query history as specified by the UID.
//
// Responses:
// 200: getQueryHistoryResponse
// 401: unauthorisedError
// 500: internalServerError
// swagger:route POST /query-history/migrate query_history migrateQueries
//
// Migrate queries to query history.
//
// Adds multiple queries to query history.
//
// Responses:
// 200: getQueryHistoryMigrationResponse
// 400: badRequestError
// 401: unauthorisedError
// 500: internalServerError
// swagger:route PATCH /query-history/{query_history_uid} query_history patchQueryComment
//
// Update comment for query in query history.
//
// Updates comment for query in query history as specified by the UID.
//
// Responses:
// 200: getQueryHistoryResponse
// 400: badRequestError
// 401: unauthorisedError
// 500: internalServerError
// swagger:route DELETE /query-history/{query_history_uid} query_history deleteQuery
//
// Delete query in query history.
//
// Deletes an existing query in query history as specified by the UID. This operation cannot be reverted.
//
// Responses:
// 200: getQueryHistoryDeleteQueryResponse
// 401: unauthorisedError
// 500: internalServerError
// swagger:route DELETE /query-history/star/{query_history_uid} query_history unstarQuery
//
// Remove star to query in query history.
//
// Removes star from query in query history as specified by the UID.
//
// Responses:
// 200: getQueryHistoryResponse
// 401: unauthorisedError
// 500: internalServerError
// swagger:parameters starQuery patchQueryComment deleteQuery unstarQuery
type QueryHistoryByUID struct {
// in:path
// required:true
UID string `json:"query_history_uid"`
}
// swagger:parameters searchQueries
type SearchQueriesParams struct {
// List of data source UIDs to search for
// in:query
// required: false
// type: array
// collectionFormat: multi
DatasourceUid []string `json:"datasourceUid"`
// Text inside query or comments that is searched for
// in:query
// required: false
SearchString string `json:"searchString"`
// Flag indicating if only starred queries should be returned
// in:query
// required: false
OnlyStarred bool `json:"onlyStarred"`
// Sort method
// in:query
// required: false
// default: time-desc
// Enum: time-desc,time-asc
Sort string `json:"sort"`
// Use this parameter to access hits beyond limit. Numbering starts at 1. limit param acts as page size.
// in:query
// required: false
Page int `json:"page"`
// Limit the number of returned results
// in:query
// required: false
Limit int `json:"limit"`
// From range for the query history search
// in:query
// required: false
From int64 `json:"from"`
// To range for the query history search
// in:query
// required: false
To int64 `json:"to"`
}
// swagger:parameters createQuery
type CreateQueryParams struct {
// in:body
// required:true
Body queryhistory.CreateQueryInQueryHistoryCommand `json:"body"`
}
// swagger:parameters patchQueryComment
type PatchQueryCommentParams struct {
// in:body
// required:true
Body queryhistory.PatchQueryCommentInQueryHistoryCommand `json:"body"`
}
// swagger:parameters migrateQueries
type MigrateQueriesParams struct {
// in:body
// required:true
Body queryhistory.MigrateQueriesToQueryHistoryCommand `json:"body"`
}
//swagger:response getQueryHistorySearchResponse
type GetQueryHistorySearchResponse struct {
// in: body
Body queryhistory.QueryHistorySearchResponse `json:"body"`
}
// swagger:response getQueryHistoryResponse
type GetQueryHistoryResponse struct {
// in: body
Body queryhistory.QueryHistoryResponse `json:"body"`
}
// swagger:response getQueryHistoryDeleteQueryResponse
type GetQueryHistoryDeleteQueryResponse struct {
// in: body
Body queryhistory.QueryHistoryDeleteQueryResponse `json:"body"`
}
// swagger:response getQueryHistoryMigrationResponse
type GetQueryHistoryMigrationResponse struct {
// in: body
Body queryhistory.QueryHistoryMigrationResponse `json:"body"`
}
+4
View File
@@ -31,6 +31,10 @@
{
"name": "library_elements",
"description": "The identifier (ID) of a library element is an auto-incrementing numeric value that is unique per Grafana install.\nThe unique identifier (UID) of a library element uniquely identifies library elements between multiple Grafana installs. It’s automatically generated unless you specify it during library element creation. The UID provides consistent URLs for accessing library elements and when syncing library elements between multiple Grafana installs.\nThe maximum length of a UID is 40 characters."
},
{
"name": "query_history",
"description": "The identifier (ID) of a query in query history is an auto-incrementing numeric value that is unique per Grafana install.\nThe unique identifier (UID) of a query history uniquely identifies queries in query history between multiple Grafana installs. It’s automatically generated. The UID provides consistent URLs for accessing queries in query history."
},
{
"name": "orgs",
+8 -1
View File
@@ -25,6 +25,7 @@ func (s *QueryHistoryService) registerAPIEndpoints() {
})
}
// createHandler handles POST /api/query-history
func (s *QueryHistoryService) createHandler(c *models.ReqContext) response.Response {
cmd := CreateQueryInQueryHistoryCommand{}
if err := web.Bind(c.Req, &cmd); err != nil {
@@ -39,6 +40,7 @@ func (s *QueryHistoryService) createHandler(c *models.ReqContext) response.Respo
return response.JSON(http.StatusOK, QueryHistoryResponse{Result: query})
}
// searchHandler handles GET /api/query-history
func (s *QueryHistoryService) searchHandler(c *models.ReqContext) response.Response {
timeRange := legacydata.NewDataTimeRange(c.Query("from"), c.Query("to"))
@@ -61,6 +63,7 @@ func (s *QueryHistoryService) searchHandler(c *models.ReqContext) response.Respo
return response.JSON(http.StatusOK, QueryHistorySearchResponse{Result: result})
}
// deleteHandler handles DELETE /api/query-history/:uid
func (s *QueryHistoryService) deleteHandler(c *models.ReqContext) response.Response {
queryUID := web.Params(c.Req)[":uid"]
if len(queryUID) > 0 && !util.IsValidShortUID(queryUID) {
@@ -72,12 +75,13 @@ func (s *QueryHistoryService) deleteHandler(c *models.ReqContext) response.Respo
return response.Error(http.StatusInternalServerError, "Failed to delete query from query history", err)
}
return response.JSON(http.StatusOK, DeleteQueryFromQueryHistoryResponse{
return response.JSON(http.StatusOK, QueryHistoryDeleteQueryResponse{
Message: "Query deleted",
ID: id,
})
}
// patchCommentHandler handles PATCH /api/query-history/:uid
func (s *QueryHistoryService) patchCommentHandler(c *models.ReqContext) response.Response {
queryUID := web.Params(c.Req)[":uid"]
if len(queryUID) > 0 && !util.IsValidShortUID(queryUID) {
@@ -97,6 +101,7 @@ func (s *QueryHistoryService) patchCommentHandler(c *models.ReqContext) response
return response.JSON(http.StatusOK, QueryHistoryResponse{Result: query})
}
// starHandler handles POST /api/query-history/star/:uid
func (s *QueryHistoryService) starHandler(c *models.ReqContext) response.Response {
queryUID := web.Params(c.Req)[":uid"]
if len(queryUID) > 0 && !util.IsValidShortUID(queryUID) {
@@ -111,6 +116,7 @@ func (s *QueryHistoryService) starHandler(c *models.ReqContext) response.Respons
return response.JSON(http.StatusOK, QueryHistoryResponse{Result: query})
}
// starHandler handles DELETE /api/query-history/star/:uid
func (s *QueryHistoryService) unstarHandler(c *models.ReqContext) response.Response {
queryUID := web.Params(c.Req)[":uid"]
if len(queryUID) > 0 && !util.IsValidShortUID(queryUID) {
@@ -125,6 +131,7 @@ func (s *QueryHistoryService) unstarHandler(c *models.ReqContext) response.Respo
return response.JSON(http.StatusOK, QueryHistoryResponse{Result: query})
}
// starHandler handles POST /api/query-history/migrate
func (s *QueryHistoryService) migrateHandler(c *models.ReqContext) response.Response {
cmd := MigrateQueriesToQueryHistoryCommand{}
if err := web.Bind(c.Req, &cmd); err != nil {
+7
View File
@@ -11,6 +11,7 @@ import (
"github.com/grafana/grafana/pkg/util"
)
// createQuery adds a query into query history
func (s QueryHistoryService) createQuery(ctx context.Context, user *models.SignedInUser, cmd CreateQueryInQueryHistoryCommand) (QueryHistoryDTO, error) {
queryHistory := QueryHistory{
OrgID: user.OrgId,
@@ -43,6 +44,7 @@ func (s QueryHistoryService) createQuery(ctx context.Context, user *models.Signe
return dto, nil
}
// searchQueries searches for queries in query history based on provided parameters
func (s QueryHistoryService) searchQueries(ctx context.Context, user *models.SignedInUser, query SearchInQueryHistoryQuery) (QueryHistorySearchResult, error) {
var dtos []QueryHistoryDTO
var allQueries []interface{}
@@ -132,6 +134,7 @@ func (s QueryHistoryService) deleteQuery(ctx context.Context, user *models.Signe
return queryID, err
}
// patchQueryComment searches updates comment for query in query history
func (s QueryHistoryService) patchQueryComment(ctx context.Context, user *models.SignedInUser, UID string, cmd PatchQueryCommentInQueryHistoryCommand) (QueryHistoryDTO, error) {
var queryHistory QueryHistory
var isStarred bool
@@ -176,6 +179,7 @@ func (s QueryHistoryService) patchQueryComment(ctx context.Context, user *models
return dto, nil
}
// starQuery adds query into query_history_star table together with user_id and org_id
func (s QueryHistoryService) starQuery(ctx context.Context, user *models.SignedInUser, UID string) (QueryHistoryDTO, error) {
var queryHistory QueryHistory
var isStarred bool
@@ -225,6 +229,7 @@ func (s QueryHistoryService) starQuery(ctx context.Context, user *models.SignedI
return dto, nil
}
// unstarQuery deletes query with with user_id and org_id from query_history_star table
func (s QueryHistoryService) unstarQuery(ctx context.Context, user *models.SignedInUser, UID string) (QueryHistoryDTO, error) {
var queryHistory QueryHistory
var isStarred bool
@@ -267,6 +272,7 @@ func (s QueryHistoryService) unstarQuery(ctx context.Context, user *models.Signe
return dto, nil
}
// migrateQueries adds multiple queries into query history
func (s QueryHistoryService) migrateQueries(ctx context.Context, user *models.SignedInUser, cmd MigrateQueriesToQueryHistoryCommand) (int, int, error) {
queryHistories := make([]*QueryHistory, 0, len(cmd.Queries))
starredQueries := make([]*QueryHistoryStar, 0)
@@ -363,6 +369,7 @@ func (s QueryHistoryService) deleteStaleQueries(ctx context.Context, olderThan i
return int(rowsCount), nil
}
// enforceQueryHistoryRowLimit is run in scheduled cleanup and it removes queries and stars that exceeded limit
func (s QueryHistoryService) enforceQueryHistoryRowLimit(ctx context.Context, limit int, starredQueries bool) (int, error) {
var deletedRowsCount int64
+29 -15
View File
@@ -12,6 +12,7 @@ var (
ErrQueryAlreadyStarred = errors.New("query was already starred")
)
// QueryHistory is the model for query history definitions
type QueryHistory struct {
ID int64 `xorm:"pk autoincr 'id'"`
UID string `xorm:"uid"`
@@ -23,17 +24,13 @@ type QueryHistory struct {
Queries *simplejson.Json
}
// QueryHistory is the model for query history star definitions
type QueryHistoryStar struct {
ID int64 `xorm:"pk autoincr 'id'"`
QueryUID string `xorm:"query_uid"`
UserID int64 `xorm:"user_id"`
}
type CreateQueryInQueryHistoryCommand struct {
DatasourceUID string `json:"datasourceUid"`
Queries *simplejson.Json `json:"queries"`
}
type SearchInQueryHistoryQuery struct {
DatasourceUIDs []string `json:"datasourceUids"`
SearchString string `json:"searchString"`
@@ -45,10 +42,6 @@ type SearchInQueryHistoryQuery struct {
To int64 `json:"to"`
}
type PatchQueryCommentInQueryHistoryCommand struct {
Comment string `json:"comment"`
}
type QueryHistoryDTO struct {
UID string `json:"uid" xorm:"uid"`
DatasourceUID string `json:"datasourceUid" xorm:"datasource_uid"`
@@ -75,16 +68,12 @@ type QueryHistorySearchResponse struct {
Result QueryHistorySearchResult `json:"result"`
}
// DeleteQueryFromQueryHistoryResponse is the response struct for deleting a query from query history
type DeleteQueryFromQueryHistoryResponse struct {
// QueryHistoryDeleteQueryResponse is the response struct for deleting a query from query history
type QueryHistoryDeleteQueryResponse struct {
ID int64 `json:"id"`
Message string `json:"message"`
}
type MigrateQueriesToQueryHistoryCommand struct {
Queries []QueryToMigrate `json:"queries"`
}
type QueryToMigrate struct {
DatasourceUID string `json:"datasourceUid"`
Queries *simplejson.Json `json:"queries"`
@@ -98,3 +87,28 @@ type QueryHistoryMigrationResponse struct {
TotalCount int `json:"totalCount"`
StarredCount int `json:"starredCount"`
}
// CreateQueryInQueryHistoryCommand is the command for adding query history
// swagger:model
type CreateQueryInQueryHistoryCommand struct {
// UID of the data source for which are queries stored.
// example: PE1C5CBDA0504A6A3
DatasourceUID string `json:"datasourceUid"`
// The JSON model of queries.
// required: true
Queries *simplejson.Json `json:"queries"`
}
// PatchQueryCommentInQueryHistoryCommand is the command for updating comment for query in query history
// swagger:model
type PatchQueryCommentInQueryHistoryCommand struct {
// Updated comment
Comment string `json:"comment"`
}
// MigrateQueriesToQueryHistoryCommand is the command used for migration of old queries into query history
// swagger:model
type MigrateQueriesToQueryHistoryCommand struct {
// Array of queries to store in query history.
Queries []QueryToMigrate `json:"queries"`
}