2015-05-12 10:39:56 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'moment',
|
|
|
|
'lodash',
|
|
|
|
'jquery',
|
2015-10-30 08:44:40 -05:00
|
|
|
'app/core/utils/kbn',
|
2015-10-12 22:29:42 -05:00
|
|
|
'app/core/utils/datemath',
|
2016-02-29 12:51:35 -06:00
|
|
|
'./impression_store',
|
2016-03-01 05:05:35 -06:00
|
|
|
'app/core/config',
|
2015-05-12 10:39:56 -05:00
|
|
|
],
|
2016-03-05 05:26:21 -06:00
|
|
|
function (angular, moment, _, $, kbn, dateMath, impressionStore) {
|
2015-05-12 10:39:56 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.services');
|
|
|
|
|
|
|
|
module.service('dashboardLoaderSrv', function(backendSrv,
|
|
|
|
dashboardSrv,
|
|
|
|
datasourceSrv,
|
|
|
|
$http, $q, $timeout,
|
|
|
|
contextSrv, $routeParams,
|
|
|
|
$rootScope) {
|
|
|
|
var self = this;
|
|
|
|
|
2016-03-01 05:05:35 -06:00
|
|
|
this._dashboardLoadFailed = function(title, snapshot) {
|
|
|
|
snapshot = snapshot || false;
|
|
|
|
return {
|
|
|
|
meta: { canStar: false, isSnapshot: snapshot, canDelete: false, canSave: false, canEdit: false, dashboardNotFound: true },
|
|
|
|
dashboard: {title: title }
|
|
|
|
};
|
2015-05-12 10:39:56 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
this.loadDashboard = function(type, slug) {
|
2016-02-26 03:09:38 -06:00
|
|
|
var promise;
|
2015-05-12 10:39:56 -05:00
|
|
|
|
2016-02-26 03:09:38 -06:00
|
|
|
if (type === 'script') {
|
|
|
|
promise = this._loadScriptedDashboard(slug);
|
|
|
|
} else if (type === 'snapshot') {
|
2016-03-01 05:05:35 -06:00
|
|
|
promise = backendSrv.get('/api/snapshots/' + $routeParams.slug)
|
|
|
|
.catch(function() {
|
|
|
|
return self._dashboardLoadFailed("Snapshot not found", true);
|
|
|
|
});
|
2016-02-26 03:09:38 -06:00
|
|
|
} else {
|
|
|
|
promise = backendSrv.getDashboard($routeParams.type, $routeParams.slug)
|
|
|
|
.catch(function() {
|
|
|
|
return self._dashboardLoadFailed("Not found");
|
|
|
|
});
|
2015-05-12 10:39:56 -05:00
|
|
|
}
|
|
|
|
|
2016-02-26 03:09:38 -06:00
|
|
|
promise.then(function(result) {
|
2016-06-07 23:51:01 -05:00
|
|
|
|
2016-03-01 05:05:35 -06:00
|
|
|
if (result.meta.dashboardNotFound !== true) {
|
2016-03-05 05:26:21 -06:00
|
|
|
impressionStore.impressions.addDashboardImpression(result.dashboard.id);
|
2016-03-01 05:05:35 -06:00
|
|
|
}
|
|
|
|
|
2016-02-26 03:09:38 -06:00
|
|
|
return result;
|
2015-05-12 10:39:56 -05:00
|
|
|
});
|
2016-02-26 03:09:38 -06:00
|
|
|
|
|
|
|
return promise;
|
2015-05-12 10:39:56 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
this._loadScriptedDashboard = function(file) {
|
|
|
|
var url = 'public/dashboards/'+file.replace(/\.(?!js)/,"/") + '?' + new Date().getTime();
|
|
|
|
|
|
|
|
return $http({ url: url, method: "GET" })
|
|
|
|
.then(this._executeScript).then(function(result) {
|
2015-05-13 02:58:45 -05:00
|
|
|
return { meta: { fromScript: true, canDelete: false, canSave: false, canStar: false}, dashboard: result.data };
|
2015-05-12 10:39:56 -05:00
|
|
|
}, function(err) {
|
|
|
|
console.log('Script dashboard error '+ err);
|
|
|
|
$rootScope.appEvent('alert-error', ["Script Error", "Please make sure it exists and returns a valid dashboard"]);
|
|
|
|
return self._dashboardLoadFailed('Scripted dashboard');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this._executeScript = function(result) {
|
|
|
|
var services = {
|
|
|
|
dashboardSrv: dashboardSrv,
|
|
|
|
datasourceSrv: datasourceSrv,
|
|
|
|
$q: $q,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*jshint -W054 */
|
2015-10-12 22:29:42 -05:00
|
|
|
var script_func = new Function('ARGS','kbn','dateMath','_','moment','window','document','$','jQuery', 'services', result.data);
|
|
|
|
var script_result = script_func($routeParams, kbn, dateMath, _ , moment, window, document, $, $, services);
|
2015-05-12 10:39:56 -05:00
|
|
|
|
|
|
|
// Handle async dashboard scripts
|
|
|
|
if (_.isFunction(script_result)) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
script_result(function(dashboard) {
|
|
|
|
$timeout(function() {
|
|
|
|
deferred.resolve({ data: dashboard });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { data: script_result };
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|