Browse Source

Merge pull request #3918 from utkarshcmu/metadata

Added metadata fields under Settings tab
Torkel Ödegaard 10 years ago
parent
commit
db27bbb162

+ 1 - 1
docs/sources/reference/http_api.md

@@ -75,7 +75,7 @@ Creates a new dashboard or updates an existing dashboard.
 
 
 JSON Body schema:
 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.
 - **overwrite** – Set to true if you want to overwrite existing dashboard with newer version or with same dashboard title.
 
 
 **Example Response**:
 **Example Response**:

+ 22 - 13
pkg/api/dashboard.go

@@ -49,17 +49,13 @@ func GetDashboard(c *middleware.Context) {
 
 
 	dash := query.Result
 	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{
 	dto := dtos.DashboardFullWithMeta{
@@ -74,12 +70,25 @@ func GetDashboard(c *middleware.Context) {
 			Created:   dash.Created,
 			Created:   dash.Created,
 			Updated:   dash.Updated,
 			Updated:   dash.Updated,
 			UpdatedBy: updater,
 			UpdatedBy: updater,
+			CreatedBy: creator,
+			Version:   dash.Version,
 		},
 		},
 	}
 	}
 
 
 	c.JSON(200, dto)
 	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) {
 func DeleteDashboard(c *middleware.Context) {
 	slug := c.Params(":slug")
 	slug := c.Params(":slug")
 
 
@@ -104,9 +113,9 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
 	cmd.OrgId = c.OrgId
 	cmd.OrgId = c.OrgId
 
 
 	if !c.IsSignedIn {
 	if !c.IsSignedIn {
-		cmd.UpdatedBy = 0
+		cmd.UserId = -1
 	} else {
 	} else {
-		cmd.UpdatedBy = c.UserId
+		cmd.UserId = c.UserId
 	}
 	}
 
 
 	dash := cmd.GetDashboardModel()
 	dash := cmd.GetDashboardModel()

+ 2 - 0
pkg/api/dtos/models.go

@@ -42,6 +42,8 @@ type DashboardMeta struct {
 	Created    time.Time `json:"created"`
 	Created    time.Time `json:"created"`
 	Updated    time.Time `json:"updated"`
 	Updated    time.Time `json:"updated"`
 	UpdatedBy  string    `json:"updatedBy"`
 	UpdatedBy  string    `json:"updatedBy"`
+	CreatedBy  string    `json:"createdBy"`
+	Version    int       `json:"version"`
 }
 }
 
 
 type DashboardFullWithMeta struct {
 type DashboardFullWithMeta struct {

+ 7 - 3
pkg/models/dashboards.go

@@ -34,6 +34,7 @@ type Dashboard struct {
 	Updated time.Time
 	Updated time.Time
 
 
 	UpdatedBy int64
 	UpdatedBy int64
+	CreatedBy int64
 
 
 	Title string
 	Title string
 	Data  map[string]interface{}
 	Data  map[string]interface{}
@@ -91,8 +92,11 @@ func NewDashboardFromJson(data map[string]interface{}) *Dashboard {
 // GetDashboardModel turns the command into the savable model
 // GetDashboardModel turns the command into the savable model
 func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
 func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
 	dash := NewDashboardFromJson(cmd.Dashboard)
 	dash := NewDashboardFromJson(cmd.Dashboard)
+	if dash.Data["version"] == 0 {
+		dash.CreatedBy = cmd.UserId
+	}
+	dash.UpdatedBy = cmd.UserId
 	dash.OrgId = cmd.OrgId
 	dash.OrgId = cmd.OrgId
-	dash.UpdatedBy = cmd.UpdatedBy
 	dash.UpdateSlug()
 	dash.UpdateSlug()
 	return dash
 	return dash
 }
 }
@@ -114,9 +118,9 @@ func (dash *Dashboard) UpdateSlug() {
 
 
 type SaveDashboardCommand struct {
 type SaveDashboardCommand struct {
 	Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
 	Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
-	Overwrite bool                   `json:"overwrite"`
+	UserId    int64                  `json:"userId"`
 	OrgId     int64                  `json:"-"`
 	OrgId     int64                  `json:"-"`
-	UpdatedBy int64                  `json:"-"`
+	Overwrite bool                   `json:"overwrite"`
 
 
 	Result *Dashboard
 	Result *Dashboard
 }
 }

+ 5 - 0
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{
 	mg.AddMigration("Add column updated_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{
 		Name: "updated_by", Type: DB_Int, Nullable: true,
 		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,
+	}))
 }
 }

+ 29 - 7
public/app/features/dashboard/partials/settings.html

@@ -115,9 +115,9 @@
 	</div>
 	</div>
 
 
   <div ng-if="editor.index == 4">
   <div ng-if="editor.index == 4">
-    <div class="editor-row">
-      <div class="tight-form-section">
-        <h5>Dashboard info</h5>
+    <div class="row">
+      <h5>Dashboard info</h5>
+      <div class="pull-left tight-form">
         <div class="tight-form">
         <div class="tight-form">
           <ul class="tight-form-list">
           <ul class="tight-form-list">
             <li class="tight-form-item" style="width: 120px">
             <li class="tight-form-item" style="width: 120px">
@@ -129,6 +129,17 @@
           </ul>
           </ul>
           <div class="clearfix"></div>
           <div class="clearfix"></div>
         </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 class="tight-form">
         <div class="tight-form">
           <ul class="tight-form-list">
           <ul class="tight-form-list">
             <li class="tight-form-item" style="width: 120px">
             <li class="tight-form-item" style="width: 120px">
@@ -136,17 +147,28 @@
             </li>
             </li>
             <li class="tight-form-item" style="width: 180px">
             <li class="tight-form-item" style="width: 180px">
               {{formatDate(dashboardMeta.created)}}
               {{formatDate(dashboardMeta.created)}}
-           </li>
+            </li>
           </ul>
           </ul>
           <div class="clearfix"></div>
           <div class="clearfix"></div>
         </div>
         </div>
-        <div class="tight-form last">
+        <div class="tight-form">
           <ul class="tight-form-list">
           <ul class="tight-form-list">
             <li class="tight-form-item" style="width: 120px">
             <li class="tight-form-item" style="width: 120px">
-              Last updated by:
+              Created by:
             </li>
             </li>
             <li class="tight-form-item" style="width: 180px">
             <li class="tight-form-item" style="width: 180px">
-              {{dashboardMeta.updatedBy}}
+              {{dashboardMeta.createdBy}}
+            </li>
+          </ul>
+          <div class="clearfix"></div>
+        </div>
+        <div class="tight-form">
+          <ul class="tight-form-list">
+            <li class="tight-form-item" style="width: 120px">
+              Current version:
+            </li>
+            <li class="tight-form-item" style="width: 180px">
+              {{dashboardMeta.version}}
             </li>
             </li>
           </ul>
           </ul>
           <div class="clearfix"></div>
           <div class="clearfix"></div>