diff --git a/docs/sources/reference/http_api.md b/docs/sources/reference/http_api.md index 4b0dff6c4b66..5ffd03c41111 100644 --- a/docs/sources/reference/http_api.md +++ b/docs/sources/reference/http_api.md @@ -70,13 +70,15 @@ Creates a new dashboard or updates an existing dashboard. "schemaVersion": 6, "version": 0 }, - "overwrite": false + "overwrite": false, + "userId:": 3 } 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. +- **userId** - Set userId if you want to record who created or updated a dashboard. **Example Response**: diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index 5effc1d06d73..8bcb7fc6b749 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -52,11 +52,11 @@ func GetDashboard(c *middleware.Context) { // Finding the last creator and updater of the dashboard updater, creator := "Anonymous", "Anonymous" if dash.UpdatedBy > 0 { - updater = getUserLogin(dash.UpdatedBy) - } - if dash.CreatedBy > 0 { - creator = getUserLogin(dash.CreatedBy) - } + updater = getUserLogin(dash.UpdatedBy) + } + if dash.CreatedBy > 0 { + creator = getUserLogin(dash.CreatedBy) + } dto := dtos.DashboardFullWithMeta{ Dashboard: dash.Data, @@ -70,7 +70,7 @@ func GetDashboard(c *middleware.Context) { Created: dash.Created, Updated: dash.Updated, UpdatedBy: updater, - CreatedBy: creator, + CreatedBy: creator, }, } @@ -78,14 +78,14 @@ func GetDashboard(c *middleware.Context) { } 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 - } + 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) { @@ -114,8 +114,8 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) { if !c.IsSignedIn { cmd.UserId = -1 } else { - cmd.UserId = c.UserId - } + cmd.UserId = c.UserId + } dash := cmd.GetDashboardModel() if dash.Id == 0 { diff --git a/pkg/api/dtos/models.go b/pkg/api/dtos/models.go index b93d36c0aeea..b35b5c472b63 100644 --- a/pkg/api/dtos/models.go +++ b/pkg/api/dtos/models.go @@ -42,7 +42,7 @@ type DashboardMeta struct { Created time.Time `json:"created"` Updated time.Time `json:"updated"` UpdatedBy string `json:"updatedBy"` - CreatedBy string `json:"createdBy"` + CreatedBy string `json:"createdBy"` } type DashboardFullWithMeta struct { diff --git a/pkg/models/dashboards.go b/pkg/models/dashboards.go index c9718dda2956..a85e98a8b91f 100644 --- a/pkg/models/dashboards.go +++ b/pkg/models/dashboards.go @@ -34,7 +34,7 @@ type Dashboard struct { Updated time.Time UpdatedBy int64 - CreatedBy int64 + CreatedBy int64 Title string Data map[string]interface{} @@ -67,7 +67,7 @@ func (dash *Dashboard) GetTags() []string { return b } -func NewDashboardFromJson(data map[string]interface {}) *Dashboard { +func NewDashboardFromJson(data map[string]interface{}) *Dashboard { dash := &Dashboard{} dash.Data = data dash.Title = dash.Data["title"].(string) @@ -93,10 +93,10 @@ func NewDashboardFromJson(data map[string]interface {}) *Dashboard { 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.CreatedBy = cmd.UserId + } + dash.UpdatedBy = cmd.UserId + dash.OrgId = cmd.OrgId dash.UpdateSlug() return dash } @@ -118,9 +118,9 @@ func (dash *Dashboard) UpdateSlug() { type SaveDashboardCommand struct { Dashboard map[string]interface{} `json:"dashboard" binding:"Required"` - UserId int64 `json:"userId"` + UserId int64 `json:"userId"` OrgId int64 `json:"-"` - Overwrite bool `json:"overwrite"` + 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 43fb8e36cb2f..a4a8629b331d 100644 --- a/pkg/services/sqlstore/migrations/dashboard_mig.go +++ b/pkg/services/sqlstore/migrations/dashboard_mig.go @@ -98,8 +98,8 @@ func addDashboardMigration(mg *Migrator) { 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, - })) + // 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, + })) }