mirror of https://github.com/grafana/grafana
Query history: Cleanup (#48303)
* Query history: Clean up stale history after 14 days * Add unstarring sleanup * Add wraapping * Update sql for mysql database * Update * Remove fmt.Print * Refactor and simplify solution * Update pkg/services/queryhistory/database.go Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Adjust SQL to limit number of deleted queries * Add limit enforcmenet to cleanup * Change limit * Update Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>pull/48657/head
parent
cd462c5b21
commit
4661c9ca47
@ -0,0 +1,52 @@ |
||||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
package queryhistory |
||||
|
||||
import ( |
||||
"context" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/grafana/grafana/pkg/web" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestDeleteStaleQueryFromQueryHistory(t *testing.T) { |
||||
testScenarioWithQueryInQueryHistory(t, "Stale query history can be deleted", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
olderThan := time.Now().Unix() + 60 |
||||
rowsDeleted, err := sc.service.DeleteStaleQueriesInQueryHistory(context.Background(), olderThan) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 1, rowsDeleted) |
||||
}) |
||||
|
||||
testScenarioWithQueryInQueryHistory(t, "Stale single starred query history can not be deleted", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID}) |
||||
resp := sc.service.starHandler(sc.reqContext) |
||||
require.Equal(t, 200, resp.Status()) |
||||
|
||||
olderThan := time.Now().Unix() + 60 |
||||
rowsDeleted, err := sc.service.DeleteStaleQueriesInQueryHistory(context.Background(), olderThan) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 0, rowsDeleted) |
||||
}) |
||||
|
||||
testScenarioWithQueryInQueryHistory(t, "Not stale query history is not deleted", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
olderThan := time.Now().Unix() - 60 |
||||
rowsDeleted, err := sc.service.DeleteStaleQueriesInQueryHistory(context.Background(), olderThan) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 0, rowsDeleted) |
||||
}) |
||||
|
||||
// In this scenario we have 2 starred queries and 1 not starred query
|
||||
testScenarioWithMultipleQueriesInQueryHistory(t, "Stale starred query history can not be deleted", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
olderThan := time.Now().Unix() + 60 |
||||
rowsDeleted, err := sc.service.DeleteStaleQueriesInQueryHistory(context.Background(), olderThan) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 1, rowsDeleted) |
||||
}) |
||||
} |
||||
@ -0,0 +1,48 @@ |
||||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
package queryhistory |
||||
|
||||
import ( |
||||
"context" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestEnforceRowLimitInQueryHistory(t *testing.T) { |
||||
testScenarioWithQueryInQueryHistory(t, "Enforce limit for query_history", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
limit := 0 |
||||
rowsDeleted, err := sc.service.EnforceRowLimitInQueryHistory(context.Background(), limit, false) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 1, rowsDeleted) |
||||
}) |
||||
|
||||
// In this scenario we have 2 starred queries and 1 not starred query
|
||||
testScenarioWithMultipleQueriesInQueryHistory(t, "Enforce limit for unstarred queries in query_history", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
limit := 2 |
||||
rowsDeleted, err := sc.service.EnforceRowLimitInQueryHistory(context.Background(), limit, false) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 1, rowsDeleted) |
||||
}) |
||||
|
||||
// In this scenario we have 2 starred queries and 1 not starred query
|
||||
testScenarioWithMultipleQueriesInQueryHistory(t, "Enforce limit for stars in query_history_star", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
limit := 1 |
||||
rowsDeleted, err := sc.service.EnforceRowLimitInQueryHistory(context.Background(), limit, true) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 1, rowsDeleted) |
||||
}) |
||||
|
||||
// In this scenario we have 2 starred queries and 1 not starred query
|
||||
testScenarioWithMultipleQueriesInQueryHistory(t, "Enforce limit for stars in query_history_star", |
||||
func(t *testing.T, sc scenarioContext) { |
||||
limit := 0 |
||||
rowsDeleted, err := sc.service.EnforceRowLimitInQueryHistory(context.Background(), limit, true) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 2, rowsDeleted) |
||||
}) |
||||
} |
||||
Loading…
Reference in new issue