mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Dashboard snapshot: added delete key which can be used to delete snapshots, #1623
This commit is contained in:
parent
7d0ae23c0e
commit
4322f29f34
@ -44,7 +44,9 @@ func Register(r *macaron.Macaron) {
|
||||
// dashboard snapshots
|
||||
r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot)
|
||||
r.Get("/dashboard/snapshots/*", Index)
|
||||
|
||||
r.Get("/api/snapshots/:key", GetDashboardSnapshot)
|
||||
r.Get("/api/snapshots-delete/:key", DeleteDashboardSnapshot)
|
||||
|
||||
// authed api
|
||||
r.Group("/api", func() {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
@ -15,12 +14,15 @@ import (
|
||||
|
||||
func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapshotCommand) {
|
||||
cmd.Key = util.GetRandomString(32)
|
||||
cmd.DeleteKey = util.GetRandomString(32)
|
||||
|
||||
if cmd.External {
|
||||
cmd.OrgId = -1
|
||||
cmd.UserId = -1
|
||||
metrics.M_Api_Dashboard_Snapshot_External.Inc(1)
|
||||
} else {
|
||||
cmd.OrgId = c.OrgId
|
||||
cmd.UserId = c.UserId
|
||||
metrics.M_Api_Dashboard_Snapshot_Create.Inc(1)
|
||||
}
|
||||
|
||||
@ -29,7 +31,12 @@ func CreateDashboardSnapshot(c *middleware.Context, cmd m.CreateDashboardSnapsho
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, util.DynMap{"key": cmd.Key, "url": setting.ToAbsUrl("dashboard/snapshot/" + cmd.Key)})
|
||||
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) {
|
||||
@ -58,9 +65,18 @@ func GetDashboardSnapshot(c *middleware.Context) {
|
||||
|
||||
metrics.M_Api_Dashboard_Snapshot_Get.Inc(1)
|
||||
|
||||
maxAge := int64(snapshot.Expires.Sub(time.Now()).Seconds())
|
||||
|
||||
c.Resp.Header().Set("Cache-Control", "public, max-age="+strconv.FormatInt(maxAge, 10))
|
||||
|
||||
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 is cleared from a CDN cache."})
|
||||
}
|
||||
|
@ -7,7 +7,11 @@ type DashboardSnapshot struct {
|
||||
Id int64
|
||||
Name string
|
||||
Key string
|
||||
DeleteKey string
|
||||
OrgId int64
|
||||
UserId int64
|
||||
External bool
|
||||
ExternalUrl string
|
||||
|
||||
Expires time.Time
|
||||
Created time.Time
|
||||
@ -22,14 +26,21 @@ type DashboardSnapshot struct {
|
||||
type CreateDashboardSnapshotCommand struct {
|
||||
Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
|
||||
External bool `json:"external"`
|
||||
ExternalUrl string `json:"externalUrl"`
|
||||
Expires int64 `json:"expires"`
|
||||
|
||||
OrgId int64 `json:"-"`
|
||||
UserId int64 `json:"-"`
|
||||
Key string `json:"-"`
|
||||
DeleteKey string `json:"-"`
|
||||
|
||||
Result *DashboardSnapshot
|
||||
}
|
||||
|
||||
type DeleteDashboardSnapshotCommand struct {
|
||||
DeleteKey string `json:"-"`
|
||||
}
|
||||
|
||||
type GetDashboardSnapshotQuery struct {
|
||||
Key string
|
||||
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
func init() {
|
||||
bus.AddHandler("sql", CreateDashboardSnapshot)
|
||||
bus.AddHandler("sql", GetDashboardSnapshot)
|
||||
bus.AddHandler("sql", DeleteDashboardSnapshot)
|
||||
}
|
||||
|
||||
func CreateDashboardSnapshot(cmd *m.CreateDashboardSnapshotCommand) error {
|
||||
@ -24,7 +25,11 @@ func CreateDashboardSnapshot(cmd *m.CreateDashboardSnapshotCommand) error {
|
||||
|
||||
snapshot := &m.DashboardSnapshot{
|
||||
Key: cmd.Key,
|
||||
DeleteKey: cmd.DeleteKey,
|
||||
OrgId: cmd.OrgId,
|
||||
UserId: cmd.UserId,
|
||||
External: cmd.External,
|
||||
ExternalUrl: cmd.ExternalUrl,
|
||||
Dashboard: cmd.Dashboard,
|
||||
Expires: expires,
|
||||
Created: time.Now(),
|
||||
@ -38,6 +43,14 @@ func CreateDashboardSnapshot(cmd *m.CreateDashboardSnapshotCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteDashboardSnapshot(cmd *m.DeleteDashboardSnapshotCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
var rawSql = "DELETE FROM dashboard_snapshot WHERE delete_key=?"
|
||||
_, err := sess.Exec(rawSql, cmd.DeleteKey)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func GetDashboardSnapshot(query *m.GetDashboardSnapshotQuery) error {
|
||||
snapshot := m.DashboardSnapshot{Key: query.Key}
|
||||
has, err := x.Get(&snapshot)
|
||||
|
@ -19,12 +19,36 @@ func addDashboardSnapshotMigrations(mg *Migrator) {
|
||||
},
|
||||
}
|
||||
|
||||
// add v4
|
||||
mg.AddMigration("create dashboard_snapshot table v4", NewAddTableMigration(snapshotV4))
|
||||
addTableIndicesMigrations(mg, "v4", snapshotV4)
|
||||
|
||||
mg.AddMigration("add org_id to dashboard_snapshot", new(AddColumnMigration).
|
||||
Table("dashboard_snapshot").Column(&Column{Name: "org_id", Type: DB_BigInt, Nullable: true}))
|
||||
// drop v4
|
||||
addDropAllIndicesMigrations(mg, "v4", snapshotV4)
|
||||
mg.AddMigration("drop table dashboard_snapshot_v4 #1", NewDropTableMigration("dashboard_snapshot"))
|
||||
|
||||
mg.AddMigration("add index org_id to dashboard_snapshot",
|
||||
NewAddIndexMigration(snapshotV4, &Index{Cols: []string{"org_id"}}))
|
||||
snapshotV5 := Table{
|
||||
Name: "dashboard_snapshot",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "key", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "delete_key", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "user_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "external", Type: DB_Bool, Nullable: false},
|
||||
{Name: "external_url", Type: DB_NVarchar, Length: 255, Nullable: false},
|
||||
{Name: "dashboard", Type: DB_Text, Nullable: false},
|
||||
{Name: "expires", Type: DB_DateTime, Nullable: false},
|
||||
{Name: "created", Type: DB_DateTime, Nullable: false},
|
||||
{Name: "updated", Type: DB_DateTime, Nullable: false},
|
||||
},
|
||||
Indices: []*Index{
|
||||
{Cols: []string{"key"}, Type: UniqueIndex},
|
||||
{Cols: []string{"delete_key"}, Type: UniqueIndex},
|
||||
{Cols: []string{"user_id"}},
|
||||
},
|
||||
}
|
||||
|
||||
mg.AddMigration("create dashboard_snapshot table v5 #2", NewAddTableMigration(snapshotV5))
|
||||
addTableIndicesMigrations(mg, "v5", snapshotV5)
|
||||
}
|
||||
|
@ -110,6 +110,9 @@
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="pull-right" ng-if="step === 2" style="padding: 5px">
|
||||
Did you make a mistake? <a href="{{deleteUrl}}" target="_blank">delete snapshot.</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -84,10 +84,12 @@ function (angular, _) {
|
||||
$scope.loading = false;
|
||||
|
||||
if (external) {
|
||||
$scope.deleteUrl = results.deleteUrl;
|
||||
$scope.snapshotUrl = results.url;
|
||||
} else {
|
||||
var baseUrl = $location.absUrl().replace($location.url(), "");
|
||||
$scope.snapshotUrl = baseUrl + '/dashboard/snapshot/' + results.key;
|
||||
$scope.deleteUrl = baseUrl + '/api/snapshots-delete/' + results.deleteKey;
|
||||
}
|
||||
|
||||
$scope.step = 2;
|
||||
|
Loading…
Reference in New Issue
Block a user