Skip to content

Commit

Permalink
update grafana.Client.SearchDashboards to support 'query' parameter a…
Browse files Browse the repository at this point in the history
…nd retrieve dashboards url
  • Loading branch information
negrel committed Aug 10, 2024
1 parent a70b634 commit 21265b4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
7 changes: 5 additions & 2 deletions pkg/grafana/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"time"

Expand Down Expand Up @@ -65,6 +66,8 @@ type Dashboard struct {
type SearchDashboardResult struct {
Uid Uid `json:"uid"`
Title string `json:"title"`
// This is just dashboard path but we match schema of API response.
Url string `json:"url"`
}

// CreateUpdateDashboard creates/updates a dashboard in the given organization and folder.
Expand Down Expand Up @@ -184,12 +187,12 @@ func (c Client) DeleteDashboardByUid(ctx context.Context, orgId OrgId, dashboard
}

// SearchDashboards searches dashboard within the given organization.
func (c Client) SearchDashboards(ctx context.Context, orgId OrgId, limit, page int) ([]SearchDashboardResult, error) {
func (c Client) SearchDashboards(ctx context.Context, orgId OrgId, limit, page int, query string) ([]SearchDashboardResult, error) {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)

req.Header.SetMethod("GET")
req.SetRequestURI(fmt.Sprintf("%v/api/search?type=dash-db&limit=%v&page=%v", c.cfg.Url, limit, page))
req.SetRequestURI(fmt.Sprintf("%v/api/search?type=dash-db&limit=%v&page=%v&query=%v", c.cfg.Url, limit, page, url.QueryEscape(query)))
c.addAuthorizationHeader(req)
req.Header.Set(GrafanaOrgIdHeader, fmt.Sprint(orgId))

Expand Down
55 changes: 45 additions & 10 deletions pkg/grafana/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/rand"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -318,7 +319,7 @@ func TestIntegSearchDashboards(t *testing.T) {
orgId, err := cli.CreateOrg(context.Background(), orgName)
require.NoError(t, err)

results, err := cli.SearchDashboards(context.Background(), orgId, 100, 1)
results, err := cli.SearchDashboards(context.Background(), orgId, 100, 1, "")
require.NoError(t, err)
require.Len(t, results, 0)
})
Expand All @@ -331,7 +332,7 @@ func TestIntegSearchDashboards(t *testing.T) {
dashboardId, err := cli.CreateUpdateDashboard(context.Background(), orgId, Uid{}, map[string]any{"title": "Dashboard 1"}, true)
require.NoError(t, err)

results, err := cli.SearchDashboards(context.Background(), orgId, 100, 1)
results, err := cli.SearchDashboards(context.Background(), orgId, 100, 1, "")
require.NoError(t, err)
require.Len(t, results, 1)
require.Equal(t, dashboardId, results[0].Uid)
Expand All @@ -349,13 +350,24 @@ func TestIntegSearchDashboards(t *testing.T) {
dashboardId, err := cli.CreateUpdateDashboard(context.Background(), orgId, Uid{}, map[string]any{"title": dashboardTitle}, true)
require.NoError(t, err)

expectedSearchResults = append(expectedSearchResults, SearchDashboardResult{dashboardId, dashboardTitle})
expectedSearchResults = append(expectedSearchResults, SearchDashboardResult{
Uid: dashboardId,
Title: dashboardTitle,
Url: "", // Checked separately.
})
}

results, err := cli.SearchDashboards(context.Background(), orgId, 100, 1)
searchResults, err := cli.SearchDashboards(context.Background(), orgId, 100, 1, "")
require.NoError(t, err)
require.Len(t, results, len(expectedSearchResults))
require.Equal(t, expectedSearchResults, results)
require.Len(t, searchResults, len(expectedSearchResults))

// result.Url is random so we check it here.
for i, result := range searchResults {
require.Contains(t, result.Url, "/d/")
require.Contains(t, result.Url, strings.ReplaceAll(strings.ToLower(result.Title), " ", "-"))
searchResults[i].Url = ""
}
require.Equal(t, expectedSearchResults, searchResults)
})

t.Run("MultiplePage", func(t *testing.T) {
Expand All @@ -369,26 +381,49 @@ func TestIntegSearchDashboards(t *testing.T) {
dashboardId, err := cli.CreateUpdateDashboard(context.Background(), orgId, Uid{}, map[string]any{"title": dashboardTitle}, true)
require.NoError(t, err)

expectedSearchResults = append(expectedSearchResults, SearchDashboardResult{dashboardId, dashboardTitle})
expectedSearchResults = append(expectedSearchResults, SearchDashboardResult{
Uid: dashboardId,
Title: dashboardTitle,
Url: "",
})
}

// Fetch first page.
page1, err := cli.SearchDashboards(context.Background(), orgId, 5, 1)
page1, err := cli.SearchDashboards(context.Background(), orgId, 5, 1, "")
require.NoError(t, err)
require.Len(t, page1, 5)

// Fetch second page.
page2, err := cli.SearchDashboards(context.Background(), orgId, 5, 2)
page2, err := cli.SearchDashboards(context.Background(), orgId, 5, 2, "")
require.NoError(t, err)
require.Len(t, page2, 5)

var searchResults []SearchDashboardResult
searchResults = append(searchResults, page1...)
searchResults = append(searchResults, page2...)

// result.Url is random so we check it here.
for i, result := range searchResults {
require.Contains(t, result.Url, "/d/")
require.Contains(t, result.Url, strings.ReplaceAll(strings.ToLower(result.Title), " ", "-"))
searchResults[i].Url = ""
}
require.Equal(t, expectedSearchResults, searchResults)
})

t.Run("NoResult", func(t *testing.T) {
orgName := fmt.Sprintf("foo-%v", rand.Int())
orgId, err := cli.CreateOrg(context.Background(), orgName)
require.NoError(t, err)

_, err = cli.CreateUpdateDashboard(context.Background(), orgId, Uid{}, map[string]any{"title": "Dashboard 1"}, true)
require.NoError(t, err)

results, err := cli.SearchDashboards(context.Background(), orgId, 100, 9, "Non existent dashboard")
require.NoError(t, err)
require.Len(t, results, 0)
})

t.Run("NonExistentPage", func(t *testing.T) {
orgName := fmt.Sprintf("foo-%v", rand.Int())
orgId, err := cli.CreateOrg(context.Background(), orgName)
Expand All @@ -397,7 +432,7 @@ func TestIntegSearchDashboards(t *testing.T) {
_, err = cli.CreateUpdateDashboard(context.Background(), orgId, Uid{}, map[string]any{"title": "Dashboard 1"}, true)
require.NoError(t, err)

results, err := cli.SearchDashboards(context.Background(), orgId, 100, 9)
results, err := cli.SearchDashboards(context.Background(), orgId, 100, 9, "")
require.NoError(t, err)
require.Len(t, results, 0)
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/grafana/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestIntegService(t *testing.T) {
require.Equal(t, folders[0].Title, "Prisme Analytics")

// Check dashboards were created.
dashboards, err := cli.SearchDashboards(ctx, orgId, 100, 0)
dashboards, err := cli.SearchDashboards(ctx, orgId, 100, 0, "Web Analytics")
require.NoError(t, err)
require.Len(t, dashboards, 1)
require.Equal(t, dashboards[0].Title, "Web Analytics")
Expand Down

0 comments on commit 21265b4

Please sign in to comment.