chronograf/bolt/dashboards.go

99 lines
2.5 KiB
Go
Raw Normal View History

2016-12-09 03:28:40 +00:00
package bolt
import (
"context"
2016-12-14 06:57:52 +00:00
"strconv"
2016-12-09 03:28:40 +00:00
"github.com/boltdb/bolt"
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/bolt/internal"
)
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 {
client *Client
2016-12-14 06:57:52 +00:00
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-09 03:28:40 +00:00
func (s *DashboardsStore) All(ctx context.Context) ([]chronograf.Dashboard, error) {
var srcs []chronograf.Dashboard
if err := s.client.db.View(func(tx *bolt.Tx) error {
if err := tx.Bucket(DashboardBucket).ForEach(func(k, v []byte) error {
2016-12-14 06:57:52 +00:00
var src chronograf.Dashboard
2016-12-09 03:28:40 +00:00
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-13 10:44:19 +00:00
// Add creates a new Dashboard in the DashboardsStore
2016-12-14 06:57:52 +00:00
func (d *DashboardsStore) Add(ctx context.Context, src *chronograf.Dashboard) (*chronograf.Dashboard, error) {
2016-12-13 10:44:19 +00:00
if err := d.client.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(DashboardBucket)
id, err := d.IDs.Generate()
if err != nil {
return err
}
src.ID = id
2016-12-14 06:57:52 +00:00
strID := strconv.Itoa(int(id))
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-14 06:57:52 +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-14 06:57:52 +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 06:57:52 +00:00
strID := strconv.Itoa(int(id))
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-14 06:57:52 +00:00
return &chronograf.Dashboard{}, err
2016-12-13 10:44:19 +00:00
}
2016-12-14 06:57:52 +00:00
return &src, nil
}
// Delete the dashboard from DashboardsStore
func (s *DashboardsStore) Delete(ctx context.Context, d chronograf.Dashboard) error {
if err := s.client.db.Update(func(tx *bolt.Tx) error {
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
}