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:
@@ -49,7 +49,12 @@ func GetDashboard(c *middleware.Context) {
|
|||||||
dash := query.Result
|
dash := query.Result
|
||||||
dto := dtos.DashboardFullWithMeta{
|
dto := dtos.DashboardFullWithMeta{
|
||||||
Dashboard: dash.Data,
|
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)
|
c.JSON(200, dto)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
define([
|
define([
|
||||||
'./dashboardCtrl',
|
'./dashboardCtrl',
|
||||||
|
'./dashboardLoaderSrv',
|
||||||
'./dashboardNavCtrl',
|
'./dashboardNavCtrl',
|
||||||
'./snapshotTopNavCtrl',
|
'./snapshotTopNavCtrl',
|
||||||
'./saveDashboardAsCtrl',
|
'./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,
|
timeSrv,
|
||||||
$location,
|
$location,
|
||||||
templateValuesSrv,
|
templateValuesSrv,
|
||||||
|
dashboardLoaderSrv,
|
||||||
contextSrv) {
|
contextSrv) {
|
||||||
|
|
||||||
var panelId;
|
var panelId;
|
||||||
@@ -25,24 +26,15 @@ function (angular, $) {
|
|||||||
var params = $location.search();
|
var params = $location.search();
|
||||||
panelId = parseInt(params.panelId);
|
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.initPanelScope = function() {
|
||||||
$scope.dashboard = dashboardSrv.create(dashData.dashboard, dashData.meta);
|
|
||||||
|
|
||||||
$scope.row = {
|
$scope.row = {
|
||||||
height: ($(window).height() - 10) + 'px',
|
height: ($(window).height() - 10) + 'px',
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -15,30 +15,26 @@ define([
|
|||||||
controller : 'LoadDashboardCtrl',
|
controller : 'LoadDashboardCtrl',
|
||||||
reloadOnSearch: false,
|
reloadOnSearch: false,
|
||||||
})
|
})
|
||||||
.when('/dashboard/import/:file', {
|
|
||||||
templateUrl: 'app/partials/dashboard.html',
|
|
||||||
controller : 'DashFromImportCtrl',
|
|
||||||
reloadOnSearch: false,
|
|
||||||
})
|
|
||||||
.when('/dashboard/:type/:slug', {
|
.when('/dashboard/:type/:slug', {
|
||||||
templateUrl: 'app/partials/dashboard.html',
|
templateUrl: 'app/partials/dashboard.html',
|
||||||
controller : 'LoadDashboardCtrl',
|
controller : 'LoadDashboardCtrl',
|
||||||
reloadOnSearch: false,
|
reloadOnSearch: false,
|
||||||
})
|
})
|
||||||
.when('/dashboard/solo/db/:slug', {
|
.when('/dashboard-solo/:type/:slug', {
|
||||||
templateUrl: 'app/features/panel/partials/soloPanel.html',
|
templateUrl: 'app/features/panel/partials/soloPanel.html',
|
||||||
controller : 'SoloPanelCtrl',
|
controller : 'SoloPanelCtrl',
|
||||||
})
|
})
|
||||||
.when('/dashboard/solo/snapshot/:key', {
|
.when('/dashboard-import/:file', {
|
||||||
templateUrl: 'app/features/panel/partials/soloPanel.html',
|
templateUrl: 'app/partials/dashboard.html',
|
||||||
controller : 'SoloPanelCtrl',
|
controller : 'DashFromImportCtrl',
|
||||||
|
reloadOnSearch: false,
|
||||||
})
|
})
|
||||||
.when('/dashboard/new', {
|
.when('/dashboard/new', {
|
||||||
templateUrl: 'app/partials/dashboard.html',
|
templateUrl: 'app/partials/dashboard.html',
|
||||||
controller : 'NewDashboardCtrl',
|
controller : 'NewDashboardCtrl',
|
||||||
reloadOnSearch: false,
|
reloadOnSearch: false,
|
||||||
})
|
})
|
||||||
.when('/dashboard/import', {
|
.when('/import/dashboard', {
|
||||||
templateUrl: 'app/features/dashboard/partials/import.html',
|
templateUrl: 'app/features/dashboard/partials/import.html',
|
||||||
controller : 'DashboardImportCtrl',
|
controller : 'DashboardImportCtrl',
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,96 +5,24 @@ define([
|
|||||||
'moment',
|
'moment',
|
||||||
'jquery',
|
'jquery',
|
||||||
],
|
],
|
||||||
function (angular, _, kbn, moment, $) {
|
function (angular) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var module = angular.module('grafana.routes');
|
var module = angular.module('grafana.routes');
|
||||||
|
|
||||||
module.controller('LoadDashboardCtrl', function(
|
module.controller('LoadDashboardCtrl', function($scope, $routeParams, dashboardLoaderSrv, backendSrv) {
|
||||||
$scope, $routeParams, backendSrv, dashboardSrv, datasourceSrv, $http, $q, $timeout, contextSrv) {
|
|
||||||
|
|
||||||
function dashboardLoadFailed(title) {
|
|
||||||
$scope.initDashboard({meta: {}, dashboard: {title: title}}, $scope);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Home dashboard
|
|
||||||
if (!$routeParams.slug) {
|
if (!$routeParams.slug) {
|
||||||
backendSrv.get('/api/dashboards/home').then(function(result) {
|
backendSrv.get('/api/dashboards/home').then(function(result) {
|
||||||
var meta = result.meta;
|
var meta = result.meta;
|
||||||
meta.canSave = meta.canShare = meta.canEdit = meta.canStar = false;
|
meta.canSave = meta.canShare = meta.canEdit = meta.canStar = false;
|
||||||
$scope.initDashboard(result, $scope);
|
$scope.initDashboard(result, $scope);
|
||||||
},function() {
|
|
||||||
dashboardLoadFailed('Not found');
|
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scripted dashboards
|
dashboardLoaderSrv.loadDashboard($routeParams.type, $routeParams.slug).then(function(result) {
|
||||||
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) {
|
|
||||||
$scope.initDashboard(result, $scope);
|
$scope.initDashboard(result, $scope);
|
||||||
}, function() {
|
|
||||||
dashboardLoadFailed('Not found');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user