| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-06-15 22:08:27 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2019-04-30 19:32:18 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2018-10-13 13:53:28 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/alerting" | 
					
						
							| 
									
										
										
										
											2017-12-01 20:50:47 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2017-06-06 06:14:40 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/dashdiffs" | 
					
						
							| 
									
										
										
										
											2016-12-08 17:25:05 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2019-02-24 06:35:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/metrics" | 
					
						
							| 
									
										
										
										
											2017-06-12 21:48:55 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/guardian" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-22 16:50:00 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	anonString = "Anonymous" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | func isDashboardStarredByUser(c *models.ReqContext, dashID int64) (bool, error) { | 
					
						
							| 
									
										
										
										
											2015-02-02 18:32:32 +08:00
										 |  |  | 	if !c.IsSignedIn { | 
					
						
							|  |  |  | 		return false, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.IsStarredByUserQuery{UserId: c.UserId, DashboardId: dashID} | 
					
						
							| 
									
										
										
										
											2021-06-15 22:08:27 +08:00
										 |  |  | 	if err := bus.DispatchCtx(c.Req.Context(), &query); err != nil { | 
					
						
							| 
									
										
										
										
											2015-02-02 18:32:32 +08:00
										 |  |  | 		return false, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return query.Result, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func dashboardGuardianResponse(err error) response.Response { | 
					
						
							| 
									
										
										
										
											2017-06-20 01:47:44 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Error while checking dashboard permissions", err) | 
					
						
							| 
									
										
										
										
											2017-06-20 01:47:44 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-20 21:42:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Error(403, "Access denied to this dashboard", nil) | 
					
						
							| 
									
										
										
										
											2017-06-20 01:47:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-28 19:38:33 +08:00
										 |  |  | func (hs *HTTPServer) TrimDashboard(c *models.ReqContext, cmd models.TrimDashboardCommand) response.Response { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	dash := cmd.Dashboard | 
					
						
							|  |  |  | 	meta := cmd.Meta | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 04:29:30 +08:00
										 |  |  | 	trimedResult := *dash | 
					
						
							|  |  |  | 	if !hs.LoadSchemaService.IsDisabled() { | 
					
						
							|  |  |  | 		trimedResult, err = hs.LoadSchemaService.DashboardTrimDefaults(*dash) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return response.Error(500, "Error while exporting with default values removed", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-04-28 19:38:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dto := dtos.TrimDashboardFullWithMeta{ | 
					
						
							|  |  |  | 		Dashboard: &trimedResult, | 
					
						
							|  |  |  | 		Meta:      meta, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.TimeRequest(metrics.MApiDashboardGet) | 
					
						
							|  |  |  | 	return response.JSON(200, dto) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) GetDashboard(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	uid := c.Params(":uid") | 
					
						
							| 
									
										
										
										
											2021-06-15 22:08:27 +08:00
										 |  |  | 	dash, rsp := getDashboardHelper(c.Req.Context(), c.OrgId, 0, uid) | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 	if rsp != nil { | 
					
						
							|  |  |  | 		return rsp | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-04 21:41:17 +08:00
										 |  |  | 	// When dash contains only keys id, uid that means dashboard data is not valid and json decode failed.
 | 
					
						
							|  |  |  | 	if dash.Data != nil { | 
					
						
							|  |  |  | 		isEmptyData := true | 
					
						
							|  |  |  | 		for k := range dash.Data.MustMap() { | 
					
						
							|  |  |  | 			if k != "id" && k != "uid" { | 
					
						
							|  |  |  | 				isEmptyData = false | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if isEmptyData { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(500, "Error while loading dashboard, dashboard data is invalid", nil) | 
					
						
							| 
									
										
										
										
											2021-01-04 21:41:17 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 	guardian := guardian.New(dash.Id, c.OrgId, c.SignedInUser) | 
					
						
							| 
									
										
										
										
											2017-06-20 01:47:44 +08:00
										 |  |  | 	if canView, err := guardian.CanView(); err != nil || !canView { | 
					
						
							|  |  |  | 		return dashboardGuardianResponse(err) | 
					
						
							| 
									
										
										
										
											2017-06-12 21:48:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 	canEdit, _ := guardian.CanEdit() | 
					
						
							|  |  |  | 	canSave, _ := guardian.CanSave() | 
					
						
							| 
									
										
										
										
											2017-06-23 06:34:19 +08:00
										 |  |  | 	canAdmin, _ := guardian.CanAdmin() | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-12 21:48:55 +08:00
										 |  |  | 	isStarred, err := isDashboardStarredByUser(c, dash.Id) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Error while checking if dashboard was starred by user", err) | 
					
						
							| 
									
										
										
										
											2017-06-12 21:48:55 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-18 19:38:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-29 01:53:19 +08:00
										 |  |  | 	// Finding creator and last updater of the dashboard
 | 
					
						
							| 
									
										
										
										
											2018-09-22 16:50:00 +08:00
										 |  |  | 	updater, creator := anonString, anonString | 
					
						
							| 
									
										
										
										
											2016-01-28 13:55:54 +08:00
										 |  |  | 	if dash.UpdatedBy > 0 { | 
					
						
							| 
									
										
										
										
											2021-06-21 13:51:33 +08:00
										 |  |  | 		updater = getUserLogin(c.Req.Context(), dash.UpdatedBy) | 
					
						
							| 
									
										
										
										
											2016-01-28 14:00:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if dash.CreatedBy > 0 { | 
					
						
							| 
									
										
										
										
											2021-06-21 13:51:33 +08:00
										 |  |  | 		creator = getUserLogin(c.Req.Context(), dash.CreatedBy) | 
					
						
							| 
									
										
										
										
											2016-01-28 14:00:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-18 19:38:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 	meta := dtos.DashboardMeta{ | 
					
						
							|  |  |  | 		IsStarred:   isStarred, | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 		Slug:        dash.Slug, | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		Type:        models.DashTypeDB, | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 		CanStar:     c.IsSignedIn, | 
					
						
							|  |  |  | 		CanSave:     canSave, | 
					
						
							|  |  |  | 		CanEdit:     canEdit, | 
					
						
							| 
									
										
										
										
											2017-06-23 06:34:19 +08:00
										 |  |  | 		CanAdmin:    canAdmin, | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 		Created:     dash.Created, | 
					
						
							|  |  |  | 		Updated:     dash.Updated, | 
					
						
							|  |  |  | 		UpdatedBy:   updater, | 
					
						
							|  |  |  | 		CreatedBy:   creator, | 
					
						
							|  |  |  | 		Version:     dash.Version, | 
					
						
							|  |  |  | 		HasAcl:      dash.HasAcl, | 
					
						
							|  |  |  | 		IsFolder:    dash.IsFolder, | 
					
						
							| 
									
										
										
										
											2017-06-24 04:00:26 +08:00
										 |  |  | 		FolderId:    dash.FolderId, | 
					
						
							| 
									
										
										
										
											2018-02-01 06:14:48 +08:00
										 |  |  | 		Url:         dash.GetUrl(), | 
					
						
							| 
									
										
										
										
											2018-02-02 17:33:31 +08:00
										 |  |  | 		FolderTitle: "General", | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// lookup folder title
 | 
					
						
							| 
									
										
										
										
											2017-06-24 04:00:26 +08:00
										 |  |  | 	if dash.FolderId > 0 { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		query := models.GetDashboardQuery{Id: dash.FolderId, OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2021-06-15 22:08:27 +08:00
										 |  |  | 		if err := bus.DispatchCtx(c.Req.Context(), &query); err != nil { | 
					
						
							| 
									
										
										
										
											2021-05-26 22:20:13 +08:00
										 |  |  | 			if errors.Is(err, models.ErrFolderNotFound) { | 
					
						
							|  |  |  | 				return response.Error(404, "Folder not found", err) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(500, "Dashboard folder could not be read", err) | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-05-26 22:20:13 +08:00
										 |  |  | 		meta.FolderUid = query.Result.Uid | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 		meta.FolderTitle = query.Result.Title | 
					
						
							| 
									
										
										
										
											2018-02-05 18:09:05 +08:00
										 |  |  | 		meta.FolderUrl = query.Result.GetUrl() | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 23:06:10 +08:00
										 |  |  | 	svc := dashboards.NewProvisioningService(hs.SQLStore) | 
					
						
							| 
									
										
										
										
											2021-03-12 18:51:02 +08:00
										 |  |  | 	provisioningData, err := svc.GetProvisionedDashboardDataByDashboardID(dash.Id) | 
					
						
							| 
									
										
										
										
											2018-04-10 15:31:35 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Error while checking if dashboard is provisioned", err) | 
					
						
							| 
									
										
										
										
											2018-04-10 15:31:35 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 19:32:18 +08:00
										 |  |  | 	if provisioningData != nil { | 
					
						
							| 
									
										
										
										
											2020-04-15 14:12:52 +08:00
										 |  |  | 		allowUIUpdate := hs.ProvisioningService.GetAllowUIUpdatesFromConfig(provisioningData.Name) | 
					
						
							|  |  |  | 		if !allowUIUpdate { | 
					
						
							| 
									
										
										
										
											2019-10-31 21:27:31 +08:00
										 |  |  | 			meta.Provisioned = true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 19:32:18 +08:00
										 |  |  | 		meta.ProvisionedExternalId, err = filepath.Rel( | 
					
						
							|  |  |  | 			hs.ProvisioningService.GetDashboardProvisionerResolvedPath(provisioningData.Name), | 
					
						
							|  |  |  | 			provisioningData.ExternalId, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			// Not sure when this could happen so not sure how to better handle this. Right now ProvisionedExternalId
 | 
					
						
							|  |  |  | 			// is for better UX, showing in Save/Delete dialogs and so it won't break anything if it is empty.
 | 
					
						
							|  |  |  | 			hs.log.Warn("Failed to create ProvisionedExternalId", "err", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-28 01:51:04 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 21:40:10 +08:00
										 |  |  | 	// make sure db version is in sync with json model version
 | 
					
						
							|  |  |  | 	dash.Data.Set("version", dash.Version) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 	// load library panels JSON for this dashboard
 | 
					
						
							|  |  |  | 	err = hs.LibraryPanelService.LoadLibraryPanelsForDashboard(c, dash) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(500, "Error while loading library panels", err) | 
					
						
							| 
									
										
										
										
											2021-01-20 16:28:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 14:36:44 +08:00
										 |  |  | 	dto := dtos.DashboardFullWithMeta{ | 
					
						
							|  |  |  | 		Dashboard: dash.Data, | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 		Meta:      meta, | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 22:58:46 +08:00
										 |  |  | 	c.TimeRequest(metrics.MApiDashboardGet) | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.JSON(200, dto) | 
					
						
							| 
									
										
										
										
											2017-06-12 21:48:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 13:51:33 +08:00
										 |  |  | func getUserLogin(ctx context.Context, userID int64) string { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.GetUserByIdQuery{Id: userID} | 
					
						
							| 
									
										
										
										
											2021-06-21 13:51:33 +08:00
										 |  |  | 	err := bus.DispatchCtx(ctx, &query) | 
					
						
							| 
									
										
										
										
											2016-01-28 14:00:24 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-09-22 16:50:00 +08:00
										 |  |  | 		return anonString | 
					
						
							| 
									
										
										
										
											2016-01-28 14:00:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	return query.Result.Login | 
					
						
							| 
									
										
										
										
											2016-01-28 13:55:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 22:08:27 +08:00
										 |  |  | func getDashboardHelper(ctx context.Context, orgID int64, id int64, uid string) (*models.Dashboard, response.Response) { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	var query models.GetDashboardQuery | 
					
						
							| 
									
										
										
										
											2018-01-30 04:23:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if len(uid) > 0 { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		query = models.GetDashboardQuery{Uid: uid, Id: id, OrgId: orgID} | 
					
						
							| 
									
										
										
										
											2018-01-30 04:23:07 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2021-06-03 21:20:13 +08:00
										 |  |  | 		query = models.GetDashboardQuery{Id: id, OrgId: orgID} | 
					
						
							| 
									
										
										
										
											2018-01-30 04:23:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 22:08:27 +08:00
										 |  |  | 	if err := bus.DispatchCtx(ctx, &query); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return nil, response.Error(404, "Dashboard not found", err) | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-29 20:51:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 	return query.Result, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-20 16:28:10 +08:00
										 |  |  | func (hs *HTTPServer) DeleteDashboardBySlug(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.GetDashboardsBySlugQuery{OrgId: c.OrgId, Slug: c.Params(":slug")} | 
					
						
							| 
									
										
										
										
											2018-01-31 23:51:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to retrieve dashboards by slug", err) | 
					
						
							| 
									
										
										
										
											2018-01-31 23:51:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(query.Result) > 1 { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.JSON(412, util.DynMap{"status": "multiple-slugs-exists", "message": models.ErrDashboardsWithSameSlugExists.Error()}) | 
					
						
							| 
									
										
										
										
											2018-01-31 23:51:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-20 16:28:10 +08:00
										 |  |  | 	return hs.deleteDashboard(c) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-20 16:28:10 +08:00
										 |  |  | func (hs *HTTPServer) DeleteDashboardByUID(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return hs.deleteDashboard(c) | 
					
						
							| 
									
										
										
										
											2019-04-10 19:29:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-20 16:28:10 +08:00
										 |  |  | func (hs *HTTPServer) deleteDashboard(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-06-15 22:08:27 +08:00
										 |  |  | 	dash, rsp := getDashboardHelper(c.Req.Context(), c.OrgId, 0, c.Params(":uid")) | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 	if rsp != nil { | 
					
						
							|  |  |  | 		return rsp | 
					
						
							| 
									
										
										
										
											2017-06-13 05:05:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 	guardian := guardian.New(dash.Id, c.OrgId, c.SignedInUser) | 
					
						
							| 
									
										
										
										
											2017-06-20 01:47:44 +08:00
										 |  |  | 	if canSave, err := guardian.CanSave(); err != nil || !canSave { | 
					
						
							|  |  |  | 		return dashboardGuardianResponse(err) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 	// disconnect all library elements for this dashboard
 | 
					
						
							|  |  |  | 	err := hs.LibraryElementService.DisconnectElementsFromDashboard(c, dash.Id) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		hs.log.Error("Failed to disconnect library elements", "dashboard", dash.Id, "user", c.SignedInUser.UserId, "error", err) | 
					
						
							| 
									
										
										
										
											2021-01-20 16:28:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 23:06:10 +08:00
										 |  |  | 	svc := dashboards.NewService(hs.SQLStore) | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 	err = svc.DeleteDashboard(dash.Id, c.OrgId) | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		var dashboardErr models.DashboardErr | 
					
						
							|  |  |  | 		if ok := errors.As(err, &dashboardErr); ok { | 
					
						
							|  |  |  | 			if errors.Is(err, models.ErrDashboardCannotDeleteProvisionedDashboard) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 				return response.Error(dashboardErr.StatusCode, dashboardErr.Error(), err) | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to delete dashboard", err) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-04 23:44:55 +08:00
										 |  |  | 	if hs.Live != nil { | 
					
						
							| 
									
										
										
										
											2021-05-12 03:03:04 +08:00
										 |  |  | 		err := hs.Live.GrafanaScope.Dashboards.DashboardDeleted(c.OrgId, c.ToUserDisplayDTO(), dash.Uid) | 
					
						
							| 
									
										
										
										
											2021-04-24 03:55:31 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			hs.log.Error("Failed to broadcast delete info", "dashboard", dash.Uid, "error", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.JSON(200, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2018-02-21 23:38:09 +08:00
										 |  |  | 		"title":   dash.Title, | 
					
						
							|  |  |  | 		"message": fmt.Sprintf("Dashboard %s deleted", dash.Title), | 
					
						
							| 
									
										
										
										
											2020-07-31 14:22:09 +08:00
										 |  |  | 		"id":      dash.Id, | 
					
						
							| 
									
										
										
										
											2018-02-21 23:38:09 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) PostDashboard(c *models.ReqContext, cmd models.SaveDashboardCommand) response.Response { | 
					
						
							| 
									
										
										
										
											2021-04-28 19:38:33 +08:00
										 |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2017-06-05 22:34:32 +08:00
										 |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							|  |  |  | 	cmd.UserId = c.UserId | 
					
						
							| 
									
										
										
										
											2021-05-26 22:20:13 +08:00
										 |  |  | 	if cmd.FolderUid != "" { | 
					
						
							|  |  |  | 		folders := dashboards.NewFolderService(c.OrgId, c.SignedInUser, hs.SQLStore) | 
					
						
							|  |  |  | 		folder, err := folders.GetFolderByUID(cmd.FolderUid) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			if errors.Is(err, models.ErrFolderNotFound) { | 
					
						
							|  |  |  | 				return response.Error(400, "Folder not found", err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return response.Error(500, "Error while checking folder ID", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		cmd.FolderId = folder.Id | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 22:38:25 +08:00
										 |  |  | 	dash := cmd.GetDashboardModel() | 
					
						
							| 
									
										
										
										
											2021-05-26 22:20:13 +08:00
										 |  |  | 	newDashboard := dash.Id == 0 | 
					
						
							| 
									
										
										
										
											2019-03-05 23:44:41 +08:00
										 |  |  | 	if newDashboard { | 
					
						
							| 
									
										
										
										
											2019-02-12 04:12:01 +08:00
										 |  |  | 		limitReached, err := hs.QuotaService.QuotaReached(c, "dashboard") | 
					
						
							| 
									
										
										
										
											2015-07-20 22:38:25 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(500, "failed to get quota", err) | 
					
						
							| 
									
										
										
										
											2015-07-20 22:38:25 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if limitReached { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(403, "Quota reached", nil) | 
					
						
							| 
									
										
										
										
											2015-07-20 22:38:25 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 23:06:10 +08:00
										 |  |  | 	svc := dashboards.NewProvisioningService(hs.SQLStore) | 
					
						
							| 
									
										
										
										
											2021-03-12 18:51:02 +08:00
										 |  |  | 	provisioningData, err := svc.GetProvisionedDashboardDataByDashboardID(dash.Id) | 
					
						
							| 
									
										
										
										
											2019-10-31 21:27:31 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Error while checking if dashboard is provisioned", err) | 
					
						
							| 
									
										
										
										
											2019-10-31 21:27:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 14:57:03 +08:00
										 |  |  | 	allowUiUpdate := true | 
					
						
							|  |  |  | 	if provisioningData != nil { | 
					
						
							| 
									
										
										
										
											2020-04-15 14:12:52 +08:00
										 |  |  | 		allowUiUpdate = hs.ProvisioningService.GetAllowUIUpdatesFromConfig(provisioningData.Name) | 
					
						
							| 
									
										
										
										
											2019-11-01 14:57:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-31 21:27:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 	// clean up all unnecessary library panels JSON properties so we store a minimum JSON
 | 
					
						
							|  |  |  | 	err = hs.LibraryPanelService.CleanLibraryPanelsForDashboard(dash) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(500, "Error while cleaning library panels", err) | 
					
						
							| 
									
										
										
										
											2021-01-20 16:28:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 19:28:56 +08:00
										 |  |  | 	dashItem := &dashboards.SaveDashboardDTO{ | 
					
						
							| 
									
										
										
										
											2017-12-01 20:50:47 +08:00
										 |  |  | 		Dashboard: dash, | 
					
						
							|  |  |  | 		Message:   cmd.Message, | 
					
						
							| 
									
										
										
										
											2016-11-24 18:22:13 +08:00
										 |  |  | 		OrgId:     c.OrgId, | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 		User:      c.SignedInUser, | 
					
						
							| 
									
										
										
										
											2017-12-12 20:18:00 +08:00
										 |  |  | 		Overwrite: cmd.Overwrite, | 
					
						
							| 
									
										
										
										
											2016-11-24 18:22:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 23:06:10 +08:00
										 |  |  | 	dashSvc := dashboards.NewService(hs.SQLStore) | 
					
						
							| 
									
										
										
										
											2021-03-12 18:51:02 +08:00
										 |  |  | 	dashboard, err := dashSvc.SaveDashboard(dashItem, allowUiUpdate) | 
					
						
							| 
									
										
										
										
											2021-04-24 03:55:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-04 23:44:55 +08:00
										 |  |  | 	if hs.Live != nil { | 
					
						
							|  |  |  | 		// Tell everyone listening that the dashboard changed
 | 
					
						
							| 
									
										
										
										
											2021-04-24 03:55:31 +08:00
										 |  |  | 		if dashboard == nil { | 
					
						
							|  |  |  | 			dashboard = dash // the original request
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// This will broadcast all save requets only if a `gitops` observer exists.
 | 
					
						
							|  |  |  | 		// gitops is useful when trying to save dashboards in an environment where the user can not save
 | 
					
						
							|  |  |  | 		channel := hs.Live.GrafanaScope.Dashboards | 
					
						
							| 
									
										
										
										
											2021-05-12 03:03:04 +08:00
										 |  |  | 		liveerr := channel.DashboardSaved(c.SignedInUser.OrgId, c.SignedInUser.ToUserDisplayDTO(), cmd.Message, dashboard, err) | 
					
						
							| 
									
										
										
										
											2021-04-24 03:55:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// When an error exists, but the value broadcast to a gitops listener return 202
 | 
					
						
							| 
									
										
										
										
											2021-05-12 03:03:04 +08:00
										 |  |  | 		if liveerr == nil && err != nil && channel.HasGitOpsObserver(c.SignedInUser.OrgId) { | 
					
						
							| 
									
										
										
										
											2021-04-24 03:55:31 +08:00
										 |  |  | 			return response.JSON(202, util.DynMap{ | 
					
						
							|  |  |  | 				"status":  "pending", | 
					
						
							|  |  |  | 				"message": "changes were broadcast to the gitops listener", | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if liveerr != nil { | 
					
						
							|  |  |  | 			hs.log.Warn("unable to broadcast save event", "uid", dashboard.Uid, "error", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-04 23:44:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-20 18:42:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-03-17 23:06:10 +08:00
										 |  |  | 		return hs.dashboardSaveErrorToApiResponse(err) | 
					
						
							| 
									
										
										
										
											2019-12-20 18:42:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if hs.Cfg.EditorsCanAdmin && newDashboard { | 
					
						
							|  |  |  | 		inFolder := cmd.FolderId > 0 | 
					
						
							| 
									
										
										
										
											2021-03-17 23:06:10 +08:00
										 |  |  | 		err := dashSvc.MakeUserAdmin(cmd.OrgId, cmd.UserId, dashboard.Id, !inFolder) | 
					
						
							| 
									
										
										
										
											2019-12-20 18:42:47 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			hs.log.Error("Could not make user admin", "dashboard", dashboard.Title, "user", c.SignedInUser.UserId, "error", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 	// connect library panels for this dashboard after the dashboard is stored and has an ID
 | 
					
						
							|  |  |  | 	err = hs.LibraryPanelService.ConnectLibraryPanelsForDashboard(c, dashboard) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(500, "Error while connecting library panels", err) | 
					
						
							| 
									
										
										
										
											2021-01-20 16:28:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-20 18:42:47 +08:00
										 |  |  | 	c.TimeRequest(metrics.MApiDashboardSave) | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.JSON(200, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2019-12-20 18:42:47 +08:00
										 |  |  | 		"status":  "success", | 
					
						
							|  |  |  | 		"slug":    dashboard.Slug, | 
					
						
							|  |  |  | 		"version": dashboard.Version, | 
					
						
							|  |  |  | 		"id":      dashboard.Id, | 
					
						
							|  |  |  | 		"uid":     dashboard.Uid, | 
					
						
							|  |  |  | 		"url":     dashboard.GetUrl(), | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-01 20:50:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 23:06:10 +08:00
										 |  |  | func (hs *HTTPServer) dashboardSaveErrorToApiResponse(err error) response.Response { | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 	var dashboardErr models.DashboardErr | 
					
						
							|  |  |  | 	if ok := errors.As(err, &dashboardErr); ok { | 
					
						
							|  |  |  | 		if body := dashboardErr.Body(); body != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.JSON(dashboardErr.StatusCode, body) | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-06-16 20:56:55 +08:00
										 |  |  | 		if dashboardErr.StatusCode != 400 { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(dashboardErr.StatusCode, dashboardErr.Error(), err) | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(dashboardErr.StatusCode, dashboardErr.Error(), nil) | 
					
						
							| 
									
										
										
										
											2017-12-01 20:50:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 	if errors.Is(err, models.ErrFolderNotFound) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(400, err.Error(), nil) | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 	var validationErr alerting.ValidationError | 
					
						
							|  |  |  | 	if ok := errors.As(err, &validationErr); ok { | 
					
						
							| 
									
										
										
										
											2021-06-16 20:56:55 +08:00
										 |  |  | 		return response.Error(422, validationErr.Error(), err) | 
					
						
							| 
									
										
										
										
											2016-11-24 18:22:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 	var pluginErr models.UpdatePluginDashboardError | 
					
						
							|  |  |  | 	if ok := errors.As(err, &pluginErr); ok { | 
					
						
							|  |  |  | 		message := fmt.Sprintf("The dashboard belongs to plugin %s.", pluginErr.PluginId) | 
					
						
							| 
									
										
										
										
											2019-12-20 18:42:47 +08:00
										 |  |  | 		// look up plugin name
 | 
					
						
							| 
									
										
										
										
											2021-03-17 23:06:10 +08:00
										 |  |  | 		if pluginDef := hs.PluginManager.GetPlugin(pluginErr.PluginId); pluginDef != nil { | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 			message = fmt.Sprintf("The dashboard belongs to plugin %s.", pluginDef.Name) | 
					
						
							| 
									
										
										
										
											2019-03-05 23:44:41 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.JSON(412, util.DynMap{"status": "plugin-dashboard", "message": message}) | 
					
						
							| 
									
										
										
										
											2019-03-05 23:44:41 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Error(500, "Failed to save dashboard", err) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-23 00:00:39 +08:00
										 |  |  | // GetHomeDashboard returns the home dashboard.
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) GetHomeDashboard(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	prefsQuery := models.GetPreferencesWithDefaultsQuery{User: c.SignedInUser} | 
					
						
							| 
									
										
										
										
											2021-04-13 21:27:51 +08:00
										 |  |  | 	homePage := hs.Cfg.HomePage | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-16 18:40:03 +08:00
										 |  |  | 	if err := hs.Bus.DispatchCtx(c.Req.Context(), &prefsQuery); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get preferences", err) | 
					
						
							| 
									
										
										
										
											2016-03-17 17:29:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-13 21:27:51 +08:00
										 |  |  | 	if prefsQuery.Result.HomeDashboardId == 0 && len(homePage) > 0 { | 
					
						
							|  |  |  | 		homePageRedirect := dtos.DashboardRedirect{RedirectUri: homePage} | 
					
						
							|  |  |  | 		return response.JSON(200, &homePageRedirect) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | 	if prefsQuery.Result.HomeDashboardId != 0 { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		slugQuery := models.GetDashboardRefByIdQuery{Id: prefsQuery.Result.HomeDashboardId} | 
					
						
							| 
									
										
										
										
											2021-07-16 18:40:03 +08:00
										 |  |  | 		err := hs.Bus.DispatchCtx(c.Req.Context(), &slugQuery) | 
					
						
							| 
									
										
										
										
											2016-05-24 13:39:58 +08:00
										 |  |  | 		if err == nil { | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 			url := models.GetDashboardUrl(slugQuery.Result.Uid, slugQuery.Result.Slug) | 
					
						
							| 
									
										
										
										
											2018-02-05 17:24:48 +08:00
										 |  |  | 			dashRedirect := dtos.DashboardRedirect{RedirectUri: url} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.JSON(200, &dashRedirect) | 
					
						
							| 
									
										
										
										
											2016-03-17 17:29:34 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-12-03 17:11:14 +08:00
										 |  |  | 		hs.log.Warn("Failed to get slug from database", "err", err) | 
					
						
							| 
									
										
										
										
											2016-03-17 17:29:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-23 00:00:39 +08:00
										 |  |  | 	filePath := hs.Cfg.DefaultHomeDashboardPath | 
					
						
							|  |  |  | 	if filePath == "" { | 
					
						
							| 
									
										
										
										
											2020-10-19 23:35:31 +08:00
										 |  |  | 		filePath = filepath.Join(hs.Cfg.StaticRootPath, "dashboards/home.json") | 
					
						
							| 
									
										
										
										
											2020-06-23 00:00:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 05:13:06 +08:00
										 |  |  | 	// It's safe to ignore gosec warning G304 since the variable part of the file path comes from a configuration
 | 
					
						
							|  |  |  | 	// variable
 | 
					
						
							|  |  |  | 	// nolint:gosec
 | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	file, err := os.Open(filePath) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to load home dashboard", err) | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-03 17:11:14 +08:00
										 |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if err := file.Close(); err != nil { | 
					
						
							|  |  |  | 			hs.log.Warn("Failed to close dashboard file", "path", filePath, "err", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 14:36:44 +08:00
										 |  |  | 	dash := dtos.DashboardFullWithMeta{} | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	dash.Meta.IsHome = true | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	dash.Meta.CanEdit = c.SignedInUser.HasRole(models.ROLE_EDITOR) | 
					
						
							| 
									
										
										
										
											2018-02-02 17:33:31 +08:00
										 |  |  | 	dash.Meta.FolderTitle = "General" | 
					
						
							| 
									
										
										
										
											2017-06-17 09:25:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	jsonParser := json.NewDecoder(file) | 
					
						
							| 
									
										
										
										
											2015-05-04 14:36:44 +08:00
										 |  |  | 	if err := jsonParser.Decode(&dash.Dashboard); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to load home dashboard", err) | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 23:12:22 +08:00
										 |  |  | 	hs.addGettingStartedPanelToHomeDashboard(c, dash.Dashboard) | 
					
						
							| 
									
										
										
										
											2016-12-08 17:25:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.JSON(200, &dash) | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-12 20:11:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 23:12:22 +08:00
										 |  |  | func (hs *HTTPServer) addGettingStartedPanelToHomeDashboard(c *models.ReqContext, dash *simplejson.Json) { | 
					
						
							|  |  |  | 	// We only add this getting started panel for Admins who have not dismissed it,
 | 
					
						
							|  |  |  | 	// and if a custom default home dashboard hasn't been configured
 | 
					
						
							|  |  |  | 	if !c.HasUserRole(models.ROLE_ADMIN) || | 
					
						
							|  |  |  | 		c.HasHelpFlag(models.HelpFlagGettingStartedPanelDismissed) || | 
					
						
							|  |  |  | 		hs.Cfg.DefaultHomeDashboardPath != "" { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-25 20:47:57 +08:00
										 |  |  | 	panels := dash.Get("panels").MustArray() | 
					
						
							| 
									
										
										
										
											2016-12-08 17:25:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	newpanel := simplejson.NewFromAny(map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2017-10-11 01:48:06 +08:00
										 |  |  | 		"type": "gettingstarted", | 
					
						
							|  |  |  | 		"id":   123123, | 
					
						
							|  |  |  | 		"gridPos": map[string]interface{}{ | 
					
						
							|  |  |  | 			"x": 0, | 
					
						
							| 
									
										
										
										
											2019-01-28 22:01:42 +08:00
										 |  |  | 			"y": 3, | 
					
						
							| 
									
										
										
										
											2017-10-25 01:22:56 +08:00
										 |  |  | 			"w": 24, | 
					
						
							| 
									
										
										
										
											2020-05-13 14:00:40 +08:00
										 |  |  | 			"h": 9, | 
					
						
							| 
									
										
										
										
											2017-10-11 01:48:06 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-12-08 17:25:05 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	panels = append(panels, newpanel) | 
					
						
							| 
									
										
										
										
											2017-08-25 20:47:57 +08:00
										 |  |  | 	dash.Set("panels", panels) | 
					
						
							| 
									
										
										
										
											2016-12-08 17:25:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 05:57:09 +08:00
										 |  |  | // GetDashboardVersions returns all dashboard versions as JSON
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func GetDashboardVersions(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	dashID := c.ParamsInt64(":dashboardId") | 
					
						
							| 
									
										
										
										
											2017-06-14 06:28:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	guardian := guardian.New(dashID, c.OrgId, c.SignedInUser) | 
					
						
							| 
									
										
										
										
											2017-06-20 01:47:44 +08:00
										 |  |  | 	if canSave, err := guardian.CanSave(); err != nil || !canSave { | 
					
						
							|  |  |  | 		return dashboardGuardianResponse(err) | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.GetDashboardVersionsQuery{ | 
					
						
							| 
									
										
										
										
											2017-06-05 22:34:32 +08:00
										 |  |  | 		OrgId:       c.OrgId, | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		DashboardId: dashID, | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 		Limit:       c.QueryInt("limit"), | 
					
						
							|  |  |  | 		Start:       c.QueryInt("start"), | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, fmt.Sprintf("No versions found for dashboardId %d", dashID), err) | 
					
						
							| 
									
										
										
										
											2017-06-02 05:57:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 04:59:04 +08:00
										 |  |  | 	for _, version := range query.Result { | 
					
						
							|  |  |  | 		if version.RestoredFrom == version.Version { | 
					
						
							| 
									
										
										
										
											2017-06-07 05:04:14 +08:00
										 |  |  | 			version.Message = "Initial save (created by migration)" | 
					
						
							| 
									
										
										
										
											2017-06-06 04:59:04 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if version.RestoredFrom > 0 { | 
					
						
							|  |  |  | 			version.Message = fmt.Sprintf("Restored from version %d", version.RestoredFrom) | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if version.ParentVersion == 0 { | 
					
						
							|  |  |  | 			version.Message = "Initial save" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.JSON(200, query.Result) | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // GetDashboardVersion returns the dashboard version with the given ID.
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func GetDashboardVersion(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	dashID := c.ParamsInt64(":dashboardId") | 
					
						
							| 
									
										
										
										
											2017-06-14 06:28:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	guardian := guardian.New(dashID, c.OrgId, c.SignedInUser) | 
					
						
							| 
									
										
										
										
											2017-06-20 01:47:44 +08:00
										 |  |  | 	if canSave, err := guardian.CanSave(); err != nil || !canSave { | 
					
						
							|  |  |  | 		return dashboardGuardianResponse(err) | 
					
						
							| 
									
										
										
										
											2017-06-14 06:28:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.GetDashboardVersionQuery{ | 
					
						
							| 
									
										
										
										
											2017-06-05 22:34:32 +08:00
										 |  |  | 		OrgId:       c.OrgId, | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 		DashboardId: dashID, | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 		Version:     int(c.ParamsInt64(":id")), | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-05 22:34:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, fmt.Sprintf("Dashboard version %d not found for dashboardId %d", query.Version, dashID), err) | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-22 16:50:00 +08:00
										 |  |  | 	creator := anonString | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	if query.Result.CreatedBy > 0 { | 
					
						
							| 
									
										
										
										
											2021-06-21 13:51:33 +08:00
										 |  |  | 		creator = getUserLogin(c.Req.Context(), query.Result.CreatedBy) | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	dashVersionMeta := &models.DashboardVersionMeta{ | 
					
						
							| 
									
										
										
										
											2019-04-15 15:18:28 +08:00
										 |  |  | 		Id:            query.Result.Id, | 
					
						
							|  |  |  | 		DashboardId:   query.Result.DashboardId, | 
					
						
							|  |  |  | 		Data:          query.Result.Data, | 
					
						
							|  |  |  | 		ParentVersion: query.Result.ParentVersion, | 
					
						
							|  |  |  | 		RestoredFrom:  query.Result.RestoredFrom, | 
					
						
							|  |  |  | 		Version:       query.Result.Version, | 
					
						
							|  |  |  | 		Created:       query.Result.Created, | 
					
						
							|  |  |  | 		Message:       query.Result.Message, | 
					
						
							|  |  |  | 		CreatedBy:     creator, | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.JSON(200, dashVersionMeta) | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-07 17:50:09 +08:00
										 |  |  | // POST /api/dashboards/calculate-diff performs diffs on two dashboards
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func CalculateDashboardDiff(c *models.ReqContext, apiOptions dtos.CalculateDiffOptions) response.Response { | 
					
						
							| 
									
										
										
										
											2018-02-28 00:53:30 +08:00
										 |  |  | 	guardianBase := guardian.New(apiOptions.Base.DashboardId, c.OrgId, c.SignedInUser) | 
					
						
							|  |  |  | 	if canSave, err := guardianBase.CanSave(); err != nil || !canSave { | 
					
						
							|  |  |  | 		return dashboardGuardianResponse(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if apiOptions.Base.DashboardId != apiOptions.New.DashboardId { | 
					
						
							|  |  |  | 		guardianNew := guardian.New(apiOptions.New.DashboardId, c.OrgId, c.SignedInUser) | 
					
						
							|  |  |  | 		if canSave, err := guardianNew.CanSave(); err != nil || !canSave { | 
					
						
							|  |  |  | 			return dashboardGuardianResponse(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-07 17:50:09 +08:00
										 |  |  | 	options := dashdiffs.Options{ | 
					
						
							|  |  |  | 		OrgId:    c.OrgId, | 
					
						
							|  |  |  | 		DiffType: dashdiffs.ParseDiffType(apiOptions.DiffType), | 
					
						
							|  |  |  | 		Base: dashdiffs.DiffTarget{ | 
					
						
							|  |  |  | 			DashboardId:      apiOptions.Base.DashboardId, | 
					
						
							|  |  |  | 			Version:          apiOptions.Base.Version, | 
					
						
							|  |  |  | 			UnsavedDashboard: apiOptions.Base.UnsavedDashboard, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		New: dashdiffs.DiffTarget{ | 
					
						
							|  |  |  | 			DashboardId:      apiOptions.New.DashboardId, | 
					
						
							|  |  |  | 			Version:          apiOptions.New.Version, | 
					
						
							|  |  |  | 			UnsavedDashboard: apiOptions.New.UnsavedDashboard, | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-07 17:50:09 +08:00
										 |  |  | 	result, err := dashdiffs.CalculateDiff(&options) | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.Is(err, models.ErrDashboardVersionNotFound) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(404, "Dashboard version not found", err) | 
					
						
							| 
									
										
										
										
											2017-06-07 20:21:40 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Unable to compute diff", err) | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-06 05:29:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-07 17:50:09 +08:00
										 |  |  | 	if options.DiffType == dashdiffs.DiffDelta { | 
					
						
							| 
									
										
										
										
											2021-03-18 01:12:28 +08:00
										 |  |  | 		return response.Respond(200, result.Delta).SetHeader("Content-Type", "application/json") | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-28 00:53:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 01:12:28 +08:00
										 |  |  | 	return response.Respond(200, result.Delta).SetHeader("Content-Type", "text/html") | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // RestoreDashboardVersion restores a dashboard to the given version.
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) RestoreDashboardVersion(c *models.ReqContext, apiCmd dtos.RestoreDashboardVersionCommand) response.Response { | 
					
						
							| 
									
										
										
										
											2021-06-15 22:08:27 +08:00
										 |  |  | 	dash, rsp := getDashboardHelper(c.Req.Context(), c.OrgId, c.ParamsInt64(":dashboardId"), "") | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 	if rsp != nil { | 
					
						
							|  |  |  | 		return rsp | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 	guardian := guardian.New(dash.Id, c.OrgId, c.SignedInUser) | 
					
						
							| 
									
										
										
										
											2017-06-20 01:47:44 +08:00
										 |  |  | 	if canSave, err := guardian.CanSave(); err != nil || !canSave { | 
					
						
							|  |  |  | 		return dashboardGuardianResponse(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	versionQuery := models.GetDashboardVersionQuery{DashboardId: dash.Id, Version: apiCmd.Version, OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2017-06-05 23:45:27 +08:00
										 |  |  | 	if err := bus.Dispatch(&versionQuery); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Dashboard version not found", nil) | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 23:45:27 +08:00
										 |  |  | 	version := versionQuery.Result | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	saveCmd := models.SaveDashboardCommand{} | 
					
						
							| 
									
										
										
										
											2017-06-06 04:59:04 +08:00
										 |  |  | 	saveCmd.RestoredFrom = version.Version | 
					
						
							| 
									
										
										
										
											2017-06-05 23:45:27 +08:00
										 |  |  | 	saveCmd.OrgId = c.OrgId | 
					
						
							|  |  |  | 	saveCmd.UserId = c.UserId | 
					
						
							|  |  |  | 	saveCmd.Dashboard = version.Data | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 	saveCmd.Dashboard.Set("version", dash.Version) | 
					
						
							| 
									
										
										
										
											2018-02-01 01:16:45 +08:00
										 |  |  | 	saveCmd.Dashboard.Set("uid", dash.Uid) | 
					
						
							| 
									
										
										
										
											2017-06-06 04:59:04 +08:00
										 |  |  | 	saveCmd.Message = fmt.Sprintf("Restored from version %d", version.Version) | 
					
						
							| 
									
										
										
										
											2019-03-06 21:38:40 +08:00
										 |  |  | 	saveCmd.FolderId = dash.FolderId | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 04:12:01 +08:00
										 |  |  | 	return hs.PostDashboard(c, saveCmd) | 
					
						
							| 
									
										
											  
											
												History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
  version, along with a dashboard version model and structs to represent
  the queries and commands necessary for the dashboard version API
  methods.
- API endpoints were created to support working with dashboard
  versions.
- Methods were added to create, update, read, and destroy dashboard
  versions in the database.
  - Logic was added to compute the diff between two versions, and
  display it to the user.
  - The dashboard migration logic was updated to save a "Version
  1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
  are `version`, `created`, `created_by`, `message`. Default
  is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
  "dashboardId": <int>,
  "version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
  was added and vendored.
											
										 
											2017-05-25 07:14:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | func GetDashboardTags(c *models.ReqContext) { | 
					
						
							|  |  |  | 	query := models.GetDashboardTagsQuery{OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2015-05-13 16:45:53 +08:00
										 |  |  | 	err := bus.Dispatch(&query) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		c.JsonApiErr(500, "Failed to get tags from database", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.JSON(200, query.Result) | 
					
						
							|  |  |  | } |