mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Query History: Remove migration (#67470)
This commit is contained in:
@@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package queryhistory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 {
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
package queryhistory
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
)
|
||||
|
||||
func TestIntegrationMigrateQueriesToQueryHistory(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
testScenario(t, "When users tries to migrate 1 query in query history it should succeed",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
command := MigrateQueriesToQueryHistoryCommand{
|
||||
Queries: []QueryToMigrate{
|
||||
{
|
||||
DatasourceUID: "NCzh67i",
|
||||
Queries: simplejson.NewFromAny(map[string]interface{}{
|
||||
"expr": "test",
|
||||
}),
|
||||
Comment: "",
|
||||
Starred: false,
|
||||
CreatedAt: sc.service.now().Unix(),
|
||||
},
|
||||
},
|
||||
}
|
||||
sc.reqContext.Req.Body = mockRequestBody(command)
|
||||
resp := sc.service.migrateHandler(sc.reqContext)
|
||||
var response QueryHistoryMigrationResponse
|
||||
err := json.Unmarshal(resp.Body(), &response)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 200, resp.Status())
|
||||
require.Equal(t, "Query history successfully migrated", response.Message)
|
||||
require.Equal(t, 1, response.TotalCount)
|
||||
require.Equal(t, 0, response.StarredCount)
|
||||
})
|
||||
|
||||
testScenario(t, "When users tries to migrate multiple queries in query history it should succeed",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
command := MigrateQueriesToQueryHistoryCommand{
|
||||
Queries: []QueryToMigrate{
|
||||
{
|
||||
DatasourceUID: "NCzh67i",
|
||||
Queries: simplejson.NewFromAny(map[string]interface{}{
|
||||
"expr": "test1",
|
||||
}),
|
||||
Comment: "",
|
||||
Starred: false,
|
||||
CreatedAt: sc.service.now().Unix(),
|
||||
},
|
||||
{
|
||||
DatasourceUID: "NCzh67i",
|
||||
Queries: simplejson.NewFromAny(map[string]interface{}{
|
||||
"expr": "test2",
|
||||
}),
|
||||
Comment: "",
|
||||
Starred: false,
|
||||
CreatedAt: sc.service.now().Unix() - int64(100),
|
||||
},
|
||||
{
|
||||
DatasourceUID: "ABch68f",
|
||||
Queries: simplejson.NewFromAny(map[string]interface{}{
|
||||
"expr": "test3",
|
||||
}),
|
||||
Comment: "",
|
||||
Starred: false,
|
||||
CreatedAt: sc.service.now().Unix() - int64(1000),
|
||||
},
|
||||
},
|
||||
}
|
||||
sc.reqContext.Req.Body = mockRequestBody(command)
|
||||
resp := sc.service.migrateHandler(sc.reqContext)
|
||||
var response QueryHistoryMigrationResponse
|
||||
err := json.Unmarshal(resp.Body(), &response)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 200, resp.Status())
|
||||
require.Equal(t, "Query history successfully migrated", response.Message)
|
||||
require.Equal(t, 3, response.TotalCount)
|
||||
require.Equal(t, 0, response.StarredCount)
|
||||
})
|
||||
|
||||
testScenario(t, "When users tries to migrate starred and not starred query in query history it should succeed",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
command := MigrateQueriesToQueryHistoryCommand{
|
||||
Queries: []QueryToMigrate{
|
||||
{
|
||||
DatasourceUID: "NCzh67i",
|
||||
Queries: simplejson.NewFromAny(map[string]interface{}{
|
||||
"expr": "test1",
|
||||
}),
|
||||
Comment: "",
|
||||
Starred: true,
|
||||
CreatedAt: sc.service.now().Unix(),
|
||||
},
|
||||
{
|
||||
DatasourceUID: "NCzh67i",
|
||||
Queries: simplejson.NewFromAny(map[string]interface{}{
|
||||
"expr": "test2",
|
||||
}),
|
||||
Comment: "",
|
||||
Starred: false,
|
||||
CreatedAt: sc.service.now().Unix() - int64(100),
|
||||
},
|
||||
},
|
||||
}
|
||||
sc.reqContext.Req.Body = mockRequestBody(command)
|
||||
resp := sc.service.migrateHandler(sc.reqContext)
|
||||
var response QueryHistoryMigrationResponse
|
||||
err := json.Unmarshal(resp.Body(), &response)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 200, resp.Status())
|
||||
require.Equal(t, "Query history successfully migrated", response.Message)
|
||||
require.Equal(t, 2, response.TotalCount)
|
||||
require.Equal(t, 1, response.StarredCount)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user