Skip to content

Commit

Permalink
Merge pull request #60 from K-Phoen/slimsag-time
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen authored Apr 7, 2020
2 parents 7a7a837 + 0b84929 commit 4f76f76
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
42 changes: 31 additions & 11 deletions dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ type TagAnnotation struct {
// dashboard.
type Option func(dashboard *Builder)

// TimezoneOption represents a possible value for the dashboard's timezone
// configuration.
type TimezoneOption string

// DefaultTimezone sets the dashboard's timezone to the default one used by
// Grafana.
const DefaultTimezone TimezoneOption = ""

// UTC sets the dashboard's timezone to UTC.
const UTC TimezoneOption = "utc"

// Browser sets the dashboard's timezone to the browser's one.
const Browser TimezoneOption = "browser"

// Builder is the main builder used to configure dashboards.
type Builder struct {
board *sdk.Board
Expand All @@ -33,7 +47,6 @@ type Builder struct {
func New(title string, options ...Option) Builder {
board := sdk.NewBoard(title)
board.ID = 0
board.Timezone = ""

builder := &Builder{board: board}

Expand All @@ -47,20 +60,12 @@ func New(title string, options ...Option) Builder {
func defaults() []Option {
return []Option{
defaultTimePicker(),
defaultTime(),
Timezone(DefaultTimezone),
Time("now-3h", "now"),
SharedCrossHair(),
}
}

func defaultTime() Option {
return func(builder *Builder) {
builder.board.Time = sdk.Time{
From: "now-3h",
To: "now",
}
}
}

func defaultTimePicker() Option {
return func(builder *Builder) {
builder.board.Timepicker = sdk.Timepicker{
Expand Down Expand Up @@ -203,3 +208,18 @@ func AutoRefresh(interval string) Option {
builder.board.Refresh = &sdk.BoolString{Flag: true, Value: interval}
}
}

// Time defines the default time range for the dashboard, e.g. from "now-6h" to
// "now".
func Time(from, to string) Option {
return func(builder *Builder) {
builder.board.Time = sdk.Time{From: from, To: to}
}
}

// Timezone defines the default timezone for the dashboard, e.g. "utc".
func Timezone(timezone TimezoneOption) Option {
return func(builder *Builder) {
builder.board.Timezone = string(timezone)
}
}
17 changes: 17 additions & 0 deletions dashboard/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ func TestDashboardCanBeAutoRefreshed(t *testing.T) {
req.Equal("5s", panel.board.Refresh.Value)
}

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

panel := New("", Time("now-6h", "now"))

req.Equal("now-6h", panel.board.Time.From)
req.Equal("now", panel.board.Time.To)
}

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

panel := New("", Timezone(UTC))

req.Equal("utc", panel.board.Timezone)
}

func TestDashboardCanHaveTags(t *testing.T) {
req := require.New(t)
tags := []string{"generated", "grabana"}
Expand Down

0 comments on commit 4f76f76

Please sign in to comment.