Procházet zdrojové kódy

Merge pull request #3446 from utkarshcmu/new-columns

Displaying Last UpdatedBy as Metadata
Carl Bergquist před 10 roky
rodič
revize
af9b864b83

+ 21 - 0
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")

+ 1 - 0
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 {

+ 4 - 0
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
 }

+ 5 - 0
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,
+	}))
 }

+ 11 - 0
public/app/features/dashboard/partials/settings.html

@@ -140,6 +140,17 @@
           </ul>
           <div class="clearfix"></div>
         </div>
+        <div class="tight-form">
+          <ul class="tight-form-list">
+            <li class="tight-form-item" style="width: 120px">
+              Last updated by:
+            </li>
+            <li class="tight-form-item" style="width: 180px">
+              {{dashboardMeta.updatedBy}}
+            </li>
+          </ul>
+          <div class="clearfix"></div>
+        </div>
       </div>
     </div>
   </div>