Skip to content

Commit

Permalink
Merge pull request #218 from K-Phoen/text-var
Browse files Browse the repository at this point in the history
Text var
  • Loading branch information
K-Phoen authored Mar 21, 2023
2 parents 5dcfa2c + 322c68b commit 5dfb42c
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 14 deletions.
14 changes: 13 additions & 1 deletion dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"encoding/json"

"github.com/K-Phoen/grabana/alert"

"github.com/K-Phoen/grabana/row"
"github.com/K-Phoen/grabana/variable/constant"
"github.com/K-Phoen/grabana/variable/custom"
"github.com/K-Phoen/grabana/variable/datasource"
"github.com/K-Phoen/grabana/variable/interval"
"github.com/K-Phoen/grabana/variable/query"
"github.com/K-Phoen/grabana/variable/text"
"github.com/K-Phoen/sdk"
)

Expand Down Expand Up @@ -213,6 +213,18 @@ func VariableAsDatasource(name string, options ...datasource.Option) Option {
}
}

// VariableAsText adds a templated variable, defined as a free text input.
// See https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#add-a-text-box-variable
func VariableAsText(name string, options ...text.Option) Option {
return func(builder *Builder) error {
templatedVar := text.New(name, options...)

builder.board.Templating.List = append(builder.board.Templating.List, templatedVar.Builder)

return nil
}
}

// ExternalLinks adds a dashboard-level external links.
// See https://grafana.com/docs/grafana/latest/dashboards/build-dashboards/manage-dashboard-links/#add-a-url-link-to-a-dashboard
func ExternalLinks(links ...ExternalLink) Option {
Expand Down
13 changes: 11 additions & 2 deletions dashboard/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

"github.com/K-Phoen/grabana/variable/datasource"
"github.com/K-Phoen/grabana/variable/text"
"github.com/stretchr/testify/require"
)

func requireJSON(t *testing.T, payload []byte) {
Expand Down Expand Up @@ -215,6 +215,15 @@ func TestDashboardCanHaveVariablesAsDatasource(t *testing.T) {
req.Len(panel.board.Templating.List, 1)
}

func TestDashboardCanHaveVariablesAsText(t *testing.T) {
req := require.New(t)

panel, err := New("", VariableAsText("filter", text.Label("Filter")))

req.NoError(err)
req.Len(panel.board.Templating.List, 1)
}

func TestDashboardCanHaveRows(t *testing.T) {
req := require.New(t)

Expand Down
55 changes: 44 additions & 11 deletions decoder/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/K-Phoen/grabana/variable/datasource"
"github.com/K-Phoen/grabana/variable/interval"
"github.com/K-Phoen/grabana/variable/query"
"github.com/K-Phoen/grabana/variable/text"
)

var ErrVariableNotConfigured = fmt.Errorf("variable not configured")
Expand All @@ -20,6 +21,7 @@ type DashboardVariable struct {
Query *VariableQuery `yaml:",omitempty"`
Const *VariableConst `yaml:",omitempty"`
Datasource *VariableDatasource `yaml:",omitempty"`
Text *VariableText `yaml:",omitempty"`
}

func (variable *DashboardVariable) toOption() (dashboard.Option, error) {
Expand All @@ -38,14 +40,17 @@ func (variable *DashboardVariable) toOption() (dashboard.Option, error) {
if variable.Datasource != nil {
return variable.Datasource.toOption()
}
if variable.Text != nil {
return variable.Text.toOption()
}

return nil, ErrVariableNotConfigured
}

type VariableInterval struct {
Name string
Label string
Default string
Label string `yaml:",omitempty"`
Default string `yaml:",omitempty"`
Values []string `yaml:",flow"`
Hide string `yaml:",omitempty"`
}
Expand Down Expand Up @@ -79,8 +84,8 @@ func (variable *VariableInterval) toOption() (dashboard.Option, error) {

type VariableCustom struct {
Name string
Label string
Default string
Label string `yaml:",omitempty"`
Default string `yaml:",omitempty"`
ValuesMap map[string]string `yaml:"values_map"`
IncludeAll bool `yaml:"include_all"`
AllValue string `yaml:"all_value,omitempty"`
Expand Down Expand Up @@ -126,8 +131,8 @@ func (variable *VariableCustom) toOption() (dashboard.Option, error) {

type VariableConst struct {
Name string
Label string
Default string
Label string `yaml:",omitempty"`
Default string `yaml:",omitempty"`
ValuesMap map[string]string `yaml:"values_map"`
Hide string `yaml:",omitempty"`
}
Expand Down Expand Up @@ -161,12 +166,12 @@ func (variable *VariableConst) toOption() (dashboard.Option, error) {

type VariableQuery struct {
Name string
Label string
Label string `yaml:",omitempty"`

Datasource string
Datasource string `yaml:",omitempty"`
Request string

Regex string
Regex string `yaml:",omitempty"`
IncludeAll bool `yaml:"include_all"`
DefaultAll bool `yaml:"default_all"`
AllValue string `yaml:"all_value,omitempty"`
Expand Down Expand Up @@ -218,11 +223,11 @@ func (variable *VariableQuery) toOption() (dashboard.Option, error) {

type VariableDatasource struct {
Name string
Label string
Label string `yaml:",omitempty"`

Type string

Regex string
Regex string `yaml:",omitempty"`
IncludeAll bool `yaml:"include_all"`
Hide string `yaml:",omitempty"`
Multiple bool `yaml:",omitempty"`
Expand Down Expand Up @@ -260,3 +265,31 @@ func (variable *VariableDatasource) toOption() (dashboard.Option, error) {

return dashboard.VariableAsDatasource(variable.Name, opts...), nil
}

type VariableText struct {
Name string
Label string `yaml:",omitempty"`
Hide string `yaml:",omitempty"`
}

func (variable *VariableText) toOption() (dashboard.Option, error) {
var opts []text.Option

if variable.Label != "" {
opts = append(opts, text.Label(variable.Label))
}

switch variable.Hide {
case "":
// Nothing to do
break
case "label":
opts = append(opts, text.HideLabel())
case "variable":
opts = append(opts, text.Hide())
default:
return dashboard.VariableAsQuery(variable.Name), ErrInvalidHideValue
}

return dashboard.VariableAsText(variable.Name, opts...), nil
}
50 changes: 50 additions & 0 deletions variable/text/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package text

import (
"github.com/K-Phoen/sdk"
)

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

// Text represents a "textbox" templated variable.
type Text struct {
Builder sdk.TemplateVar
}

// New creates a new "query" templated variable.
func New(name string, options ...Option) *Text {
query := &Text{Builder: sdk.TemplateVar{
Name: name,
Label: name,
Type: "textbox",
Options: []sdk.Option{},
}}

for _, opt := range options {
opt(query)
}

return query
}

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

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

// Hide ensures that the variable will not be displayed.
func Hide() Option {
return func(query *Text) {
query.Builder.Hide = 2
}
}
42 changes: 42 additions & 0 deletions variable/text/text_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package text

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNewTextVariablesCanBeCreated(t *testing.T) {
req := require.New(t)

panel := New("filter")

req.Equal("filter", panel.Builder.Name)
req.Equal("filter", panel.Builder.Label)
req.Equal("textbox", panel.Builder.Type)
}

func TestLabelCanBeSet(t *testing.T) {
req := require.New(t)

panel := New("filter", Label("Filter"))

req.Equal("filter", panel.Builder.Name)
req.Equal("Filter", panel.Builder.Label)
}

func TestLabelCanBeHidden(t *testing.T) {
req := require.New(t)

panel := New("", HideLabel())

req.Equal(uint8(1), panel.Builder.Hide)
}

func TestVariableCanBeHidden(t *testing.T) {
req := require.New(t)

panel := New("", Hide())

req.Equal(uint8(2), panel.Builder.Hide)
}

0 comments on commit 5dfb42c

Please sign in to comment.