2015-03-21 07:53:16 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-03-25 03:04:38 -05:00
|
|
|
"time"
|
|
|
|
|
2015-03-21 09:56:26 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2015-03-21 07:53:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2015-03-24 10:49:12 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2015-03-21 07:53:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-03-25 03:04:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-03-21 07:53:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
2015-03-21 09:56:26 -05:00
|
|
|
func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapshotCommand) {
|
2015-03-25 03:04:38 -05:00
|
|
|
if cmd.External {
|
2015-03-26 14:59:41 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-03-26 06:00:52 -05:00
|
|
|
cmd.OrgId = -1
|
2015-03-26 14:34:58 -05:00
|
|
|
cmd.UserId = -1
|
2015-03-26 06:00:52 -05:00
|
|
|
metrics.M_Api_Dashboard_Snapshot_External.Inc(1)
|
|
|
|
} else {
|
2015-03-26 14:59:41 -05:00
|
|
|
cmd.Key = util.GetRandomString(32)
|
|
|
|
cmd.DeleteKey = util.GetRandomString(32)
|
2015-03-26 06:00:52 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
2015-03-26 14:34:58 -05:00
|
|
|
cmd.UserId = c.UserId
|
2015-03-26 06:00:52 -05:00
|
|
|
metrics.M_Api_Dashboard_Snapshot_Create.Inc(1)
|
2015-03-25 03:04:38 -05:00
|
|
|
}
|
2015-03-21 07:53:16 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
|
c.JsonApiErr(500, "Failed to create snaphost", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-26 14:34:58 -05:00
|
|
|
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),
|
|
|
|
})
|
2015-03-25 03:04:38 -05:00
|
|
|
}
|
|
|
|
|
2015-03-21 07:53:16 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-26 06:00:52 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-21 09:56:26 -05:00
|
|
|
dto := dtos.Dashboard{
|
2015-03-26 06:00:52 -05:00
|
|
|
Model: snapshot.Dashboard,
|
2015-03-21 09:56:26 -05:00
|
|
|
Meta: dtos.DashboardMeta{IsSnapshot: true},
|
|
|
|
}
|
|
|
|
|
2015-03-24 10:49:12 -05:00
|
|
|
metrics.M_Api_Dashboard_Snapshot_Get.Inc(1)
|
|
|
|
|
2015-03-26 14:34:58 -05:00
|
|
|
c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
|
|
|
|
c.JSON(200, dto)
|
|
|
|
}
|
2015-03-26 06:00:52 -05:00
|
|
|
|
2015-03-26 14:34:58 -05:00
|
|
|
func DeleteDashboardSnapshot(c *middleware.Context) {
|
|
|
|
key := c.Params(":key")
|
|
|
|
cmd := &m.DeleteDashboardSnapshotCommand{DeleteKey: key}
|
2015-03-24 10:49:12 -05:00
|
|
|
|
2015-03-26 14:34:58 -05:00
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
|
|
|
c.JsonApiErr(500, "Failed to delete dashboard snapshot", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-26 14:59:41 -05:00
|
|
|
c.JSON(200, util.DynMap{"message": "Snapshot deleted. It might take an hour before it's cleared from a CDN cache."})
|
2015-03-21 07:53:16 -05:00
|
|
|
}
|