2024-02-15 01:12:57 +01:00
|
|
|
package grafana
|
|
|
|
|
|
|
|
import (
|
2024-02-16 21:56:27 +01:00
|
|
|
"bytes"
|
2024-02-15 01:12:57 +01:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
type BoardProperties struct {
|
|
|
|
IsStarred bool `json:"isStarred,omitempty"`
|
|
|
|
IsHome bool `json:"isHome,omitempty"`
|
|
|
|
IsSnapshot bool `json:"isSnapshot,omitempty"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
CanSave bool `json:"canSave"`
|
|
|
|
CanEdit bool `json:"canEdit"`
|
|
|
|
CanStar bool `json:"canStar"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
Expires time.Time `json:"expires"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Updated time.Time `json:"updated"`
|
|
|
|
UpdatedBy string `json:"updatedBy"`
|
|
|
|
CreatedBy string `json:"createdBy"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
FolderID int `json:"folderId"`
|
|
|
|
FolderTitle string `json:"folderTitle"`
|
|
|
|
FolderURL string `json:"folderUrl"`
|
|
|
|
}
|
|
|
|
|
2024-02-15 01:12:57 +01:00
|
|
|
type DashboardVersion struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
DashboardID uint `json:"dashboardId"`
|
2024-02-16 21:56:27 +01:00
|
|
|
DashboardUID string `json:"uid"`
|
2024-02-15 01:12:57 +01:00
|
|
|
ParentVersion uint `json:"parentVersion"`
|
|
|
|
RestoredFrom uint `json:"restoredFrom"`
|
|
|
|
Version uint `json:"version"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
CreatedBy string `json:"createdBy"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
func (c *Client) getRawDashboardByUID(ctx context.Context, path string) ([]byte, BoardProperties, error) {
|
|
|
|
raw, code, err := c.get(ctx, fmt.Sprintf("api/dashboards/%s", path), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BoardProperties{}, err
|
|
|
|
}
|
|
|
|
if code != 200 {
|
|
|
|
return raw, BoardProperties{}, fmt.Errorf("HTTP error %d: returns %s", code, raw)
|
|
|
|
}
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
var result struct {
|
|
|
|
Meta BoardProperties `json:"meta"`
|
|
|
|
}
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
dec := json.NewDecoder(bytes.NewReader(raw))
|
|
|
|
dec.UseNumber()
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
if err := dec.Decode(&result); err != nil {
|
|
|
|
return raw, BoardProperties{}, fmt.Errorf("failed unmarshalling dashboard from path %s: %v", path, err)
|
|
|
|
}
|
|
|
|
return raw, result.Meta, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getRawDashboardFromVersion(ctx context.Context, path string) ([]byte, DashboardVersion, error) {
|
|
|
|
var versionInfo DashboardVersion
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
raw, code, err := c.get(ctx, fmt.Sprintf("api/dashboards/%s", path), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, versionInfo, err
|
|
|
|
}
|
|
|
|
if code != 200 {
|
|
|
|
return raw, versionInfo, fmt.Errorf("HTTP error %d: returns %s", code, raw)
|
|
|
|
}
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
dec := json.NewDecoder(bytes.NewReader(raw))
|
|
|
|
dec.UseNumber()
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
if err := dec.Decode(&versionInfo); err != nil {
|
|
|
|
return raw, versionInfo, fmt.Errorf("failed unmarshalling dashboard from path %s: %v", path, err)
|
|
|
|
}
|
|
|
|
return raw, versionInfo, nil
|
|
|
|
}
|
|
|
|
|
2024-02-15 01:12:57 +01:00
|
|
|
func queryParams(params ...QueryParam) url.Values {
|
|
|
|
u := url.URL{}
|
|
|
|
q := u.Query()
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-02-15 01:12:57 +01:00
|
|
|
for _, p := range params {
|
|
|
|
p(&q)
|
|
|
|
}
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
func (c *Client) GetDashboardVersionsByDashboardUID(ctx context.Context, uid string, params ...QueryParam) ([]DashboardVersion, error) {
|
2024-02-15 01:12:57 +01:00
|
|
|
var (
|
|
|
|
raw []byte
|
|
|
|
code int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
if raw, code, err = c.get(ctx, fmt.Sprintf("api/dashboards/uid/%s/versions", uid), queryParams(params...)); err != nil {
|
2024-02-15 01:12:57 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if code != 200 {
|
|
|
|
return nil, fmt.Errorf("HTTP error %d: returns %s", code, raw)
|
|
|
|
}
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-02-15 01:12:57 +01:00
|
|
|
var versions []DashboardVersion
|
|
|
|
err = json.Unmarshal(raw, &versions)
|
|
|
|
|
|
|
|
return versions, err
|
|
|
|
}
|
|
|
|
|
2024-02-16 21:56:27 +01:00
|
|
|
func (c *Client) GetRawDashboardByUID(ctx context.Context, uid string) ([]byte, BoardProperties, error) {
|
|
|
|
return c.getRawDashboardByUID(ctx, "uid/"+uid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetRawDashboardByUIDAndVersion(ctx context.Context, uid string, version uint) ([]byte, DashboardVersion, error) {
|
|
|
|
return c.getRawDashboardFromVersion(ctx, "uid/"+uid+"/versions/"+fmt.Sprint(version))
|
|
|
|
}
|
|
|
|
|
2024-02-15 01:12:57 +01:00
|
|
|
func (c *Client) SearchDashboards(ctx context.Context, query string, starred bool, tags ...string) ([]FoundBoard, error) {
|
|
|
|
params := []SearchParam{
|
|
|
|
SearchType(SearchTypeDashboard),
|
|
|
|
SearchQuery(query),
|
|
|
|
SearchStarred(starred),
|
|
|
|
}
|
|
|
|
for _, tag := range tags {
|
|
|
|
params = append(params, SearchTag(tag))
|
|
|
|
}
|
|
|
|
return c.Search(ctx, params...)
|
|
|
|
}
|
2024-02-16 21:56:27 +01:00
|
|
|
|
|
|
|
func ConvertRawToIndent(raw []byte) (string, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
err := json.Indent(&buf, raw, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error pritty-printing raw json string: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|