From ccf93b4781ab10167d0fe09c62056448b0d1151e Mon Sep 17 00:00:00 2001 From: Jade McGough Date: Tue, 13 Dec 2016 22:57:52 -0800 Subject: [PATCH] fix errors --- bolt/dashboards.go | 37 ++++++++++++++++++++++++++++--------- chronograf.go | 6 +++--- server/dashboards.go | 10 ++++++++++ 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/bolt/dashboards.go b/bolt/dashboards.go index bfd0dcde9..774852668 100644 --- a/bolt/dashboards.go +++ b/bolt/dashboards.go @@ -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 } diff --git a/chronograf.go b/chronograf.go index 0c03f9de5..72a9eb67b 100644 --- a/chronograf.go +++ b/chronograf.go @@ -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 } diff --git a/server/dashboards.go b/server/dashboards.go index 76bfb25b5..2dd457d3a 100644 --- a/server/dashboards.go +++ b/server/dashboards.go @@ -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