fix errors

pull/691/head
Jade McGough 2016-12-13 22:57:52 -08:00
parent aeefafdc29
commit ccf93b4781
3 changed files with 41 additions and 12 deletions

View File

@ -2,18 +2,21 @@ package bolt
import (
"context"
"strconv"
"github.com/boltdb/bolt"
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/bolt/internal"
)
// Ensure DashboardsStore implements chronograf.DashboardsStore.
var _ chronograf.DashboardsStore = &DashboardsStore{}
var DashboardBucket = []byte("Dashoard")
type DashboardsStore struct {
client *Client
IDs chronograf.DashboardID
}
// All returns all known dashboards
@ -21,7 +24,7 @@ func (s *DashboardsStore) All(ctx context.Context) ([]chronograf.Dashboard, erro
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 {
var src chonograf.Dashboard
var src chronograf.Dashboard
if err := internal.UnmarshalDashboard(v, &src); err != nil {
return err
}
@ -39,7 +42,7 @@ func (s *DashboardsStore) All(ctx context.Context) ([]chronograf.Dashboard, erro
}
// Add creates a new Dashboard in the DashboardsStore
func (d *DashboardsStore) Add(ctx context.Context, src chronograf.Dashboard) (chronograf.Dashboard, error) {
func (d *DashboardsStore) Add(ctx context.Context, src *chronograf.Dashboard) (*chronograf.Dashboard, error) {
if err := d.client.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(DashboardBucket)
id, err := d.IDs.Generate()
@ -48,32 +51,48 @@ func (d *DashboardsStore) Add(ctx context.Context, src chronograf.Dashboard) (ch
}
src.ID = id
if v, err := internal.MarshalDashboard(src); err != nil {
strID := strconv.Itoa(int(id))
if v, err := internal.MarshalDashboard(*src); err != nil {
return err
} else if err := b.Put([]byte(src.ID), v); err != nil {
} else if err := b.Put([]byte(strID), v); err != nil {
return err
}
return nil
}); err != nil {
return chronograf.Dashboard{}, err
return &chronograf.Dashboard{}, err
}
return src, nil
}
// Get returns a Dashboard if the id exists.
func (d *DashboardsStore) Get(ctx context.Context, id int) (chronograf.Dashboard, error) {
func (d *DashboardsStore) Get(ctx context.Context, id chronograf.DashboardID) (*chronograf.Dashboard, error) {
var src chronograf.Dashboard
if err := d.client.db.View(func(tx *bolt.Tx) error {
if v := tx.Bucket(LayoutBucket).Get([]byte(id)); v == nil {
strID := strconv.Itoa(int(id))
if v := tx.Bucket(DashboardBucket).Get([]byte(strID)); v == nil {
return chronograf.ErrDashboardNotFound
} else if err := internal.UnmarshalDashboard(v, &src); err != nil {
return err
}
return nil
}); err != nil {
return chronograf.Dashboard{}, err
return &chronograf.Dashboard{}, err
}
return src, nil
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
}

View File

@ -249,10 +249,10 @@ type DashboardCell struct {
type DashboardsStore interface {
// Create a new Dashboard in the DashboardStore
Add(context.Context, *Dashboard) (*Dashboard, error)
// Delete the Dashboard from the DashboardStore
Delete(context.Context, *Dashboard) error
// Delete the Dashboard from the DashboardStore if `ID` exists.
Delete(context.Context, Dashboard) error
// Get retrieves a dashboard if `ID` exists.
Get(ctx context.Context, ID DashboardID) (*Dashboard, error)
Get(ctx context.Context, id DashboardID) (*Dashboard, error)
// Update replaces the dashboard information
Update(context.Context, *Dashboard) error
}

View File

@ -11,6 +11,10 @@ type dashboardResponse struct {
Links dashboardLinks `json:"links"`
}
type getDashboardsResponse struct {
Dashboards []ldashboardResponse `json:"dashboards"`
}
func newDashboardResponse(d *chronograf.Dashboard) dashboardResponse {
base := "/chronograf/v1/dashboards"
return dashboardResponse{
@ -23,7 +27,13 @@ func newDashboardResponse(d *chronograf.Dashboard) dashboardResponse {
// Dashboards returns all dashboards within the store
func (s *Service) Dashboards(w http.ResponseWriter, r *http.Request) {
dashboards, err := s.DashboardsStore.All(ctx)
if err != nil {
Error(w, http.StatusInternalServerError, "Error loading layouts", s.Logger)
return
}
encodeJSON(w, http.StatusOK, getDashboardsResponse{Dashboards: []dashboardResponse{}}, s.Logger)
}
// DashboardID returns a single specified dashboard