11package framework
22
33import (
4- "fmt"
5- "net/url"
64 "time"
75
86 "github.com/go-resty/resty/v2"
@@ -13,47 +11,19 @@ type Client struct {
1311}
1412
1513// NewGrafanaClient initializes a new Grafana client with the specified URL and API key.
16- func NewGrafanaClient (url , apiKey string ) * Client {
14+ func NewGrafanaClient (url , bearerToken string ) * Client {
1715 return & Client {
1816 resty : resty .New ().
1917 SetBaseURL (url ).
20- SetHeader ("Authorization" , "Bearer " + apiKey ),
18+ SetHeader ("Authorization" , "Bearer " + bearerToken ),
2119 }
2220}
2321
2422type 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
5323 PanelID * int
54- DashboardUID string
55- Time * time.Time
56- TimeEnd * time.Time
24+ DashboardUID [] string
25+ StartTime * time.Time
26+ EndTime * time.Time
5727 Tags []string
5828 Text string
5929}
@@ -63,67 +33,39 @@ type PostAnnotationResponse struct {
6333 ID int64 `json:"id"`
6434}
6535
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- }
36+ // Annotate adds annotation to all the dashboards, works for both single point annotation with just StartTime and for ranges with StartTime/EndTime
37+ func (c * Client ) Annotate (annotation Annotation ) ([]PostAnnotationResponse , []* resty.Response , error ) {
38+ var results []PostAnnotationResponse
39+ var responses []* resty.Response
8440
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- }
41+ for _ , uid := range annotation .DashboardUID {
42+ a := map [string ]interface {}{
43+ "dashboardUID" : uid ,
44+ "tags" : annotation .Tags ,
45+ "text" : annotation .Text ,
46+ }
47+ if annotation .PanelID != nil {
48+ a ["panelId" ] = * annotation .PanelID
49+ }
50+ if annotation .StartTime != nil {
51+ a ["time" ] = annotation .StartTime .UnixMilli ()
52+ }
53+ if annotation .EndTime != nil {
54+ a ["timeEnd" ] = annotation .EndTime .UnixMilli ()
55+ }
8856
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- }
57+ var result PostAnnotationResponse
58+ r , err := c .resty .R ().
59+ SetBody (a ).
60+ SetResult (& result ).
61+ Post ("/api/annotations" )
62+ if err != nil {
63+ return nil , nil , err // Return early if any request fails
64+ }
9565
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
66+ results = append (results , result )
67+ responses = append (responses , r )
11368 }
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
69+
70+ return results , responses , nil
12971}
0 commit comments