Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add The Ability To Query For Dashboards by Title #163

Closed
gatlee21 opened this issue Feb 14, 2022 · 2 comments · Fixed by #164
Closed

Add The Ability To Query For Dashboards by Title #163

gatlee21 opened this issue Feb 14, 2022 · 2 comments · Fixed by #164
Labels
enhancement New feature or request

Comments

@gatlee21
Copy link

Using the client we are able to find if a folder exists. Likewise, It would be great if we could query whether a dashboard already exists.

It could be accomplished with two functions like this:

// This function queries the /api/search endpoint and returns the dashboard if it exists
func (client *Client) getDashboardByTitle(ctx context.Context, title string) (*DashboardQuery, error) {
	// 1. make a request to search for the dash board
	query := fmt.Sprintf("/api/search?query=%s", title)
	resp, err := client.get(ctx, query)

	if err != nil {
		err1 := fmt.Errorf("grafput: getDashboardByTitle %s", err)
		return nil, err1
	}

	// 2. check the status code
	if resp.StatusCode != http.StatusOK {
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			err1 := fmt.Errorf("grafput: getDashboardByTitle: %s", err)
			return nil, err1
		}

		return nil, fmt.Errorf("could not read body after querying dashboard: %s (HTTP status %d)", body, resp.StatusCode)
	}

	defer func() { _ = resp.Body.Close() }()

	// 3. decode json using our Dashboard Query struct
	var dashboard []DashboardQuery
	if err := decodeJSON(resp.Body, &dashboardQuery); err != nil {
		err1 := fmt.Errorf("grafput: getDashboardByTitle: %s", err)
		return nil, err1
	}

	if len(dashboardQuery) == 0 {
		return nil, nil
	}

	// 4. iterate through the list of dashboards until we match title
	for i := range dashboardQuery {
		if strings.EqualFold(dashboardQuery[i].Title, title) {
			return &dashboardQuery[i], nil
		}
	}

	return nil, nil
}
// This function enables us to search Grafana for a dashboard by its title
func (client *Client) CheckIfDashboardExists(ctx context.Context, title string) (string, error) {
	dashboard, err := client.getDashboardByTitle(ctx, title)
	if err != nil {
		err1 := fmt.Errorf("grafput: CheckIfDashboard Exists: %s", err)
		return "", err1
	}

	if dashboard == nil {
		return "", nil
	}

	dashboardUrl := dashboard.Url

       // here we could return the dashboard object or the URL 
	return dashboardUrl, nil
}
type DashboardQuery struct {
	Id          int      `json:"id"`
	Uid         string   `json:"uid"`
	Title       string   `json:"title"`
	Uri         string   `json:"uri"`
	Url         string   `json:"url"`
	Slug        string   `json:"slug"`
	Type        string   `json:"type"`
	Tags        []string `json:"tags"`
	IsStarred   bool     `json:"isStarred"`
	FolderId    int      `json:"folderId"`
	FolderUid   string   `json:"folderUid"`
	FolderTitle string   `json:"folderTitle"`
	FolderUrl   string   `json:"folderUrl"`
	SortMeta    int      `json:"sortMeta"`
}
@K-Phoen K-Phoen added the enhancement New feature or request label Feb 17, 2022
@K-Phoen
Copy link
Owner

K-Phoen commented Feb 17, 2022

To keep the API surface to a minimum, I think grabana should only expose a GetDashboardByTitle (since the existence check can be implemented rather easily by calling GetDashboardByTitle and checking for a well-known error like ErrDashboardNotFound).

Would you be willing to contribute this? :)

@gatlee21
Copy link
Author

Yeah, I agree that it would make more sense for the API to only expose GetDashboardByTitle. And yes, I would be interested in contributing. I can make a pull request sometime this or next week.

@K-Phoen K-Phoen linked a pull request Mar 8, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants