Skip to content

Commit

Permalink
Query history: Add swagger definitions (grafana#50498)
Browse files Browse the repository at this point in the history
* 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 b1eb073.

* Update

* Update
  • Loading branch information
ivanahuckova authored Jun 13, 2022
1 parent 088a188 commit 0ddb3b7
Show file tree
Hide file tree
Showing 9 changed files with 1,211 additions and 56 deletions.
1 change: 1 addition & 0 deletions docs/sources/developers/http_api/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dashboards, creating users, and updating data sources.
- [Playlists API]({{< relref "playlist/" >}})
- [Preferences API]({{< relref "preferences/" >}})
- [Short URL API]({{< relref "short_url/" >}})
- [Query history API]({{< relref "query_history/" >}})
- [Snapshot API]({{< relref "snapshot/" >}})
- [Team API]({{< relref "team/" >}})
- [User API]({{< relref "user/" >}})
Expand Down
77 changes: 71 additions & 6 deletions docs/sources/developers/http_api/query_history.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ Status codes:

- **200** – OK
- **400** - Errors (invalid JSON, missing or invalid fields)
- **500** – Unable to add query to the database
- **401** – Unauthorized
- **500** – Internal error

## Query history search

Expand Down Expand Up @@ -144,7 +145,8 @@ Content-Type: application/json
Status codes:

- **200** – OK
- **500** – Unable to add query to the database
- **401** – Unauthorized
- **500** – Internal error

## Delete query from Query history by UID

Expand Down Expand Up @@ -176,7 +178,8 @@ Content-Type: application/json
Status codes:

- **200** – OK
- **500** – Unable to delete query from the database
- **401** – Unauthorized
- **500** – Internal error

## Update comment of query in Query history by UID

Expand Down Expand Up @@ -232,7 +235,8 @@ Status codes:

- **200** – OK
- **400** - Errors (invalid JSON, missing or invalid fields)
- **500** – Unable to update comment of query in the database
- **401** – Unauthorized
- **500** – Internal error

## Star query in Query history

Expand Down Expand Up @@ -280,7 +284,8 @@ Content-Type: application/json
Status codes:

- **200** – OK
- **500** – Unable to star query in the database
- **401** – Unauthorized
- **500** – Internal error

## Unstar query in Query history

Expand Down Expand Up @@ -328,4 +333,64 @@ Content-Type: application/json
Status codes:

- **200** – OK
- **500** – Unable to unstar query in the database
- **401** – Unauthorized
- **500** – Internal error

## Migrate queries to Query history

`POST /api/query-history/migrate`

Migrates multiple queries in to query history.

**Example request:**

```http
POST /api/query-history HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
{
"queries": [
{
"datasourceUid": "PE1C5CBDA0504A6A3",
"queries": [
{
"refId": "A",
"key": "Q-87fed8e3-62ba-4eb2-8d2a-4129979bb4de-0",
"scenarioId": "csv_content",
"datasource": {
"type": "testdata",
"uid": "PD8C576611E62080A"
}
}
],
"starred": false,
"createdAt": 1643630762,
"comment": "debugging"
}
]
}
```

JSON body schema:

- **queries** – JSON of query history items.

**Example response:**

```http
HTTP/1.1 200
Content-Type: application/json
{
"message": "Query history successfully migrated",
"totalCount": 105,
"starredCount": 10
}
```

Status codes:

- **200** – OK
- **400** - Errors (invalid JSON, missing or invalid fields)
- **401** – Unauthorized
- **500** – Internal error
179 changes: 179 additions & 0 deletions pkg/api/docs/definitions/query_history.go
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions pkg/api/docs/tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 8 additions & 1 deletion pkg/services/queryhistory/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"))

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 0ddb3b7

Please sign in to comment.