The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/pkg/services/sqlstore/dashboard_snapshot.go

103 lines
2.5 KiB

package sqlstore
import (
"time"
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
)
func init() {
bus.AddHandler("sql", CreateDashboardSnapshot)
bus.AddHandler("sql", GetDashboardSnapshot)
bus.AddHandler("sql", DeleteDashboardSnapshot)
10 years ago
bus.AddHandler("sql", SearchDashboardSnapshots)
bus.AddHandler("sql", DeleteExpiredSnapshots)
}
func DeleteExpiredSnapshots(cmd *m.DeleteExpiredSnapshotsCommand) error {
return inTransaction(func(sess *xorm.Session) error {
var expiredCount int64 = 0
if setting.SnapShotRemoveExpired {
deleteExpiredSql := "DELETE FROM dashboard_snapshot WHERE expires < ?"
expiredResponse, err := x.Exec(deleteExpiredSql, time.Now)
if err != nil {
return err
}
expiredCount, _ = expiredResponse.RowsAffected()
}
sqlog.Debug("Deleted old/expired snaphots", "expired", expiredCount)
return nil
})
}
func CreateDashboardSnapshot(cmd *m.CreateDashboardSnapshotCommand) error {
return inTransaction(func(sess *xorm.Session) error {
// never
var expires = time.Now().Add(time.Hour * 24 * 365 * 50)
if cmd.Expires > 0 {
expires = time.Now().Add(time.Second * time.Duration(cmd.Expires))
}
snapshot := &m.DashboardSnapshot{
Name: cmd.Name,
Key: cmd.Key,
DeleteKey: cmd.DeleteKey,
OrgId: cmd.OrgId,
UserId: cmd.UserId,
External: cmd.External,
Dashboard: cmd.Dashboard,
Expires: expires,
Created: time.Now(),
Updated: time.Now(),
}
_, err := sess.Insert(snapshot)
cmd.Result = snapshot
return err
})
}
func DeleteDashboardSnapshot(cmd *m.DeleteDashboardSnapshotCommand) error {
return inTransaction(func(sess *xorm.Session) error {
var rawSql = "DELETE FROM dashboard_snapshot WHERE delete_key=?"
_, err := sess.Exec(rawSql, cmd.DeleteKey)
return err
})
}
func GetDashboardSnapshot(query *m.GetDashboardSnapshotQuery) error {
snapshot := m.DashboardSnapshot{Key: query.Key}
has, err := x.Get(&snapshot)
if err != nil {
return err
} else if has == false {
return m.ErrDashboardSnapshotNotFound
}
query.Result = &snapshot
return nil
}
func SearchDashboardSnapshots(query *m.GetDashboardSnapshotsQuery) error {
10 years ago
var snapshots = make(m.DashboardSnapshots, 0)
10 years ago
sess := x.Limit(query.Limit)
10 years ago
if query.Name != "" {
sess.Where("name LIKE ?", query.Name)
}
10 years ago
sess.Where("org_id = ?", query.OrgId)
err := sess.Find(&snapshots)
query.Result = snapshots
return err
}