grafana/public/app/features/dashboard/services/DashboardLoaderSrv.ts

144 lines
3.9 KiB
TypeScript
Raw Normal View History

/* tslint:disable:import-blacklist */
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';
import { dateMath, AppEvents } from '@grafana/data';
2017-12-21 06:22:20 -06:00
import impressionSrv from 'app/core/services/impression_srv';
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';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
2017-12-21 06:22:20 -06:00
export class DashboardLoaderSrv {
/** @ngInject */
constructor(
private backendSrv: BackendSrv,
private dashboardSrv: DashboardSrv,
private datasourceSrv: DatasourceSrv,
private $http: any,
private $timeout: any,
contextSrv: any,
private $routeParams: any,
private $rootScope: GrafanaRootScope
2017-12-21 06:22:20 -06: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,
},
dashboard: { title },
2017-12-21 06:22:20 -06:00
};
}
loadDashboard(type: UrlQueryValue, slug: any, uid: any) {
let promise;
2017-12-21 06:22:20 -06:00
if (type === 'script') {
promise = this._loadScriptedDashboard(slug);
} else if (type === 'snapshot') {
promise = this.backendSrv.get('/api/snapshots/' + slug).catch(() => {
return this._dashboardLoadFailed('Snapshot not found', true);
});
2017-12-21 06:22:20 -06:00
} else {
promise = this.backendSrv
.getDashboardByUid(uid)
.then((result: any) => {
2017-12-21 06:22:20 -06:00
if (result.meta.isFolder) {
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);
});
}
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;
}
_loadScriptedDashboard(file: string) {
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' })
.then(this._executeScript.bind(this))
2017-12-21 06:22:20 -06:00
.then(
(result: any) => {
2017-12-21 06:22:20 -06:00
return {
meta: {
fromScript: true,
canDelete: false,
canSave: false,
canStar: false,
},
dashboard: result.data,
};
},
(err: any) => {
2017-12-21 06:22:20 -06:00
console.log('Script dashboard error ' + err);
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');
}
);
}
_executeScript(result: any) {
const services = {
2017-12-21 06:22:20 -06:00
dashboardSrv: this.dashboardSrv,
datasourceSrv: this.datasourceSrv,
};
/*jshint -W054 */
const scriptFunc = new Function(
2017-12-21 06:22:20 -06:00
'ARGS',
'kbn',
'dateMath',
'_',
'moment',
'window',
'document',
'$',
'jQuery',
'services',
result.data
);
const scriptResult = scriptFunc(this.$routeParams, kbn, dateMath, _, moment, window, document, $, $, services);
2017-12-21 06:22:20 -06:00
// Handle async dashboard scripts
if (_.isFunction(scriptResult)) {
return new Promise(resolve => {
scriptResult((dashboard: any) => {
this.$timeout(() => {
resolve({ data: dashboard });
});
2017-12-21 06:22:20 -06:00
});
});
}
return { data: scriptResult };
2017-12-21 06:22:20 -06:00
}
}
angular.module('grafana.services').service('dashboardLoaderSrv', DashboardLoaderSrv);