Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dearyhud authored and jsteenb2 committed Dec 5, 2019
1 parent 2329159 commit 0950be0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 104 deletions.
80 changes: 68 additions & 12 deletions dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,82 @@ type Cell struct {
}

// Marshals the cell
func (cell *Cell) MarshalJSON() ([]byte, error) {
// func (cell *Cell) MarshalJSON() ([]byte, error) {
// type resp struct {
// ID ID `json:"id,omitempty"`
// CellProperty
// ViewProperties ViewProperties `json:"properties,omitempty"`
// Name string `json:"name,omitempty"`
// }

// response := resp{
// ID: cell.ID,
// CellProperty: cell.CellProperty,
// }

// if cell.View != nil {
// response.ViewProperties = cell.View.Properties
// response.Name = cell.View.Name
// }
// return json.Marshal(response)
// }
// Marshals the cell
func (c *Cell) MarshalJSON() ([]byte, error) {
type resp struct {
ID ID `json:"id,omitempty"`
ID *ID `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ViewProperties json.RawMessage `json:"properties,omitempty"`
CellProperty
ViewProperties ViewProperties `json:"properties,omitempty"`
Name string `json:"name,omitempty"`
}

response := resp{
ID: cell.ID,
CellProperty: cell.CellProperty,
CellProperty: c.CellProperty,
}

if cell.View != nil {
response.ViewProperties = cell.View.Properties
response.Name = cell.View.Name
if c.ID != 0 {
response.ID = &c.ID
}
if c.View != nil {
response.Name = c.View.Name
rawJSON, err := MarshalViewPropertiesJSON(c.View.Properties)
if err != nil {
return nil, err
}
response.ViewProperties = rawJSON
}
return json.Marshal(response)
}

func (c *Cell) UnmarshalJSON(b []byte) error {
var newCell struct {
ID ID `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ViewProperties json.RawMessage `json:"properties,omitempty"`
CellProperty
}
if err := json.Unmarshal(b, &newCell); err != nil {
return err
}

c.ID = newCell.ID
c.CellProperty = newCell.CellProperty

if newCell.Name != "" {
if c.View == nil {
c.View = new(View)
}
c.View.Name = newCell.Name
}

props, err := UnmarshalViewPropertiesJSON(newCell.ViewProperties)
if err == nil {
if c.View == nil {
c.View = new(View)
}
c.View.Properties = props
}

return nil
}

// CellProperty contains the properties of a cell.
type CellProperty struct {
X int32 `json:"x"`
Expand Down Expand Up @@ -451,7 +507,7 @@ func UnmarshalViewPropertiesJSON(b []byte) (ViewProperties, error) {
}
vis = ev
default:
return nil, fmt.Errorf("unknown type %v", t.Shape)
return nil, fmt.Errorf("unknown shape %v", t.Shape)
}

return vis, nil
Expand Down
96 changes: 5 additions & 91 deletions http/dashboard_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,110 +459,24 @@ func newGetDashboardsResponse(ctx context.Context, dashboards []*platform.Dashbo
// handlePostDashboard creates a new dashboard.
func (h *DashboardHandler) handlePostDashboard(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
req, err := decodePostDashboardRequest(ctx, r)
if err != nil {
var d platform.Dashboard
if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
h.HandleHTTPError(ctx, err, w)
return
}

pd := req.Dashboard.toPlatform()
if err := h.DashboardService.CreateDashboard(ctx, pd); err != nil {
if err := h.DashboardService.CreateDashboard(ctx, &d); err != nil {
h.HandleHTTPError(ctx, err, w)
return
}

if err := encodeResponse(ctx, w, http.StatusCreated, newDashboardResponse(pd, []*platform.Label{})); err != nil {
if err := encodeResponse(ctx, w, http.StatusCreated, newDashboardResponse(&d, []*platform.Label{})); err != nil {
logEncodingError(h.log, r, err)
return
}
}

func (d *dashboard) toPlatform() *platform.Dashboard {
pd := &platform.Dashboard{
ID: d.ID,
OrganizationID: d.OrganizationID,
Name: d.Name,
Description: d.Description,
Meta: d.Meta,
}

for _, cell := range d.Cells {
pd.Cells = append(pd.Cells, cell.toPlatform())
}

return pd
}

func (c *cell) toPlatform() *platform.Cell {
view := &platform.View{
ViewContents: platform.ViewContents{Name: c.Name, ID: c.ID},
}

if c.ViewProperties != nil {
view.Properties = *c.ViewProperties
}

return &platform.Cell{
ID: c.ID,
CellProperty: c.CellProperty,
View: view,
}
}

type cell struct {
platform.Cell
ViewProperties *platform.ViewProperties `json:"properties,omitempty"`
Name string `json:"name"`
}

type name struct {
Name string `json:"name"`
}

func (c *cell) UnmarshalJSON(b []byte) error {
platformCell := platform.Cell{}
if err := json.Unmarshal(b, &platformCell); err != nil {
return err
}

v, err := platform.UnmarshalViewPropertiesJSON(b)
if err != nil {
return err
}

n := name{}
if err := json.Unmarshal(b, &n); err != nil {
return err
}

if v.GetType() != "" {
c.ViewProperties = &v
}
c.Name = n.Name
c.Cell = platformCell
return nil
}

type dashboard struct {
platform.Dashboard
Cells []*cell `json:"cells"`
}

type postDashboardRequest struct {
Dashboard *dashboard
}

func decodePostDashboardRequest(ctx context.Context, r *http.Request) (*postDashboardRequest, error) {
c := &dashboard{}
if err := json.NewDecoder(r.Body).Decode(c); err != nil {
return nil, err
}
return &postDashboardRequest{
Dashboard: c,
}, nil
}

// hanldeGetDashboard retrieves a dashboard by ID.
// handleGetDashboard retrieves a dashboard by ID.
func (h *DashboardHandler) handleGetDashboard(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
req, err := decodeGetDashboardRequest(ctx, r)
Expand Down
6 changes: 5 additions & 1 deletion pkger/clone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ type cellView struct {
}

func convertCellView(cell influxdb.Cell) chart {
var name string
if cell.View != nil {
name = cell.View.Name
}
ch := chart{
Name: cell.View.Name,
Name: name,
Height: int(cell.H),
Width: int(cell.W),
XPos: int(cell.X),
Expand Down

0 comments on commit 0950be0

Please sign in to comment.