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
+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"`
}