Skip to content

Commit

Permalink
Improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
tlindsay committed Sep 25, 2024
1 parent 41e266f commit a5d0b52
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 30 deletions.
98 changes: 77 additions & 21 deletions fastly/observability_custom_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ObservabilityCustomDashboard struct {
// DashboardItem describes an item (or "widget") of a dashboard
type DashboardItem struct {
// DataSource describes the source of the metrics to be displayed (required)
DataSource DataSource `json:"data_source"`
DataSource DashboardDataSource `json:"data_source"`
// ID is a unique identifier for the DashboardItem (read-only)
ID string `json:"id,omitempty"`
// Span is the number of columns (1-12) for the DashboardItem to span (default: 4)
Expand All @@ -50,38 +50,36 @@ type DashboardItem struct {
// Title is a human-readable title to display (optional)
Title string `json:"title"`
// Visualization describes the way the DashboardItem should display data (required)
Visualization Visualization `json:"visualization"`
Visualization DashboardVisualization `json:"visualization"`
}

type SourceType string
type DashboardSourceType string

const (
SourceTypeStatsEdge = "stats.edge"
SourceTypeStatsDomain = "stats.domain"
SourceTypeStatsOrigin = "stats.origin"
)

// DataSource describes the data to display in a DashboardItem
type DataSource struct {
// DashboardDataSource describes the data to display in a DashboardItem
type DashboardDataSource struct {
// Config describes configuration options for the selected data source (required)
Config SourceConfig `json:"config"`
Config DashboardSourceConfig `json:"config"`
// Type is the source of the data to display (required)
Type SourceType `json:"type"`
Type DashboardSourceType `json:"type"`
}

type Metric string

type SourceConfig struct {
type DashboardSourceConfig struct {
// Metrics is the list metrics to visualize (required)
// Valid options are defined by the selected SourceType. See https://www.fastly.com/documentation/reference/api/observability/custom-dashboards/#data-source
Metrics []Metric `json:"metrics"`
Metrics []string `json:"metrics"`
}

type VisualizationType string

const VisualizationTypeChart VisualizationType = "chart"

type Visualization struct {
type DashboardVisualization struct {
// Config describes configuration options for the given visualization (required)
Config VisualizationConfig `json:"config"`
// Type is type of visualization to display. Currently only "chart" is supported (required)
Expand All @@ -100,15 +98,15 @@ const (
type VisualizationFormat string

const (
FormatNumber VisualizationFormat = "number"
FormatBytes VisualizationFormat = "bytes"
FormatPercent VisualizationFormat = "percent"
FormatRequests VisualizationFormat = "requests"
FormatResponses VisualizationFormat = "responses"
FormatSeconds VisualizationFormat = "seconds"
FormatMilliseconds VisualizationFormat = "milliseconds"
FormatRatio VisualizationFormat = "ratio"
FormatBitrate VisualizationFormat = "bitrate"
VisualizationFormatNumber VisualizationFormat = "number"
VisualizationFormatBytes VisualizationFormat = "bytes"
VisualizationFormatPercent VisualizationFormat = "percent"
VisualizationFormatRequests VisualizationFormat = "requests"
VisualizationFormatResponses VisualizationFormat = "responses"
VisualizationFormatSeconds VisualizationFormat = "seconds"
VisualizationFormatMilliseconds VisualizationFormat = "milliseconds"
VisualizationFormatRatio VisualizationFormat = "ratio"
VisualizationFormatBitrate VisualizationFormat = "bitrate"
)

type CalculationMethod string
Expand All @@ -130,6 +128,64 @@ type VisualizationConfig struct {
PlotType PlotType `json:"plot_type"`
}

type DashboardItemOption interface {
apply(*DashboardItem)
}
type optionFunc func(*DashboardItem)

func (f optionFunc) apply(di *DashboardItem) {
f(di)
}

func WithTitle(title string) DashboardItemOption {
return optionFunc(func(di *DashboardItem) {
di.Title = title
})
}
func WithSubtitle(subtitle string) DashboardItemOption {
return optionFunc(func(di *DashboardItem) {
di.Subtitle = subtitle
})
}
func WithSpan(span uint8) DashboardItemOption {
return optionFunc(func(di *DashboardItem) {
di.Span = span
})
}
func WithCalculationMethod(calculationMethod CalculationMethod) DashboardItemOption {
return optionFunc(func(di *DashboardItem) {
di.Visualization.Config.CalculationMethod = &calculationMethod
})
}
func WithFormat(format VisualizationFormat) DashboardItemOption {
return optionFunc(func(di *DashboardItem) {
di.Visualization.Config.Format = &format
})
}

func NewDashboardItem(sourceType DashboardSourceType, metrics []string, plotType PlotType, options ...DashboardItemOption) DashboardItem {
di := DashboardItem{
DataSource: DashboardDataSource{
Type: sourceType,
Config: DashboardSourceConfig{
Metrics: metrics,
},
},
Visualization: DashboardVisualization{
Type: VisualizationTypeChart,
Config: VisualizationConfig{
PlotType: plotType,
},
},
}

for _, o := range options {
o.apply(&di)
}

return di
}

type ListDashboardsResponse struct {
Data []ObservabilityCustomDashboard `json:"data"`
Meta DashboardMeta `json:"meta"`
Expand Down
18 changes: 9 additions & 9 deletions fastly/observability_custom_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ func TestClient_ObservabilityCustomDashboards(t *testing.T) {
Description: ToPointer("My dashboard is super cool."),
Name: "My Cool Dashboard",
Items: []DashboardItem{{
DataSource: DataSource{
Config: SourceConfig{
Metrics: []Metric{"requests"},
DataSource: DashboardDataSource{
Config: DashboardSourceConfig{
Metrics: []string{"requests"},
},
Type: SourceTypeStatsEdge,
},
Span: 4,
Subtitle: "This is a subtitle",
Title: "A Dashboard Item",
Visualization: Visualization{
Visualization: DashboardVisualization{
Config: VisualizationConfig{PlotType: PlotTypeLine},
Type: VisualizationTypeChart,
},
Expand Down Expand Up @@ -90,21 +90,21 @@ func TestClient_ObservabilityCustomDashboards(t *testing.T) {
// Update
var ucd *ObservabilityCustomDashboard
items := ocd.Items
items[0].DataSource.Config.Metrics = []Metric{"edge_hit_requests"}
items[0].DataSource.Config.Metrics = []string{"edge_hit_requests"}
items[0].Visualization.Config.PlotType = PlotTypeSingleMetric
items[0].Title = "An Updated Dashboard Item"

items = append(items, DashboardItem{
DataSource: DataSource{
Config: SourceConfig{
Metrics: []Metric{"requests"},
DataSource: DashboardDataSource{
Config: DashboardSourceConfig{
Metrics: []string{"requests"},
},
Type: SourceTypeStatsEdge,
},
Span: 4,
Subtitle: "This is a subtitle",
Title: "A New Dashboard Item",
Visualization: Visualization{
Visualization: DashboardVisualization{
Config: VisualizationConfig{PlotType: PlotTypeLine},
Type: VisualizationTypeChart,
},
Expand Down

0 comments on commit a5d0b52

Please sign in to comment.