Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit ef551f5

Browse files
Merge pull request #49 from grafana/support-reports
Add support for the reporting API
2 parents e9b64e2 + 61424ea commit ef551f5

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed

report.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"time"
8+
)
9+
10+
// ReportSchedule represents the schedule from a Grafana report.
11+
type ReportSchedule struct {
12+
StartDate *time.Time `json:"startDate,omitempty"`
13+
EndDate *time.Time `json:"endDate,omitempty"`
14+
Frequency string `json:"frequency"`
15+
IntervalFrequency string `json:"intervalFrequency"`
16+
IntervalAmount int64 `json:"intervalAmount"`
17+
WorkdaysOnly bool `json:"workdaysOnly"`
18+
TimeZone string `json:"timeZone"`
19+
}
20+
21+
// ReportTimeRange represents the time range from a Grafana report.
22+
type ReportTimeRange struct {
23+
From string `json:"from"`
24+
To string `json:"to"`
25+
}
26+
27+
// ReportOptions represents the options for a Grafana report.
28+
type ReportOptions struct {
29+
Orientation string `json:"orientation"`
30+
Layout string `json:"layout"`
31+
TimeRange ReportTimeRange `json:"timeRange"`
32+
}
33+
34+
// Report represents a Grafana report.
35+
type Report struct {
36+
// ReadOnly
37+
ID int64 `json:"id,omitempty"`
38+
UserID int64 `json:"userId,omitempty"`
39+
OrgID int64 `json:"orgId,omitempty"`
40+
State string `json:"state,omitempty"`
41+
42+
DashboardID int64 `json:"dashboardId"`
43+
DashboardUID string `json:"dashboardUid"`
44+
Name string `json:"name"`
45+
Recipients string `json:"recipients"`
46+
ReplyTo string `json:"replyTo"`
47+
Message string `json:"message"`
48+
Schedule ReportSchedule `json:"schedule"`
49+
Options ReportOptions `json:"options"`
50+
EnableDashboardURL bool `json:"enableDashboardUrl"`
51+
EnableCSV bool `json:"enableCsv"`
52+
}
53+
54+
// Report fetches and returns a Grafana report.
55+
func (c *Client) Report(id int64) (*Report, error) {
56+
path := fmt.Sprintf("/api/reports/%d", id)
57+
report := &Report{}
58+
err := c.request("GET", path, nil, nil, report)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
return report, nil
64+
}
65+
66+
// NewReport creates a new Grafana report.
67+
func (c *Client) NewReport(report Report) (int64, error) {
68+
data, err := json.Marshal(report)
69+
if err != nil {
70+
return 0, err
71+
}
72+
73+
result := struct {
74+
ID int64
75+
}{}
76+
77+
err = c.request("POST", "/api/reports", nil, bytes.NewBuffer(data), &result)
78+
if err != nil {
79+
return 0, err
80+
}
81+
82+
return result.ID, nil
83+
}
84+
85+
// UpdateReport updates a Grafana report.
86+
func (c *Client) UpdateReport(report Report) error {
87+
path := fmt.Sprintf("/api/reports/%d", report.ID)
88+
data, err := json.Marshal(report)
89+
if err != nil {
90+
return err
91+
}
92+
93+
return c.request("PUT", path, nil, bytes.NewBuffer(data), nil)
94+
}
95+
96+
// DeleteReport deletes the Grafana report whose ID it's passed.
97+
func (c *Client) DeleteReport(id int64) error {
98+
path := fmt.Sprintf("/api/reports/%d", id)
99+
100+
return c.request("DELETE", path, nil, nil, nil)
101+
}

report_test.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/gobs/pretty"
8+
)
9+
10+
var (
11+
getReportJSON = `
12+
{
13+
"id": 4,
14+
"userId": 0,
15+
"orgId": 1,
16+
"dashboardId": 33,
17+
"dashboardName": "Terraform Acceptance Test",
18+
"dashboardUid": "",
19+
"name": "My Report",
20+
"recipients": "test@test.com",
21+
"replyTo": "",
22+
"message": "",
23+
"schedule": {
24+
"startDate": "2020-01-01T00:00:00Z",
25+
"endDate": null,
26+
"frequency": "custom",
27+
"intervalFrequency": "weeks",
28+
"intervalAmount": 2,
29+
"workdaysOnly": true,
30+
"dayOfMonth": "1",
31+
"day": "wednesday",
32+
"hour": 0,
33+
"minute": 0,
34+
"timeZone": "GMT"
35+
},
36+
"options": {
37+
"orientation": "landscape",
38+
"layout": "grid",
39+
"timeRange": {
40+
"from": "now-1h",
41+
"to": "now"
42+
}
43+
},
44+
"templateVars": {},
45+
"enableDashboardUrl": true,
46+
"enableCsv": true,
47+
"state": "",
48+
"created": "2022-01-11T15:09:13Z",
49+
"updated": "2022-01-11T16:18:34Z"
50+
}
51+
`
52+
createReportJSON = `
53+
{
54+
"id": 4
55+
}
56+
`
57+
now = time.Now()
58+
testReport = Report{
59+
DashboardID: 33,
60+
Name: "My Report",
61+
Recipients: "test@test.com",
62+
Schedule: ReportSchedule{
63+
StartDate: &now,
64+
EndDate: nil,
65+
Frequency: "custom",
66+
IntervalFrequency: "weeks",
67+
IntervalAmount: 2,
68+
WorkdaysOnly: true,
69+
TimeZone: "GMT",
70+
},
71+
Options: ReportOptions{
72+
Orientation: "landscape",
73+
Layout: "grid",
74+
TimeRange: ReportTimeRange{
75+
From: "now-1h",
76+
To: "now",
77+
},
78+
},
79+
EnableDashboardURL: true,
80+
EnableCSV: true,
81+
}
82+
)
83+
84+
func TestReport(t *testing.T) {
85+
server, client := gapiTestTools(t, 200, getReportJSON)
86+
defer server.Close()
87+
88+
report := int64(4)
89+
resp, err := client.Report(report)
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
94+
t.Log(pretty.PrettyFormat(resp))
95+
96+
if resp.ID != report || resp.Name != "My Report" {
97+
t.Error("Not correctly parsing returned report.")
98+
}
99+
}
100+
101+
func TestNewReport(t *testing.T) {
102+
server, client := gapiTestTools(t, 200, createReportJSON)
103+
defer server.Close()
104+
105+
resp, err := client.NewReport(testReport)
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
110+
t.Log(pretty.PrettyFormat(resp))
111+
112+
if resp != 4 {
113+
t.Error("Not correctly parsing returned creation message.")
114+
}
115+
}
116+
117+
func TestUpdateReport(t *testing.T) {
118+
server, client := gapiTestTools(t, 200, "")
119+
defer server.Close()
120+
121+
err := client.UpdateReport(testReport)
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
}
126+
127+
func TestDeleteReport(t *testing.T) {
128+
server, client := gapiTestTools(t, 200, "")
129+
defer server.Close()
130+
131+
err := client.DeleteReport(4)
132+
if err != nil {
133+
t.Fatal(err)
134+
}
135+
}

0 commit comments

Comments
 (0)