mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Rewriting anb abstracting how dashboards are loaded, unifying db, json files, and script dashboards, #960
This commit is contained in:
@@ -15,30 +15,26 @@ define([
|
||||
controller : 'LoadDashboardCtrl',
|
||||
reloadOnSearch: false,
|
||||
})
|
||||
.when('/dashboard/import/:file', {
|
||||
templateUrl: 'app/partials/dashboard.html',
|
||||
controller : 'DashFromImportCtrl',
|
||||
reloadOnSearch: false,
|
||||
})
|
||||
.when('/dashboard/:type/:slug', {
|
||||
templateUrl: 'app/partials/dashboard.html',
|
||||
controller : 'LoadDashboardCtrl',
|
||||
reloadOnSearch: false,
|
||||
})
|
||||
.when('/dashboard/solo/db/:slug', {
|
||||
.when('/dashboard-solo/:type/:slug', {
|
||||
templateUrl: 'app/features/panel/partials/soloPanel.html',
|
||||
controller : 'SoloPanelCtrl',
|
||||
})
|
||||
.when('/dashboard/solo/snapshot/:key', {
|
||||
templateUrl: 'app/features/panel/partials/soloPanel.html',
|
||||
controller : 'SoloPanelCtrl',
|
||||
.when('/dashboard-import/:file', {
|
||||
templateUrl: 'app/partials/dashboard.html',
|
||||
controller : 'DashFromImportCtrl',
|
||||
reloadOnSearch: false,
|
||||
})
|
||||
.when('/dashboard/new', {
|
||||
templateUrl: 'app/partials/dashboard.html',
|
||||
controller : 'NewDashboardCtrl',
|
||||
reloadOnSearch: false,
|
||||
})
|
||||
.when('/dashboard/import', {
|
||||
.when('/import/dashboard', {
|
||||
templateUrl: 'app/features/dashboard/partials/import.html',
|
||||
controller : 'DashboardImportCtrl',
|
||||
})
|
||||
|
||||
@@ -5,96 +5,24 @@ define([
|
||||
'moment',
|
||||
'jquery',
|
||||
],
|
||||
function (angular, _, kbn, moment, $) {
|
||||
function (angular) {
|
||||
"use strict";
|
||||
|
||||
var module = angular.module('grafana.routes');
|
||||
|
||||
module.controller('LoadDashboardCtrl', function(
|
||||
$scope, $routeParams, backendSrv, dashboardSrv, datasourceSrv, $http, $q, $timeout, contextSrv) {
|
||||
module.controller('LoadDashboardCtrl', function($scope, $routeParams, dashboardLoaderSrv, backendSrv) {
|
||||
|
||||
function dashboardLoadFailed(title) {
|
||||
$scope.initDashboard({meta: {}, dashboard: {title: title}}, $scope);
|
||||
}
|
||||
|
||||
// Home dashboard
|
||||
if (!$routeParams.slug) {
|
||||
backendSrv.get('/api/dashboards/home').then(function(result) {
|
||||
var meta = result.meta;
|
||||
meta.canSave = meta.canShare = meta.canEdit = meta.canStar = false;
|
||||
$scope.initDashboard(result, $scope);
|
||||
},function() {
|
||||
dashboardLoadFailed('Not found');
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Scripted dashboards
|
||||
var execute_script = function(result) {
|
||||
var services = {
|
||||
dashboardSrv: dashboardSrv,
|
||||
datasourceSrv: datasourceSrv,
|
||||
$q: $q,
|
||||
};
|
||||
|
||||
/*jshint -W054 */
|
||||
var script_func = new Function('ARGS','kbn','_','moment','window','document','$','jQuery', 'services', result.data);
|
||||
var script_result = script_func($routeParams, kbn, _ , moment, window, document, $, $, services);
|
||||
|
||||
// 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 };
|
||||
};
|
||||
|
||||
var script_load = function(file) {
|
||||
var url = 'public/dashboards/'+file.replace(/\.(?!js)/,"/") + '?' + new Date().getTime();
|
||||
|
||||
return $http({ url: url, method: "GET" })
|
||||
.then(execute_script)
|
||||
.then(null,function(err) {
|
||||
console.log('Script dashboard error '+ err);
|
||||
$scope.appEvent('alert-error', ["Script Error", "Please make sure it exists and returns a valid dashboard"]);
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
function loadScriptedDashboard() {
|
||||
script_load($routeParams.slug).then(function(result) {
|
||||
$scope.initDashboard({
|
||||
meta: {fromScript: true, canDelete: false, canSave: false},
|
||||
dashboard: result.data
|
||||
}, $scope);
|
||||
});
|
||||
}
|
||||
|
||||
if ($routeParams.type === 'script') {
|
||||
loadScriptedDashboard();
|
||||
return;
|
||||
}
|
||||
|
||||
if ($routeParams.type === 'snapshot') {
|
||||
contextSrv.sidemenu = false;
|
||||
backendSrv.get('/api/snapshots/' + $routeParams.slug).then(function(result) {
|
||||
$scope.initDashboard(result, $scope);
|
||||
}, function() {
|
||||
$scope.initDashboard({meta:{isSnapshot: true, canSave: false, canEdit: false}, dashboard: {title: 'Snapshot not found'}}, $scope);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
return backendSrv.getDashboard($routeParams.type, $routeParams.slug).then(function(result) {
|
||||
dashboardLoaderSrv.loadDashboard($routeParams.type, $routeParams.slug).then(function(result) {
|
||||
$scope.initDashboard(result, $scope);
|
||||
}, function() {
|
||||
dashboardLoadFailed('Not found');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user