Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 2b63a7b

Browse files
authored
Paginate Dashboards(). (#124)
* Paginate Dashboards() * fix lint
1 parent ed90dc7 commit 2b63a7b

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

dashboard.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,32 @@ func (c *Client) NewDashboard(dashboard Dashboard) (*DashboardSaveResponse, erro
7575

7676
// Dashboards fetches and returns all dashboards.
7777
func (c *Client) Dashboards() ([]FolderDashboardSearchResponse, error) {
78-
params := url.Values{
79-
"type": {"dash-db"},
78+
const limit = 1000
79+
80+
var (
81+
page = 0
82+
newDashboards []FolderDashboardSearchResponse
83+
dashboards []FolderDashboardSearchResponse
84+
query = make(url.Values)
85+
)
86+
87+
query.Set("type", "dash-db")
88+
query.Set("limit", fmt.Sprint(limit))
89+
90+
for {
91+
page++
92+
query.Set("page", fmt.Sprint(page))
93+
94+
if err := c.request("GET", "/api/search", query, nil, &newDashboards); err != nil {
95+
return nil, err
96+
}
97+
98+
dashboards = append(dashboards, newDashboards...)
99+
100+
if len(newDashboards) < limit {
101+
return dashboards, nil
102+
}
80103
}
81-
return c.FolderDashboardSearch(params)
82104
}
83105

84106
// Dashboard will be removed.

dashboard_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gapi
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/gobs/pretty"
@@ -29,8 +30,7 @@ const (
2930
}
3031
}`
3132

32-
getDashboardsJSON = `[
33-
{
33+
getDashboardsJSON = `{
3434
"id": 1,
3535
"uid": "RGAPB1cZz",
3636
"title": "Grafana Stats",
@@ -40,8 +40,7 @@ const (
4040
"type": "dash-db",
4141
"tags": [],
4242
"isStarred": false
43-
}
44-
]`
43+
}`
4544
)
4645

4746
func TestDashboardCreateAndUpdate(t *testing.T) {
@@ -142,20 +141,32 @@ func TestDashboardDelete(t *testing.T) {
142141
}
143142

144143
func TestDashboards(t *testing.T) {
145-
client := gapiTestTools(t, 200, getDashboardsJSON)
144+
mockData := strings.Repeat(getDashboardsJSON+",", 1000) // make 1000 dashboards.
145+
mockData = "[" + mockData[:len(mockData)-1] + "]" // remove trailing comma; make a json list.
146+
147+
// This creates 1000 + 1000 + 1 (2001, 3 calls) worth of dashboards.
148+
client := gapiTestToolsFromCalls(t, []mockServerCall{
149+
{200, mockData},
150+
{200, mockData},
151+
{200, "[" + getDashboardsJSON + "]"},
152+
})
153+
154+
const dashCount = 2001
146155

147156
dashboards, err := client.Dashboards()
148157
if err != nil {
149158
t.Fatal(err)
150159
}
151160

152-
t.Log(pretty.PrettyFormat(dashboards))
153-
154-
if len(dashboards) != 1 {
155-
t.Error("Length of returned dashboards should be 1")
161+
if len(dashboards) != dashCount {
162+
t.Errorf("Length of returned dashboards should be %d", dashCount)
156163
}
157164

158165
if dashboards[0].ID != 1 || dashboards[0].Title != "Grafana Stats" {
159166
t.Error("Not correctly parsing returned dashboards.")
160167
}
168+
169+
if dashboards[dashCount-1].ID != 1 || dashboards[dashCount-1].Title != "Grafana Stats" {
170+
t.Error("Not correctly parsing returned dashboards.")
171+
}
161172
}

0 commit comments

Comments
 (0)