Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query History: Remove migration #67470

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions docs/sources/developers/http_api/query_history.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,62 +335,3 @@ Status codes:
- **200** – OK
- **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
40 changes: 0 additions & 40 deletions pkg/services/queryhistory/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ func (s *QueryHistoryService) registerAPIEndpoints() {
entities.Post("/star/:uid", middleware.ReqSignedIn, routing.Wrap(s.starHandler))
entities.Delete("/star/:uid", middleware.ReqSignedIn, routing.Wrap(s.unstarHandler))
entities.Patch("/:uid", middleware.ReqSignedIn, routing.Wrap(s.patchCommentHandler))
// Remove migrate endpoint in Grafana v10 as breaking change
entities.Post("/migrate", middleware.ReqSignedIn, routing.Wrap(s.migrateHandler))
})
}

Expand Down Expand Up @@ -189,31 +187,6 @@ func (s *QueryHistoryService) unstarHandler(c *contextmodel.ReqContext) response
return response.JSON(http.StatusOK, QueryHistoryResponse{Result: query})
}

// 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
func (s *QueryHistoryService) migrateHandler(c *contextmodel.ReqContext) response.Response {
cmd := MigrateQueriesToQueryHistoryCommand{}
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}

totalCount, starredCount, err := s.MigrateQueriesToQueryHistory(c.Req.Context(), c.SignedInUser, cmd)
if err != nil {
return response.Error(http.StatusInternalServerError, "Failed to migrate query history", err)
}

return response.JSON(http.StatusOK, QueryHistoryMigrationResponse{Message: "Query history successfully migrated", TotalCount: totalCount, StarredCount: starredCount})
}

// swagger:parameters starQuery patchQueryComment deleteQuery unstarQuery
type QueryHistoryByUID struct {
// in:path
Expand Down Expand Up @@ -275,13 +248,6 @@ type PatchQueryCommentParams struct {
Body PatchQueryCommentInQueryHistoryCommand `json:"body"`
}

// swagger:parameters migrateQueries
type MigrateQueriesParams struct {
// in:body
// required:true
Body MigrateQueriesToQueryHistoryCommand `json:"body"`
}

//swagger:response getQueryHistorySearchResponse
type GetQueryHistorySearchResponse struct {
// in: body
Expand All @@ -299,9 +265,3 @@ type GetQueryHistoryDeleteQueryResponse struct {
// in: body
Body QueryHistoryDeleteQueryResponse `json:"body"`
}

// swagger:response getQueryHistoryMigrationResponse
type GetQueryHistoryMigrationResponse struct {
// in: body
Body QueryHistoryMigrationResponse `json:"body"`
}
60 changes: 0 additions & 60 deletions pkg/services/queryhistory/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package queryhistory

import (
"context"
"fmt"
"strconv"

"github.com/grafana/grafana/pkg/infra/db"
Expand Down Expand Up @@ -271,65 +270,6 @@ func (s QueryHistoryService) unstarQuery(ctx context.Context, user *user.SignedI
return dto, nil
}

// migrateQueries adds multiple queries into query history
func (s QueryHistoryService) migrateQueries(ctx context.Context, usr *user.SignedInUser, cmd MigrateQueriesToQueryHistoryCommand) (int, int, error) {
queryHistories := make([]*QueryHistory, 0, len(cmd.Queries))
starredQueries := make([]*QueryHistoryStar, 0)

err := s.store.WithTransactionalDbSession(ctx, func(session *db.Session) error {
for _, query := range cmd.Queries {
uid := util.GenerateShortUID()
queryHistories = append(queryHistories, &QueryHistory{
OrgID: usr.OrgID,
UID: uid,
Queries: query.Queries,
DatasourceUID: query.DatasourceUID,
CreatedBy: usr.UserID,
CreatedAt: query.CreatedAt,
Comment: query.Comment,
})

if query.Starred {
starredQueries = append(starredQueries, &QueryHistoryStar{
UserID: usr.UserID,
QueryUID: uid,
})
}
}

batchSize := 50
var err error
for i := 0; i < len(queryHistories); i += batchSize {
j := i + batchSize
if j > len(queryHistories) {
j = len(queryHistories)
}
_, err = session.InsertMulti(queryHistories[i:j])
if err != nil {
return err
}
}

for i := 0; i < len(starredQueries); i += batchSize {
j := i + batchSize
if j > len(starredQueries) {
j = len(starredQueries)
}
_, err = session.InsertMulti(starredQueries[i:j])
if err != nil {
return err
}
}
return err
})

if err != nil {
return 0, 0, fmt.Errorf("failed to migrate query history: %w", err)
}

return len(queryHistories), len(starredQueries), nil
}

func (s QueryHistoryService) deleteStaleQueries(ctx context.Context, olderThan int64) (int, error) {
var rowsCount int64

Expand Down
21 changes: 0 additions & 21 deletions pkg/services/queryhistory/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,6 @@ type QueryHistoryDeleteQueryResponse struct {
Message string `json:"message"`
}

type QueryToMigrate struct {
DatasourceUID string `json:"datasourceUid"`
Queries *simplejson.Json `json:"queries"`
CreatedAt int64 `json:"createdAt"`
Comment string `json:"comment"`
Starred bool `json:"starred"`
}

type QueryHistoryMigrationResponse struct {
Message string `json:"message"`
TotalCount int `json:"totalCount"`
StarredCount int `json:"starredCount"`
}

// CreateQueryInQueryHistoryCommand is the command for adding query history
// swagger:model
type CreateQueryInQueryHistoryCommand struct {
Expand All @@ -105,10 +91,3 @@ 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"`
}
5 changes: 0 additions & 5 deletions pkg/services/queryhistory/queryhistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type Service interface {
PatchQueryCommentInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string, cmd PatchQueryCommentInQueryHistoryCommand) (QueryHistoryDTO, error)
StarQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (QueryHistoryDTO, error)
UnstarQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (QueryHistoryDTO, error)
MigrateQueriesToQueryHistory(ctx context.Context, user *user.SignedInUser, cmd MigrateQueriesToQueryHistoryCommand) (int, int, error)
DeleteStaleQueriesInQueryHistory(ctx context.Context, olderThan int64) (int, error)
EnforceRowLimitInQueryHistory(ctx context.Context, limit int, starredQueries bool) (int, error)
}
Expand Down Expand Up @@ -72,10 +71,6 @@ func (s QueryHistoryService) UnstarQueryInQueryHistory(ctx context.Context, user
return s.unstarQuery(ctx, user, UID)
}

func (s QueryHistoryService) MigrateQueriesToQueryHistory(ctx context.Context, user *user.SignedInUser, cmd MigrateQueriesToQueryHistoryCommand) (int, int, error) {
return s.migrateQueries(ctx, user, cmd)
}

func (s QueryHistoryService) DeleteStaleQueriesInQueryHistory(ctx context.Context, olderThan int64) (int, error) {
return s.deleteStaleQueries(ctx, olderThan)
}
Expand Down
120 changes: 0 additions & 120 deletions pkg/services/queryhistory/queryhistory_migrate_test.go

This file was deleted.

Loading