formatting
parent
11cb9a795d
commit
bdf65400f2
|
@ -1,12 +1,12 @@
|
|||
package bolt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/influxdata/chronograf"
|
||||
"github.com/influxdata/chronograf/bolt/internal"
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/influxdata/chronograf"
|
||||
"github.com/influxdata/chronograf/bolt/internal"
|
||||
)
|
||||
|
||||
// Ensure DashboardsStore implements chronograf.DashboardsStore.
|
||||
|
@ -15,40 +15,40 @@ var _ chronograf.DashboardsStore = &DashboardsStore{}
|
|||
var DashboardBucket = []byte("Dashoard")
|
||||
|
||||
type DashboardsStore struct {
|
||||
client *Client
|
||||
IDs chronograf.DashboardID
|
||||
client *Client
|
||||
IDs chronograf.DashboardID
|
||||
}
|
||||
|
||||
// All returns all known dashboards
|
||||
func (d *DashboardsStore) All(ctx context.Context) ([]chronograf.Dashboard, error) {
|
||||
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
|
||||
}
|
||||
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
|
||||
return srcs, nil
|
||||
}
|
||||
|
||||
// Add creates a new Dashboard in the DashboardsStore
|
||||
func (d *DashboardsStore) Add(ctx context.Context, src *chronograf.Dashboard) (*chronograf.Dashboard, error) {
|
||||
if err := d.client.db.Update(func(tx *bolt.Tx) error {
|
||||
if err := d.client.db.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket(DashboardBucket)
|
||||
id, _ := b.NextSequence()
|
||||
|
||||
src.ID = chronograf.DashboardID(id)
|
||||
strID := strconv.Itoa(int(id))
|
||||
strID := strconv.Itoa(int(id))
|
||||
if v, err := internal.MarshalDashboard(*src); err != nil {
|
||||
return err
|
||||
} else if err := b.Put([]byte(strID), v); err != nil {
|
||||
|
@ -66,7 +66,7 @@ func (d *DashboardsStore) Add(ctx context.Context, src *chronograf.Dashboard) (*
|
|||
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 {
|
||||
strID := strconv.Itoa(int(id))
|
||||
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 {
|
||||
|
@ -82,7 +82,7 @@ func (d *DashboardsStore) Get(ctx context.Context, id chronograf.DashboardID) (*
|
|||
|
||||
// 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 := s.client.db.Update(func(tx *bolt.Tx) error {
|
||||
if err := tx.Bucket(DashboardBucket).Delete(itob(int(d.ID))); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -96,23 +96,23 @@ func (s *DashboardsStore) Delete(ctx context.Context, d *chronograf.Dashboard) e
|
|||
|
||||
// Update the dashboard in DashboardsStore
|
||||
func (s *DashboardsStore) Update(ctx context.Context, d *chronograf.Dashboard) error {
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
if v, err := internal.MarshalDashboard(*d); err != nil {
|
||||
return err
|
||||
} else if err := b.Put([]byte(strID), v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if v, err := internal.MarshalDashboard(*d); err != nil {
|
||||
return err
|
||||
} else if err := b.Put([]byte(strID), v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -205,9 +205,9 @@ func MarshalDashboard(d chronograf.Dashboard) ([]byte, error) {
|
|||
}
|
||||
|
||||
return proto.Marshal(&Dashboard{
|
||||
ID: int64(d.ID),
|
||||
Cells: cells,
|
||||
Name: d.Name,
|
||||
ID: int64(d.ID),
|
||||
Cells: cells,
|
||||
Name: d.Name,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -231,9 +231,9 @@ func UnmarshalDashboard(data []byte, d *chronograf.Dashboard) error {
|
|||
}
|
||||
}
|
||||
|
||||
d.ID = chronograf.DashboardID(pb.ID)
|
||||
d.ID = chronograf.DashboardID(pb.ID)
|
||||
d.Cells = cells
|
||||
d.Name = pb.Name
|
||||
d.Name = pb.Name
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -229,20 +229,20 @@ type DashboardID int
|
|||
|
||||
// Dashboard represents all visual and query data for a dashboard
|
||||
type Dashboard struct {
|
||||
ID DashboardID `json:"id"`
|
||||
ID DashboardID `json:"id"`
|
||||
Cells []DashboardCell `json:"cells"`
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// DashboardCell holds visual and query information for a cell
|
||||
type DashboardCell struct {
|
||||
X int32 `json:"x"`
|
||||
Y int32 `json:"y"`
|
||||
W int32 `json:"w"`
|
||||
H int32 `json:"h"`
|
||||
Name string `json:"name"`
|
||||
X int32 `json:"x"`
|
||||
Y int32 `json:"y"`
|
||||
W int32 `json:"w"`
|
||||
H int32 `json:"h"`
|
||||
Name string `json:"name"`
|
||||
Queries []string `json:"queries"`
|
||||
Type string `json:"type"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
// DashboardsStore is the storage and retrieval of dashboards
|
||||
|
@ -295,7 +295,7 @@ type Cell struct {
|
|||
I string `json:"i"`
|
||||
Name string `json:"name"`
|
||||
Queries []Query `json:"queries"`
|
||||
Type string `json:"type"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
// Layout is a collection of Cells for visualization
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/influxdata/chronograf"
|
||||
"github.com/influxdata/chronograf"
|
||||
)
|
||||
|
||||
type dashboardLinks struct {
|
||||
Self string `json:"self"` // Self link mapping to this resource
|
||||
Self string `json:"self"` // Self link mapping to this resource
|
||||
}
|
||||
|
||||
type dashboardResponse struct {
|
||||
chronograf.Dashboard
|
||||
Links dashboardLinks `json:"links"`
|
||||
chronograf.Dashboard
|
||||
Links dashboardLinks `json:"links"`
|
||||
}
|
||||
|
||||
type getDashboardsResponse struct {
|
||||
|
@ -21,52 +21,52 @@ type getDashboardsResponse struct {
|
|||
}
|
||||
|
||||
func newDashboardResponse(d chronograf.Dashboard) dashboardResponse {
|
||||
base := "/chronograf/v1/dashboards"
|
||||
return dashboardResponse{
|
||||
Dashboard: d,
|
||||
Links: dashboardLinks{
|
||||
Self: fmt.Sprintf("%s/%d", base, d.ID),
|
||||
},
|
||||
}
|
||||
base := "/chronograf/v1/dashboards"
|
||||
return dashboardResponse{
|
||||
Dashboard: d,
|
||||
Links: dashboardLinks{
|
||||
Self: fmt.Sprintf("%s/%d", base, d.ID),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Dashboards returns all dashboards within the store
|
||||
func (s *Service) Dashboards(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
dashboards, err := s.DashboardsStore.All(ctx)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "Error loading layouts", s.Logger)
|
||||
return
|
||||
}
|
||||
ctx := r.Context()
|
||||
dashboards, err := s.DashboardsStore.All(ctx)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "Error loading layouts", s.Logger)
|
||||
return
|
||||
}
|
||||
|
||||
res := getDashboardsResponse{
|
||||
Dashboards: []dashboardResponse{},
|
||||
}
|
||||
res := getDashboardsResponse{
|
||||
Dashboards: []dashboardResponse{},
|
||||
}
|
||||
|
||||
for _, dashboard := range dashboards {
|
||||
res.Dashboards = append(res.Dashboards, newDashboardResponse(dashboard))
|
||||
}
|
||||
for _, dashboard := range dashboards {
|
||||
res.Dashboards = append(res.Dashboards, newDashboardResponse(dashboard))
|
||||
}
|
||||
|
||||
encodeJSON(w, http.StatusOK, res, s.Logger)
|
||||
encodeJSON(w, http.StatusOK, res, s.Logger)
|
||||
}
|
||||
|
||||
// DashboardID returns a single specified dashboard
|
||||
func (s *Service) DashboardID(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := paramID("id", r)
|
||||
if err != nil {
|
||||
Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
|
||||
return
|
||||
}
|
||||
id, err := paramID("id", r)
|
||||
if err != nil {
|
||||
Error(w, http.StatusUnprocessableEntity, err.Error(), s.Logger)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
e, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id))
|
||||
if err != nil {
|
||||
notFound(w, id, s.Logger)
|
||||
return
|
||||
}
|
||||
ctx := r.Context()
|
||||
e, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id))
|
||||
if err != nil {
|
||||
notFound(w, id, s.Logger)
|
||||
return
|
||||
}
|
||||
|
||||
res := newDashboardResponse(e)
|
||||
encodeJSON(w, http.StatusOK, res, s.Logger)
|
||||
res := newDashboardResponse(e)
|
||||
encodeJSON(w, http.StatusOK, res, s.Logger)
|
||||
}
|
||||
|
||||
// type postDashboardRequest struct {
|
||||
|
@ -81,7 +81,7 @@ func (s *Service) NewDashboard(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// RemoveDashboard deletes a dashboard
|
||||
func (s *Service) RemoveDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := paramID("id", r)
|
||||
id, err := paramID("id", r)
|
||||
if err != nil {
|
||||
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
|
||||
return
|
||||
|
|
|
@ -10,7 +10,7 @@ type Service struct {
|
|||
LayoutStore chronograf.LayoutStore
|
||||
AlertRulesStore chronograf.AlertRulesStore
|
||||
UsersStore chronograf.UsersStore
|
||||
DashboardsStore chronograf.DashboardsStore
|
||||
DashboardsStore chronograf.DashboardsStore
|
||||
TimeSeries chronograf.TimeSeries
|
||||
Logger chronograf.Logger
|
||||
UseAuth bool
|
||||
|
|
Loading…
Reference in New Issue