diff --git a/docs/sources/reference/http_api.md b/docs/sources/reference/http_api.md index 4b0dff6c4b6..70431565741 100644 --- a/docs/sources/reference/http_api.md +++ b/docs/sources/reference/http_api.md @@ -75,7 +75,7 @@ Creates a new dashboard or updates an existing dashboard. JSON Body schema: -- **dashboard** – The complete dashboard model, id = null to create a new dashboard +- **dashboard** – The complete dashboard model, id = null to create a new dashboard. - **overwrite** – Set to true if you want to overwrite existing dashboard with newer version or with same dashboard title. **Example Response**: diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index 79485fa8cca..c0c7cd0e4f5 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -49,17 +49,13 @@ 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 - } + // Finding creator and last updater of the dashboard + updater, creator := "Anonymous", "Anonymous" + if dash.UpdatedBy > 0 { + updater = getUserLogin(dash.UpdatedBy) + } + if dash.CreatedBy > 0 { + creator = getUserLogin(dash.CreatedBy) } dto := dtos.DashboardFullWithMeta{ @@ -74,12 +70,25 @@ func GetDashboard(c *middleware.Context) { Created: dash.Created, Updated: dash.Updated, UpdatedBy: updater, + CreatedBy: creator, + Version: dash.Version, }, } c.JSON(200, dto) } +func getUserLogin(userId int64) string { + query := m.GetUserByIdQuery{Id: userId} + err := bus.Dispatch(&query) + if err != nil { + return "Anonymous" + } else { + user := query.Result + return user.Login + } +} + func DeleteDashboard(c *middleware.Context) { slug := c.Params(":slug") @@ -104,9 +113,9 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) { cmd.OrgId = c.OrgId if !c.IsSignedIn { - cmd.UpdatedBy = 0 + cmd.UserId = -1 } else { - cmd.UpdatedBy = c.UserId + cmd.UserId = c.UserId } dash := cmd.GetDashboardModel() diff --git a/pkg/api/dtos/models.go b/pkg/api/dtos/models.go index a5e8e74fb46..83176e836e5 100644 --- a/pkg/api/dtos/models.go +++ b/pkg/api/dtos/models.go @@ -42,6 +42,8 @@ type DashboardMeta struct { Created time.Time `json:"created"` Updated time.Time `json:"updated"` UpdatedBy string `json:"updatedBy"` + CreatedBy string `json:"createdBy"` + Version int `json:"version"` } type DashboardFullWithMeta struct { diff --git a/pkg/models/dashboards.go b/pkg/models/dashboards.go index 63ed1f5c006..1015dfbe28c 100644 --- a/pkg/models/dashboards.go +++ b/pkg/models/dashboards.go @@ -34,6 +34,7 @@ type Dashboard struct { Updated time.Time UpdatedBy int64 + CreatedBy int64 Title string Data map[string]interface{} @@ -91,8 +92,11 @@ func NewDashboardFromJson(data map[string]interface{}) *Dashboard { // GetDashboardModel turns the command into the savable model func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard { dash := NewDashboardFromJson(cmd.Dashboard) + if dash.Data["version"] == 0 { + dash.CreatedBy = cmd.UserId + } + dash.UpdatedBy = cmd.UserId dash.OrgId = cmd.OrgId - dash.UpdatedBy = cmd.UpdatedBy dash.UpdateSlug() return dash } @@ -114,9 +118,9 @@ func (dash *Dashboard) UpdateSlug() { type SaveDashboardCommand struct { Dashboard map[string]interface{} `json:"dashboard" binding:"Required"` - Overwrite bool `json:"overwrite"` + UserId int64 `json:"userId"` OrgId int64 `json:"-"` - UpdatedBy int64 `json:"-"` + Overwrite bool `json:"overwrite"` Result *Dashboard } diff --git a/pkg/services/sqlstore/migrations/dashboard_mig.go b/pkg/services/sqlstore/migrations/dashboard_mig.go index b94f1a7d6ac..a4a8629b331 100644 --- a/pkg/services/sqlstore/migrations/dashboard_mig.go +++ b/pkg/services/sqlstore/migrations/dashboard_mig.go @@ -97,4 +97,9 @@ func addDashboardMigration(mg *Migrator) { mg.AddMigration("Add column updated_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{ Name: "updated_by", Type: DB_Int, Nullable: true, })) + + // add column to store creator of a dashboard + mg.AddMigration("Add column created_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{ + Name: "created_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 f582252cd3c..7af2abfe076 100644 --- a/public/app/features/dashboard/partials/settings.html +++ b/public/app/features/dashboard/partials/settings.html @@ -115,9 +115,9 @@