Skip to content

Commit af9b864

Browse files
committed
Merge pull request grafana#3446 from utkarshcmu/new-columns
Displaying Last UpdatedBy as Metadata
2 parents 406ee43 + d8b9072 commit af9b864

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

pkg/api/dashboard.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ func GetDashboard(c *middleware.Context) {
4848
}
4949

5050
dash := query.Result
51+
52+
// Finding the last updater of the dashboard
53+
updater := "Anonymous"
54+
if dash.UpdatedBy != 0 {
55+
userQuery := m.GetUserByIdQuery{Id: dash.UpdatedBy}
56+
userErr := bus.Dispatch(&userQuery)
57+
if userErr != nil {
58+
updater = "Unknown"
59+
} else {
60+
user := userQuery.Result
61+
updater = user.Login
62+
}
63+
}
64+
5165
dto := dtos.DashboardFullWithMeta{
5266
Dashboard: dash.Data,
5367
Meta: dtos.DashboardMeta{
@@ -59,6 +73,7 @@ func GetDashboard(c *middleware.Context) {
5973
CanEdit: canEditDashboard(c.OrgRole),
6074
Created: dash.Created,
6175
Updated: dash.Updated,
76+
UpdatedBy: updater,
6277
},
6378
}
6479

@@ -88,6 +103,12 @@ func DeleteDashboard(c *middleware.Context) {
88103
func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
89104
cmd.OrgId = c.OrgId
90105

106+
if !c.IsSignedIn {
107+
cmd.UpdatedBy = 0
108+
} else {
109+
cmd.UpdatedBy = c.UserId
110+
}
111+
91112
dash := cmd.GetDashboardModel()
92113
if dash.Id == 0 {
93114
limitReached, err := middleware.QuotaReached(c, "dashboard")

pkg/api/dtos/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type DashboardMeta struct {
4141
Expires time.Time `json:"expires"`
4242
Created time.Time `json:"created"`
4343
Updated time.Time `json:"updated"`
44+
UpdatedBy string `json:"updatedBy"`
4445
}
4546

4647
type DashboardFullWithMeta struct {

pkg/models/dashboards.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type Dashboard struct {
3333
Created time.Time
3434
Updated time.Time
3535

36+
UpdatedBy int64
37+
3638
Title string
3739
Data map[string]interface{}
3840
}
@@ -90,6 +92,7 @@ func NewDashboardFromJson(data map[string]interface{}) *Dashboard {
9092
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
9193
dash := NewDashboardFromJson(cmd.Dashboard)
9294
dash.OrgId = cmd.OrgId
95+
dash.UpdatedBy = cmd.UpdatedBy
9396
dash.UpdateSlug()
9497
return dash
9598
}
@@ -113,6 +116,7 @@ type SaveDashboardCommand struct {
113116
Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
114117
Overwrite bool `json:"overwrite"`
115118
OrgId int64 `json:"-"`
119+
UpdatedBy int64 `json:"-"`
116120

117121
Result *Dashboard
118122
}

pkg/services/sqlstore/migrations/dashboard_mig.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,9 @@ func addDashboardMigration(mg *Migrator) {
9292
Sqlite("SELECT 0 WHERE 0;").
9393
Postgres("SELECT 0;").
9494
Mysql("ALTER TABLE dashboard MODIFY data MEDIUMTEXT;"))
95+
96+
// add column to store updater of a dashboard
97+
mg.AddMigration("Add column updated_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{
98+
Name: "updated_by", Type: DB_Int, Nullable: true,
99+
}))
95100
}

public/app/features/dashboard/partials/settings.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ <h5>Dashboard info</h5>
140140
</ul>
141141
<div class="clearfix"></div>
142142
</div>
143+
<div class="tight-form">
144+
<ul class="tight-form-list">
145+
<li class="tight-form-item" style="width: 120px">
146+
Last updated by:
147+
</li>
148+
<li class="tight-form-item" style="width: 180px">
149+
{{dashboardMeta.updatedBy}}
150+
</li>
151+
</ul>
152+
<div class="clearfix"></div>
153+
</div>
143154
</div>
144155
</div>
145156
</div>

0 commit comments

Comments
 (0)