2019-05-08 06:51:44 -05:00
|
|
|
/* tslint:disable:import-blacklist */
|
2019-12-05 03:04:03 -06:00
|
|
|
import angular from 'angular';
|
2017-12-21 06:22:20 -06:00
|
|
|
import moment from 'moment';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import $ from 'jquery';
|
|
|
|
import kbn from 'app/core/utils/kbn';
|
2019-10-14 03:27:47 -05:00
|
|
|
import { dateMath, AppEvents } from '@grafana/data';
|
2017-12-21 06:22:20 -06:00
|
|
|
import impressionSrv from 'app/core/services/impression_srv';
|
2019-07-30 08:49:32 -05:00
|
|
|
import { BackendSrv } from 'app/core/services/backend_srv';
|
|
|
|
import { DashboardSrv } from './DashboardSrv';
|
|
|
|
import DatasourceSrv from 'app/features/plugins/datasource_srv';
|
|
|
|
import { UrlQueryValue } from '@grafana/runtime';
|
2019-10-14 03:27:47 -05:00
|
|
|
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
|
2017-12-21 06:22:20 -06:00
|
|
|
|
|
|
|
export class DashboardLoaderSrv {
|
|
|
|
/** @ngInject */
|
|
|
|
constructor(
|
2019-07-30 08:49:32 -05:00
|
|
|
private backendSrv: BackendSrv,
|
|
|
|
private dashboardSrv: DashboardSrv,
|
|
|
|
private datasourceSrv: DatasourceSrv,
|
|
|
|
private $http: any,
|
|
|
|
private $timeout: any,
|
|
|
|
contextSrv: any,
|
|
|
|
private $routeParams: any,
|
2019-10-14 03:27:47 -05:00
|
|
|
private $rootScope: GrafanaRootScope
|
2017-12-21 06:22:20 -06:00
|
|
|
) {}
|
|
|
|
|
2019-07-30 08:49:32 -05:00
|
|
|
_dashboardLoadFailed(title: string, snapshot?: boolean) {
|
2017-12-21 06:22:20 -06:00
|
|
|
snapshot = snapshot || false;
|
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
canStar: false,
|
|
|
|
isSnapshot: snapshot,
|
|
|
|
canDelete: false,
|
|
|
|
canSave: false,
|
|
|
|
canEdit: false,
|
|
|
|
dashboardNotFound: true,
|
|
|
|
},
|
2019-07-30 08:49:32 -05:00
|
|
|
dashboard: { title },
|
2017-12-21 06:22:20 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-30 08:49:32 -05:00
|
|
|
loadDashboard(type: UrlQueryValue, slug: any, uid: any) {
|
2018-08-30 01:58:43 -05:00
|
|
|
let promise;
|
2017-12-21 06:22:20 -06:00
|
|
|
|
|
|
|
if (type === 'script') {
|
|
|
|
promise = this._loadScriptedDashboard(slug);
|
|
|
|
} else if (type === 'snapshot') {
|
2018-01-30 17:26:26 -06:00
|
|
|
promise = this.backendSrv.get('/api/snapshots/' + slug).catch(() => {
|
2017-12-26 05:26:50 -06:00
|
|
|
return this._dashboardLoadFailed('Snapshot not found', true);
|
|
|
|
});
|
2017-12-21 06:22:20 -06:00
|
|
|
} else {
|
|
|
|
promise = this.backendSrv
|
2018-01-30 17:26:26 -06:00
|
|
|
.getDashboardByUid(uid)
|
2019-07-30 08:49:32 -05:00
|
|
|
.then((result: any) => {
|
2017-12-21 06:22:20 -06:00
|
|
|
if (result.meta.isFolder) {
|
2019-10-14 03:27:47 -05:00
|
|
|
this.$rootScope.appEvent(AppEvents.alertError, ['Dashboard not found']);
|
2017-12-21 06:22:20 -06:00
|
|
|
throw new Error('Dashboard not found');
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
return this._dashboardLoadFailed('Not found', true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-30 08:49:32 -05:00
|
|
|
promise.then((result: any) => {
|
2017-12-21 06:22:20 -06:00
|
|
|
if (result.meta.dashboardNotFound !== true) {
|
|
|
|
impressionSrv.addDashboardImpression(result.dashboard.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2019-07-30 08:49:32 -05:00
|
|
|
_loadScriptedDashboard(file: string) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const url = 'public/dashboards/' + file.replace(/\.(?!js)/, '/') + '?' + new Date().getTime();
|
2017-12-21 06:22:20 -06:00
|
|
|
|
|
|
|
return this.$http({ url: url, method: 'GET' })
|
2017-12-27 02:36:25 -06:00
|
|
|
.then(this._executeScript.bind(this))
|
2017-12-21 06:22:20 -06:00
|
|
|
.then(
|
2019-07-30 08:49:32 -05:00
|
|
|
(result: any) => {
|
2017-12-21 06:22:20 -06:00
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
fromScript: true,
|
|
|
|
canDelete: false,
|
|
|
|
canSave: false,
|
|
|
|
canStar: false,
|
|
|
|
},
|
|
|
|
dashboard: result.data,
|
|
|
|
};
|
|
|
|
},
|
2019-07-30 08:49:32 -05:00
|
|
|
(err: any) => {
|
2017-12-21 06:22:20 -06:00
|
|
|
console.log('Script dashboard error ' + err);
|
2019-10-14 03:27:47 -05:00
|
|
|
this.$rootScope.appEvent(AppEvents.alertError, [
|
2017-12-21 06:22:20 -06:00
|
|
|
'Script Error',
|
|
|
|
'Please make sure it exists and returns a valid dashboard',
|
|
|
|
]);
|
|
|
|
return this._dashboardLoadFailed('Scripted dashboard');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-30 08:49:32 -05:00
|
|
|
_executeScript(result: any) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const services = {
|
2017-12-21 06:22:20 -06:00
|
|
|
dashboardSrv: this.dashboardSrv,
|
|
|
|
datasourceSrv: this.datasourceSrv,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*jshint -W054 */
|
2018-09-03 04:00:46 -05:00
|
|
|
const scriptFunc = new Function(
|
2017-12-21 06:22:20 -06:00
|
|
|
'ARGS',
|
|
|
|
'kbn',
|
|
|
|
'dateMath',
|
|
|
|
'_',
|
|
|
|
'moment',
|
|
|
|
'window',
|
|
|
|
'document',
|
|
|
|
'$',
|
|
|
|
'jQuery',
|
|
|
|
'services',
|
|
|
|
result.data
|
|
|
|
);
|
2018-09-03 04:00:46 -05:00
|
|
|
const scriptResult = scriptFunc(this.$routeParams, kbn, dateMath, _, moment, window, document, $, $, services);
|
2017-12-21 06:22:20 -06:00
|
|
|
|
|
|
|
// Handle async dashboard scripts
|
2018-09-03 04:00:46 -05:00
|
|
|
if (_.isFunction(scriptResult)) {
|
2019-12-05 03:04:03 -06:00
|
|
|
return new Promise(resolve => {
|
|
|
|
scriptResult((dashboard: any) => {
|
|
|
|
this.$timeout(() => {
|
|
|
|
resolve({ data: dashboard });
|
|
|
|
});
|
2017-12-21 06:22:20 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-03 04:00:46 -05:00
|
|
|
return { data: scriptResult };
|
2017-12-21 06:22:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 05:26:50 -06:00
|
|
|
angular.module('grafana.services').service('dashboardLoaderSrv', DashboardLoaderSrv);
|