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
f976b690
Commit
f976b690
authored
Mar 21, 2018
by
Alexander Zobnin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
limit number of rows deleted by dashboard version cleanup
parent
fc2d1d6c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
32 deletions
+17
-32
pkg/services/sqlstore/dashboard_version.go
+17
-32
No files found.
pkg/services/sqlstore/dashboard_version.go
View file @
f976b690
...
@@ -2,6 +2,7 @@ package sqlstore
...
@@ -2,6 +2,7 @@ package sqlstore
import
(
import
(
"fmt"
"fmt"
"strings"
"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,6 +70,8 @@ func GetDashboardVersions(query *m.GetDashboardVersionsQuery) error {
...
@@ -69,6 +70,8 @@ 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
{
const
MAX_VERSIONS_TO_DELETE
=
100
versionsToKeep
:=
setting
.
DashboardVersionsToKeep
versionsToKeep
:=
setting
.
DashboardVersionsToKeep
if
versionsToKeep
<
1
{
if
versionsToKeep
<
1
{
versionsToKeep
=
1
versionsToKeep
=
1
...
@@ -88,44 +91,26 @@ func DeleteExpiredVersions(cmd *m.DeleteExpiredVersionsCommand) error {
...
@@ -88,44 +91,26 @@ func DeleteExpiredVersions(cmd *m.DeleteExpiredVersionsCommand) error {
AND version < vtd.min + vtd.count - %v`
AND version < vtd.min + vtd.count - %v`
versionIdsToDeleteSubquery
:=
fmt
.
Sprintf
(
versionIdsToDeleteSybqueryTemplate
,
versionsToKeep
)
versionIdsToDeleteSubquery
:=
fmt
.
Sprintf
(
versionIdsToDeleteSybqueryTemplate
,
versionsToKeep
)
deleteExpiredSql
:=
fmt
.
Sprintf
(
`DELETE FROM dashboard_version WHERE id IN (%s)`
,
versionIdsToDeleteSubquery
)
versions
:=
[]
string
{}
e
xpiredResponse
,
err
:=
sess
.
Exec
(
deleteExpiredSql
)
e
rr
:=
sess
.
SQL
(
versionIdsToDeleteSubquery
)
.
Find
(
&
versions
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
cmd
.
DeletedRows
,
_
=
expiredResponse
.
RowsAffected
()
return
nil
})
}
// Short version of DashboardVersion for getting expired versions
type
DashboardVersionExp
struct
{
Id
int64
`json:"id"`
DashboardId
int64
`json:"dashboardId"`
Version
int
`json:"version"`
}
func
getVersionIDsToDelete
(
versions
[]
DashboardVersionExp
,
versionsToKeep
int
)
[]
interface
{}
{
// Don't delete more than MAX_VERSIONS_TO_DELETE version per time
versionIds
:=
make
([]
interface
{},
0
)
limit
:=
MAX_VERSIONS_TO_DELETE
if
len
(
versions
)
<
MAX_VERSIONS_TO_DELETE
{
if
len
(
versions
)
==
0
{
limit
=
len
(
versions
)
return
versionIds
}
}
versions
=
versions
[
:
limit
]
currentDashboard
:=
versions
[
0
]
.
DashboardId
deleteExpiredSql
:=
fmt
.
Sprintf
(
`DELETE FROM dashboard_version WHERE id IN (%s)`
,
strings
.
Join
(
versions
,
`,`
))
count
:=
0
expiredResponse
,
err
:=
sess
.
Exec
(
deleteExpiredSql
)
for
_
,
v
:=
range
versions
{
if
err
!=
nil
{
if
v
.
DashboardId
==
currentDashboard
{
return
err
count
++
}
else
{
count
=
1
currentDashboard
=
v
.
DashboardId
}
if
count
>
versionsToKeep
{
versionIds
=
append
(
versionIds
,
v
.
Id
)
}
}
}
cmd
.
DeletedRows
,
_
=
expiredResponse
.
RowsAffected
()
return
versionIds
return
nil
})
}
}
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