Skip to content

Commit

Permalink
Make text panel height smaller.
Browse files Browse the repository at this point in the history
The way Grafana handles these small heights is to render with a small height if the text can fit.
If the text is too large, it renders the panel larger.
This closes #52.
  • Loading branch information
IzakMarais committed Aug 3, 2018
1 parent f0dccb1 commit 997082a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
5 changes: 4 additions & 1 deletion grafana/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,12 @@ func (g client) getPanelURL(p Panel, dashName string, t TimeRange) string {
values.Add("panelId", strconv.Itoa(p.Id))
values.Add("from", t.From)
values.Add("to", t.To)
if p.IsSingleStat() {
if p.Is(SingleStat) {
values.Add("width", "300")
values.Add("height", "150")
} else if p.Is(Text) {
values.Add("width", "1000")
values.Add("height", "100")
} else {
values.Add("width", "1000")
values.Add("height", "500")
Expand Down
17 changes: 11 additions & 6 deletions grafana/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ func TestGrafanaClientFetchesPanelPNG(t *testing.T) {
So(requestURI, ShouldContainSubstring, "to=now")
})

Convey(fmt.Sprintf("The %s client should render singlestat panels should request a smaller size", clientDesc), func() {
So(requestURI, ShouldContainSubstring, "width=300")
So(requestURI, ShouldContainSubstring, "height=150")
})

Convey(fmt.Sprintf("The %s client should insert auth token should in request header", clientDesc), func() {
So(requestHeaders.Get("Authorization"), ShouldContainSubstring, apiToken)
})
Expand All @@ -111,13 +106,23 @@ func TestGrafanaClientFetchesPanelPNG(t *testing.T) {
So(requestURI, ShouldContainSubstring, "var-port=adapter")
})

Convey(fmt.Sprintf("The %s client should request singlestat panels at a smaller size", clientDesc), func() {
So(requestURI, ShouldContainSubstring, "width=300")
So(requestURI, ShouldContainSubstring, "height=150")
})

Convey(fmt.Sprintf("The %s client should request text panels with a small height", clientDesc), func() {
grf.GetPanelPng(Panel{44, "text", "title"}, "testDash", TimeRange{"now", "now-1h"})
So(requestURI, ShouldContainSubstring, "width=1000")
So(requestURI, ShouldContainSubstring, "height=100")
})

Convey(fmt.Sprintf("The %s client should request other panels in a larger size", clientDesc), func() {
grf.GetPanelPng(Panel{44, "graph", "title"}, "testDash", TimeRange{"now", "now-1h"})
So(requestURI, ShouldContainSubstring, "width=1000")
So(requestURI, ShouldContainSubstring, "height=500")
})
}

})
}

Expand Down
24 changes: 23 additions & 1 deletion grafana/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ import (
"strings"
)

type PanelType int

const (
SingleStat PanelType = iota
Text
Graph
Table
)

func (p PanelType) string() string {
return [...]string{
"singlestat",
"text",
"graph",
"table",
}[p]
}

// Panel represents a Grafana dashboard panel
type Panel struct {
Id int
Expand Down Expand Up @@ -106,7 +124,11 @@ func populatePanelsFromV5JSON(dash Dashboard, dc dashContainer) Dashboard {
}

func (p Panel) IsSingleStat() bool {
if p.Type == "singlestat" {
return p.Is(SingleStat)
}

func (p Panel) Is(t PanelType) bool {
if p.Type == t.string() {
return true
}
return false
Expand Down
27 changes: 17 additions & 10 deletions grafana/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ func TestV4Dashboard(t *testing.T) {
}`
dash := NewDashboard([]byte(v4DashJSON), url.Values{})

Convey("Panel IsSingelStat should work for all panels", func() {
So(dash.Panels[0].IsSingleStat(), ShouldBeTrue)
So(dash.Panels[1].IsSingleStat(), ShouldBeFalse)
So(dash.Panels[2].IsSingleStat(), ShouldBeTrue)
Convey("Panel Is(type) should work for all panels", func() {
So(dash.Panels[0].Is(Graph), ShouldBeFalse)
So(dash.Panels[0].Is(Text), ShouldBeFalse)
So(dash.Panels[0].Is(Table), ShouldBeFalse)
So(dash.Panels[0].Is(SingleStat), ShouldBeTrue)
So(dash.Panels[1].Is(Graph), ShouldBeTrue)
So(dash.Panels[2].Is(SingleStat), ShouldBeTrue)
})

Convey("Row title should be parsed and santised", func() {
Expand Down Expand Up @@ -82,7 +85,9 @@ func TestV5Dashboard(t *testing.T) {
[{"Type":"singlestat", "Id":0},
{"Type":"graph", "Id":1},
{"Type":"singlestat", "Id":2, "Title":"Panel3Title #"},
{"Type":"row", "Id":3}],
{"Type":"text", "Id":3},
{"Type":"table", "Id":4},
{"Type":"row", "Id":5}],
"Title":"DashTitle #"
},
Expand All @@ -91,18 +96,20 @@ func TestV5Dashboard(t *testing.T) {
}`
dash := NewDashboard([]byte(v5DashJSON), url.Values{})

Convey("Panel IsSingelStat should work for all panels", func() {
So(dash.Panels[0].IsSingleStat(), ShouldBeTrue)
So(dash.Panels[1].IsSingleStat(), ShouldBeFalse)
So(dash.Panels[2].IsSingleStat(), ShouldBeTrue)
Convey("Panel Is(type) should work for all panels", func() {
So(dash.Panels[0].Is(SingleStat), ShouldBeTrue)
So(dash.Panels[1].Is(Graph), ShouldBeTrue)
So(dash.Panels[2].Is(SingleStat), ShouldBeTrue)
So(dash.Panels[3].Is(Text), ShouldBeTrue)
So(dash.Panels[4].Is(Table), ShouldBeTrue)
})

Convey("Panel titles should be parsed and sanitised", func() {
So(dash.Panels[2].Title, ShouldEqual, "Panel3Title \\#")
})

Convey("Panels should contain all panels that have type != row", func() {
So(dash.Panels, ShouldHaveLength, 3)
So(dash.Panels, ShouldHaveLength, 5)
So(dash.Panels[0].Id, ShouldEqual, 0)
So(dash.Panels[1].Id, ShouldEqual, 1)
So(dash.Panels[2].Id, ShouldEqual, 2)
Expand Down
2 changes: 1 addition & 1 deletion report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (e *errClient) GetDashboard(dashName string) (grafana.Dashboard, error) {
return grafana.NewDashboard([]byte(dashJSON), e.variables), nil
}

//Procude an error on the 2nd panel fetched
//Produce an error on the 2nd panel fetched
func (e *errClient) GetPanelPng(p grafana.Panel, dashName string, t grafana.TimeRange) (io.ReadCloser, error) {
e.getPanelCallCount++
if e.getPanelCallCount == 2 {
Expand Down

0 comments on commit 997082a

Please sign in to comment.