123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package utils
- import (
- "encoding/json"
- "fmt"
- )
- const (
- APIDASHBOARD = "%s/api/dashboards/db" //创建grafana-dasbboard
- )
- type GrafanaViewer struct {
- Url string `json:"url"`
- Method string `json:"method"`
- Token string `json:"token"`
- }
- type GrDashBoard struct {
- ID int `json:"id"`
- UID string `json:"uid"`
- Title string `json:"title"`
- Tags []string `json:"tags"`
- TimeZone string `json:"timezone"`
- SchemaVersion int `json:"schemaVersion"`
- Refresh string `json:"refresh"`
- //
- // other response
- Slug string `json:"slug"`
- Status string `json:"status"`
- Url string `json:"url"`
- Version int `json:"version"`
- }
- type GrDashBoardReq struct {
- Dashboard *GrDashBoard `json:"dashboard"`
- FolderUid string `json:"folderUid"`
- Message string `json:"message"`
- Overwrite bool `json:"overwrote"`
- }
- func (g *GrafanaViewer) CreateDashBoard(gdb *GrDashBoardReq, res *GrDashBoard) error {
- params, _ := json.Marshal(gdb)
- header := map[string]string{"Authorization": fmt.Sprintf("Bearer %s", g.Token)}
- resp, err := Post(fmt.Sprintf(APIDASHBOARD, g.Url), params, header)
- if err != nil {
- fmt.Sprintln("post error: ", err.Error())
- return err
- }
- if err := json.Unmarshal(resp, res); err != nil {
- fmt.Sprintln("unmarshal error: ", err.Error())
- return err
- }
- return nil
- }
|