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