-
Notifications
You must be signed in to change notification settings - Fork 7
/
dashboards.go
194 lines (172 loc) · 4.5 KB
/
dashboards.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package bolt
import (
"context"
"strconv"
"github.com/boltdb/bolt"
cmp "github.com/snetsystems/cmp/backend"
"github.com/snetsystems/cmp/backend/bolt/internal"
)
// Ensure DashboardsStore implements cmp.DashboardsStore.
var _ cmp.DashboardsStore = &DashboardsStore{}
// DashboardsBucket is the bolt bucket dashboards are stored in
var DashboardsBucket = []byte("Dashboard")
// DashboardsStore is the bolt implementation of storing dashboards
type DashboardsStore struct {
client *Client
IDs cmp.ID
}
// AddIDs is a migration function that adds ID information to existing dashboards
func (d *DashboardsStore) AddIDs(ctx context.Context, boards []cmp.Dashboard) error {
for _, board := range boards {
update := false
for i, cell := range board.Cells {
// If there are is no id set, we generate one and update the dashboard
if cell.ID == "" {
id, err := d.IDs.Generate()
if err != nil {
return err
}
cell.ID = id
board.Cells[i] = cell
update = true
}
}
if !update {
continue
}
if err := d.Update(ctx, board); err != nil {
return err
}
}
return nil
}
// Migrate updates the dashboards at runtime
func (d *DashboardsStore) Migrate(ctx context.Context) error {
// 1. Add UUIDs to cells without one
boards, err := d.All(ctx)
if err != nil {
return err
}
if err := d.AddIDs(ctx, boards); err != nil {
return nil
}
defaultOrg, err := d.client.OrganizationsStore.DefaultOrganization(ctx)
if err != nil {
return err
}
for _, board := range boards {
if board.Organization == "" {
board.Organization = defaultOrg.ID
if err := d.Update(ctx, board); err != nil {
return nil
}
}
}
return nil
}
// All returns all known dashboards
func (d *DashboardsStore) All(ctx context.Context) ([]cmp.Dashboard, error) {
var srcs []cmp.Dashboard
if err := d.client.db.View(func(tx *bolt.Tx) error {
if err := tx.Bucket(DashboardsBucket).ForEach(func(k, v []byte) error {
var src cmp.Dashboard
if err := internal.UnmarshalDashboard(v, &src); err != nil {
return err
}
srcs = append(srcs, src)
return nil
}); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
return srcs, nil
}
// Add creates a new Dashboard in the DashboardsStore
func (d *DashboardsStore) Add(ctx context.Context, src cmp.Dashboard) (cmp.Dashboard, error) {
if err := d.client.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(DashboardsBucket)
id, _ := b.NextSequence()
src.ID = cmp.DashboardID(id)
// TODO: use FormatInt
strID := strconv.Itoa(int(id))
for i, cell := range src.Cells {
cid, err := d.IDs.Generate()
if err != nil {
return err
}
cell.ID = cid
src.Cells[i] = cell
}
v, err := internal.MarshalDashboard(src)
if err != nil {
return err
}
return b.Put([]byte(strID), v)
}); err != nil {
return cmp.Dashboard{}, err
}
return src, nil
}
// Get returns a Dashboard if the id exists.
func (d *DashboardsStore) Get(ctx context.Context, id cmp.DashboardID) (cmp.Dashboard, error) {
var src cmp.Dashboard
if err := d.client.db.View(func(tx *bolt.Tx) error {
strID := strconv.Itoa(int(id))
if v := tx.Bucket(DashboardsBucket).Get([]byte(strID)); v == nil {
return cmp.ErrDashboardNotFound
} else if err := internal.UnmarshalDashboard(v, &src); err != nil {
return err
}
return nil
}); err != nil {
return cmp.Dashboard{}, err
}
return src, nil
}
// Delete the dashboard from DashboardsStore
func (d *DashboardsStore) Delete(ctx context.Context, dash cmp.Dashboard) error {
if err := d.client.db.Update(func(tx *bolt.Tx) error {
strID := strconv.Itoa(int(dash.ID))
if err := tx.Bucket(DashboardsBucket).Delete([]byte(strID)); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}
// Update the dashboard in DashboardsStore
func (d *DashboardsStore) Update(ctx context.Context, dash cmp.Dashboard) error {
if err := d.client.db.Update(func(tx *bolt.Tx) error {
// Get an existing dashboard with the same ID.
b := tx.Bucket(DashboardsBucket)
strID := strconv.Itoa(int(dash.ID))
if v := b.Get([]byte(strID)); v == nil {
return cmp.ErrDashboardNotFound
}
for i, cell := range dash.Cells {
if cell.ID != "" {
continue
}
cid, err := d.IDs.Generate()
if err != nil {
return err
}
cell.ID = cid
dash.Cells[i] = cell
}
if v, err := internal.MarshalDashboard(dash); err != nil {
return err
} else if err := b.Put([]byte(strID), v); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}