Skip to content

Commit

Permalink
feat: adds properties to each cell on GET /dashboards/:dashboardID
Browse files Browse the repository at this point in the history
  • Loading branch information
dearyhud committed Nov 29, 2019
1 parent 380b411 commit e09f09b
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 22 deletions.
30 changes: 26 additions & 4 deletions dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ func SortDashboards(opts FindOptions, ds []*Dashboard) {
type Cell struct {
ID ID `json:"id,omitempty"`
CellProperty
View *View `json:"-"`
}

// Marshals the cell
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)
}

// CellProperty contains the properties of a cell.
Expand Down Expand Up @@ -550,8 +572,8 @@ func MarshalViewPropertiesJSON(v ViewProperties) ([]byte, error) {
}

// MarshalJSON encodes a view to JSON bytes.
func (c View) MarshalJSON() ([]byte, error) {
vis, err := MarshalViewPropertiesJSON(c.Properties)
func (v View) MarshalJSON() ([]byte, error) {
viewProperties, err := MarshalViewPropertiesJSON(v.Properties)
if err != nil {
return nil, err
}
Expand All @@ -560,8 +582,8 @@ func (c View) MarshalJSON() ([]byte, error) {
ViewContents
ViewProperties json.RawMessage `json:"properties"`
}{
ViewContents: c.ViewContents,
ViewProperties: vis,
ViewContents: v.ViewContents,
ViewProperties: viewProperties,
})
}

Expand Down
42 changes: 42 additions & 0 deletions http/dashboard_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,32 @@ type dashboardCellResponse struct {
Links map[string]string `json:"links"`
}

func (d *dashboardCellResponse) MarshalJSON() ([]byte, error) {
r := struct {
ID platform.ID `json:"id,omitempty"`
X int32 `json:"x"`
Y int32 `json:"y"`
W int32 `json:"w"`
H int32 `json:"h"`
Name string `json:"name,omitempty"`
ViewProperties platform.ViewProperties `json:"properties,omitempty"`
Links map[string]string `json:"links"`
}{
ID: d.Cell.ID,
X: d.Cell.X,
Y: d.Cell.Y,
W: d.Cell.W,
H: d.Cell.H,
Links: d.Links,
}

if d.Cell.View != nil {
r.ViewProperties = d.Cell.View.Properties
r.Name = d.Cell.View.Name
}
return json.Marshal(r)
}

func (c dashboardCellResponse) toPlatform() *platform.Cell {
return &c.Cell
}
Expand Down Expand Up @@ -472,12 +498,28 @@ func (h *DashboardHandler) handleGetDashboard(w http.ResponseWriter, r *http.Req
return
}

showViewProperties := r.URL.Query().Get("include") == "properties"

dashboard, err := h.DashboardService.FindDashboardByID(ctx, req.DashboardID)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}

if showViewProperties {
for _, c := range dashboard.Cells {
view, err := h.DashboardService.GetDashboardCellView(ctx, dashboard.ID, c.ID)
if err != nil {
h.HandleHTTPError(ctx, err, w)
return
}

if view != nil {
c.View = view
}
}
}

labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: dashboard.ID})
if err != nil {
h.HandleHTTPError(ctx, err, w)
Expand Down
Loading

0 comments on commit e09f09b

Please sign in to comment.