Skip to content

Commit

Permalink
Add some documentation for the public functions
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Feb 2, 2020
1 parent 9809925 commit 855b589
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions axis/axis.go
Expand Up @@ -4,12 +4,15 @@ import (
"github.com/grafana-tools/sdk"
)

// Option represents an option that can be used to configure an axis.
type Option func(axis *Axis)

// Axis represents a visualization axis.
type Axis struct {
Builder *sdk.Axis
}

// New creates a new Axis configuration.
func New(options ...Option) *Axis {
axis := &Axis{Builder: &sdk.Axis{
Format: "short",
Expand Down
8 changes: 8 additions & 0 deletions client.go
Expand Up @@ -16,15 +16,22 @@ import (
"github.com/grafana-tools/sdk"
)

// ErrFolderNotFound is returned when the given folder can not be found.
var ErrFolderNotFound = errors.New("folder not found")

// ErrAlertChannelNotFound is returned when the given alert notification
// channel can not be found.
var ErrAlertChannelNotFound = errors.New("alert channel not found")

// Dashboard represents a Grafana dashboard.
type Dashboard struct {
ID uint `json:"id"`
UID string `json:"uid"`
URL string `json:"url"`
}

// Folder represents a dashboard folder.
// See https://grafana.com/docs/grafana/latest/reference/dashboard_folders/
type Folder struct {
ID uint `json:"id"`
UID string `json:"uid"`
Expand All @@ -37,6 +44,7 @@ type Client struct {
apiToken string
}

// NewClient creates a new Grafana HTTP client.
func NewClient(http *http.Client, host string, apiToken string) *Client {
return &Client{
http: http,
Expand Down
4 changes: 4 additions & 0 deletions dashboard.go
Expand Up @@ -18,12 +18,16 @@ type TagAnnotation struct {
Tags []string
}

// DashboardBuilderOption represents an option that can be used to configure a
// dashboard.
type DashboardBuilderOption func(dashboard *DashboardBuilder)

// DashboardBuilder is the main builder used to configure dashboards.
type DashboardBuilder struct {
board *sdk.Board
}

// NewDashboardBuilder creates a new dashboard builder.
func NewDashboardBuilder(title string, options ...DashboardBuilderOption) DashboardBuilder {
board := sdk.NewBoard(title)
board.ID = 0
Expand Down
4 changes: 4 additions & 0 deletions graph/graph.go
Expand Up @@ -7,12 +7,15 @@ import (
"github.com/grafana-tools/sdk"
)

// Option represents an option that can be used to configure a graph panel.
type Option func(graph *Graph)

// Graph represents a graph panel.
type Graph struct {
Builder *sdk.Panel
}

// New creates a new graph panel.
func New(title string, options ...Option) *Graph {
panel := &Graph{Builder: sdk.NewGraph(title)}

Expand Down Expand Up @@ -86,6 +89,7 @@ func defaultLegend() Option {
}
}

// WithPrometheusTarget adds a prometheus query to the graph.
func WithPrometheusTarget(query string, options ...prometheus.Option) Option {
target := prometheus.New(query, options...)

Expand Down
3 changes: 3 additions & 0 deletions row/row.go
Expand Up @@ -7,12 +7,15 @@ import (
"github.com/grafana-tools/sdk"
)

// Option represents an option that can be used to configure a row.
type Option func(row *Row)

// Row represents a dashboard row.
type Row struct {
builder *sdk.Row
}

// New creates a new row.
func New(board *sdk.Board, title string, options ...Option) *Row {
panel := &Row{builder: board.AddRow(title)}

Expand Down
4 changes: 4 additions & 0 deletions table/table.go
Expand Up @@ -6,6 +6,8 @@ import (
)

type AggregationType string

// Option represents an option that can be used to configure a table panel.
type Option func(table *Table)

const AVG AggregationType = "avg"
Expand All @@ -19,10 +21,12 @@ type Aggregation struct {
Type AggregationType
}

// Table represents a table panel.
type Table struct {
Builder *sdk.Panel
}

// New creates a new table panel.
func New(title string, options ...Option) *Table {
panel := &Table{Builder: sdk.NewTable(title)}
empty := ""
Expand Down
3 changes: 3 additions & 0 deletions text/text.go
Expand Up @@ -4,12 +4,15 @@ import (
"github.com/grafana-tools/sdk"
)

// Option represents an option that can be used to configure a text panel.
type Option func(text *Text)

// Text represents a text panel.
type Text struct {
Builder *sdk.Panel
}

// New creates a new text panel.
func New(title string, options ...Option) *Text {
panel := &Text{Builder: sdk.NewText(title)}

Expand Down
8 changes: 8 additions & 0 deletions variable/constant/constant.go
Expand Up @@ -4,15 +4,18 @@ import (
"github.com/grafana-tools/sdk"
)

// Option represents an option that can be used to configure a constant.
type Option func(constant *Constant)

// ValuesMap represent a "label" to "value" map of options for a constant variable.
type ValuesMap map[string]string

// Constant represents a "constant" templated variable.
type Constant struct {
Builder sdk.TemplateVar
}

// New creates a new "constant" templated variable.
func New(name string, options ...Option) *Constant {
constant := &Constant{Builder: sdk.TemplateVar{
Name: name,
Expand All @@ -27,6 +30,7 @@ func New(name string, options ...Option) *Constant {
return constant
}

// Values sets the possible values for the variable.
func Values(values ValuesMap) Option {
return func(constant *Constant) {
for label, value := range values {
Expand All @@ -38,6 +42,7 @@ func Values(values ValuesMap) Option {
}
}

// Default sets the default value of the variable.
func Default(value string) Option {
return func(constant *Constant) {
constant.Builder.Current = sdk.Current{
Expand All @@ -46,18 +51,21 @@ func Default(value string) Option {
}
}

// Label sets the label of the variable.
func Label(label string) Option {
return func(constant *Constant) {
constant.Builder.Label = label
}
}

// HideLabel ensures that this variable's label will not be displayed.
func HideLabel() Option {
return func(constant *Constant) {
constant.Builder.Hide = 1
}
}

// Hide ensures that the variable will not be displayed.
func Hide() Option {
return func(constant *Constant) {
constant.Builder.Hide = 2
Expand Down
10 changes: 10 additions & 0 deletions variable/custom/custom.go
Expand Up @@ -4,15 +4,18 @@ import (
"github.com/grafana-tools/sdk"
)

// Option represents an option that can be used to configure a custom variable.
type Option func(constant *Custom)

// ValuesMap represent a "label" to "value" map of options for a custom variable.
type ValuesMap map[string]string

// Custom represents a "custom" templated variable.
type Custom struct {
Builder sdk.TemplateVar
}

// New creates a new "custom" templated variable.
func New(name string, options ...Option) *Custom {
constant := &Custom{Builder: sdk.TemplateVar{
Name: name,
Expand All @@ -27,6 +30,7 @@ func New(name string, options ...Option) *Custom {
return constant
}

// Values sets the possible values for the variable.
func Values(values ValuesMap) Option {
return func(constant *Custom) {
for label, value := range values {
Expand All @@ -38,6 +42,7 @@ func Values(values ValuesMap) Option {
}
}

// Default sets the default value of the variable.
func Default(value string) Option {
return func(constant *Custom) {
constant.Builder.Current = sdk.Current{
Expand All @@ -46,30 +51,35 @@ func Default(value string) Option {
}
}

// Label sets the label of the variable.
func Label(label string) Option {
return func(constant *Custom) {
constant.Builder.Label = label
}
}

// HideLabel ensures that this variable's label will not be displayed.
func HideLabel() Option {
return func(constant *Custom) {
constant.Builder.Hide = 1
}
}

// Hide ensures that the variable will not be displayed.
func Hide() Option {
return func(constant *Custom) {
constant.Builder.Hide = 2
}
}

// Multi allows several values to be selected.
func Multi() Option {
return func(constant *Custom) {
constant.Builder.Multi = true
}
}

// IncludeAll adds an option to allow all values to be selected.
func IncludeAll() Option {
return func(constant *Custom) {
constant.Builder.IncludeAll = true
Expand Down
8 changes: 8 additions & 0 deletions variable/interval/interval.go
Expand Up @@ -6,15 +6,18 @@ import (
"github.com/grafana-tools/sdk"
)

// Option represents an option that can be used to configure an interval.
type Option func(interval *Interval)

// ValuesList represent a list of options for an interval variable.
type ValuesList []string

// Interval represents a "interval" templated variable.
type Interval struct {
Builder sdk.TemplateVar
}

// New creates a new "interval" templated variable.
func New(name string, options ...Option) *Interval {
interval := &Interval{Builder: sdk.TemplateVar{
Name: name,
Expand All @@ -29,12 +32,14 @@ func New(name string, options ...Option) *Interval {
return interval
}

// Values sets the possible values for the variable.
func Values(values ValuesList) Option {
return func(interval *Interval) {
interval.Builder.Query = strings.Join(values, ",")
}
}

// Default sets the default value of the variable.
func Default(value string) Option {
return func(interval *Interval) {
interval.Builder.Current = sdk.Current{
Expand All @@ -43,18 +48,21 @@ func Default(value string) Option {
}
}

// Label sets the label of the variable.
func Label(label string) Option {
return func(interval *Interval) {
interval.Builder.Label = label
}
}

// HideLabel ensures that this variable's label will not be displayed.
func HideLabel() Option {
return func(interval *Interval) {
interval.Builder.Hide = 1
}
}

// Hide ensures that the variable will not be displayed.
func Hide() Option {
return func(interval *Interval) {
interval.Builder.Hide = 2
Expand Down

0 comments on commit 855b589

Please sign in to comment.