chronograf/bolt/dashboards.go

119 lines
3.0 KiB
Go
Raw Normal View History

2016-12-09 03:28:40 +00:00
package bolt
import (
2016-12-14 20:12:20 +00:00
"context"
"strconv"
2016-12-09 03:28:40 +00:00
2016-12-14 20:12:20 +00:00
"github.com/boltdb/bolt"
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/bolt/internal"
2016-12-09 03:28:40 +00:00
)
2016-12-14 06:57:52 +00:00
// Ensure DashboardsStore implements chronograf.DashboardsStore.
2016-12-09 03:28:40 +00:00
var _ chronograf.DashboardsStore = &DashboardsStore{}
2016-12-13 10:44:19 +00:00
var DashboardBucket = []byte("Dashoard")
2016-12-09 03:28:40 +00:00
type DashboardsStore struct {
2016-12-14 20:12:20 +00:00
client *Client
IDs chronograf.DashboardID
2016-12-09 03:28:40 +00:00
}
2016-12-13 10:44:19 +00:00
// All returns all known dashboards
2016-12-14 07:56:26 +00:00
func (d *DashboardsStore) All(ctx context.Context) ([]chronograf.Dashboard, error) {
2016-12-14 20:12:20 +00:00
var srcs []chronograf.Dashboard
if err := d.client.db.View(func(tx *bolt.Tx) error {
if err := tx.Bucket(DashboardBucket).ForEach(func(k, v []byte) error {
var src chronograf.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
2016-12-09 03:28:40 +00:00
}
2016-12-13 10:44:19 +00:00
// Add creates a new Dashboard in the DashboardsStore
2016-12-15 21:37:11 +00:00
func (d *DashboardsStore) Add(ctx context.Context, src chronograf.Dashboard) (chronograf.Dashboard, error) {
2016-12-14 20:12:20 +00:00
if err := d.client.db.Update(func(tx *bolt.Tx) error {
2016-12-13 10:44:19 +00:00
b := tx.Bucket(DashboardBucket)
2016-12-14 07:22:54 +00:00
id, _ := b.NextSequence()
2016-12-13 10:44:19 +00:00
2016-12-14 07:22:54 +00:00
src.ID = chronograf.DashboardID(id)
2016-12-14 20:12:20 +00:00
strID := strconv.Itoa(int(id))
2016-12-15 21:37:11 +00:00
if v, err := internal.MarshalDashboard(src); err != nil {
2016-12-13 10:44:19 +00:00
return err
2016-12-14 06:57:52 +00:00
} else if err := b.Put([]byte(strID), v); err != nil {
2016-12-13 10:44:19 +00:00
return err
}
return nil
}); err != nil {
2016-12-15 21:37:11 +00:00
return chronograf.Dashboard{}, err
2016-12-13 10:44:19 +00:00
}
return src, nil
}
// Get returns a Dashboard if the id exists.
2016-12-15 21:37:11 +00:00
func (d *DashboardsStore) Get(ctx context.Context, id chronograf.DashboardID) (chronograf.Dashboard, error) {
2016-12-13 10:44:19 +00:00
var src chronograf.Dashboard
if err := d.client.db.View(func(tx *bolt.Tx) error {
2016-12-14 20:12:20 +00:00
strID := strconv.Itoa(int(id))
2016-12-14 06:57:52 +00:00
if v := tx.Bucket(DashboardBucket).Get([]byte(strID)); v == nil {
2016-12-13 10:44:19 +00:00
return chronograf.ErrDashboardNotFound
} else if err := internal.UnmarshalDashboard(v, &src); err != nil {
return err
}
return nil
}); err != nil {
2016-12-15 21:37:11 +00:00
return chronograf.Dashboard{}, err
2016-12-13 10:44:19 +00:00
}
2016-12-15 21:37:11 +00:00
return src, nil
2016-12-14 06:57:52 +00:00
}
// Delete the dashboard from DashboardsStore
2016-12-15 21:37:11 +00:00
func (s *DashboardsStore) Delete(ctx context.Context, d chronograf.Dashboard) error {
2016-12-14 20:12:20 +00:00
if err := s.client.db.Update(func(tx *bolt.Tx) error {
2016-12-14 06:57:52 +00:00
if err := tx.Bucket(DashboardBucket).Delete(itob(int(d.ID))); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
2016-12-13 10:44:19 +00:00
}
2016-12-14 07:22:54 +00:00
// Update the dashboard in DashboardsStore
2016-12-15 21:37:11 +00:00
func (s *DashboardsStore) Update(ctx context.Context, d chronograf.Dashboard) error {
2016-12-14 20:12:20 +00:00
if err := s.client.db.Update(func(tx *bolt.Tx) error {
// Get an existing dashboard with the same ID.
b := tx.Bucket(DashboardBucket)
strID := strconv.Itoa(int(d.ID))
if v := b.Get([]byte(strID)); v == nil {
return chronograf.ErrDashboardNotFound
}
2016-12-15 21:37:11 +00:00
if v, err := internal.MarshalDashboard(d); err != nil {
2016-12-14 20:12:20 +00:00
return err
} else if err := b.Put([]byte(strID), v); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
2016-12-14 07:22:54 +00:00
}