|
| 1 | +package framework |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/go-resty/resty/v2" |
| 9 | +) |
| 10 | + |
| 11 | +type Client struct { |
| 12 | + resty *resty.Client |
| 13 | +} |
| 14 | + |
| 15 | +// NewGrafanaClient initializes a new Grafana client with the specified URL and API key. |
| 16 | +func NewGrafanaClient(url, apiKey string) *Client { |
| 17 | + return &Client{ |
| 18 | + resty: resty.New(). |
| 19 | + SetBaseURL(url). |
| 20 | + SetHeader("Authorization", "Bearer "+apiKey), |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +type Annotation struct { |
| 25 | + ID int64 `json:"id"` |
| 26 | + AlertID int64 `json:"alertId"` |
| 27 | + DashboardID int64 `json:"dashboardId"` |
| 28 | + DashboardUID string `json:"dashboardUID"` |
| 29 | + PanelID int64 `json:"panelId"` |
| 30 | + PrevState string `json:"prevState"` |
| 31 | + NewState string `json:"newState"` |
| 32 | + Text string `json:"text"` |
| 33 | + Time time.Time `json:"time"` |
| 34 | + TimeEnd time.Time `json:"timeEnd"` |
| 35 | + Created time.Time `json:"created"` |
| 36 | + Updated time.Time `json:"updated"` |
| 37 | + Tags []interface{} `json:"tags"` |
| 38 | + Data interface{} `json:"data"` |
| 39 | +} |
| 40 | + |
| 41 | +type AnnotationsQueryParams struct { |
| 42 | + Limit *int |
| 43 | + AlertID *int |
| 44 | + DashboardID *int |
| 45 | + DashboardUID *string |
| 46 | + Type *string |
| 47 | + From *time.Time |
| 48 | + To *time.Time |
| 49 | +} |
| 50 | + |
| 51 | +type PostAnnotation struct { |
| 52 | + DashboardID *int |
| 53 | + PanelID *int |
| 54 | + DashboardUID string |
| 55 | + Time *time.Time |
| 56 | + TimeEnd *time.Time |
| 57 | + Tags []string |
| 58 | + Text string |
| 59 | +} |
| 60 | + |
| 61 | +type PostAnnotationResponse struct { |
| 62 | + Message string `json:"message"` |
| 63 | + ID int64 `json:"id"` |
| 64 | +} |
| 65 | + |
| 66 | +// GetAnnotations retrieves a list of annotations based on specified query parameters. |
| 67 | +func (c *Client) GetAnnotations(params AnnotationsQueryParams) ([]Annotation, *resty.Response, error) { |
| 68 | + query := make(url.Values) |
| 69 | + if params.Limit != nil { |
| 70 | + query.Set("limit", fmt.Sprintf("%d", *params.Limit)) |
| 71 | + } |
| 72 | + if params.AlertID != nil { |
| 73 | + query.Set("alertId", fmt.Sprintf("%d", *params.AlertID)) |
| 74 | + } |
| 75 | + if params.DashboardID != nil { |
| 76 | + query.Set("dashboardId", fmt.Sprintf("%d", *params.DashboardID)) |
| 77 | + } |
| 78 | + if params.DashboardUID != nil { |
| 79 | + query.Set("dashboardUID", *params.DashboardUID) |
| 80 | + } |
| 81 | + if params.Type != nil { |
| 82 | + query.Set("type", *params.Type) |
| 83 | + } |
| 84 | + |
| 85 | + if (params.From != nil && params.To == nil) || (params.To != nil && params.From == nil) { |
| 86 | + return nil, nil, fmt.Errorf("both From and To must be set") |
| 87 | + } |
| 88 | + |
| 89 | + if params.From != nil { |
| 90 | + query.Set("from", fmt.Sprintf("%d", params.From.UnixMilli())) |
| 91 | + } |
| 92 | + if params.To != nil { |
| 93 | + query.Set("to", fmt.Sprintf("%d", params.To.UnixMilli())) |
| 94 | + } |
| 95 | + |
| 96 | + var result []Annotation |
| 97 | + r, err := c.resty.R(). |
| 98 | + SetResult(&result). |
| 99 | + SetQueryString(query.Encode()). |
| 100 | + Get("/api/annotations") |
| 101 | + return result, r, err |
| 102 | +} |
| 103 | + |
| 104 | +// PostAnnotation sends a new annotation to a specified dashboard. |
| 105 | +func (c *Client) PostAnnotation(annotation PostAnnotation) (PostAnnotationResponse, *resty.Response, error) { |
| 106 | + a := map[string]interface{}{ |
| 107 | + "dashboardUID": annotation.DashboardUID, |
| 108 | + "tags": annotation.Tags, |
| 109 | + "text": annotation.Text, |
| 110 | + } |
| 111 | + if annotation.DashboardID != nil { |
| 112 | + a["dashboardId"] = *annotation.DashboardID |
| 113 | + } |
| 114 | + if annotation.PanelID != nil { |
| 115 | + a["panelId"] = *annotation.PanelID |
| 116 | + } |
| 117 | + if annotation.Time != nil { |
| 118 | + a["time"] = annotation.Time.UnixMilli() |
| 119 | + } |
| 120 | + if annotation.TimeEnd != nil { |
| 121 | + a["timeEnd"] = annotation.TimeEnd.UnixMilli() |
| 122 | + } |
| 123 | + var result PostAnnotationResponse |
| 124 | + r, err := c.resty.R(). |
| 125 | + SetBody(a). |
| 126 | + SetResult(&result). |
| 127 | + Post("/api/annotations") |
| 128 | + return result, r, err |
| 129 | +} |
0 commit comments