2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-06-15 09:08:27 -05:00
|
|
|
"context"
|
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"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2015-02-03 08:04:35 -06:00
|
|
|
"os"
|
2019-04-30 06:32:18 -05:00
|
|
|
"path/filepath"
|
2021-11-17 03:57:37 -06:00
|
|
|
"strconv"
|
2015-02-03 08:04:35 -06:00
|
|
|
|
2022-01-28 03:28:33 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/apierrors"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2015-02-05 03:37:13 -06:00
|
|
|
"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"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
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"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
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}
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
2015-02-02 04:32:32 -06:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return query.Result, nil
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func dashboardGuardianResponse(err error) response.Response {
|
2017-06-19 12:47:44 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Error while checking dashboard permissions", err)
|
2017-06-19 12:47:44 -05:00
|
|
|
}
|
2017-12-20 07:42:09 -06:00
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Access denied to this dashboard", nil)
|
2017-06-19 12:47:44 -05:00
|
|
|
}
|
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) TrimDashboard(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.TrimDashboardCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2021-04-28 06:38:33 -05:00
|
|
|
var err error
|
|
|
|
dash := cmd.Dashboard
|
|
|
|
meta := cmd.Meta
|
|
|
|
|
2021-05-28 15:29:30 -05:00
|
|
|
trimedResult := *dash
|
|
|
|
if !hs.LoadSchemaService.IsDisabled() {
|
|
|
|
trimedResult, err = hs.LoadSchemaService.DashboardTrimDefaults(*dash)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Error while exporting with default values removed", err)
|
|
|
|
}
|
2021-04-28 06:38:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
dto := dtos.TrimDashboardFullWithMeta{
|
|
|
|
Dashboard: &trimedResult,
|
|
|
|
Meta: meta,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.TimeRequest(metrics.MApiDashboardGet)
|
|
|
|
return response.JSON(200, dto)
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) GetDashboard(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
uid := web.Params(c.Req)[":uid"]
|
2021-06-15 09:08:27 -05:00
|
|
|
dash, rsp := getDashboardHelper(c.Req.Context(), c.OrgId, 0, uid)
|
2017-06-17 17:24:38 -05:00
|
|
|
if rsp != nil {
|
|
|
|
return rsp
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2021-01-04 07:41:17 -06:00
|
|
|
// When dash contains only keys id, uid that means dashboard data is not valid and json decode failed.
|
|
|
|
if dash.Data != nil {
|
|
|
|
isEmptyData := true
|
|
|
|
for k := range dash.Data.MustMap() {
|
|
|
|
if k != "id" && k != "uid" {
|
|
|
|
isEmptyData = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isEmptyData {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Error while loading dashboard, dashboard data is invalid", nil)
|
2021-01-04 07:41:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 10:43:32 -05:00
|
|
|
guardian := guardian.New(c.Req.Context(), 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 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.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 {
|
2021-06-21 00:51:33 -05:00
|
|
|
updater = getUserLogin(c.Req.Context(), dash.UpdatedBy)
|
2016-01-28 00:00:24 -06:00
|
|
|
}
|
|
|
|
if dash.CreatedBy > 0 {
|
2021-06-21 00:51:33 -05:00
|
|
|
creator = getUserLogin(c.Req.Context(), dash.CreatedBy)
|
2016-01-28 00:00:24 -06:00
|
|
|
}
|
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}
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
2021-05-26 09:20:13 -05:00
|
|
|
if errors.Is(err, models.ErrFolderNotFound) {
|
|
|
|
return response.Error(404, "Folder not found", err)
|
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Dashboard folder could not be read", err)
|
2017-06-16 15:57:37 -05:00
|
|
|
}
|
2021-05-26 09:20:13 -05:00
|
|
|
meta.FolderUid = query.Result.Uid
|
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
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
svc := dashboards.NewProvisioningService(hs.SQLStore)
|
2021-03-12 04:51:02 -06:00
|
|
|
provisioningData, err := svc.GetProvisionedDashboardDataByDashboardID(dash.Id)
|
2018-04-10 02:31:35 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Error while checking if dashboard is provisioned", err)
|
2018-04-10 02:31:35 -05:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2021-05-12 01:48:17 -05:00
|
|
|
// load library panels JSON for this dashboard
|
2021-09-27 02:04:36 -05:00
|
|
|
err = hs.LibraryPanelService.LoadLibraryPanelsForDashboard(c.Req.Context(), dash)
|
2021-05-12 01:48:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Error while loading library panels", err)
|
2021-01-20 02:28:10 -06:00
|
|
|
}
|
2021-05-12 01:48:17 -05:00
|
|
|
|
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)
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, dto)
|
2017-06-12 08:48:55 -05:00
|
|
|
}
|
|
|
|
|
2021-06-21 00:51:33 -05:00
|
|
|
func getUserLogin(ctx context.Context, userID int64) string {
|
2020-03-04 05:57:20 -06:00
|
|
|
query := models.GetUserByIdQuery{Id: userID}
|
2021-12-28 10:36:22 -06:00
|
|
|
err := bus.Dispatch(ctx, &query)
|
2016-01-28 00:00:24 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-15 09:08:27 -05:00
|
|
|
func getDashboardHelper(ctx context.Context, orgID int64, id int64, uid string) (*models.Dashboard, response.Response) {
|
2020-03-04 05:57:20 -06:00
|
|
|
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 {
|
2021-06-03 08:20:13 -05:00
|
|
|
query = models.GetDashboardQuery{Id: id, OrgId: orgID}
|
2018-01-29 14:23:07 -06:00
|
|
|
}
|
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(ctx, &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return nil, response.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
|
|
|
|
}
|
|
|
|
|
2021-01-20 02:28:10 -06:00
|
|
|
func (hs *HTTPServer) DeleteDashboardBySlug(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
query := models.GetDashboardsBySlugQuery{OrgId: c.OrgId, Slug: web.Params(c.Req)[":slug"]}
|
2018-01-31 09:51:06 -06:00
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to retrieve dashboards by slug", err)
|
2018-01-31 09:51:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(query.Result) > 1 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(412, util.DynMap{"status": "multiple-slugs-exists", "message": models.ErrDashboardsWithSameSlugExists.Error()})
|
2018-01-31 09:51:06 -06:00
|
|
|
}
|
|
|
|
|
2021-01-20 02:28:10 -06:00
|
|
|
return hs.deleteDashboard(c)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2021-01-20 02:28:10 -06:00
|
|
|
func (hs *HTTPServer) DeleteDashboardByUID(c *models.ReqContext) response.Response {
|
|
|
|
return hs.deleteDashboard(c)
|
2019-04-10 06:29:10 -05:00
|
|
|
}
|
|
|
|
|
2021-01-20 02:28:10 -06:00
|
|
|
func (hs *HTTPServer) deleteDashboard(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
dash, rsp := getDashboardHelper(c.Req.Context(), c.OrgId, 0, web.Params(c.Req)[":uid"])
|
2017-06-17 17:24:38 -05:00
|
|
|
if rsp != nil {
|
|
|
|
return rsp
|
2017-06-12 16:05:32 -05:00
|
|
|
}
|
|
|
|
|
2021-09-23 10:43:32 -05:00
|
|
|
guardian := guardian.New(c.Req.Context(), 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
|
|
|
}
|
|
|
|
|
2021-05-12 01:48:17 -05:00
|
|
|
// disconnect all library elements for this dashboard
|
2021-09-27 02:04:36 -05:00
|
|
|
err := hs.LibraryElementService.DisconnectElementsFromDashboard(c.Req.Context(), dash.Id)
|
2021-05-12 01:48:17 -05:00
|
|
|
if err != nil {
|
|
|
|
hs.log.Error("Failed to disconnect library elements", "dashboard", dash.Id, "user", c.SignedInUser.UserId, "error", err)
|
2021-01-20 02:28:10 -06:00
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
svc := dashboards.NewService(hs.SQLStore)
|
2021-11-19 07:32:14 -06:00
|
|
|
err = svc.DeleteDashboard(c.Req.Context(), 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) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(dashboardErr.StatusCode, dashboardErr.Error(), err)
|
2020-07-21 04:12:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete dashboard", err)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2021-05-04 10:44:55 -05:00
|
|
|
if hs.Live != nil {
|
2021-05-11 14:03:04 -05:00
|
|
|
err := hs.Live.GrafanaScope.Dashboards.DashboardDeleted(c.OrgId, c.ToUserDisplayDTO(), dash.Uid)
|
2021-04-23 14:55:31 -05:00
|
|
|
if err != nil {
|
|
|
|
hs.log.Error("Failed to broadcast delete info", "dashboard", dash.Uid, "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.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
|
|
|
}
|
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) PostDashboard(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.SaveDashboardCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
|
|
|
return hs.postDashboard(c, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HTTPServer) postDashboard(c *models.ReqContext, cmd models.SaveDashboardCommand) response.Response {
|
2021-09-14 09:08:04 -05:00
|
|
|
ctx := c.Req.Context()
|
2021-04-28 06:38:33 -05:00
|
|
|
var err error
|
2017-06-05 09:34:32 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
|
|
|
cmd.UserId = c.UserId
|
2021-05-26 09:20:13 -05:00
|
|
|
if cmd.FolderUid != "" {
|
|
|
|
folders := dashboards.NewFolderService(c.OrgId, c.SignedInUser, hs.SQLStore)
|
2021-09-14 09:08:04 -05:00
|
|
|
folder, err := folders.GetFolderByUID(ctx, cmd.FolderUid)
|
2021-05-26 09:20:13 -05:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, models.ErrFolderNotFound) {
|
|
|
|
return response.Error(400, "Folder not found", err)
|
|
|
|
}
|
|
|
|
return response.Error(500, "Error while checking folder ID", err)
|
|
|
|
}
|
|
|
|
cmd.FolderId = folder.Id
|
|
|
|
}
|
|
|
|
|
2015-07-20 09:38:25 -05:00
|
|
|
dash := cmd.GetDashboardModel()
|
2021-05-26 09:20:13 -05:00
|
|
|
newDashboard := dash.Id == 0
|
2019-03-05 09:44:41 -06:00
|
|
|
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 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "failed to get quota", err)
|
2015-07-20 09:38:25 -05:00
|
|
|
}
|
|
|
|
if limitReached {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Quota reached", nil)
|
2015-07-20 09:38:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
svc := dashboards.NewProvisioningService(hs.SQLStore)
|
2021-12-17 09:31:52 -06:00
|
|
|
var provisioningData *models.DashboardProvisioning
|
|
|
|
if dash.Id != 0 {
|
|
|
|
data, err := svc.GetProvisionedDashboardDataByDashboardID(dash.Id)
|
|
|
|
if err != nil {
|
2022-01-11 10:39:53 -06:00
|
|
|
return response.Error(500, "Error while checking if dashboard is provisioned using ID", err)
|
2021-12-17 09:31:52 -06:00
|
|
|
}
|
|
|
|
provisioningData = data
|
|
|
|
} else if dash.Uid != "" {
|
|
|
|
data, err := svc.GetProvisionedDashboardDataByDashboardUID(dash.OrgId, dash.Uid)
|
2022-01-11 10:39:53 -06:00
|
|
|
if err != nil && (!errors.Is(err, models.ErrProvisionedDashboardNotFound) && !errors.Is(err, models.ErrDashboardNotFound)) {
|
|
|
|
return response.Error(500, "Error while checking if dashboard is provisioned using UID", err)
|
2021-12-17 09:31:52 -06:00
|
|
|
}
|
|
|
|
provisioningData = data
|
2019-10-31 08:27:31 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-05-12 01:48:17 -05:00
|
|
|
// clean up all unnecessary library panels JSON properties so we store a minimum JSON
|
|
|
|
err = hs.LibraryPanelService.CleanLibraryPanelsForDashboard(dash)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Error while cleaning library panels", err)
|
2021-01-20 02:28:10 -06: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
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
dashSvc := dashboards.NewService(hs.SQLStore)
|
2021-12-02 09:41:24 -06:00
|
|
|
dashboard, err := dashSvc.SaveDashboard(alerting.WithUAEnabled(ctx, hs.Cfg.UnifiedAlerting.IsEnabled()), dashItem, allowUiUpdate)
|
2021-04-23 14:55:31 -05:00
|
|
|
|
2021-05-04 10:44:55 -05:00
|
|
|
if hs.Live != nil {
|
|
|
|
// Tell everyone listening that the dashboard changed
|
2021-04-23 14:55:31 -05:00
|
|
|
if dashboard == nil {
|
|
|
|
dashboard = dash // the original request
|
|
|
|
}
|
|
|
|
|
2021-10-15 03:33:01 -05:00
|
|
|
// This will broadcast all save requests only if a `gitops` observer exists.
|
2021-04-23 14:55:31 -05:00
|
|
|
// gitops is useful when trying to save dashboards in an environment where the user can not save
|
|
|
|
channel := hs.Live.GrafanaScope.Dashboards
|
2021-05-11 14:03:04 -05:00
|
|
|
liveerr := channel.DashboardSaved(c.SignedInUser.OrgId, c.SignedInUser.ToUserDisplayDTO(), cmd.Message, dashboard, err)
|
2021-04-23 14:55:31 -05:00
|
|
|
|
|
|
|
// When an error exists, but the value broadcast to a gitops listener return 202
|
2021-05-11 14:03:04 -05:00
|
|
|
if liveerr == nil && err != nil && channel.HasGitOpsObserver(c.SignedInUser.OrgId) {
|
2021-04-23 14:55:31 -05:00
|
|
|
return response.JSON(202, util.DynMap{
|
|
|
|
"status": "pending",
|
|
|
|
"message": "changes were broadcast to the gitops listener",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if liveerr != nil {
|
|
|
|
hs.log.Warn("unable to broadcast save event", "uid", dashboard.Uid, "error", err)
|
|
|
|
}
|
|
|
|
}
|
2021-05-04 10:44:55 -05:00
|
|
|
|
2019-12-20 04:42:47 -06:00
|
|
|
if err != nil {
|
2022-01-28 03:28:33 -06:00
|
|
|
return apierrors.ToDashboardErrorResponse(ctx, hs.pluginStore, err)
|
2019-12-20 04:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if hs.Cfg.EditorsCanAdmin && newDashboard {
|
|
|
|
inFolder := cmd.FolderId > 0
|
2021-09-14 09:08:04 -05:00
|
|
|
err := dashSvc.MakeUserAdmin(ctx, cmd.OrgId, cmd.UserId, dashboard.Id, !inFolder)
|
2019-12-20 04:42:47 -06:00
|
|
|
if err != nil {
|
|
|
|
hs.log.Error("Could not make user admin", "dashboard", dashboard.Title, "user", c.SignedInUser.UserId, "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 01:48:17 -05:00
|
|
|
// connect library panels for this dashboard after the dashboard is stored and has an ID
|
2021-11-17 05:04:22 -06:00
|
|
|
err = hs.LibraryPanelService.ConnectLibraryPanelsForDashboard(ctx, c.SignedInUser, dashboard)
|
2021-05-12 01:48:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Error while connecting library panels", err)
|
2021-01-20 02:28:10 -06:00
|
|
|
}
|
|
|
|
|
2019-12-20 04:42:47 -06:00
|
|
|
c.TimeRequest(metrics.MApiDashboardSave)
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, util.DynMap{
|
2019-12-20 04:42:47 -06:00
|
|
|
"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
|
|
|
|
2020-06-22 11:00:39 -05:00
|
|
|
// GetHomeDashboard returns the home dashboard.
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) GetHomeDashboard(c *models.ReqContext) response.Response {
|
2020-03-04 05:57:20 -06:00
|
|
|
prefsQuery := models.GetPreferencesWithDefaultsQuery{User: c.SignedInUser}
|
2021-04-13 08:27:51 -05:00
|
|
|
homePage := hs.Cfg.HomePage
|
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := hs.Bus.Dispatch(c.Req.Context(), &prefsQuery); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get preferences", err)
|
2016-03-17 04:29:34 -05:00
|
|
|
}
|
|
|
|
|
2021-04-13 08:27:51 -05:00
|
|
|
if prefsQuery.Result.HomeDashboardId == 0 && len(homePage) > 0 {
|
|
|
|
homePageRedirect := dtos.DashboardRedirect{RedirectUri: homePage}
|
|
|
|
return response.JSON(200, &homePageRedirect)
|
|
|
|
}
|
|
|
|
|
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}
|
2021-12-28 10:36:22 -06:00
|
|
|
err := hs.Bus.Dispatch(c.Req.Context(), &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}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.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
|
|
|
}
|
|
|
|
|
2020-12-03 15:13:06 -06:00
|
|
|
// It's safe to ignore gosec warning G304 since the variable part of the file path comes from a configuration
|
|
|
|
// variable
|
|
|
|
// nolint:gosec
|
2015-02-03 08:04:35 -06:00
|
|
|
file, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.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 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.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
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.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
|
2021-01-15 07:43:20 -06:00
|
|
|
func GetDashboardVersions(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
dashID, err := strconv.ParseInt(web.Params(c.Req)[":dashboardId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "dashboardId is invalid", err)
|
|
|
|
}
|
2017-06-13 17:28:34 -05:00
|
|
|
|
2021-09-23 10:43:32 -05:00
|
|
|
guardian := guardian.New(c.Req.Context(), 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
|
|
|
}
|
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, query.Result)
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-24 18:14:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDashboardVersion returns the dashboard version with the given ID.
|
2021-01-15 07:43:20 -06:00
|
|
|
func GetDashboardVersion(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
dashID, err := strconv.ParseInt(web.Params(c.Req)[":dashboardId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "dashboardId is invalid", err)
|
|
|
|
}
|
2017-06-13 17:28:34 -05:00
|
|
|
|
2021-09-23 10:43:32 -05:00
|
|
|
guardian := guardian.New(c.Req.Context(), 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
|
|
|
}
|
|
|
|
|
2021-12-13 08:56:14 -06:00
|
|
|
version, _ := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 32)
|
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,
|
2021-11-17 03:57:37 -06:00
|
|
|
Version: int(version),
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-24 18:14:39 -05:00
|
|
|
}
|
2017-06-05 09:34:32 -05:00
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, fmt.Sprintf("Dashboard version %d not found for dashboardId %d", query.Version, dashID), err)
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-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 {
|
2021-06-21 00:51:33 -05:00
|
|
|
creator = getUserLogin(c.Req.Context(), query.Result.CreatedBy)
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-24 18:14:39 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, dashVersionMeta)
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-24 18:14:39 -05:00
|
|
|
}
|
|
|
|
|
2017-06-07 04:50:09 -05:00
|
|
|
// POST /api/dashboards/calculate-diff performs diffs on two dashboards
|
2021-11-29 03:18:01 -06:00
|
|
|
func CalculateDashboardDiff(c *models.ReqContext) response.Response {
|
|
|
|
apiOptions := dtos.CalculateDiffOptions{}
|
|
|
|
if err := web.Bind(c.Req, &apiOptions); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2021-09-23 10:43:32 -05:00
|
|
|
guardianBase := guardian.New(c.Req.Context(), apiOptions.Base.DashboardId, c.OrgId, c.SignedInUser)
|
2018-02-27 10:53:30 -06:00
|
|
|
if canSave, err := guardianBase.CanSave(); err != nil || !canSave {
|
|
|
|
return dashboardGuardianResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if apiOptions.Base.DashboardId != apiOptions.New.DashboardId {
|
2021-09-23 10:43:32 -05:00
|
|
|
guardianNew := guardian.New(c.Req.Context(), apiOptions.New.DashboardId, c.OrgId, c.SignedInUser)
|
2018-02-27 10:53:30 -06:00
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-17 03:57:37 -06:00
|
|
|
result, err := dashdiffs.CalculateDiff(c.Req.Context(), &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) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Dashboard version not found", err)
|
2017-06-07 07:21:40 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Unable to compute diff", err)
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-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 {
|
2021-03-17 12:12:28 -05:00
|
|
|
return response.Respond(200, result.Delta).SetHeader("Content-Type", "application/json")
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-24 18:14:39 -05:00
|
|
|
}
|
2018-02-27 10:53:30 -06:00
|
|
|
|
2021-03-17 12:12:28 -05:00
|
|
|
return response.Respond(200, result.Delta).SetHeader("Content-Type", "text/html")
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-24 18:14:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreDashboardVersion restores a dashboard to the given version.
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) RestoreDashboardVersion(c *models.ReqContext) response.Response {
|
|
|
|
apiCmd := dtos.RestoreDashboardVersionCommand{}
|
|
|
|
if err := web.Bind(c.Req, &apiCmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
dashboardId, err := strconv.ParseInt(web.Params(c.Req)[":dashboardId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "dashboardId is invalid", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dash, rsp := getDashboardHelper(c.Req.Context(), c.OrgId, 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
|
|
|
}
|
|
|
|
|
2021-09-23 10:43:32 -05:00
|
|
|
guardian := guardian.New(c.Req.Context(), 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}
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(c.Req.Context(), &versionQuery); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Dashboard version not found", nil)
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-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
|
|
|
|
2021-11-29 03:18: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}
|
2021-12-28 10:36:22 -06:00
|
|
|
err := bus.Dispatch(c.Req.Context(), &query)
|
2015-05-13 03:45:53 -05:00
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(500, "Failed to get tags from database", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, query.Result)
|
|
|
|
}
|