2014-12-22 05:25:08 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2015-02-04 04:35:59 -06:00
|
|
|
"bytes"
|
2015-02-05 04:10:56 -06:00
|
|
|
"fmt"
|
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
|
|
|
"time"
|
2015-02-04 04:35:59 -06:00
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2015-03-22 14:14:00 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2015-02-05 03:37:13 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-06-05 01:15:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/search"
|
2014-12-22 05:25:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-12-29 06:58:06 -06:00
|
|
|
bus.AddHandler("sql", SaveDashboard)
|
|
|
|
bus.AddHandler("sql", GetDashboard)
|
2016-01-28 18:41:23 -06:00
|
|
|
bus.AddHandler("sql", GetDashboards)
|
2014-12-29 06:58:06 -06:00
|
|
|
bus.AddHandler("sql", DeleteDashboard)
|
|
|
|
bus.AddHandler("sql", SearchDashboards)
|
2015-01-06 11:39:26 -06:00
|
|
|
bus.AddHandler("sql", GetDashboardTags)
|
2016-03-17 03:01:58 -05:00
|
|
|
bus.AddHandler("sql", GetDashboardSlugById)
|
2016-07-08 02:35:06 -05:00
|
|
|
bus.AddHandler("sql", GetDashboardsByPluginId)
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func SaveDashboard(cmd *m.SaveDashboardCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2014-12-22 05:25:08 -06:00
|
|
|
dash := cmd.GetDashboardModel()
|
2017-06-01 16:30:31 -05:00
|
|
|
|
2015-01-05 10:04:29 -06:00
|
|
|
// try get existing dashboard
|
2015-05-04 01:19:29 -05:00
|
|
|
var existing, sameTitle m.Dashboard
|
2015-01-05 10:04:29 -06:00
|
|
|
|
2015-05-04 01:19:29 -05:00
|
|
|
if dash.Id > 0 {
|
|
|
|
dashWithIdExists, err := sess.Where("id=? AND org_id=?", dash.Id, dash.OrgId).Get(&existing)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !dashWithIdExists {
|
|
|
|
return m.ErrDashboardNotFound
|
2015-03-02 15:24:01 -06:00
|
|
|
}
|
2015-05-04 01:19:29 -05:00
|
|
|
|
2015-03-02 15:24:01 -06:00
|
|
|
// check for is someone else has written in between
|
|
|
|
if dash.Version != existing.Version {
|
|
|
|
if cmd.Overwrite {
|
|
|
|
dash.Version = existing.Version
|
|
|
|
} else {
|
|
|
|
return m.ErrDashboardVersionMismatch
|
|
|
|
}
|
|
|
|
}
|
2016-07-08 06:41:46 -05:00
|
|
|
|
|
|
|
// do not allow plugin dashboard updates without overwrite flag
|
|
|
|
if existing.PluginId != "" && cmd.Overwrite == false {
|
|
|
|
return m.UpdatePluginDashboardError{PluginId: existing.PluginId}
|
|
|
|
}
|
2015-01-05 10:04:29 -06:00
|
|
|
}
|
|
|
|
|
2015-05-04 01:19:29 -05:00
|
|
|
sameTitleExists, err := sess.Where("org_id=? AND slug=?", dash.OrgId, dash.Slug).Get(&sameTitle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sameTitleExists {
|
|
|
|
// another dashboard with same name
|
|
|
|
if dash.Id != sameTitle.Id {
|
|
|
|
if cmd.Overwrite {
|
|
|
|
dash.Id = sameTitle.Id
|
2017-06-06 07:39:57 -05:00
|
|
|
dash.Version = sameTitle.Version
|
2015-05-04 01:19:29 -05:00
|
|
|
} else {
|
|
|
|
return m.ErrDashboardWithSameNameExists
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
parentVersion := dash.Version
|
|
|
|
affectedRows := int64(0)
|
2017-06-05 09:34:32 -05:00
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
if dash.Id == 0 {
|
2017-06-06 08:40:10 -05:00
|
|
|
dash.Version = 1
|
2015-03-22 14:14:00 -05:00
|
|
|
metrics.M_Models_Dashboard_Insert.Inc(1)
|
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
|
|
|
dash.Data.Set("version", dash.Version)
|
2015-05-04 00:46:46 -05:00
|
|
|
affectedRows, err = sess.Insert(dash)
|
2014-12-22 05:25:08 -06:00
|
|
|
} else {
|
2017-06-05 09:34:32 -05:00
|
|
|
dash.Version += 1
|
2016-03-12 03:13:49 -06:00
|
|
|
dash.Data.Set("version", dash.Version)
|
2017-05-31 09:53:12 -05:00
|
|
|
affectedRows, err = sess.MustCols("parent_id").Id(dash.Id).Update(dash)
|
2015-05-04 00:46:46 -05:00
|
|
|
}
|
2017-06-05 09:34:32 -05:00
|
|
|
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-24 18:14:39 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-05 09:34:32 -05:00
|
|
|
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-24 18:14:39 -05:00
|
|
|
if affectedRows == 0 {
|
|
|
|
return m.ErrDashboardNotFound
|
|
|
|
}
|
2015-05-04 00:46:46 -05:00
|
|
|
|
History and Version Control for Dashboard Updates
A simple version control system for dashboards. Closes #1504.
Goals
1. To create a new dashboard version every time a dashboard is saved.
2. To allow users to view all versions of a given dashboard.
3. To allow users to rollback to a previous version of a dashboard.
4. To allow users to compare two versions of a dashboard.
Usage
Navigate to a dashboard, and click the settings cog. From there, click
the "Changelog" button to be brought to the Changelog view. In this
view, a table containing each version of a dashboard can be seen. Each
entry in the table represents a dashboard version. A selectable
checkbox, the version number, date created, name of the user who created
that version, and commit message is shown in the table, along with a
button that allows a user to restore to a previous version of that
dashboard. If a user wants to restore to a previous version of their
dashboard, they can do so by clicking the previously mentioned button.
If a user wants to compare two different versions of a dashboard, they
can do so by clicking the checkbox of two different dashboard versions,
then clicking the "Compare versions" button located below the dashboard.
From there, the user is brought to a view showing a summary of the
dashboard differences. Each summarized change contains a link that can
be clicked to take the user a JSON diff highlighting the changes line by
line.
Overview of Changes
Backend Changes
- A `dashboard_version` table was created to store each dashboard
version, along with a dashboard version model and structs to represent
the queries and commands necessary for the dashboard version API
methods.
- API endpoints were created to support working with dashboard
versions.
- Methods were added to create, update, read, and destroy dashboard
versions in the database.
- Logic was added to compute the diff between two versions, and
display it to the user.
- The dashboard migration logic was updated to save a "Version
1" of each existing dashboard in the database.
Frontend Changes
- New views
- Methods to pull JSON and HTML from endpoints
New API Endpoints
Each endpoint requires the authorization header to be sent in
the format,
```
Authorization: Bearer <jwt>
```
where `<jwt>` is a JSON web token obtained from the Grafana
admin panel.
`GET "/api/dashboards/db/:dashboardId/versions?orderBy=<string>&limit=<int>&start=<int>"`
Get all dashboard versions for the given dashboard ID. Accepts
three URL parameters:
- `orderBy` String to order the results by. Possible values
are `version`, `created`, `created_by`, `message`. Default
is `versions`. Ordering is always in descending order.
- `limit` Maximum number of results to return
- `start` Position in results to start from
`GET "/api/dashboards/db/:dashboardId/versions/:id"`
Get an individual dashboard version by ID, for the given
dashboard ID.
`POST "/api/dashboards/db/:dashboardId/restore"`
Restore to the given dashboard version. Post body is of
content-type `application/json`, and must contain.
```json
{
"dashboardId": <int>,
"version": <int>
}
```
`GET "/api/dashboards/db/:dashboardId/compare/:versionA...:versionB"`
Compare two dashboard versions by ID for the given
dashboard ID, returning a JSON delta formatted
representation of the diff. The URL format follows
what GitHub does. For example, visiting
[/api/dashboards/db/18/compare/22...33](http://ec2-54-80-139-44.compute-1.amazonaws.com:3000/api/dashboards/db/18/compare/22...33)
will return the diff between versions 22 and 33 for
the dashboard ID 18.
Dependencies Added
- The Go package [gojsondiff](https://github.com/yudai/gojsondiff)
was added and vendored.
2017-05-24 18:14:39 -05:00
|
|
|
dashVersion := &m.DashboardVersion{
|
|
|
|
DashboardId: dash.Id,
|
|
|
|
ParentVersion: parentVersion,
|
2017-06-05 15:59:04 -05:00
|
|
|
RestoredFrom: cmd.RestoredFrom,
|
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
|
|
|
Version: dash.Version,
|
|
|
|
Created: time.Now(),
|
|
|
|
CreatedBy: dash.UpdatedBy,
|
|
|
|
Message: cmd.Message,
|
|
|
|
Data: dash.Data,
|
|
|
|
}
|
2017-06-05 09:34:32 -05:00
|
|
|
|
|
|
|
// insert version entry
|
|
|
|
if affectedRows, err = sess.Insert(dashVersion); err != 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
|
|
|
return err
|
2017-06-05 09:34:32 -05:00
|
|
|
} else if affectedRows == 0 {
|
2015-05-04 00:46:46 -05:00
|
|
|
return m.ErrDashboardNotFound
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
// delete existing tabs
|
|
|
|
_, err = sess.Exec("DELETE FROM dashboard_tag WHERE dashboard_id=?", dash.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert new tags
|
|
|
|
tags := dash.GetTags()
|
|
|
|
if len(tags) > 0 {
|
|
|
|
for _, tag := range tags {
|
2015-01-20 07:15:48 -06:00
|
|
|
if _, err := sess.Insert(&DashboardTag{DashboardId: dash.Id, Term: tag}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-07 05:37:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
cmd.Result = dash
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func GetDashboard(query *m.GetDashboardQuery) error {
|
2017-06-05 10:45:27 -05:00
|
|
|
dashboard := m.Dashboard{Slug: query.Slug, OrgId: query.OrgId, Id: query.Id}
|
2014-12-22 05:25:08 -06:00
|
|
|
has, err := x.Get(&dashboard)
|
2017-06-05 10:45:27 -05:00
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
if err != nil {
|
2014-12-29 06:58:06 -06:00
|
|
|
return err
|
2014-12-22 05:25:08 -06:00
|
|
|
} else if has == false {
|
2014-12-29 06:58:06 -06:00
|
|
|
return m.ErrDashboardNotFound
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2016-03-12 03:13:49 -06:00
|
|
|
dashboard.Data.Set("id", dashboard.Id)
|
2014-12-29 06:58:06 -06:00
|
|
|
query.Result = &dashboard
|
|
|
|
return nil
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
type DashboardSearchProjection struct {
|
2017-06-16 20:00:13 -05:00
|
|
|
Id int64
|
|
|
|
Title string
|
|
|
|
Slug string
|
|
|
|
Term string
|
|
|
|
IsFolder bool
|
|
|
|
ParentId int64
|
|
|
|
FolderSlug string
|
|
|
|
FolderTitle string
|
2015-01-07 05:37:24 -06:00
|
|
|
}
|
|
|
|
|
2017-03-27 07:36:28 -05:00
|
|
|
func findDashboards(query *search.FindPersistedDashboardsQuery) ([]DashboardSearchProjection, error) {
|
2015-02-04 04:35:59 -06:00
|
|
|
var sql bytes.Buffer
|
|
|
|
params := make([]interface{}, 0)
|
|
|
|
|
|
|
|
sql.WriteString(`SELECT
|
|
|
|
dashboard.id,
|
|
|
|
dashboard.title,
|
|
|
|
dashboard.slug,
|
2017-03-27 07:36:28 -05:00
|
|
|
dashboard_tag.term,
|
|
|
|
dashboard.is_folder,
|
2017-06-16 19:33:53 -05:00
|
|
|
dashboard.parent_id,
|
2017-06-16 20:00:13 -05:00
|
|
|
f.slug as folder_slug,
|
|
|
|
f.title as folder_title
|
2015-02-04 04:35:59 -06:00
|
|
|
FROM dashboard
|
2017-06-16 20:00:13 -05:00
|
|
|
LEFT OUTER JOIN dashboard f on f.id = dashboard.parent_id
|
2015-02-04 04:35:59 -06:00
|
|
|
LEFT OUTER JOIN dashboard_tag on dashboard_tag.dashboard_id = dashboard.id`)
|
|
|
|
|
|
|
|
if query.IsStarred {
|
|
|
|
sql.WriteString(" INNER JOIN star on star.dashboard_id = dashboard.id")
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
sql.WriteString(` WHERE dashboard.org_id=?`)
|
2017-06-16 19:33:53 -05:00
|
|
|
params = append(params, query.SignedInUser.OrgId)
|
2015-01-06 10:15:52 -06:00
|
|
|
|
2015-02-04 04:35:59 -06:00
|
|
|
if query.IsStarred {
|
|
|
|
sql.WriteString(` AND star.user_id=?`)
|
2017-06-16 19:33:53 -05:00
|
|
|
params = append(params, query.SignedInUser.UserId)
|
2015-02-04 04:35:59 -06:00
|
|
|
}
|
|
|
|
|
2016-03-05 05:26:21 -06:00
|
|
|
if len(query.DashboardIds) > 0 {
|
|
|
|
sql.WriteString(" AND (")
|
|
|
|
for i, dashboardId := range query.DashboardIds {
|
|
|
|
if i != 0 {
|
2016-03-07 04:04:02 -06:00
|
|
|
sql.WriteString(" OR")
|
2016-03-05 05:26:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
sql.WriteString(" dashboard.id = ?")
|
|
|
|
params = append(params, dashboardId)
|
|
|
|
}
|
|
|
|
sql.WriteString(")")
|
|
|
|
}
|
|
|
|
|
2017-06-16 19:33:53 -05:00
|
|
|
if query.SignedInUser.OrgRole != m.ROLE_ADMIN {
|
|
|
|
allowedDashboardsSubQuery := ` AND (dashboard.has_acl = 0 OR dashboard.id in (
|
|
|
|
SELECT distinct d.id AS DashboardId
|
|
|
|
FROM dashboard AS d
|
|
|
|
LEFT JOIN dashboard AS df ON d.parent_id = df.id
|
|
|
|
LEFT JOIN dashboard_acl as dfa on d.parent_id = dfa.dashboard_id or d.id = dfa.dashboard_id
|
|
|
|
LEFT JOIN user_group_member as ugm on ugm.user_group_id = dfa.user_group_id
|
|
|
|
WHERE
|
|
|
|
d.has_acl = 1 and
|
|
|
|
(dfa.user_id = ? or ugm.user_id = ?)
|
|
|
|
and d.org_id = ?
|
|
|
|
))`
|
|
|
|
|
|
|
|
sql.WriteString(allowedDashboardsSubQuery)
|
|
|
|
params = append(params, query.SignedInUser.UserId, query.SignedInUser.UserId, query.SignedInUser.OrgId)
|
|
|
|
}
|
|
|
|
|
2015-02-04 04:35:59 -06:00
|
|
|
if len(query.Title) > 0 {
|
2015-08-10 13:25:01 -05:00
|
|
|
sql.WriteString(" AND dashboard.title " + dialect.LikeStr() + " ?")
|
2015-02-04 04:35:59 -06:00
|
|
|
params = append(params, "%"+query.Title+"%")
|
|
|
|
}
|
2014-12-22 05:25:08 -06:00
|
|
|
|
2017-05-24 11:28:13 -05:00
|
|
|
if len(query.Type) > 0 && query.Type == "dash-folder" {
|
|
|
|
sql.WriteString(" AND dashboard.is_folder = 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(query.Type) > 0 && query.Type == "dash-db" {
|
|
|
|
sql.WriteString(" AND dashboard.is_folder = 0")
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:30:31 -05:00
|
|
|
if query.ParentId > 0 {
|
|
|
|
sql.WriteString(" AND dashboard.parent_id = ?")
|
|
|
|
params = append(params, query.ParentId)
|
|
|
|
}
|
|
|
|
|
2016-06-17 04:22:39 -05:00
|
|
|
sql.WriteString(fmt.Sprintf(" ORDER BY dashboard.title ASC LIMIT 1000"))
|
2015-02-05 04:10:56 -06:00
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
var res []DashboardSearchProjection
|
2017-06-01 16:30:31 -05:00
|
|
|
|
2015-02-04 04:35:59 -06:00
|
|
|
err := x.Sql(sql.String(), params...).Find(&res)
|
2017-03-27 07:36:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchDashboards(query *search.FindPersistedDashboardsQuery) error {
|
|
|
|
res, err := findDashboards(query)
|
2015-01-07 05:37:24 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:30:31 -05:00
|
|
|
if query.Mode == "tree" {
|
|
|
|
res, err = appendDashboardFolders(res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-08 15:23:25 -05:00
|
|
|
}
|
|
|
|
|
2017-06-01 16:30:31 -05:00
|
|
|
makeQueryResult(query, res)
|
2015-01-07 05:37:24 -06:00
|
|
|
|
2017-06-01 16:30:31 -05:00
|
|
|
if query.Mode == "tree" {
|
|
|
|
convertToDashboardFolders(query)
|
2015-01-07 05:37:24 -06:00
|
|
|
}
|
2014-12-22 05:25:08 -06:00
|
|
|
|
2017-06-01 16:30:31 -05:00
|
|
|
return nil
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2017-05-08 15:23:25 -05:00
|
|
|
// appends parent folders for any hits to the search result
|
|
|
|
func appendDashboardFolders(res []DashboardSearchProjection) ([]DashboardSearchProjection, error) {
|
|
|
|
for _, item := range res {
|
|
|
|
if item.ParentId > 0 {
|
|
|
|
res = append(res, DashboardSearchProjection{
|
2017-06-16 20:00:13 -05:00
|
|
|
Id: item.ParentId,
|
2017-05-08 15:23:25 -05:00
|
|
|
IsFolder: true,
|
2017-06-16 20:00:13 -05:00
|
|
|
Slug: item.FolderSlug,
|
|
|
|
Title: item.FolderTitle,
|
2017-05-08 15:23:25 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2017-03-27 07:36:28 -05:00
|
|
|
func getHitType(item DashboardSearchProjection) search.HitType {
|
|
|
|
var hitType search.HitType
|
|
|
|
if item.IsFolder {
|
|
|
|
hitType = search.DashHitFolder
|
|
|
|
} else {
|
|
|
|
hitType = search.DashHitDB
|
|
|
|
}
|
|
|
|
|
|
|
|
return hitType
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:30:31 -05:00
|
|
|
func makeQueryResult(query *search.FindPersistedDashboardsQuery, res []DashboardSearchProjection) {
|
|
|
|
query.Result = make([]*search.Hit, 0)
|
|
|
|
hits := make(map[int64]*search.Hit)
|
|
|
|
|
|
|
|
for _, item := range res {
|
|
|
|
hit, exists := hits[item.Id]
|
|
|
|
if !exists {
|
|
|
|
hit = &search.Hit{
|
|
|
|
Id: item.Id,
|
|
|
|
Title: item.Title,
|
|
|
|
Uri: "db/" + item.Slug,
|
|
|
|
Type: getHitType(item),
|
|
|
|
ParentId: item.ParentId,
|
|
|
|
Tags: []string{},
|
|
|
|
}
|
|
|
|
query.Result = append(query.Result, hit)
|
|
|
|
hits[item.Id] = hit
|
|
|
|
}
|
|
|
|
if len(item.Term) > 0 {
|
|
|
|
hit.Tags = append(hit.Tags, item.Term)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 07:36:28 -05:00
|
|
|
func convertToDashboardFolders(query *search.FindPersistedDashboardsQuery) error {
|
|
|
|
root := make(map[int64]*search.Hit)
|
|
|
|
var keys []int64
|
|
|
|
|
|
|
|
// Add dashboards and folders that should be at the root level
|
|
|
|
for _, item := range query.Result {
|
|
|
|
if item.Type == search.DashHitFolder || item.ParentId == 0 {
|
|
|
|
root[item.Id] = item
|
|
|
|
keys = append(keys, item.Id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate folders with their child dashboards
|
|
|
|
for _, item := range query.Result {
|
|
|
|
if item.Type == search.DashHitDB && item.ParentId > 0 {
|
|
|
|
root[item.ParentId].Dashboards = append(root[item.ParentId].Dashboards, *item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = make([]*search.Hit, 0)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
query.Result = append(query.Result, root[key])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-06 11:39:26 -06:00
|
|
|
func GetDashboardTags(query *m.GetDashboardTagsQuery) error {
|
2015-01-20 08:23:14 -06:00
|
|
|
sql := `SELECT
|
|
|
|
COUNT(*) as count,
|
|
|
|
term
|
|
|
|
FROM dashboard
|
|
|
|
INNER JOIN dashboard_tag on dashboard_tag.dashboard_id = dashboard.id
|
2015-02-23 13:07:49 -06:00
|
|
|
WHERE dashboard.org_id=?
|
2015-01-20 08:23:14 -06:00
|
|
|
GROUP BY term`
|
|
|
|
|
|
|
|
query.Result = make([]*m.DashboardTagCloudItem, 0)
|
2015-02-23 13:07:49 -06:00
|
|
|
sess := x.Sql(sql, query.OrgId)
|
2015-01-07 05:37:24 -06:00
|
|
|
err := sess.Find(&query.Result)
|
|
|
|
return err
|
2015-01-06 11:39:26 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func DeleteDashboard(cmd *m.DeleteDashboardCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2017-06-17 17:24:38 -05:00
|
|
|
dashboard := m.Dashboard{Id: cmd.Id, OrgId: cmd.OrgId}
|
2016-04-25 04:22:13 -05:00
|
|
|
has, err := sess.Get(&dashboard)
|
2015-08-12 02:23:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has == false {
|
|
|
|
return m.ErrDashboardNotFound
|
|
|
|
}
|
2014-12-22 05:25:08 -06:00
|
|
|
|
2015-08-12 02:23:46 -05:00
|
|
|
deletes := []string{
|
|
|
|
"DELETE FROM dashboard_tag WHERE dashboard_id = ? ",
|
|
|
|
"DELETE FROM star WHERE dashboard_id = ? ",
|
|
|
|
"DELETE FROM dashboard WHERE id = ?",
|
2016-11-06 03:49:43 -06:00
|
|
|
"DELETE FROM playlist_item WHERE type = 'dashboard_by_id' AND value = ?",
|
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
|
|
|
"DELETE FROM dashboard_version WHERE dashboard_id = ?",
|
2017-06-16 14:14:42 -05:00
|
|
|
"DELETE FROM dashboard WHERE parent_id = ?",
|
2015-08-12 02:23:46 -05:00
|
|
|
}
|
2014-12-22 05:25:08 -06:00
|
|
|
|
2015-08-12 02:23:46 -05:00
|
|
|
for _, sql := range deletes {
|
|
|
|
_, err := sess.Exec(sql, dashboard.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 03:56:23 -05:00
|
|
|
if err := DeleteAlertDefinition(dashboard.Id, sess); err != nil {
|
2016-04-26 08:48:29 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-12 02:23:46 -05:00
|
|
|
return nil
|
|
|
|
})
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
2016-01-28 18:41:23 -06:00
|
|
|
|
|
|
|
func GetDashboards(query *m.GetDashboardsQuery) error {
|
|
|
|
if len(query.DashboardIds) == 0 {
|
|
|
|
return m.ErrCommandValidationFailed
|
|
|
|
}
|
|
|
|
|
2016-06-23 09:30:12 -05:00
|
|
|
var dashboards = make([]*m.Dashboard, 0)
|
2016-01-28 18:41:23 -06:00
|
|
|
|
|
|
|
err := x.In("id", query.DashboardIds).Find(&dashboards)
|
2016-06-23 09:30:12 -05:00
|
|
|
query.Result = dashboards
|
2016-01-28 18:41:23 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-17 03:01:58 -05:00
|
|
|
|
2016-07-08 02:35:06 -05:00
|
|
|
func GetDashboardsByPluginId(query *m.GetDashboardsByPluginIdQuery) error {
|
|
|
|
var dashboards = make([]*m.Dashboard, 0)
|
|
|
|
|
|
|
|
err := x.Where("org_id=? AND plugin_id=?", query.OrgId, query.PluginId).Find(&dashboards)
|
|
|
|
query.Result = dashboards
|
2016-01-28 18:41:23 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-17 03:01:58 -05:00
|
|
|
|
2016-03-20 05:52:19 -05:00
|
|
|
type DashboardSlugDTO struct {
|
|
|
|
Slug string
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:01:58 -05:00
|
|
|
func GetDashboardSlugById(query *m.GetDashboardSlugByIdQuery) error {
|
2016-03-20 05:52:19 -05:00
|
|
|
var rawSql = `SELECT slug from dashboard WHERE Id=?`
|
|
|
|
var slug = DashboardSlugDTO{}
|
|
|
|
|
|
|
|
exists, err := x.Sql(rawSql, query.Id).Get(&slug)
|
2016-03-17 03:01:58 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-03-20 05:52:19 -05:00
|
|
|
} else if exists == false {
|
2016-03-17 03:01:58 -05:00
|
|
|
return m.ErrDashboardNotFound
|
|
|
|
}
|
|
|
|
|
2016-03-20 05:52:19 -05:00
|
|
|
query.Result = slug.Slug
|
2016-03-17 03:01:58 -05:00
|
|
|
return nil
|
|
|
|
}
|