2018-05-31 14:44:23 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// ErrDashboardNotFound is the error for a missing dashboard.
|
2018-08-27 17:09:17 +00:00
|
|
|
const ErrDashboardNotFound = ChronografError("dashboard not found")
|
2018-08-07 20:10:05 +00:00
|
|
|
|
|
|
|
// ErrCellNotFound is the error for a missing cell.
|
2018-08-27 17:09:17 +00:00
|
|
|
const ErrCellNotFound = ChronografError("cell not found")
|
2018-08-07 20:10:05 +00:00
|
|
|
|
2018-05-31 14:44:23 +00:00
|
|
|
// DashboardService represents a service for managing dashboard data.
|
|
|
|
type DashboardService interface {
|
|
|
|
// FindDashboardByID returns a single dashboard by ID.
|
|
|
|
FindDashboardByID(ctx context.Context, id ID) (*Dashboard, error)
|
|
|
|
|
|
|
|
// FindDashboards returns a list of dashboards that match filter and the total count of matching dashboards.
|
|
|
|
// Additional options provide pagination & sorting.
|
|
|
|
FindDashboards(ctx context.Context, filter DashboardFilter) ([]*Dashboard, int, error)
|
|
|
|
|
|
|
|
// CreateDashboard creates a new dashboard and sets b.ID with the new identifier.
|
|
|
|
CreateDashboard(ctx context.Context, b *Dashboard) error
|
|
|
|
|
|
|
|
// UpdateDashboard updates a single dashboard with changeset.
|
|
|
|
// Returns the new dashboard state after update.
|
|
|
|
UpdateDashboard(ctx context.Context, id ID, upd DashboardUpdate) (*Dashboard, error)
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// AddDashboardCell adds a cell to a dashboard.
|
|
|
|
AddDashboardCell(ctx context.Context, id ID, c *Cell, opts AddDashboardCellOptions) error
|
2018-05-31 14:44:23 +00:00
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// RemoveDashboardCell removes a dashbaord.
|
2018-05-31 14:44:23 +00:00
|
|
|
RemoveDashboardCell(ctx context.Context, dashboardID, cellID ID) error
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// UpdateDashboardCell replaces the dashboard cell with the provided ID.
|
|
|
|
UpdateDashboardCell(ctx context.Context, dashboardID, cellID ID, upd CellUpdate) (*Cell, error)
|
2018-05-31 14:44:23 +00:00
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// DeleteDashboard removes a dashboard by ID.
|
|
|
|
DeleteDashboard(ctx context.Context, id ID) error
|
|
|
|
|
|
|
|
// ReplaceDashboardCells replaces all cells in a dashboard
|
|
|
|
ReplaceDashboardCells(ctx context.Context, id ID, c []*Cell) error
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dashboard represents all visual and query data for a dashboard
|
|
|
|
type Dashboard struct {
|
2018-08-07 20:10:05 +00:00
|
|
|
ID ID `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Cells []*Cell `json:"cells"`
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// Cell holds positional information about a cell on dashboard and a reference to a cell.
|
|
|
|
type Cell struct {
|
|
|
|
ID ID `json:"id"`
|
|
|
|
X int32 `json:"x"`
|
|
|
|
Y int32 `json:"y"`
|
|
|
|
W int32 `json:"w"`
|
|
|
|
H int32 `json:"h"`
|
|
|
|
ViewID ID `json:"viewID"`
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// DashboardFilter is a filter for dashboards.
|
|
|
|
type DashboardFilter struct {
|
2018-10-04 23:30:51 +00:00
|
|
|
IDs []*ID
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// DashboardUpdate is the patch structure for a dashboard.
|
|
|
|
type DashboardUpdate struct {
|
|
|
|
Name *string `json:"name"`
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// AddDashboardCellOptions are options for adding a dashboard.
|
|
|
|
type AddDashboardCellOptions struct {
|
|
|
|
// UsingView specifies the view that should be used as a template
|
|
|
|
// for the new cells view.
|
|
|
|
UsingView ID
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// Apply applies an update to a dashboard.
|
|
|
|
func (u DashboardUpdate) Apply(d *Dashboard) error {
|
|
|
|
if u.Name != nil {
|
|
|
|
d.Name = *u.Name
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// CellUpdate is the patch structure for a cell.
|
|
|
|
type CellUpdate struct {
|
|
|
|
X *int32 `json:"x"`
|
|
|
|
Y *int32 `json:"y"`
|
|
|
|
W *int32 `json:"w"`
|
|
|
|
H *int32 `json:"h"`
|
2018-07-20 10:24:07 +00:00
|
|
|
ViewID ID `json:"viewID"`
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// Apply applies an update to a Cell.
|
|
|
|
func (u CellUpdate) Apply(c *Cell) error {
|
|
|
|
if u.X != nil {
|
|
|
|
c.X = *u.X
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
if u.Y != nil {
|
|
|
|
c.Y = *u.Y
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
if u.W != nil {
|
|
|
|
c.W = *u.W
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
if u.H != nil {
|
|
|
|
c.H = *u.H
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 10:24:07 +00:00
|
|
|
if u.ViewID.Valid() {
|
|
|
|
c.ViewID = u.ViewID
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
return nil
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 20:10:05 +00:00
|
|
|
// Valid returns an error if the dashboard update is invalid.
|
|
|
|
func (u DashboardUpdate) Valid() error {
|
|
|
|
if u.Name == nil {
|
|
|
|
return fmt.Errorf("must update at least one attribute")
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|
2018-08-07 20:10:05 +00:00
|
|
|
|
|
|
|
return nil
|
2018-05-31 14:44:23 +00:00
|
|
|
}
|