File tree 5 files changed +170
-0
lines changed
5 files changed +170
-0
lines changed Original file line number Diff line number Diff line change
1
+ package grafana
2
+
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "net/http"
7
+ )
8
+
9
+ type Grafana struct {
10
+ url string
11
+ key string
12
+ }
13
+
14
+ func New (url , key string ) * Grafana {
15
+ return & Grafana {url : url , key : key }
16
+ }
17
+
18
+ func (cli * Grafana ) request (method , path string , result interface {}) error {
19
+ req , err := http .NewRequest (method , cli .url + path , nil )
20
+ if err != nil {
21
+ return err
22
+ }
23
+ req .Header .Set ("Authorization" , "Bearer " + cli .key )
24
+
25
+ resp , err := http .DefaultClient .Do (req )
26
+ if err != nil {
27
+ return err
28
+ }
29
+ defer resp .Body .Close ()
30
+
31
+ if resp .StatusCode != http .StatusOK {
32
+ return fmt .Errorf ("%s" , resp .Status )
33
+ }
34
+
35
+ err = json .NewDecoder (resp .Body ).Decode (result )
36
+ if err != nil {
37
+ return err
38
+ }
39
+
40
+ return nil
41
+ }
Original file line number Diff line number Diff line change
1
+ package grafana
2
+
3
+ type DashboardMeta struct {
4
+ Type string
5
+ CanSave bool
6
+ CanEdit bool
7
+ CanAdmin bool
8
+ CanStar bool
9
+ Slug string
10
+ Url string
11
+ Expires string
12
+ Created string
13
+ Updated string
14
+ UpdatedBy string
15
+ CreatedBy string
16
+ Version int
17
+ HasAcl bool
18
+ IsFolder bool
19
+ FolderId int
20
+ FolderUid string
21
+ FolderTitle string
22
+ FolderUrl string
23
+ Provisioned bool
24
+ ProvisionedExternalId string
25
+ }
26
+
27
+ type PanelTarget struct {
28
+ Exemplar bool
29
+ Expr string
30
+ Format string
31
+ Interval string
32
+ IntervalFactor int
33
+ LegendFormat string
34
+ RefId string
35
+ }
36
+
37
+ type Panel struct {
38
+ Id int
39
+ DataSource string
40
+ Type string
41
+ Title string
42
+ TimeFrom interface {}
43
+ TimeShift interface {}
44
+ PluginVersion string
45
+ GridPos struct { H , W , X , Y int }
46
+ Targets []PanelTarget
47
+ }
48
+
49
+ type Dashboard struct {
50
+ Id int
51
+ Uid string
52
+ Title string
53
+ Panels []Panel
54
+ }
55
+
56
+ func (cli * Grafana ) Dashboard (uid string ) (DashboardMeta , Dashboard , error ) {
57
+ var result struct {
58
+ Meta DashboardMeta
59
+ Dashboard Dashboard
60
+ }
61
+ err := cli .request ("GET" , "/api/dashboards/uid/" + uid , & result )
62
+ if err != nil {
63
+ return DashboardMeta {}, Dashboard {}, err
64
+ }
65
+
66
+ return result .Meta , result .Dashboard , nil
67
+ }
Original file line number Diff line number Diff line change
1
+ package grafana
2
+
3
+ type Folder struct {
4
+ Id int
5
+ Uid string
6
+ Title string
7
+ }
8
+
9
+ func (cli * Grafana ) Folders () ([]Folder , error ) {
10
+ var folders []Folder
11
+ err := cli .request ("GET" , "/api/folders" , & folders )
12
+ if err != nil {
13
+ return nil , err
14
+ }
15
+ return folders , nil
16
+ }
Original file line number Diff line number Diff line change
1
+ module github.com/athurg/grafana
2
+
3
+ go 1.16
Original file line number Diff line number Diff line change
1
+ package grafana
2
+
3
+ import (
4
+ "net/url"
5
+ )
6
+
7
+ type SearchItem struct {
8
+ Id int
9
+ Uid string
10
+ Title string
11
+ Type string
12
+ IsStarred bool
13
+ SortMeta int
14
+ Slug string
15
+ Uri string
16
+ Url string
17
+ Tags []string
18
+ }
19
+
20
+ func (cli * Grafana ) SearchAll () ([]SearchItem , error ) {
21
+ return cli .search ("" )
22
+ }
23
+
24
+ func (cli * Grafana ) SearchFolders () ([]SearchItem , error ) {
25
+ return cli .search ("dash-folder" )
26
+ }
27
+
28
+ func (cli * Grafana ) SearchDashboards () ([]SearchItem , error ) {
29
+ return cli .search ("dash-db" )
30
+ }
31
+
32
+ func (cli * Grafana ) search (searchType string ) ([]SearchItem , error ) {
33
+ var items []SearchItem
34
+ q := url.Values {
35
+ "type" : {searchType },
36
+ }
37
+
38
+ err := cli .request ("GET" , "/api/search?" + q .Encode (), & items )
39
+ if err != nil {
40
+ return nil , err
41
+ }
42
+ return items , nil
43
+ }
You can’t perform that action at this time.
0 commit comments