diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index 6490a118861..79485fa8cca 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -48,6 +48,20 @@ func GetDashboard(c *middleware.Context) { } dash := query.Result + + // Finding the last updater of the dashboard + updater := "Anonymous" + if dash.UpdatedBy != 0 { + userQuery := m.GetUserByIdQuery{Id: dash.UpdatedBy} + userErr := bus.Dispatch(&userQuery) + if userErr != nil { + updater = "Unknown" + } else { + user := userQuery.Result + updater = user.Login + } + } + dto := dtos.DashboardFullWithMeta{ Dashboard: dash.Data, Meta: dtos.DashboardMeta{ @@ -59,6 +73,7 @@ func GetDashboard(c *middleware.Context) { CanEdit: canEditDashboard(c.OrgRole), Created: dash.Created, Updated: dash.Updated, + UpdatedBy: updater, }, } @@ -88,6 +103,12 @@ func DeleteDashboard(c *middleware.Context) { func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) { cmd.OrgId = c.OrgId + if !c.IsSignedIn { + cmd.UpdatedBy = 0 + } else { + cmd.UpdatedBy = c.UserId + } + dash := cmd.GetDashboardModel() if dash.Id == 0 { limitReached, err := middleware.QuotaReached(c, "dashboard") diff --git a/pkg/api/dtos/models.go b/pkg/api/dtos/models.go index 50db279b25b..a7ff9353fa9 100644 --- a/pkg/api/dtos/models.go +++ b/pkg/api/dtos/models.go @@ -41,6 +41,7 @@ type DashboardMeta struct { Expires time.Time `json:"expires"` Created time.Time `json:"created"` Updated time.Time `json:"updated"` + UpdatedBy string `json:"updatedBy"` } type DashboardFullWithMeta struct { diff --git a/pkg/models/dashboards.go b/pkg/models/dashboards.go index 5b926c3e314..ddf5dd244f5 100644 --- a/pkg/models/dashboards.go +++ b/pkg/models/dashboards.go @@ -33,6 +33,8 @@ type Dashboard struct { Created time.Time Updated time.Time + UpdatedBy int64 + Title string Data map[string]interface{} } @@ -90,6 +92,7 @@ func NewDashboardFromJson(data map[string]interface{}) *Dashboard { func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard { dash := NewDashboardFromJson(cmd.Dashboard) dash.OrgId = cmd.OrgId + dash.UpdatedBy = cmd.UpdatedBy dash.UpdateSlug() return dash } @@ -113,6 +116,7 @@ type SaveDashboardCommand struct { Dashboard map[string]interface{} `json:"dashboard" binding:"Required"` Overwrite bool `json:"overwrite"` OrgId int64 `json:"-"` + UpdatedBy int64 `json:"-"` Result *Dashboard } diff --git a/pkg/services/sqlstore/migrations/dashboard_mig.go b/pkg/services/sqlstore/migrations/dashboard_mig.go index 5d440d85ebc..b94f1a7d6ac 100644 --- a/pkg/services/sqlstore/migrations/dashboard_mig.go +++ b/pkg/services/sqlstore/migrations/dashboard_mig.go @@ -92,4 +92,9 @@ func addDashboardMigration(mg *Migrator) { Sqlite("SELECT 0 WHERE 0;"). Postgres("SELECT 0;"). Mysql("ALTER TABLE dashboard MODIFY data MEDIUMTEXT;")) + + // add column to store updater of a dashboard + mg.AddMigration("Add column updated_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{ + Name: "updated_by", Type: DB_Int, Nullable: true, + })) } diff --git a/public/app/features/dashboard/partials/settings.html b/public/app/features/dashboard/partials/settings.html index 9b0e5674fe0..d9a71655667 100644 --- a/public/app/features/dashboard/partials/settings.html +++ b/public/app/features/dashboard/partials/settings.html @@ -140,6 +140,17 @@
+
+ +
+