Skip to content

Commit

Permalink
feat(http): add cell properties to GET /dashboards/:dashboardID response
Browse files Browse the repository at this point in the history
issue #16057
  • Loading branch information
dearyhud authored Dec 3, 2019
1 parent 996c88e commit e21ebeb
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
1. [15749](https://github.com/influxdata/influxdb/pull/15749): Expose bundle analysis tools for frontend resources
1. [15674](https://github.com/influxdata/influxdb/pull/15674): Allow users to view just the output section of a telegraf config
1. [15314](https://github.com/influxdata/influxdb/issues/15314): Allow the users to see string data in the single stat graph type
1. [16057] https://github.com/influxdata/influxdb/pull/16057: Adds `properties` to each cell on GET /dashboards/{dashboardID}

### Bug Fixes

Expand Down
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
44 changes: 42 additions & 2 deletions http/dashboard_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,47 @@ func newDashboardResponse(d *platform.Dashboard, labels []*platform.Label) dashb

type dashboardCellResponse struct {
platform.Cell
Links map[string]string `json:"links"`
Properties platform.ViewProperties `json:"-"`
Name string `json:"name,omitempty"`
Links map[string]string `json:"links"`
}

func (d *dashboardCellResponse) MarshalJSON() ([]byte, error) {
r := struct {
platform.Cell
Properties platform.ViewProperties `json:"properties,omitempty"`
Name string `json:"name,omitempty"`
Links map[string]string `json:"links"`
}{
Cell: d.Cell,
Links: d.Links,
}

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

func (c dashboardCellResponse) toPlatform() *platform.Cell {
return &c.Cell
}

func newDashboardCellResponse(dashboardID platform.ID, c *platform.Cell) dashboardCellResponse {
return dashboardCellResponse{
resp := dashboardCellResponse{
Cell: *c,
Links: map[string]string{
"self": fmt.Sprintf("/api/v2/dashboards/%s/cells/%s", dashboardID, c.ID),
"view": fmt.Sprintf("/api/v2/dashboards/%s/cells/%s/view", dashboardID, c.ID),
},
}

if c.View != nil {
resp.Properties = c.View.Properties
resp.Name = c.View.Name
}
return resp
}

type dashboardCellsResponse struct {
Expand Down Expand Up @@ -478,6 +504,20 @@ func (h *DashboardHandler) handleGetDashboard(w http.ResponseWriter, r *http.Req
return
}

if r.URL.Query().Get("include") == "properties" {
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 e21ebeb

Please sign in to comment.