mirror of
https://github.com/grafana/grafana.git
synced 2024-11-27 11:20:27 -06:00
88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package api
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
"github.com/grafana/grafana/pkg/metrics"
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
)
|
|
|
|
func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapshotCommand) {
|
|
if cmd.External {
|
|
// external snapshot ref requires key and delete key
|
|
if cmd.Key != "" && cmd.DeleteKey != "" {
|
|
c.JsonApiErr(400, "Missing key and delete key for external snapshot", nil)
|
|
return
|
|
}
|
|
|
|
cmd.OrgId = -1
|
|
cmd.UserId = -1
|
|
metrics.M_Api_Dashboard_Snapshot_External.Inc(1)
|
|
} else {
|
|
cmd.Key = util.GetRandomString(32)
|
|
cmd.DeleteKey = util.GetRandomString(32)
|
|
cmd.OrgId = c.OrgId
|
|
cmd.UserId = c.UserId
|
|
metrics.M_Api_Dashboard_Snapshot_Create.Inc(1)
|
|
}
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
c.JsonApiErr(500, "Failed to create snaphost", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(200, util.DynMap{
|
|
"key": cmd.Key,
|
|
"deleteKey": cmd.DeleteKey,
|
|
"url": setting.ToAbsUrl("dashboard/snapshot/" + cmd.Key),
|
|
"deleteUrl": setting.ToAbsUrl("api/snapshots-delete/" + cmd.DeleteKey),
|
|
})
|
|
}
|
|
|
|
func GetDashboardSnapshot(c *middleware.Context) {
|
|
key := c.Params(":key")
|
|
|
|
query := &m.GetDashboardSnapshotQuery{Key: key}
|
|
|
|
err := bus.Dispatch(query)
|
|
if err != nil {
|
|
c.JsonApiErr(500, "Failed to get dashboard snapshot", err)
|
|
return
|
|
}
|
|
|
|
snapshot := query.Result
|
|
|
|
// expired snapshots should also be removed from db
|
|
if snapshot.Expires.Before(time.Now()) {
|
|
c.JsonApiErr(404, "Snapshot not found", err)
|
|
return
|
|
}
|
|
|
|
dto := dtos.Dashboard{
|
|
Model: snapshot.Dashboard,
|
|
Meta: dtos.DashboardMeta{IsSnapshot: true},
|
|
}
|
|
|
|
metrics.M_Api_Dashboard_Snapshot_Get.Inc(1)
|
|
|
|
c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
|
|
c.JSON(200, dto)
|
|
}
|
|
|
|
func DeleteDashboardSnapshot(c *middleware.Context) {
|
|
key := c.Params(":key")
|
|
cmd := &m.DeleteDashboardSnapshotCommand{DeleteKey: key}
|
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
|
c.JsonApiErr(500, "Failed to delete dashboard snapshot", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(200, util.DynMap{"message": "Snapshot deleted. It might take an hour before it's cleared from a CDN cache."})
|
|
}
|