|
|
@ -1,17 +1,27 @@ |
|
|
|
package api |
|
|
|
package api |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"bytes" |
|
|
|
|
|
|
|
"encoding/json" |
|
|
|
|
|
|
|
"io/ioutil" |
|
|
|
|
|
|
|
"net/http" |
|
|
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos" |
|
|
|
"github.com/grafana/grafana/pkg/api/dtos" |
|
|
|
"github.com/grafana/grafana/pkg/bus" |
|
|
|
"github.com/grafana/grafana/pkg/bus" |
|
|
|
"github.com/grafana/grafana/pkg/metrics" |
|
|
|
"github.com/grafana/grafana/pkg/metrics" |
|
|
|
"github.com/grafana/grafana/pkg/middleware" |
|
|
|
"github.com/grafana/grafana/pkg/middleware" |
|
|
|
m "github.com/grafana/grafana/pkg/models" |
|
|
|
m "github.com/grafana/grafana/pkg/models" |
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/setting" |
|
|
|
"github.com/grafana/grafana/pkg/util" |
|
|
|
"github.com/grafana/grafana/pkg/util" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapshotCommand) { |
|
|
|
func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapshotCommand) { |
|
|
|
cmd.Key = util.GetRandomString(32) |
|
|
|
if cmd.External { |
|
|
|
|
|
|
|
createExternalSnapshot(c, cmd) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cmd.Key = util.GetRandomString(32) |
|
|
|
if err := bus.Dispatch(&cmd); err != nil { |
|
|
|
if err := bus.Dispatch(&cmd); err != nil { |
|
|
|
c.JsonApiErr(500, "Failed to create snaphost", err) |
|
|
|
c.JsonApiErr(500, "Failed to create snaphost", err) |
|
|
|
return |
|
|
|
return |
|
|
@ -19,7 +29,30 @@ func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapsho |
|
|
|
|
|
|
|
|
|
|
|
metrics.M_Api_Dashboard_Snapshot_Create.Inc(1) |
|
|
|
metrics.M_Api_Dashboard_Snapshot_Create.Inc(1) |
|
|
|
|
|
|
|
|
|
|
|
c.JSON(200, util.DynMap{"key": cmd.Key}) |
|
|
|
c.JSON(200, util.DynMap{"key": cmd.Key, "url": setting.ToAbsUrl("/dashboard/snapshots")}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func createExternalSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapshotCommand) { |
|
|
|
|
|
|
|
metrics.M_Api_Dashboard_Snapshot_External.Inc(1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json, _ := json.Marshal(cmd) |
|
|
|
|
|
|
|
jsonData := bytes.NewBuffer(json) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client := http.Client{Timeout: time.Duration(5 * time.Second)} |
|
|
|
|
|
|
|
resp, err := client.Post("http://snapshots-origin.raintank.io/api/snapshots", "application/json", jsonData) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
c.JsonApiErr(500, "Failed to publish external snapshot", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.Header().Set("Content-Type", resp.Header.Get("Content-Type")) |
|
|
|
|
|
|
|
c.WriteHeader(resp.StatusCode) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if resp.ContentLength > 0 { |
|
|
|
|
|
|
|
bytes, _ := ioutil.ReadAll(resp.Body) |
|
|
|
|
|
|
|
c.Write(bytes) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func GetDashboardSnapshot(c *middleware.Context) { |
|
|
|
func GetDashboardSnapshot(c *middleware.Context) { |
|
|
|