feat(dashboards): extend dashboard update API to take cells as a put doc operation

pull/17801/head
Johnny Steenbergen 2020-04-20 12:28:40 -07:00 committed by Johnny Steenbergen
parent 69fe9ed1ba
commit e5e5500401
5 changed files with 114 additions and 5 deletions

View File

@ -223,8 +223,9 @@ func (f DashboardFilter) QueryParams() map[string][]string {
// DashboardUpdate is the patch structure for a dashboard.
type DashboardUpdate struct {
Name *string `json:"name"`
Description *string `json:"description"`
Name *string `json:"name"`
Description *string `json:"description"`
Cells *[]*Cell `json:"cells"`
}
// Apply applies an update to a dashboard.
@ -237,6 +238,10 @@ func (u DashboardUpdate) Apply(d *Dashboard) error {
d.Description = *u.Description
}
if u.Cells != nil {
d.Cells = *u.Cells
}
return nil
}

View File

@ -2282,7 +2282,17 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/Dashboard"
type: object
properties:
name:
description: optional, when provided will replace the name
type: string
description:
description: optional, when provided will replace the description
type: string
cells:
description: optional, when provided will replace all existing cells with the cells provided
$ref: "#/components/schemas/CellWithViewProperties"
parameters:
- $ref: '#/components/parameters/TraceSpan'
- in: path

View File

@ -660,7 +660,7 @@ func (s *Service) UpdateDashboardCell(ctx context.Context, dashboardID, cellID i
func (s *Service) PutDashboard(ctx context.Context, d *influxdb.Dashboard) error {
return s.kv.Update(ctx, func(tx Tx) error {
for _, cell := range d.Cells {
if err := s.createCellView(ctx, tx, d.ID, cell.ID, nil); err != nil {
if err := s.createCellView(ctx, tx, d.ID, cell.ID, cell.View); err != nil {
return err
}
}
@ -818,6 +818,22 @@ func (s *Service) updateDashboard(ctx context.Context, tx Tx, id influxdb.ID, up
return nil, err
}
if upd.Cells != nil {
for _, c := range *upd.Cells {
if !c.ID.Valid() {
c.ID = s.IDGenerator.ID()
if c.View != nil {
c.View.ViewContents.ID = c.ID
}
}
}
for _, c := range d.Cells {
if err := s.deleteDashboardCellView(ctx, tx, d.ID, c.ID); err != nil {
return nil, err
}
}
}
if err := upd.Apply(d); err != nil {
return nil, err
}
@ -830,6 +846,14 @@ func (s *Service) updateDashboard(ctx context.Context, tx Tx, id influxdb.ID, up
return nil, err
}
if upd.Cells != nil {
for _, c := range d.Cells {
if err := s.putDashboardCellView(ctx, tx, d.ID, c.ID, c.View); err != nil {
return nil, err
}
}
}
return d, nil
}

View File

@ -1044,6 +1044,7 @@ func UpdateDashboard(
name string
description string
id platform.ID
cells []*platform.Cell
}
type wants struct {
err error
@ -1155,6 +1156,69 @@ func UpdateDashboard(
},
},
},
{
name: "update description name and cells",
fields: DashboardFields{
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2009, time.November, 10, 24, 0, 0, 0, time.UTC)},
IDGenerator: mock.IDGenerator{IDFn: func() platform.ID {
return 5
}},
Dashboards: []*platform.Dashboard{
{
ID: MustIDBase16(dashOneID),
OrganizationID: 1,
Name: "dashboard1",
},
{
ID: MustIDBase16(dashTwoID),
OrganizationID: 1,
Name: "dashboard2",
},
},
},
args: args{
id: MustIDBase16(dashOneID),
description: "changed",
name: "changed",
cells: []*platform.Cell{
{
CellProperty: platform.CellProperty{X: 0, Y: 2},
View: &platform.View{
Properties: &platform.SingleStatViewProperties{
Type: platform.ViewPropertyTypeSingleStat,
Queries: []platform.DashboardQuery{{Text: "buckets() |> count()"}},
},
},
},
},
},
wants: wants{
dashboard: &platform.Dashboard{
ID: MustIDBase16(dashOneID),
OrganizationID: 1,
Name: "changed",
Description: "changed",
Meta: platform.DashboardMeta{
UpdatedAt: time.Date(2009, time.November, 10, 24, 0, 0, 0, time.UTC),
},
Cells: []*platform.Cell{
{
ID: 5,
CellProperty: platform.CellProperty{X: 0, Y: 2},
View: &platform.View{
ViewContents: platform.ViewContents{
ID: 5,
},
Properties: &platform.SingleStatViewProperties{
Type: platform.ViewPropertyTypeSingleStat,
Queries: []platform.DashboardQuery{{Text: "buckets() |> count()"}},
},
},
},
},
},
},
},
{
name: "update with id not exist",
fields: DashboardFields{
@ -1200,6 +1264,9 @@ func UpdateDashboard(
if tt.args.description != "" {
upd.Description = &tt.args.description
}
if tt.args.cells != nil {
upd.Cells = &tt.args.cells
}
dashboard, err := s.UpdateDashboard(ctx, tt.args.id, upd)
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)

View File

@ -353,7 +353,10 @@ export const updateDashboard = (
try {
const resp = await api.patchDashboard({
dashboardID: dashboard.id,
data: dashboard,
data: {
name: dashboard.name,
description: dashboard.description,
},
})
if (resp.status !== 200) {