forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard_snapshot.go
82 lines (66 loc) · 1.87 KB
/
dashboard_snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package sqlstore
import (
"time"
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
)
func init() {
bus.AddHandler("sql", CreateDashboardSnapshot)
bus.AddHandler("sql", GetDashboardSnapshot)
bus.AddHandler("sql", DeleteDashboardSnapshot)
bus.AddHandler("sql", SearchDashboardSnapshots)
}
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 {
var snapshots = make(m.DashboardSnapshots, 0)
sess := x.Limit(query.Limit)
if query.Name != "" {
sess.Where("name LIKE ?", query.Name)
}
sess.Where("org_id = ?", query.OrgId)
err := sess.Find(&snapshots)
query.Result = snapshots
return err
}