Add git functions & improve grafana functions
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Tom Neuber 2024-02-16 21:56:27 +01:00
parent 2d08f75545
commit 0fb6e0b6e9
Signed by: tom
GPG key ID: F17EFE4272D89FF6
7 changed files with 579 additions and 16 deletions

View file

@ -1,6 +1,7 @@
package grafana
import (
"bytes"
"context"
"encoding/json"
"fmt"
@ -8,9 +9,30 @@ import (
"time"
)
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"`
}
type DashboardVersion struct {
ID uint `json:"id"`
DashboardID uint `json:"dashboardId"`
DashboardUID string `json:"uid"`
ParentVersion uint `json:"parentVersion"`
RestoredFrom uint `json:"restoredFrom"`
Version uint `json:"version"`
@ -19,6 +41,42 @@ type DashboardVersion struct {
Message string `json:"message"`
}
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)
}
var result struct {
Meta BoardProperties `json:"meta"`
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
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
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)
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
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
}
func queryParams(params ...QueryParam) url.Values {
u := url.URL{}
q := u.Query()
@ -28,14 +86,14 @@ func queryParams(params ...QueryParam) url.Values {
return q
}
func (c *Client) GetDashboardVersionsByDashboardID(ctx context.Context, dashboardID uint, params ...QueryParam) ([]DashboardVersion, error) {
func (c *Client) GetDashboardVersionsByDashboardUID(ctx context.Context, uid string, params ...QueryParam) ([]DashboardVersion, error) {
var (
raw []byte
code int
err error
)
if raw, code, err = c.get(ctx, fmt.Sprintf("api/dashboards/id/%d/versions", dashboardID), queryParams(params...)); err != nil {
if raw, code, err = c.get(ctx, fmt.Sprintf("api/dashboards/uid/%s/versions", uid), queryParams(params...)); err != nil {
return nil, err
}
if code != 200 {
@ -47,6 +105,14 @@ func (c *Client) GetDashboardVersionsByDashboardID(ctx context.Context, dashboar
return versions, err
}
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))
}
func (c *Client) SearchDashboards(ctx context.Context, query string, starred bool, tags ...string) ([]FoundBoard, error) {
params := []SearchParam{
SearchType(SearchTypeDashboard),
@ -58,3 +124,14 @@ func (c *Client) SearchDashboards(ctx context.Context, query string, starred boo
}
return c.Search(ctx, params...)
}
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
}

View file

@ -1,7 +1,6 @@
package grafana
import (
"bytes"
"context"
"fmt"
"io"
@ -74,7 +73,3 @@ func (c *Client) doRequest(ctx context.Context, method, query string, params url
func (c *Client) get(ctx context.Context, query string, params url.Values) ([]byte, int, error) {
return c.doRequest(ctx, "GET", query, params, nil)
}
func (c *Client) post(ctx context.Context, query string, params url.Values, body []byte) ([]byte, int, error) {
return c.doRequest(ctx, "POST", query, params, bytes.NewBuffer(body))
}