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:
parent
b3be51f17f
commit
35cc0a1cc0
@ -49,7 +49,12 @@ func GetDashboard(c *middleware.Context) {
|
||||
dash := query.Result
|
||||
dto := dtos.DashboardFullWithMeta{
|
||||
Dashboard: dash.Data,
|
||||
Meta: dtos.DashboardMeta{IsStarred: isStarred, Slug: slug, Type: m.DashTypeDB},
|
||||
Meta: dtos.DashboardMeta{
|
||||
IsStarred: isStarred,
|
||||
Slug: slug,
|
||||
Type: m.DashTypeDB,
|
||||
CanSave: c.OrgRole != m.ROLE_VIEWER,
|
||||
},
|
||||
}
|
||||
|
||||
c.JSON(200, dto)
|
||||
|
@ -1,5 +1,6 @@
|
||||
define([
|
||||
'./dashboardCtrl',
|
||||
'./dashboardLoaderSrv',
|
||||
'./dashboardNavCtrl',
|
||||
'./snapshotTopNavCtrl',
|
||||
'./saveDashboardAsCtrl',
|
||||
|
82
public/app/features/dashboard/dashboardLoaderSrv.js
Normal file
82
public/app/features/dashboard/dashboardLoaderSrv.js
Normal file
@ -0,0 +1,82 @@
|
||||
define([
|
||||
'angular',
|
||||
'moment',
|
||||
'lodash',
|
||||
'jquery',
|
||||
'kbn',
|
||||
],
|
||||
function (angular, moment, _, $, kbn) {
|
||||
'use strict';
|
||||
|
||||
var module = angular.module('grafana.services');
|
||||
|
||||
module.service('dashboardLoaderSrv', function(backendSrv,
|
||||
dashboardSrv,
|
||||
datasourceSrv,
|
||||
$http, $q, $timeout,
|
||||
contextSrv, $routeParams,
|
||||
$rootScope) {
|
||||
var self = this;
|
||||
|
||||
this._dashboardLoadFailed = function(title) {
|
||||
return {meta: {}, dashboard: {title: title}};
|
||||
};
|
||||
|
||||
this.loadDashboard = function(type, slug) {
|
||||
if (type === 'script') {
|
||||
return this._loadScriptedDashboard(slug);
|
||||
}
|
||||
|
||||
if (type === 'snapshot') {
|
||||
return backendSrv.get('/api/snapshots/' + $routeParams.slug).then(function(result) {
|
||||
return result;
|
||||
}, function() {
|
||||
return {meta:{isSnapshot: true, canSave: false, canEdit: false}, dashboard: {title: 'Snapshot not found'}};
|
||||
});
|
||||
}
|
||||
|
||||
return backendSrv.getDashboard($routeParams.type, $routeParams.slug).catch(function() {
|
||||
return self._dashboardLoadFailed("Not found");
|
||||
});
|
||||
};
|
||||
|
||||
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) {
|
||||
return { meta: { fromScript: true, canDelete: false, canSave: false}, dashboard: result.data };
|
||||
}, 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 */
|
||||
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 };
|
||||
};
|
||||
|
||||
});
|
||||
});
|
@ -15,6 +15,7 @@ function (angular, $) {
|
||||
timeSrv,
|
||||
$location,
|
||||
templateValuesSrv,
|
||||
dashboardLoaderSrv,
|
||||
contextSrv) {
|
||||
|
||||
var panelId;
|
||||
@ -25,24 +26,15 @@ function (angular, $) {
|
||||
var params = $location.search();
|
||||
panelId = parseInt(params.panelId);
|
||||
|
||||
var request;
|
||||
dashboardLoaderSrv.loadDashboard($routeParams.type, $routeParams.slug).then(function(result) {
|
||||
$scope.initDashboard(result, $scope);
|
||||
|
||||
if ($routeParams.slug) {
|
||||
request = backendSrv.getDashboard($routeParams.slug);
|
||||
} else {
|
||||
request = backendSrv.get('/api/snapshots/' + $routeParams.key);
|
||||
}
|
||||
|
||||
request.then(function(dashboard) {
|
||||
$scope.initPanelScope(dashboard);
|
||||
}).then(null, function(err) {
|
||||
$scope.appEvent('alert-error', ['Load panel error', err.message]);
|
||||
});
|
||||
|
||||
$scope.onAppEvent("dashboard-loaded", $scope.initPanelScope);
|
||||
};
|
||||
|
||||
$scope.initPanelScope = function(dashData) {
|
||||
$scope.dashboard = dashboardSrv.create(dashData.dashboard, dashData.meta);
|
||||
|
||||
$scope.initPanelScope = function() {
|
||||
$scope.row = {
|
||||
height: ($(window).height() - 10) + 'px',
|
||||
};
|
||||
|
@ -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');
|
||||
});
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user