|
|
@@ -1,13 +1,18 @@
|
|
|
package sqlstore
|
|
|
|
|
|
import (
|
|
|
+ "math"
|
|
|
+ "strings"
|
|
|
+
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
+ "github.com/grafana/grafana/pkg/setting"
|
|
|
)
|
|
|
|
|
|
func init() {
|
|
|
bus.AddHandler("sql", GetDashboardVersion)
|
|
|
bus.AddHandler("sql", GetDashboardVersions)
|
|
|
+ bus.AddHandler("sql", DeleteExpiredVersions)
|
|
|
}
|
|
|
|
|
|
// GetDashboardVersion gets the dashboard version for the given dashboard ID and version number.
|
|
|
@@ -58,3 +63,71 @@ func GetDashboardVersions(query *m.GetDashboardVersionsQuery) error {
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func DeleteExpiredVersions(cmd *m.DeleteExpiredVersionsCommand) error {
|
|
|
+ return inTransaction(func(sess *DBSession) error {
|
|
|
+ var expiredCount int64 = 0
|
|
|
+ var versions []DashboardVersionExp
|
|
|
+
|
|
|
+ // Don't clean up if user set versions_to_keep to 2147483647 (MaxInt32)
|
|
|
+ if versionsToKeep := setting.DashboardVersionsToKeep; versionsToKeep < math.MaxInt32 {
|
|
|
+ err := sess.Table("dashboard_version").
|
|
|
+ Select("dashboard_version.id, dashboard_version.version, dashboard_version.dashboard_id").
|
|
|
+ Where(`dashboard_id IN (
|
|
|
+ SELECT dashboard_id FROM dashboard_version
|
|
|
+ GROUP BY dashboard_id HAVING COUNT(dashboard_version.id) > ?
|
|
|
+ )`, versionsToKeep).
|
|
|
+ Desc("dashboard_version.dashboard_id", "dashboard_version.version").
|
|
|
+ Find(&versions)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 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
|
|
|
+ }
|
|
|
+ expiredCount, _ = expiredResponse.RowsAffected()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlog.Debug("Deleted old/expired dashboard versions", "expired", expiredCount)
|
|
|
+ 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{} {
|
|
|
+ versionIds := make([]interface{}, 0)
|
|
|
+
|
|
|
+ if len(versions) == 0 {
|
|
|
+ return versionIds
|
|
|
+ }
|
|
|
+
|
|
|
+ currentDashboard := versions[0].DashboardId
|
|
|
+ count := 0
|
|
|
+ for _, v := range versions {
|
|
|
+ if v.DashboardId == currentDashboard {
|
|
|
+ count++
|
|
|
+ } else {
|
|
|
+ count = 1
|
|
|
+ currentDashboard = v.DashboardId
|
|
|
+ }
|
|
|
+ if count > versionsToKeep {
|
|
|
+ versionIds = append(versionIds, v.Id)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return versionIds
|
|
|
+}
|