| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2019-04-30 19:32:18 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2021-11-17 17:57:37 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2022-05-17 00:59:02 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-28 17:28:33 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/apierrors" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
											  
											
												Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
											
										 
											2022-11-11 04:36:40 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/kinds/dashboard" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2022-04-04 22:57:43 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/accesscontrol" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/alerting" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards" | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	dashver "github.com/grafana/grafana/pkg/services/dashboardversion" | 
					
						
							| 
									
										
										
										
											2022-05-22 08:44:12 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/featuremgmt" | 
					
						
							| 
									
										
										
										
											2022-11-11 21:28:24 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/folder" | 
					
						
							| 
									
										
										
										
											2017-06-12 21:48:55 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/guardian" | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/org" | 
					
						
							| 
									
										
										
										
											2022-04-21 21:03:17 +08:00
										 |  |  | 	pref "github.com/grafana/grafana/pkg/services/preference" | 
					
						
							| 
									
										
										
										
											2022-11-04 03:30:12 +08:00
										 |  |  | 	publicdashboardModels "github.com/grafana/grafana/pkg/services/publicdashboards/models" | 
					
						
							| 
									
										
										
										
											2022-05-19 20:32:10 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/star" | 
					
						
							| 
									
										
										
										
											2022-08-02 22:58:05 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/user" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-22 16:50:00 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	anonString = "Anonymous" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 19:43:43 +08:00
										 |  |  | func (hs *HTTPServer) isDashboardStarredByUser(c *models.ReqContext, dashID int64) (bool, error) { | 
					
						
							| 
									
										
										
										
											2015-02-02 18:32:32 +08:00
										 |  |  | 	if !c.IsSignedIn { | 
					
						
							|  |  |  | 		return false, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	query := star.IsStarredByUserQuery{UserID: c.UserID, DashboardID: dashID} | 
					
						
							| 
									
										
										
										
											2022-05-19 20:32:10 +08:00
										 |  |  | 	return hs.starService.IsStarredByUser(c.Req.Context(), &query) | 
					
						
							| 
									
										
										
										
											2015-02-02 18:32:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route POST /dashboards/trim dashboards trimDashboard
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Trim defaults from dashboard.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: trimDashboardResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) TrimDashboard(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2023-01-18 20:52:41 +08:00
										 |  |  | 	cmd := dashboards.TrimDashboardCommand{} | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-04-28 19:38:33 +08:00
										 |  |  | 	dash := cmd.Dashboard | 
					
						
							|  |  |  | 	meta := cmd.Meta | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-07 08:52:44 +08:00
										 |  |  | 	// TODO temporarily just return the input as a no-op while we convert to thema calls
 | 
					
						
							| 
									
										
										
										
											2021-04-28 19:38:33 +08:00
										 |  |  | 	dto := dtos.TrimDashboardFullWithMeta{ | 
					
						
							| 
									
										
										
										
											2022-06-07 08:52:44 +08:00
										 |  |  | 		Dashboard: dash, | 
					
						
							| 
									
										
										
										
											2021-04-28 19:38:33 +08:00
										 |  |  | 		Meta:      meta, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.TimeRequest(metrics.MApiDashboardGet) | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, dto) | 
					
						
							| 
									
										
										
										
											2021-04-28 19:38:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route GET /dashboards/uid/{uid} dashboards getDashboardByUID
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Get dashboard by uid.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Will return the dashboard given the dashboard unique identifier (uid).
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: dashboardResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) GetDashboard(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	uid := web.Params(c.Req)[":uid"] | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	dash, rsp := hs.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
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-07-20 07:44:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2022-12-22 23:31:54 +08:00
										 |  |  | 		publicDashboardEnabled = false | 
					
						
							|  |  |  | 		err                    error | 
					
						
							| 
									
										
										
										
											2022-07-20 07:44:41 +08:00
										 |  |  | 	) | 
					
						
							| 
									
										
										
										
											2022-11-04 03:30:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// If public dashboards is enabled and we have a public dashboard, update meta
 | 
					
						
							|  |  |  | 	// values
 | 
					
						
							| 
									
										
										
										
											2022-07-20 07:44:41 +08:00
										 |  |  | 	if hs.Features.IsEnabled(featuremgmt.FlagPublicDashboards) { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		publicDashboard, err := hs.PublicDashboardsApi.PublicDashboardService.FindByDashboardUid(c.Req.Context(), c.OrgID, dash.UID) | 
					
						
							| 
									
										
										
										
											2022-11-04 03:30:12 +08:00
										 |  |  | 		if err != nil && !errors.Is(err, publicdashboardModels.ErrPublicDashboardNotFound) { | 
					
						
							| 
									
										
										
										
											2022-07-20 07:44:41 +08:00
										 |  |  | 			return response.Error(500, "Error while retrieving public dashboards", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-11-04 03:30:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if publicDashboard != nil { | 
					
						
							|  |  |  | 			publicDashboardEnabled = publicDashboard.IsEnabled | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-07-20 07:44:41 +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
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 	guardian, err := guardian.NewByDashboard(c.Req.Context(), dash, c.OrgID, c.SignedInUser) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Err(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							| 
									
										
										
										
											2022-03-03 22:05:47 +08:00
										 |  |  | 	canDelete, _ := guardian.CanDelete() | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	isStarred, err := hs.isDashboardStarredByUser(c, dash.ID) | 
					
						
							| 
									
										
										
										
											2017-06-12 21:48:55 +08:00
										 |  |  | 	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
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2022-02-07 19:43:43 +08:00
										 |  |  | 		updater = hs.getUserLogin(c.Req.Context(), dash.UpdatedBy) | 
					
						
							| 
									
										
										
										
											2016-01-28 14:00:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if dash.CreatedBy > 0 { | 
					
						
							| 
									
										
										
										
											2022-02-07 19:43:43 +08:00
										 |  |  | 		creator = hs.getUserLogin(c.Req.Context(), dash.CreatedBy) | 
					
						
							| 
									
										
										
										
											2016-01-28 14:00:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-18 19:38:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-04 22:57:43 +08:00
										 |  |  | 	annotationPermissions := &dtos.AnnotationPermission{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !hs.AccessControl.IsDisabled() { | 
					
						
							|  |  |  | 		hs.getAnnotationPermissionsByScope(c, &annotationPermissions.Dashboard, accesscontrol.ScopeAnnotationsTypeDashboard) | 
					
						
							|  |  |  | 		hs.getAnnotationPermissionsByScope(c, &annotationPermissions.Organization, accesscontrol.ScopeAnnotationsTypeOrganization) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 	meta := dtos.DashboardMeta{ | 
					
						
							| 
									
										
										
										
											2022-12-22 23:31:54 +08:00
										 |  |  | 		IsStarred:              isStarred, | 
					
						
							|  |  |  | 		Slug:                   dash.Slug, | 
					
						
							| 
									
										
										
										
											2023-01-18 20:52:41 +08:00
										 |  |  | 		Type:                   dashboards.DashTypeDB, | 
					
						
							| 
									
										
										
										
											2022-12-22 23:31:54 +08:00
										 |  |  | 		CanStar:                c.IsSignedIn, | 
					
						
							|  |  |  | 		CanSave:                canSave, | 
					
						
							|  |  |  | 		CanEdit:                canEdit, | 
					
						
							|  |  |  | 		CanAdmin:               canAdmin, | 
					
						
							|  |  |  | 		CanDelete:              canDelete, | 
					
						
							|  |  |  | 		Created:                dash.Created, | 
					
						
							|  |  |  | 		Updated:                dash.Updated, | 
					
						
							|  |  |  | 		UpdatedBy:              updater, | 
					
						
							|  |  |  | 		CreatedBy:              creator, | 
					
						
							|  |  |  | 		Version:                dash.Version, | 
					
						
							|  |  |  | 		HasACL:                 dash.HasACL, | 
					
						
							|  |  |  | 		IsFolder:               dash.IsFolder, | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		FolderId:               dash.FolderID, | 
					
						
							|  |  |  | 		Url:                    dash.GetURL(), | 
					
						
							| 
									
										
										
										
											2022-12-22 23:31:54 +08:00
										 |  |  | 		FolderTitle:            "General", | 
					
						
							|  |  |  | 		AnnotationsPermissions: annotationPermissions, | 
					
						
							|  |  |  | 		PublicDashboardEnabled: publicDashboardEnabled, | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// lookup folder title
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	if dash.FolderID > 0 { | 
					
						
							|  |  |  | 		query := dashboards.GetDashboardQuery{ID: dash.FolderID, OrgID: c.OrgID} | 
					
						
							| 
									
										
										
										
											2022-07-07 02:42:39 +08:00
										 |  |  | 		if err := hs.DashboardService.GetDashboard(c.Req.Context(), &query); err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-30 21:31:54 +08:00
										 |  |  | 			if errors.Is(err, dashboards.ErrFolderNotFound) { | 
					
						
							| 
									
										
										
										
											2021-05-26 22:20:13 +08:00
										 |  |  | 				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
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		meta.FolderUid = query.Result.UID | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 		meta.FolderTitle = query.Result.Title | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		meta.FolderUrl = query.Result.GetURL() | 
					
						
							| 
									
										
										
										
											2017-06-17 04:57:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	provisioningData, err := hs.dashboardProvisioningService.GetProvisionedDashboardDataByDashboardID(c.Req.Context(), 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), | 
					
						
							| 
									
										
										
										
											2023-01-18 20:52:41 +08:00
										 |  |  | 			provisioningData.ExternalID, | 
					
						
							| 
									
										
										
										
											2019-04-30 19:32:18 +08:00
										 |  |  | 		) | 
					
						
							|  |  |  | 		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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-08 02:31:45 +08:00
										 |  |  | 	if hs.QueryLibraryService != nil && !hs.QueryLibraryService.IsDisabled() { | 
					
						
							|  |  |  | 		if err := hs.QueryLibraryService.UpdateDashboardQueries(c.Req.Context(), c.SignedInUser, dash); err != nil { | 
					
						
							|  |  |  | 			return response.Error(500, "Error while loading saved queries", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, dto) | 
					
						
							| 
									
										
										
										
											2017-06-12 21:48:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-04 22:57:43 +08:00
										 |  |  | func (hs *HTTPServer) getAnnotationPermissionsByScope(c *models.ReqContext, actions *dtos.AnnotationActions, scope string) { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-05 19:20:10 +08:00
										 |  |  | 	evaluate := accesscontrol.EvalPermission(accesscontrol.ActionAnnotationsCreate, scope) | 
					
						
							|  |  |  | 	actions.CanAdd, err = hs.AccessControl.Evaluate(c.Req.Context(), c.SignedInUser, evaluate) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		hs.log.Warn("Failed to evaluate permission", "err", err, "action", accesscontrol.ActionAnnotationsCreate, "scope", scope) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	evaluate = accesscontrol.EvalPermission(accesscontrol.ActionAnnotationsDelete, scope) | 
					
						
							| 
									
										
										
										
											2022-04-04 22:57:43 +08:00
										 |  |  | 	actions.CanDelete, err = hs.AccessControl.Evaluate(c.Req.Context(), c.SignedInUser, evaluate) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		hs.log.Warn("Failed to evaluate permission", "err", err, "action", accesscontrol.ActionAnnotationsDelete, "scope", scope) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	evaluate = accesscontrol.EvalPermission(accesscontrol.ActionAnnotationsWrite, scope) | 
					
						
							|  |  |  | 	actions.CanEdit, err = hs.AccessControl.Evaluate(c.Req.Context(), c.SignedInUser, evaluate) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		hs.log.Warn("Failed to evaluate permission", "err", err, "action", accesscontrol.ActionAnnotationsWrite, "scope", scope) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 19:43:43 +08:00
										 |  |  | func (hs *HTTPServer) getUserLogin(ctx context.Context, userID int64) string { | 
					
						
							| 
									
										
										
										
											2022-08-02 22:58:05 +08:00
										 |  |  | 	query := user.GetUserByIDQuery{ID: userID} | 
					
						
							|  |  |  | 	user, err := hs.userService.GetByID(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
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-08-02 22:58:05 +08:00
										 |  |  | 	return user.Login | 
					
						
							| 
									
										
										
										
											2016-01-28 13:55:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | func (hs *HTTPServer) getDashboardHelper(ctx context.Context, orgID int64, id int64, uid string) (*dashboards.Dashboard, response.Response) { | 
					
						
							|  |  |  | 	var query dashboards.GetDashboardQuery | 
					
						
							| 
									
										
										
										
											2018-01-30 04:23:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if len(uid) > 0 { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		query = dashboards.GetDashboardQuery{UID: uid, ID: id, OrgID: orgID} | 
					
						
							| 
									
										
										
										
											2018-01-30 04:23:07 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		query = dashboards.GetDashboardQuery{ID: id, OrgID: orgID} | 
					
						
							| 
									
										
										
										
											2018-01-30 04:23:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 02:42:39 +08:00
										 |  |  | 	if err := hs.DashboardService.GetDashboard(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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // DeleteDashboardByUID swagger:route DELETE /dashboards/uid/{uid} dashboards deleteDashboardByUID
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Delete dashboard by uid.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Will delete the dashboard given the specified unique identifier (uid).
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: deleteDashboardResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	dash, rsp := hs.getDashboardHelper(c.Req.Context(), c.OrgID, 0, web.Params(c.Req)[":uid"]) | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 	if rsp != nil { | 
					
						
							|  |  |  | 		return rsp | 
					
						
							| 
									
										
										
										
											2017-06-13 05:05:32 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 	guardian, err := guardian.NewByDashboard(c.Req.Context(), dash, c.OrgID, c.SignedInUser) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Err(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-03 22:05:47 +08:00
										 |  |  | 	if canDelete, err := guardian.CanDelete(); err != nil || !canDelete { | 
					
						
							| 
									
										
										
										
											2017-06-20 01:47:44 +08:00
										 |  |  | 		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
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	err = hs.LibraryElementService.DisconnectElementsFromDashboard(c.Req.Context(), dash.ID) | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		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
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-02-16 21:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	err = hs.DashboardService.DeleteDashboard(c.Req.Context(), dash.ID, c.OrgID) | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-30 21:31:54 +08:00
										 |  |  | 		var dashboardErr dashboards.DashboardErr | 
					
						
							| 
									
										
										
										
											2020-07-21 17:12:01 +08:00
										 |  |  | 		if ok := errors.As(err, &dashboardErr); ok { | 
					
						
							| 
									
										
										
										
											2022-06-30 21:31:54 +08:00
										 |  |  | 			if errors.Is(err, dashboards.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
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-04-27 16:29:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-04 23:44:55 +08:00
										 |  |  | 	if hs.Live != nil { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +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 { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 			hs.log.Error("Failed to broadcast delete info", "dashboard", dash.UID, "error", err) | 
					
						
							| 
									
										
										
										
											2021-04-24 03:55:31 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2018-02-21 23:38:09 +08:00
										 |  |  | 		"title":   dash.Title, | 
					
						
							|  |  |  | 		"message": fmt.Sprintf("Dashboard %s deleted", dash.Title), | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		"id":      dash.ID, | 
					
						
							| 
									
										
										
										
											2018-02-21 23:38:09 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route POST /dashboards/db dashboards postDashboard
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Create / Update dashboard
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Creates a new dashboard or updates an existing dashboard.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: postDashboardResponse
 | 
					
						
							|  |  |  | // 400: badRequestError
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 412: preconditionFailedError
 | 
					
						
							|  |  |  | // 422: unprocessableEntityError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) PostDashboard(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	cmd := dashboards.SaveDashboardCommand{} | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-05-22 08:44:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if hs.Features.IsEnabled(featuremgmt.FlagValidateDashboardsOnSave) { | 
					
						
							| 
									
										
											  
											
												Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
											
										 
											2022-11-11 04:36:40 +08:00
										 |  |  | 		kind := hs.Kinds.Dashboard() | 
					
						
							| 
									
										
										
										
											2022-06-15 21:47:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
											
										 
											2022-11-11 04:36:40 +08:00
										 |  |  | 		dashbytes, err := cmd.Dashboard.Bytes() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return response.Error(http.StatusBadRequest, "unable to parse dashboard", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-05-22 08:44:12 +08:00
										 |  |  | 		// Ideally, coremodel validation calls would be integrated into the web
 | 
					
						
							|  |  |  | 		// framework. But this does the job for now.
 | 
					
						
							| 
									
										
										
										
											2022-06-15 21:47:04 +08:00
										 |  |  | 		schv, err := cmd.Dashboard.Get("schemaVersion").Int() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Only try to validate if the schemaVersion is at least the handoff version
 | 
					
						
							|  |  |  | 		// (the minimum schemaVersion against which the dashboard schema is known to
 | 
					
						
							| 
									
										
											  
											
												Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
											
										 
											2022-11-11 04:36:40 +08:00
										 |  |  | 		// work), or if schemaVersion is absent (which will happen once the kind schema
 | 
					
						
							|  |  |  | 		// becomes canonical).
 | 
					
						
							| 
									
										
										
										
											2022-06-15 21:47:04 +08:00
										 |  |  | 		if err != nil || schv >= dashboard.HandoffSchemaVersion { | 
					
						
							| 
									
										
											  
											
												Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
											
										 
											2022-11-11 04:36:40 +08:00
										 |  |  | 			if _, _, err := kind.JSONValueMux(dashbytes); err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-15 21:47:04 +08:00
										 |  |  | 				return response.Error(http.StatusBadRequest, "invalid dashboard json", err) | 
					
						
							| 
									
										
										
										
											2022-05-22 08:44:12 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	return hs.postDashboard(c, cmd) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | func (hs *HTTPServer) postDashboard(c *models.ReqContext, cmd dashboards.SaveDashboardCommand) response.Response { | 
					
						
							| 
									
										
										
										
											2021-09-14 22:08:04 +08:00
										 |  |  | 	ctx := c.Req.Context() | 
					
						
							| 
									
										
										
										
											2021-04-28 19:38:33 +08:00
										 |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	cmd.OrgID = c.OrgID | 
					
						
							|  |  |  | 	cmd.UserID = c.UserID | 
					
						
							|  |  |  | 	if cmd.FolderUID != "" { | 
					
						
							| 
									
										
										
										
											2022-11-11 21:28:24 +08:00
										 |  |  | 		folder, err := hs.folderService.Get(ctx, &folder.GetFolderQuery{ | 
					
						
							| 
									
										
										
										
											2022-11-23 17:13:47 +08:00
										 |  |  | 			OrgID:        c.OrgID, | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 			UID:          &cmd.FolderUID, | 
					
						
							| 
									
										
										
										
											2022-11-23 17:13:47 +08:00
										 |  |  | 			SignedInUser: c.SignedInUser, | 
					
						
							| 
									
										
										
										
											2022-11-11 21:28:24 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2021-05-26 22:20:13 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-30 21:31:54 +08:00
										 |  |  | 			if errors.Is(err, dashboards.ErrFolderNotFound) { | 
					
						
							| 
									
										
										
										
											2021-05-26 22:20:13 +08:00
										 |  |  | 				return response.Error(400, "Folder not found", err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return response.Error(500, "Error while checking folder ID", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		cmd.FolderID = folder.ID | 
					
						
							| 
									
										
										
										
											2021-05-26 22:20:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-20 22:38:25 +08:00
										 |  |  | 	dash := cmd.GetDashboardModel() | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	newDashboard := dash.ID == 0 | 
					
						
							| 
									
										
										
										
											2019-03-05 23:44:41 +08:00
										 |  |  | 	if newDashboard { | 
					
						
							| 
									
										
										
										
											2022-11-15 03:08:10 +08:00
										 |  |  | 		limitReached, err := hs.QuotaService.QuotaReached(c, dashboards.QuotaTargetSrv) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 20:52:41 +08:00
										 |  |  | 	var provisioningData *dashboards.DashboardProvisioning | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	if dash.ID != 0 { | 
					
						
							|  |  |  | 		data, err := hs.dashboardProvisioningService.GetProvisionedDashboardDataByDashboardID(c.Req.Context(), dash.ID) | 
					
						
							| 
									
										
										
										
											2021-12-17 23:31:52 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-12 00:39:53 +08:00
										 |  |  | 			return response.Error(500, "Error while checking if dashboard is provisioned using ID", err) | 
					
						
							| 
									
										
										
										
											2021-12-17 23:31:52 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		provisioningData = data | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	} else if dash.UID != "" { | 
					
						
							|  |  |  | 		data, err := hs.dashboardProvisioningService.GetProvisionedDashboardDataByDashboardUID(c.Req.Context(), dash.OrgID, dash.UID) | 
					
						
							| 
									
										
										
										
											2022-06-30 21:31:54 +08:00
										 |  |  | 		if err != nil && !errors.Is(err, dashboards.ErrProvisionedDashboardNotFound) && !errors.Is(err, dashboards.ErrDashboardNotFound) { | 
					
						
							| 
									
										
										
										
											2022-02-16 21:15:44 +08:00
										 |  |  | 			return response.Error(500, "Error while checking if dashboard is provisioned", err) | 
					
						
							| 
									
										
										
										
											2021-12-17 23:31:52 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		provisioningData = data | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 19:28:56 +08:00
										 |  |  | 	dashItem := &dashboards.SaveDashboardDTO{ | 
					
						
							| 
									
										
										
										
											2017-12-01 20:50:47 +08:00
										 |  |  | 		Dashboard: dash, | 
					
						
							|  |  |  | 		Message:   cmd.Message, | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 02:42:39 +08:00
										 |  |  | 	dashboard, err := hs.DashboardService.SaveDashboard(alerting.WithUAEnabled(ctx, hs.Cfg.UnifiedAlerting.IsEnabled()), 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
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 16:33:01 +08:00
										 |  |  | 		// This will broadcast all save requests only if a `gitops` observer exists.
 | 
					
						
							| 
									
										
										
										
											2021-04-24 03:55:31 +08:00
										 |  |  | 		// gitops is useful when trying to save dashboards in an environment where the user can not save
 | 
					
						
							|  |  |  | 		channel := hs.Live.GrafanaScope.Dashboards | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +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
 | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +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 { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 			hs.log.Warn("unable to broadcast save event", "uid", dashboard.UID, "error", liveerr) | 
					
						
							| 
									
										
										
										
											2021-04-24 03:55:31 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-04 23:44:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-20 18:42:47 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-01-28 17:28:33 +08:00
										 |  |  | 		return apierrors.ToDashboardErrorResponse(ctx, hs.pluginStore, err) | 
					
						
							| 
									
										
										
										
											2019-12-20 18:42:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-24 22:38:55 +08:00
										 |  |  | 	// Clear permission cache for the user who's created the dashboard, so that new permissions are fetched for their next call
 | 
					
						
							|  |  |  | 	// Required for cases when caller wants to immediately interact with the newly created object
 | 
					
						
							|  |  |  | 	if newDashboard && !hs.accesscontrolService.IsDisabled() { | 
					
						
							|  |  |  | 		hs.accesscontrolService.ClearUserPermissionCache(c.SignedInUser) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 	// connect library panels for this dashboard after the dashboard is stored and has an ID
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	err = hs.LibraryPanelService.ConnectLibraryPanelsForDashboard(ctx, c.SignedInUser, dashboard) | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 	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) | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{ | 
					
						
							| 
									
										
										
										
											2019-12-20 18:42:47 +08:00
										 |  |  | 		"status":  "success", | 
					
						
							|  |  |  | 		"slug":    dashboard.Slug, | 
					
						
							|  |  |  | 		"version": dashboard.Version, | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		"id":      dashboard.ID, | 
					
						
							|  |  |  | 		"uid":     dashboard.UID, | 
					
						
							|  |  |  | 		"url":     dashboard.GetURL(), | 
					
						
							| 
									
										
										
										
											2019-12-20 18:42:47 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-01 20:50:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route GET /dashboards/home dashboards getHomeDashboard
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Get home dashboard.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: getHomeDashboardResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) GetHomeDashboard(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	prefsQuery := pref.GetPreferenceWithDefaultsQuery{OrgID: c.OrgID, UserID: c.SignedInUser.UserID, Teams: c.Teams} | 
					
						
							| 
									
										
										
										
											2021-04-13 21:27:51 +08:00
										 |  |  | 	homePage := hs.Cfg.HomePage | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-21 21:03:17 +08:00
										 |  |  | 	preference, err := hs.preferenceService.GetWithDefaults(c.Req.Context(), &prefsQuery) | 
					
						
							|  |  |  | 	if 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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-21 21:03:17 +08:00
										 |  |  | 	if preference.HomeDashboardID == 0 && len(homePage) > 0 { | 
					
						
							| 
									
										
										
										
											2021-04-13 21:27:51 +08:00
										 |  |  | 		homePageRedirect := dtos.DashboardRedirect{RedirectUri: homePage} | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 		return response.JSON(http.StatusOK, &homePageRedirect) | 
					
						
							| 
									
										
										
										
											2021-04-13 21:27:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-21 21:03:17 +08:00
										 |  |  | 	if preference.HomeDashboardID != 0 { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		slugQuery := dashboards.GetDashboardRefByIDQuery{ID: preference.HomeDashboardID} | 
					
						
							|  |  |  | 		err := hs.DashboardService.GetDashboardUIDByID(c.Req.Context(), &slugQuery) | 
					
						
							| 
									
										
										
										
											2016-05-24 13:39:58 +08:00
										 |  |  | 		if err == nil { | 
					
						
							| 
									
										
										
										
											2023-01-18 20:52:41 +08:00
										 |  |  | 			url := dashboards.GetDashboardURL(slugQuery.Result.UID, slugQuery.Result.Slug) | 
					
						
							| 
									
										
										
										
											2018-02-05 17:24:48 +08:00
										 |  |  | 			dashRedirect := dtos.DashboardRedirect{RedirectUri: url} | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 			return response.JSON(http.StatusOK, &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{} | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 	dash.Meta.CanEdit = c.SignedInUser.HasRole(org.RoleEditor) | 
					
						
							| 
									
										
										
										
											2018-02-02 17:33:31 +08:00
										 |  |  | 	dash.Meta.FolderTitle = "General" | 
					
						
							| 
									
										
										
										
											2022-04-12 15:30:34 +08:00
										 |  |  | 	dash.Dashboard = simplejson.New() | 
					
						
							| 
									
										
										
										
											2017-06-17 09:25:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 	jsonParser := json.NewDecoder(file) | 
					
						
							| 
									
										
										
										
											2022-04-12 15:30:34 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, &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
 | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 	if !c.HasUserRole(org.RoleAdmin) || | 
					
						
							|  |  |  | 		c.HasHelpFlag(user.HelpFlagGettingStartedPanelDismissed) || | 
					
						
							| 
									
										
										
										
											2020-11-02 23:12:22 +08:00
										 |  |  | 		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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route GET /dashboards/id/{DashboardID}/versions dashboard_versions getDashboardVersionsByID
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Gets all existing versions for the dashboard.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Please refer to [updated API](#/dashboard_versions/getDashboardVersionsByUID) instead
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Deprecated: true
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: dashboardVersionsResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:route GET /dashboards/uid/{uid}/versions dashboard_versions getDashboardVersionsByUID
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Gets all existing versions for the dashboard using UID.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: dashboardVersionsResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2022-02-07 19:43:43 +08:00
										 |  |  | func (hs *HTTPServer) GetDashboardVersions(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-05-18 00:57:27 +08:00
										 |  |  | 	var dashID int64 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	dashUID := web.Params(c.Req)[":uid"] | 
					
						
							| 
									
										
										
										
											2017-06-14 06:28:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-18 00:57:27 +08:00
										 |  |  | 	if dashUID == "" { | 
					
						
							|  |  |  | 		dashID, err = strconv.ParseInt(web.Params(c.Req)[":dashboardId"], 10, 64) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return response.Error(http.StatusBadRequest, "dashboardId is invalid", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	dash, rsp := hs.getDashboardHelper(c.Req.Context(), c.OrgID, dashID, dashUID) | 
					
						
							|  |  |  | 	if rsp != nil { | 
					
						
							|  |  |  | 		return rsp | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	guardian, err := guardian.NewByDashboard(c.Req.Context(), dash, c.OrgID, c.SignedInUser) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Err(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-02 21:59:05 +08:00
										 |  |  | 	query := dashver.ListDashboardVersionsQuery{ | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 		OrgID:        c.OrgID, | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		DashboardID:  dash.ID, | 
					
						
							|  |  |  | 		DashboardUID: dash.UID, | 
					
						
							| 
									
										
										
										
											2022-05-18 00:57:27 +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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-02 21:59:05 +08:00
										 |  |  | 	res, err := hs.dashboardVersionService.List(c.Req.Context(), &query) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		return response.Error(404, fmt.Sprintf("No versions found for dashboardId %d", dash.ID), err) | 
					
						
							| 
									
										
										
										
											2017-06-02 05:57:09 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-02 21:59:05 +08:00
										 |  |  | 	for _, version := range res { | 
					
						
							| 
									
										
										
										
											2017-06-06 04:59:04 +08:00
										 |  |  | 		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" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-02 21:59:05 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, res) | 
					
						
							| 
									
										
											  
											
												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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route GET /dashboards/id/{DashboardID}/versions/{DashboardVersionID} dashboard_versions getDashboardVersionByID
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Get a specific dashboard version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Please refer to [updated API](#/dashboard_versions/getDashboardVersionByUID) instead
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Deprecated: true
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: dashboardVersionResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:route GET /dashboards/uid/{uid}/versions/{DashboardVersionID} dashboard_versions getDashboardVersionByUID
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Get a specific dashboard version using UID.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: dashboardVersionResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2022-02-07 19:43:43 +08:00
										 |  |  | func (hs *HTTPServer) GetDashboardVersion(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-05-17 18:59:02 +08:00
										 |  |  | 	var dashID int64 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	dashUID := web.Params(c.Req)[":uid"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	var dash *dashboards.Dashboard | 
					
						
							| 
									
										
										
										
											2022-05-17 18:59:02 +08:00
										 |  |  | 	if dashUID == "" { | 
					
						
							|  |  |  | 		dashID, err = strconv.ParseInt(web.Params(c.Req)[":dashboardId"], 10, 64) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return response.Error(http.StatusBadRequest, "dashboardId is invalid", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-01-15 00:55:57 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-14 06:28:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 	dash, rsp := hs.getDashboardHelper(c.Req.Context(), c.OrgID, dashID, dashUID) | 
					
						
							|  |  |  | 	if rsp != nil { | 
					
						
							|  |  |  | 		return rsp | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	guardian, err := guardian.NewByDashboard(c.Req.Context(), dash, c.OrgID, c.SignedInUser) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Err(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 22:56:14 +08:00
										 |  |  | 	version, _ := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 32) | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	query := dashver.GetDashboardVersionQuery{ | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 		OrgID:       c.OrgID, | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		DashboardID: dash.ID, | 
					
						
							| 
									
										
										
										
											2021-11-17 17:57:37 +08:00
										 |  |  | 		Version:     int(version), | 
					
						
							| 
									
										
											  
											
												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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	res, err := hs.dashboardVersionService.Get(c.Req.Context(), &query) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		return response.Error(500, fmt.Sprintf("Dashboard version %d not found for dashboardId %d", query.Version, dash.ID), 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 | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	if res.CreatedBy > 0 { | 
					
						
							|  |  |  | 		creator = hs.getUserLogin(c.Req.Context(), res.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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 18:22:55 +08:00
										 |  |  | 	dashVersionMeta := &dashver.DashboardVersionMeta{ | 
					
						
							|  |  |  | 		ID:            res.ID, | 
					
						
							|  |  |  | 		DashboardID:   res.DashboardID, | 
					
						
							| 
									
										
										
										
											2022-05-17 18:59:02 +08:00
										 |  |  | 		DashboardUID:  dashUID, | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 		Data:          res.Data, | 
					
						
							|  |  |  | 		ParentVersion: res.ParentVersion, | 
					
						
							|  |  |  | 		RestoredFrom:  res.RestoredFrom, | 
					
						
							|  |  |  | 		Version:       res.Version, | 
					
						
							|  |  |  | 		Created:       res.Created, | 
					
						
							|  |  |  | 		Message:       res.Message, | 
					
						
							| 
									
										
										
										
											2019-04-15 15:18:28 +08:00
										 |  |  | 		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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-14 21:51:05 +08:00
										 |  |  | // swagger:route POST /dashboards/validate dashboards alpha validateDashboard
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Validates a dashboard JSON against the schema.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Produces:
 | 
					
						
							|  |  |  | // - application/json
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: validateDashboardResponse
 | 
					
						
							|  |  |  | // 412: validateDashboardResponse
 | 
					
						
							|  |  |  | // 422: validateDashboardResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | func (hs *HTTPServer) ValidateDashboard(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2023-01-18 20:52:41 +08:00
										 |  |  | 	cmd := dashboards.ValidateDashboardCommand{} | 
					
						
							| 
									
										
										
										
											2022-10-14 21:51:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
											
										 
											2022-11-11 04:36:40 +08:00
										 |  |  | 	dk := hs.Kinds.Dashboard() | 
					
						
							| 
									
										
										
										
											2022-10-14 21:51:05 +08:00
										 |  |  | 	dashboardBytes := []byte(cmd.Dashboard) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// POST api receives dashboard as a string of json (so line numbers for errors stay consistent),
 | 
					
						
							|  |  |  | 	// but we need to parse the schema version out of it
 | 
					
						
							|  |  |  | 	dashboardJson, err := simplejson.NewJson(dashboardBytes) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "unable to parse dashboard", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	schemaVersion, err := dashboardJson.Get("schemaVersion").Int() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isValid := false | 
					
						
							|  |  |  | 	statusCode := http.StatusOK | 
					
						
							|  |  |  | 	validationMessage := "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Only try to validate if the schemaVersion is at least the handoff version
 | 
					
						
							|  |  |  | 	// (the minimum schemaVersion against which the dashboard schema is known to
 | 
					
						
							|  |  |  | 	// work), or if schemaVersion is absent (which will happen once the Thema
 | 
					
						
							|  |  |  | 	// schema becomes canonical).
 | 
					
						
							|  |  |  | 	if err != nil || schemaVersion >= dashboard.HandoffSchemaVersion { | 
					
						
							| 
									
										
											  
											
												Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
											
										 
											2022-11-11 04:36:40 +08:00
										 |  |  | 		_, _, validationErr := dk.JSONValueMux(dashboardBytes) | 
					
						
							| 
									
										
										
										
											2022-10-14 21:51:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if validationErr == nil { | 
					
						
							|  |  |  | 			isValid = true | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			validationMessage = validationErr.Error() | 
					
						
							|  |  |  | 			statusCode = http.StatusUnprocessableEntity | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		validationMessage = "invalid schema version" | 
					
						
							|  |  |  | 		statusCode = http.StatusPreconditionFailed | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	respData := &ValidateDashboardResponse{ | 
					
						
							|  |  |  | 		IsValid: isValid, | 
					
						
							|  |  |  | 		Message: validationMessage, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(statusCode, respData) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route POST /dashboards/calculate-diff dashboards calculateDashboardDiff
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Perform diff on two dashboards.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Produces:
 | 
					
						
							|  |  |  | // - application/json
 | 
					
						
							|  |  |  | // - text/html
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: calculateDashboardDiffResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2022-02-10 16:58:52 +08:00
										 |  |  | func (hs *HTTPServer) CalculateDashboardDiff(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	apiOptions := dtos.CalculateDiffOptions{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &apiOptions); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 	guardianBase, err := guardian.New(c.Req.Context(), apiOptions.Base.DashboardId, c.OrgID, c.SignedInUser) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Err(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 00:53:30 +08:00
										 |  |  | 	if canSave, err := guardianBase.CanSave(); err != nil || !canSave { | 
					
						
							|  |  |  | 		return dashboardGuardianResponse(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if apiOptions.Base.DashboardId != apiOptions.New.DashboardId { | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 		guardianNew, err := guardian.New(c.Req.Context(), apiOptions.New.DashboardId, c.OrgID, c.SignedInUser) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return response.Err(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 00:53:30 +08:00
										 |  |  | 		if canSave, err := guardianNew.CanSave(); err != nil || !canSave { | 
					
						
							|  |  |  | 			return dashboardGuardianResponse(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-07 17:50:09 +08:00
										 |  |  | 	options := dashdiffs.Options{ | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 		OrgId:    c.OrgID, | 
					
						
							| 
									
										
										
										
											2017-06-07 17:50:09 +08:00
										 |  |  | 		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
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	baseVersionQuery := dashver.GetDashboardVersionQuery{ | 
					
						
							|  |  |  | 		DashboardID: options.Base.DashboardId, | 
					
						
							| 
									
										
										
										
											2022-02-10 16:58:52 +08:00
										 |  |  | 		Version:     options.Base.Version, | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 		OrgID:       options.OrgId, | 
					
						
							| 
									
										
										
										
											2022-02-10 16:58:52 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	baseVersionRes, err := hs.dashboardVersionService.Get(c.Req.Context(), &baseVersionQuery) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-08 18:22:55 +08:00
										 |  |  | 		if errors.Is(err, dashver.ErrDashboardVersionNotFound) { | 
					
						
							| 
									
										
										
										
											2022-02-10 16:58:52 +08:00
										 |  |  | 			return response.Error(404, "Dashboard version not found", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return response.Error(500, "Unable to compute diff", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	newVersionQuery := dashver.GetDashboardVersionQuery{ | 
					
						
							|  |  |  | 		DashboardID: options.New.DashboardId, | 
					
						
							| 
									
										
										
										
											2022-02-10 16:58:52 +08:00
										 |  |  | 		Version:     options.New.Version, | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 		OrgID:       options.OrgId, | 
					
						
							| 
									
										
										
										
											2022-02-10 16:58:52 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	newVersionRes, err := hs.dashboardVersionService.Get(c.Req.Context(), &newVersionQuery) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-08 18:22:55 +08:00
										 |  |  | 		if errors.Is(err, dashver.ErrDashboardVersionNotFound) { | 
					
						
							| 
									
										
										
										
											2022-02-10 16:58:52 +08:00
										 |  |  | 			return response.Error(404, "Dashboard version not found", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return response.Error(500, "Unable to compute diff", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	baseData := baseVersionRes.Data | 
					
						
							|  |  |  | 	newData := newVersionRes.Data | 
					
						
							| 
									
										
										
										
											2022-02-10 16:58:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	result, err := dashdiffs.CalculateDiff(c.Req.Context(), &options, baseData, newData) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												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 { | 
					
						
							| 
									
										
										
										
											2022-06-08 18:22:55 +08:00
										 |  |  | 		if errors.Is(err, dashver.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 { | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 		return response.Respond(http.StatusOK, 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.Respond(http.StatusOK, 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route POST /dashboards/id/{DashboardID}/restore dashboard_versions restoreDashboardVersionByID
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Restore a dashboard to a given dashboard version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Please refer to [updated API](#/dashboard_versions/restoreDashboardVersionByUID) instead
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Deprecated: true
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: postDashboardResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:route POST /dashboards/uid/{uid}/restore dashboard_versions restoreDashboardVersionByUID
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Restore a dashboard to a given dashboard version using UID.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: postDashboardResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) RestoreDashboardVersion(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-05-18 00:57:27 +08:00
										 |  |  | 	var dashID int64 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	dashUID := web.Params(c.Req)[":uid"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	apiCmd := dtos.RestoreDashboardVersionCommand{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &apiCmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-05-18 00:57:27 +08:00
										 |  |  | 	if dashUID == "" { | 
					
						
							|  |  |  | 		dashID, err = strconv.ParseInt(web.Params(c.Req)[":dashboardId"], 10, 64) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return response.Error(http.StatusBadRequest, "dashboardId is invalid", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-01-15 00:55:57 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	dash, rsp := hs.getDashboardHelper(c.Req.Context(), c.OrgID, dashID, dashUID) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 	guardian, err := guardian.NewByDashboard(c.Req.Context(), dash, c.OrgID, c.SignedInUser) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Err(err) | 
					
						
							| 
									
										
										
										
											2022-05-18 00:57:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-20 01:47:44 +08:00
										 |  |  | 	if canSave, err := guardian.CanSave(); err != nil || !canSave { | 
					
						
							|  |  |  | 		return dashboardGuardianResponse(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	versionQuery := dashver.GetDashboardVersionQuery{DashboardID: dashID, Version: apiCmd.Version, OrgID: c.OrgID} | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	version, err := hs.dashboardVersionService.Get(c.Req.Context(), &versionQuery) | 
					
						
							|  |  |  | 	if 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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	saveCmd := dashboards.SaveDashboardCommand{} | 
					
						
							| 
									
										
										
										
											2017-06-06 04:59:04 +08:00
										 |  |  | 	saveCmd.RestoredFrom = version.Version | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	saveCmd.OrgID = c.OrgID | 
					
						
							|  |  |  | 	saveCmd.UserID = c.UserID | 
					
						
							| 
									
										
										
										
											2017-06-05 23:45:27 +08:00
										 |  |  | 	saveCmd.Dashboard = version.Data | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 	saveCmd.Dashboard.Set("version", dash.Version) | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +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) | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18: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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route GET /dashboards/tags dashboards getDashboardTags
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Get all dashboards tags of an organisation.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: getDashboardsTagsResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2022-02-07 19:43:43 +08:00
										 |  |  | func (hs *HTTPServer) GetDashboardTags(c *models.ReqContext) { | 
					
						
							| 
									
										
										
										
											2023-01-18 20:52:41 +08:00
										 |  |  | 	query := dashboards.GetDashboardTagsQuery{OrgID: c.OrgID} | 
					
						
							| 
									
										
										
										
											2022-07-07 02:42:39 +08:00
										 |  |  | 	err := hs.DashboardService.GetDashboardTags(c.Req.Context(), &query) | 
					
						
							| 
									
										
										
										
											2015-05-13 16:45:53 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		c.JsonApiErr(500, "Failed to get tags from database", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	c.JSON(http.StatusOK, query.Result) | 
					
						
							| 
									
										
										
										
											2015-05-13 16:45:53 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-05-17 00:59:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GetDashboardUIDs converts internal ids to UIDs
 | 
					
						
							|  |  |  | func (hs *HTTPServer) GetDashboardUIDs(c *models.ReqContext) { | 
					
						
							|  |  |  | 	ids := strings.Split(web.Params(c.Req)[":ids"], ",") | 
					
						
							|  |  |  | 	uids := make([]string, 0, len(ids)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 	q := &dashboards.GetDashboardRefByIDQuery{} | 
					
						
							| 
									
										
										
										
											2022-05-17 00:59:02 +08:00
										 |  |  | 	for _, idstr := range ids { | 
					
						
							|  |  |  | 		id, err := strconv.ParseInt(idstr, 10, 64) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		q.ID = id | 
					
						
							|  |  |  | 		err = hs.DashboardService.GetDashboardUIDByID(c.Req.Context(), q) | 
					
						
							| 
									
										
										
										
											2022-05-17 00:59:02 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 		uids = append(uids, q.Result.UID) | 
					
						
							| 
									
										
										
										
											2022-05-17 00:59:02 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	c.JSON(http.StatusOK, uids) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters renderReportPDF
 | 
					
						
							|  |  |  | type RenderReportPDFParams struct { | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	DashboardID int64 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters restoreDashboardVersionByID
 | 
					
						
							|  |  |  | type RestoreDashboardVersionByIDParams struct { | 
					
						
							|  |  |  | 	// in:body
 | 
					
						
							|  |  |  | 	// required:true
 | 
					
						
							|  |  |  | 	Body dtos.RestoreDashboardVersionCommand | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	DashboardID int64 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters getDashboardVersionsByID
 | 
					
						
							|  |  |  | type GetDashboardVersionsByIDParams struct { | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	DashboardID int64 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters getDashboardVersionsByUID
 | 
					
						
							|  |  |  | type GetDashboardVersionsByUIDParams struct { | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	// required:true
 | 
					
						
							|  |  |  | 	UID string `json:"uid"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters restoreDashboardVersionByUID
 | 
					
						
							|  |  |  | type RestoreDashboardVersionByUIDParams struct { | 
					
						
							|  |  |  | 	// in:body
 | 
					
						
							|  |  |  | 	// required:true
 | 
					
						
							|  |  |  | 	Body dtos.RestoreDashboardVersionCommand | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	// required:true
 | 
					
						
							|  |  |  | 	UID string `json:"uid"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters getDashboardVersionByID
 | 
					
						
							|  |  |  | type GetDashboardVersionByIDParams struct { | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	DashboardID int64 | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	DashboardVersionID int64 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters getDashboardVersionByUID
 | 
					
						
							|  |  |  | type GetDashboardVersionByUIDParams struct { | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	DashboardVersionID int64 | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	// required:true
 | 
					
						
							|  |  |  | 	UID string `json:"uid"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters getDashboardVersions getDashboardVersionsByUID
 | 
					
						
							|  |  |  | type GetDashboardVersionsParams struct { | 
					
						
							|  |  |  | 	// Maximum number of results to return
 | 
					
						
							|  |  |  | 	// in:query
 | 
					
						
							|  |  |  | 	// required:false
 | 
					
						
							|  |  |  | 	// default:0
 | 
					
						
							|  |  |  | 	Limit int `json:"limit"` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Version to start from when returning queries
 | 
					
						
							|  |  |  | 	// in:query
 | 
					
						
							|  |  |  | 	// required:false
 | 
					
						
							|  |  |  | 	// default:0
 | 
					
						
							|  |  |  | 	Start int `json:"start"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters getDashboardByUID
 | 
					
						
							|  |  |  | type GetDashboardByUIDParams struct { | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	// required:true
 | 
					
						
							|  |  |  | 	UID string `json:"uid"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters deleteDashboardByUID
 | 
					
						
							|  |  |  | type DeleteDashboardByUIDParams struct { | 
					
						
							|  |  |  | 	// in:path
 | 
					
						
							|  |  |  | 	// required:true
 | 
					
						
							|  |  |  | 	UID string `json:"uid"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters postDashboard
 | 
					
						
							|  |  |  | type PostDashboardParams struct { | 
					
						
							|  |  |  | 	// in:body
 | 
					
						
							|  |  |  | 	// required:true
 | 
					
						
							| 
									
										
										
										
											2023-01-18 20:52:41 +08:00
										 |  |  | 	Body dashboards.SaveDashboardCommand | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters calculateDashboardDiff
 | 
					
						
							|  |  |  | type CalcDashboardDiffParams struct { | 
					
						
							|  |  |  | 	// in:body
 | 
					
						
							|  |  |  | 	// required:true
 | 
					
						
							|  |  |  | 	Body struct { | 
					
						
							|  |  |  | 		Base dtos.CalculateDiffTarget `json:"base" binding:"Required"` | 
					
						
							|  |  |  | 		New  dtos.CalculateDiffTarget `json:"new" binding:"Required"` | 
					
						
							|  |  |  | 		// The type of diff to return
 | 
					
						
							|  |  |  | 		// Description:
 | 
					
						
							|  |  |  | 		// * `basic`
 | 
					
						
							|  |  |  | 		// * `json`
 | 
					
						
							|  |  |  | 		// Enum: basic,json
 | 
					
						
							|  |  |  | 		DiffType string `json:"diffType" binding:"Required"` | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters trimDashboard
 | 
					
						
							|  |  |  | type TrimDashboardParams struct { | 
					
						
							|  |  |  | 	// in:body
 | 
					
						
							|  |  |  | 	// required:true
 | 
					
						
							| 
									
										
										
										
											2023-01-18 20:52:41 +08:00
										 |  |  | 	Body dashboards.TrimDashboardCommand | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:response dashboardResponse
 | 
					
						
							|  |  |  | type DashboardResponse struct { | 
					
						
							|  |  |  | 	// The response message
 | 
					
						
							|  |  |  | 	// in: body
 | 
					
						
							|  |  |  | 	Body dtos.DashboardFullWithMeta `json:"body"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:response deleteDashboardResponse
 | 
					
						
							|  |  |  | type DeleteDashboardResponse struct { | 
					
						
							|  |  |  | 	// The response message
 | 
					
						
							|  |  |  | 	// in: body
 | 
					
						
							|  |  |  | 	Body struct { | 
					
						
							|  |  |  | 		// ID Identifier of the deleted dashboard.
 | 
					
						
							|  |  |  | 		// required: true
 | 
					
						
							|  |  |  | 		// example: 65
 | 
					
						
							|  |  |  | 		ID int64 `json:"id"` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Title Title of the deleted dashboard.
 | 
					
						
							|  |  |  | 		// required: true
 | 
					
						
							|  |  |  | 		// example: My Dashboard
 | 
					
						
							|  |  |  | 		Title string `json:"title"` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Message Message of the deleted dashboard.
 | 
					
						
							|  |  |  | 		// required: true
 | 
					
						
							|  |  |  | 		// example: Dashboard My Dashboard deleted
 | 
					
						
							|  |  |  | 		Message string `json:"message"` | 
					
						
							|  |  |  | 	} `json:"body"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:response postDashboardResponse
 | 
					
						
							|  |  |  | type PostDashboardResponse struct { | 
					
						
							|  |  |  | 	// in: body
 | 
					
						
							|  |  |  | 	Body struct { | 
					
						
							|  |  |  | 		// Status status of the response.
 | 
					
						
							|  |  |  | 		// required: true
 | 
					
						
							|  |  |  | 		// example: success
 | 
					
						
							|  |  |  | 		Status string `json:"status"` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Slug The slug of the dashboard.
 | 
					
						
							|  |  |  | 		// required: true
 | 
					
						
							|  |  |  | 		// example: my-dashboard
 | 
					
						
							|  |  |  | 		Slug string `json:"title"` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Version The version of the dashboard.
 | 
					
						
							|  |  |  | 		// required: true
 | 
					
						
							|  |  |  | 		// example: 2
 | 
					
						
							|  |  |  | 		Verion int64 `json:"version"` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// ID The unique identifier (id) of the created/updated dashboard.
 | 
					
						
							|  |  |  | 		// required: true
 | 
					
						
							|  |  |  | 		// example: 1
 | 
					
						
							|  |  |  | 		ID string `json:"id"` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// UID The unique identifier (uid) of the created/updated dashboard.
 | 
					
						
							|  |  |  | 		// required: true
 | 
					
						
							|  |  |  | 		// example: nHz3SXiiz
 | 
					
						
							|  |  |  | 		UID string `json:"uid"` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// URL The relative URL for accessing the created/updated dashboard.
 | 
					
						
							|  |  |  | 		// required: true
 | 
					
						
							|  |  |  | 		// example: /d/nHz3SXiiz/my-dashboard
 | 
					
						
							|  |  |  | 		URL string `json:"url"` | 
					
						
							|  |  |  | 	} `json:"body"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:response calculateDashboardDiffResponse
 | 
					
						
							|  |  |  | type CalculateDashboardDiffResponse struct { | 
					
						
							|  |  |  | 	// in: body
 | 
					
						
							|  |  |  | 	Body []byte `json:"body"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:response trimDashboardResponse
 | 
					
						
							|  |  |  | type TrimDashboardResponse struct { | 
					
						
							|  |  |  | 	// in: body
 | 
					
						
							|  |  |  | 	Body dtos.TrimDashboardFullWithMeta `json:"body"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:response getHomeDashboardResponse
 | 
					
						
							|  |  |  | type GetHomeDashboardResponse struct { | 
					
						
							|  |  |  | 	// in: body
 | 
					
						
							|  |  |  | 	Body GetHomeDashboardResponseBody `json:"body"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:response getDashboardsTagsResponse
 | 
					
						
							|  |  |  | type DashboardsTagsResponse struct { | 
					
						
							|  |  |  | 	// in: body
 | 
					
						
							| 
									
										
										
										
											2023-01-18 20:52:41 +08:00
										 |  |  | 	Body []*dashboards.DashboardTagCloudItem `json:"body"` | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Get home dashboard response.
 | 
					
						
							|  |  |  | // swagger:model GetHomeDashboardResponse
 | 
					
						
							|  |  |  | type GetHomeDashboardResponseBody struct { | 
					
						
							|  |  |  | 	// swagger:allOf
 | 
					
						
							|  |  |  | 	// required: false
 | 
					
						
							|  |  |  | 	dtos.DashboardFullWithMeta | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// swagger:allOf
 | 
					
						
							|  |  |  | 	// required: false
 | 
					
						
							|  |  |  | 	dtos.DashboardRedirect | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:response dashboardVersionsResponse
 | 
					
						
							|  |  |  | type DashboardVersionsResponse struct { | 
					
						
							|  |  |  | 	// in: body
 | 
					
						
							|  |  |  | 	Body []*dashver.DashboardVersionDTO `json:"body"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // swagger:response dashboardVersionResponse
 | 
					
						
							|  |  |  | type DashboardVersionResponse struct { | 
					
						
							|  |  |  | 	// in: body
 | 
					
						
							|  |  |  | 	Body *dashver.DashboardVersionMeta `json:"body"` | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-10-14 21:51:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // swagger:response validateDashboardResponse
 | 
					
						
							|  |  |  | type ValidateDashboardResponse struct { | 
					
						
							|  |  |  | 	IsValid bool   `json:"isValid"` | 
					
						
							|  |  |  | 	Message string `json:"message,omitempty"` | 
					
						
							|  |  |  | } |