Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http): add cell properties to dashboard response #16057

Merged
merged 5 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
dearyhud marked this conversation as resolved.
Show resolved Hide resolved
dearyhud marked this conversation as resolved.
Show resolved Hide resolved
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