grafana/pkg/services/sqlstore
Ben Tranter b6e46c9eb8 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 19:14:39 -04:00
..
migrations History and Version Control for Dashboard Updates 2017-05-24 19:14:39 -04:00
migrator utf8mb4 encoding (#7959) 2017-03-28 14:34:53 +02:00
sqlutil utf8mb4 encoding (#7959) 2017-03-28 14:34:53 +02:00
alert_notification_test.go tech(notifications): splitt into 3 queries 2016-09-06 08:43:15 +02:00
alert_notification.go fix(sql): Add boolstr to all dialects 2016-09-23 09:36:15 +02:00
alert_test.go tech(alerting): renames pause all method 2016-12-19 15:17:49 +01:00
alert.go fix: minor fix for bug when saving alert with empty message, fixes #7927 2017-04-10 15:26:05 +02:00
annotation.go annotation: added region support to annoations 2017-04-12 16:26:34 +02:00
apikey_test.go Added expire option to dashboard snapshots, #1623 2015-03-26 12:00:52 +01:00
apikey.go HTTP API: grafana /render calls nows with api keys, Fixes #1649 2015-04-08 08:59:12 +02:00
dashboard_snapshot_test.go feat(plugins): dashboard import for data sources is working! #4298 2016-03-12 10:13:49 +01:00
dashboard_snapshot.go refactor(backend): refactoring management of go routines in backend 2016-09-28 21:06:00 +02:00
dashboard_test.go test(dashboard): add failing test for deleting dashboards 2016-04-25 08:46:15 +02:00
dashboard_version_test.go History and Version Control for Dashboard Updates 2017-05-24 19:14:39 -04:00
dashboard_version.go History and Version Control for Dashboard Updates 2017-05-24 19:14:39 -04:00
dashboard.go History and Version Control for Dashboard Updates 2017-05-24 19:14:39 -04:00
datasource_test.go (feat) support datasource delete by name api 2017-02-10 23:30:14 +09:00
datasource.go mysql: improved mysql data source, added test feature when adding data source, fixed cache issue 2017-05-11 10:50:24 +02:00
health.go feat: added api health endpoint that does not require auth and never creates sessions, returns db status as well. #3302 2017-04-25 17:17:45 +02:00
logger.go tech: added sql logger to log all sql statements sent to sql server 2017-03-06 11:51:35 +01:00
org_test.go admin: adds paging to global user list 2017-02-13 12:59:36 +01:00
org_users.go fix(): Check Organization exists before User is added (#5302) 2016-06-08 05:46:21 +02:00
org.go Org update should throw error if not found (#7066) 2017-01-03 09:21:18 +01:00
playlist_test.go chore(playlist): remove commented code 2016-01-29 20:33:01 +01:00
playlist.go style(playlist): move dashboard access to dashboard.go 2016-01-29 01:41:23 +01:00
plugin_setting.go Config Array Syntax (#8204) 2017-04-25 09:14:29 +02:00
preferences.go Add new parameter to config: default_theme. 2016-05-12 18:21:11 +03:00
quota_test.go add unittests for quota sqltore methods. 2015-09-15 17:10:46 +08:00
quota.go move toMap function to be a method on the quota structs 2015-09-15 17:18:26 +08:00
shared.go sqlite: fixed database table looked handling, now retries up to 5 times, fixes #7992 2017-05-15 12:19:19 +02:00
sql_test_data.go mysql: added default query template 2017-04-21 16:28:01 +02:00
sqlstore.go feat: added api health endpoint that does not require auth and never creates sessions, returns db status as well. #3302 2017-04-25 17:17:45 +02:00
sqlstore.goconvey feat(invite): small progress 2015-07-17 14:42:49 +02:00
star.go Changed go package path 2015-02-05 10:37:13 +01:00
stars_test.go Fixed gofmt formating, updated precommit hook 2015-03-07 16:37:19 +01:00
stats.go feat(stats): add alerts to global admin stats 2016-11-11 14:04:38 +01:00
tables.go Began work on real sql schema definitions, and migration engine 2015-01-17 21:40:22 +01:00
temp_user_test.go feat(signup): progress on new sign up and email verification flow, #2353 2015-08-28 15:14:24 +02:00
temp_user.go feat(signup): progress on new sign up and email verification flow, #2353 2015-08-28 15:14:24 +02:00
tls_mysql.go generalized database connection cert support and added to postgres 2016-11-22 15:29:22 -08:00
user_test.go users: adds search and pagination (#7753) 2017-03-07 16:03:54 +01:00
user.go use X-Grafana-Org-Id header to ensure backend uses correct org (#8122) 2017-04-14 15:47:39 +02:00