2017-12-20 05:33:33 -06:00
|
|
|
import coreModule from 'app/core/core_module';
|
2019-02-04 04:19:45 -06:00
|
|
|
import { appEvents } from 'app/core/app_events';
|
|
|
|
import { DashboardModel } from '../state/DashboardModel';
|
2019-02-05 08:15:15 -06:00
|
|
|
import { removePanel } from '../utils/panel';
|
2020-01-21 03:08:07 -06:00
|
|
|
import { CoreEvents, DashboardMeta } from 'app/types';
|
2019-10-14 03:27:47 -05:00
|
|
|
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
|
2020-07-07 14:22:47 -05:00
|
|
|
import { backendSrv } from 'app/core/services/backend_srv';
|
2020-01-21 03:08:07 -06:00
|
|
|
import { promiseToDigest } from '../../../core/utils/promiseToDigest';
|
2020-07-07 14:22:47 -05:00
|
|
|
import { saveDashboard } from 'app/features/manage-dashboards/state/actions';
|
2016-09-19 03:08:05 -05:00
|
|
|
|
2016-10-14 06:06:29 -05:00
|
|
|
export class DashboardSrv {
|
2019-02-06 10:31:52 -06:00
|
|
|
dashboard: DashboardModel;
|
2016-09-19 03:08:05 -05:00
|
|
|
|
2016-10-14 06:06:29 -05:00
|
|
|
/** @ngInject */
|
2020-04-10 09:37:26 -05:00
|
|
|
constructor(private $rootScope: GrafanaRootScope) {
|
2019-10-14 03:27:47 -05:00
|
|
|
appEvents.on(CoreEvents.removePanel, this.onRemovePanel);
|
2019-02-04 04:19:45 -06:00
|
|
|
}
|
2016-09-19 03:08:05 -05:00
|
|
|
|
2019-04-19 17:12:35 -05:00
|
|
|
create(dashboard: any, meta: DashboardMeta) {
|
2016-10-14 06:06:29 -05:00
|
|
|
return new DashboardModel(dashboard, meta);
|
2016-09-19 03:08:05 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 08:15:15 -06:00
|
|
|
setCurrent(dashboard: DashboardModel) {
|
2019-02-06 10:31:52 -06:00
|
|
|
this.dashboard = dashboard;
|
2016-09-19 03:08:05 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 08:15:15 -06:00
|
|
|
getCurrent(): DashboardModel {
|
2019-02-06 10:31:52 -06:00
|
|
|
return this.dashboard;
|
2016-09-19 03:08:05 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 08:15:15 -06:00
|
|
|
onRemovePanel = (panelId: number) => {
|
|
|
|
const dashboard = this.getCurrent();
|
2020-07-09 08:16:35 -05:00
|
|
|
removePanel(dashboard, dashboard.getPanelById(panelId)!, true);
|
2019-02-05 08:15:15 -06:00
|
|
|
};
|
|
|
|
|
2018-04-08 13:00:18 -05:00
|
|
|
saveJSONDashboard(json: string) {
|
2020-04-04 07:55:41 -05:00
|
|
|
const parsedJson = JSON.parse(json);
|
2020-07-07 14:22:47 -05:00
|
|
|
return saveDashboard({
|
|
|
|
dashboard: parsedJson,
|
2020-04-04 07:55:41 -05:00
|
|
|
folderId: this.dashboard.meta.folderId || parsedJson.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
|
|
|
}
|
2017-11-15 08:01:44 -06:00
|
|
|
|
2019-07-30 08:49:32 -05:00
|
|
|
starDashboard(dashboardId: string, isStarred: any) {
|
2017-11-15 08:01:44 -06:00
|
|
|
let promise;
|
|
|
|
|
|
|
|
if (isStarred) {
|
2020-01-21 03:08:07 -06:00
|
|
|
promise = promiseToDigest(this.$rootScope)(
|
|
|
|
backendSrv.delete('/api/user/stars/dashboard/' + dashboardId).then(() => {
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
);
|
2017-11-15 08:01:44 -06:00
|
|
|
} else {
|
2020-01-21 03:08:07 -06:00
|
|
|
promise = promiseToDigest(this.$rootScope)(
|
|
|
|
backendSrv.post('/api/user/stars/dashboard/' + dashboardId).then(() => {
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
);
|
2017-11-15 08:01:44 -06:00
|
|
|
}
|
|
|
|
|
2019-07-30 08:49:32 -05:00
|
|
|
return promise.then((res: boolean) => {
|
2019-02-06 10:31:52 -06:00
|
|
|
if (this.dashboard && this.dashboard.id === dashboardId) {
|
|
|
|
this.dashboard.meta.isStarred = res;
|
2017-11-15 08:01:44 -06:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
}
|
2016-09-19 03:08:05 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.service('dashboardSrv', DashboardSrv);
|
2019-04-19 17:12:35 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// Code below is to export the service to react components
|
|
|
|
//
|
|
|
|
|
|
|
|
let singletonInstance: DashboardSrv;
|
|
|
|
|
|
|
|
export function setDashboardSrv(instance: DashboardSrv) {
|
|
|
|
singletonInstance = instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDashboardSrv(): DashboardSrv {
|
|
|
|
return singletonInstance;
|
|
|
|
}
|