Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
fc2d1d6c
Commit
fc2d1d6c
authored
Mar 21, 2018
by
Alexander Zobnin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix dashboard version cleanup on large datasets
parent
624dac16
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
24 deletions
+19
-24
pkg/services/sqlstore/dashboard_version.go
+18
-23
pkg/services/sqlstore/dashboard_version_test.go
+1
-1
No files found.
pkg/services/sqlstore/dashboard_version.go
View file @
fc2d1d6c
package
sqlstore
package
sqlstore
import
(
import
(
"
strings
"
"
fmt
"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/bus"
m
"github.com/grafana/grafana/pkg/models"
m
"github.com/grafana/grafana/pkg/models"
...
@@ -69,36 +69,31 @@ func GetDashboardVersions(query *m.GetDashboardVersionsQuery) error {
...
@@ -69,36 +69,31 @@ func GetDashboardVersions(query *m.GetDashboardVersionsQuery) error {
func
DeleteExpiredVersions
(
cmd
*
m
.
DeleteExpiredVersionsCommand
)
error
{
func
DeleteExpiredVersions
(
cmd
*
m
.
DeleteExpiredVersionsCommand
)
error
{
return
inTransaction
(
func
(
sess
*
DBSession
)
error
{
return
inTransaction
(
func
(
sess
*
DBSession
)
error
{
versions
:=
[]
DashboardVersionExp
{}
versionsToKeep
:=
setting
.
DashboardVersionsToKeep
versionsToKeep
:=
setting
.
DashboardVersionsToKeep
if
versionsToKeep
<
1
{
if
versionsToKeep
<
1
{
versionsToKeep
=
1
versionsToKeep
=
1
}
}
err
:=
sess
.
Table
(
"dashboard_version"
)
.
// Idea of this query is finding version IDs to delete based on formula:
Select
(
"dashboard_version.id, dashboard_version.version, dashboard_version.dashboard_id"
)
.
// min_version_to_keep = min_version + (versions_count - versions_to_keep)
Where
(
`dashboard_id IN (
// where version stats is processed for each dashboard. This guarantees that we keep at least versions_to_keep
SELECT dashboard_id FROM dashboard_version
// versions, but in some cases (when versions are sparse) this number may be more.
GROUP BY dashboard_id HAVING COUNT(dashboard_version.id) > ?
versionIdsToDeleteSybqueryTemplate
:=
`SELECT id
)`
,
versionsToKeep
)
.
FROM dashboard_version, (
Desc
(
"dashboard_version.dashboard_id"
,
"dashboard_version.version"
)
.
SELECT dashboard_id, count(version) as count, min(version) as min
Find
(
&
versions
)
FROM dashboard_version
GROUP BY dashboard_id
) AS vtd
WHERE dashboard_version.dashboard_id=vtd.dashboard_id
AND version < vtd.min + vtd.count - %v`
versionIdsToDeleteSubquery
:=
fmt
.
Sprintf
(
versionIdsToDeleteSybqueryTemplate
,
versionsToKeep
)
deleteExpiredSql
:=
fmt
.
Sprintf
(
`DELETE FROM dashboard_version WHERE id IN (%s)`
,
versionIdsToDeleteSubquery
)
expiredResponse
,
err
:=
sess
.
Exec
(
deleteExpiredSql
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
cmd
.
DeletedRows
,
_
=
expiredResponse
.
RowsAffected
()
// Keep last versionsToKeep versions and delete other
versionIdsToDelete
:=
getVersionIDsToDelete
(
versions
,
versionsToKeep
)
if
len
(
versionIdsToDelete
)
>
0
{
deleteExpiredSql
:=
`DELETE FROM dashboard_version WHERE id IN (?`
+
strings
.
Repeat
(
",?"
,
len
(
versionIdsToDelete
)
-
1
)
+
`)`
expiredResponse
,
err
:=
sess
.
Exec
(
deleteExpiredSql
,
versionIdsToDelete
...
)
if
err
!=
nil
{
return
err
}
cmd
.
DeletedRows
,
_
=
expiredResponse
.
RowsAffected
()
}
return
nil
return
nil
})
})
...
...
pkg/services/sqlstore/dashboard_version_test.go
View file @
fc2d1d6c
...
@@ -136,7 +136,7 @@ func TestDeleteExpiredVersions(t *testing.T) {
...
@@ -136,7 +136,7 @@ func TestDeleteExpiredVersions(t *testing.T) {
err
:=
DeleteExpiredVersions
(
&
m
.
DeleteExpiredVersionsCommand
{})
err
:=
DeleteExpiredVersions
(
&
m
.
DeleteExpiredVersionsCommand
{})
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
query
:=
m
.
GetDashboardVersionsQuery
{
DashboardId
:
savedDash
.
Id
,
OrgId
:
1
}
query
:=
m
.
GetDashboardVersionsQuery
{
DashboardId
:
savedDash
.
Id
,
OrgId
:
1
,
Limit
:
versionsToWrite
}
GetDashboardVersions
(
&
query
)
GetDashboardVersions
(
&
query
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
versionsToWrite
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
versionsToWrite
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment