Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: global parameters for dashboards #1504

Merged
merged 6 commits into from
Jan 26, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/app/components/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ function ParametersDirective($location, $uibModal) {
parameters: '=',
syncValues: '=?',
editable: '=?',
change: '&onChange',
},
template,
link(scope) {
// is this the correct location for this logic?
if (scope.syncValues !== false) {
scope.$watch('parameters', () => {
if (scope.change) {
scope.change({});
}
scope.parameters.forEach((param) => {
if (param.value !== null || param.value !== '') {
$location.search(`p_${param.name}`, param.value);
Expand Down
4 changes: 4 additions & 0 deletions client/app/pages/dashboards/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ <h3>
This dashboard is archived and won't appear in the dashboards list or search results.
</div>

<div class="m-b-5">
<parameters parameters="$ctrl.globalParameters" on-change="$ctrl.onGlobalParametersChange()"></parameters>
</div>

<div class="m-b-5">
<filters ng-if="$ctrl.dashboard.dashboard_filters_enabled" filters="$ctrl.filters" on-change="$ctrl.filtersOnChange(filter, $modal)"></filters>
</div>
Expand Down
33 changes: 31 additions & 2 deletions client/app/pages/dashboards/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import * as _ from 'underscore';
import template from './dashboard.html';
import shareDashboardTemplate from './share-dashboard.html';

function DashboardCtrl($rootScope, $routeParams, $location, $timeout, $q, $uibModal,
function DashboardCtrl($rootScope, $scope, $routeParams, $location, $timeout, $q, $uibModal,
Title, AlertDialog, Dashboard, currentUser, clientConfig, Events) {
this.isFullscreen = false;
this.refreshRate = null;
this.showPermissionsControl = clientConfig.showPermissionsControl;
this.currentUser = currentUser;
this.globalParameters = [];
this.refreshRates = [
{ name: '10 seconds', rate: 10 },
{ name: '30 seconds', rate: 30 },
Expand All @@ -26,6 +27,32 @@ function DashboardCtrl($rootScope, $routeParams, $location, $timeout, $q, $uibMo
}
};

const extractGlobalParameters = () => {
let globalParams = {};
this.dashboard.widgets.forEach(row =>
row.forEach((widget) => {
widget.getQuery().getGlobalParametersDefs().forEach((param) => {
const defaults = {};
defaults[param.name] = _.clone(param);
defaults[param.name].locals = [];
globalParams = _.defaults(globalParams, defaults);
globalParams[param.name].locals.push(param);
});
})
);
this.globalParameters = _.values(globalParams);
};

this.onGlobalParametersChange = () => {
_.each(this.globalParameters, (global) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use forEach here and for global.locals.

_.each(global.locals, (local) => {
local.value = global.value;
});
});
};

$scope.$on('deleteDashboardWidget', extractGlobalParameters);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using an event, let's add a callback function to the widget component.


const renderDashboard = (dashboard, force) => {
Title.set(dashboard.name);
const promises = [];
Expand All @@ -42,6 +69,8 @@ function DashboardCtrl($rootScope, $routeParams, $location, $timeout, $q, $uibMo
})
);

extractGlobalParameters();

$q.all(promises).then((queryResults) => {
const filters = {};
queryResults.forEach((queryResult) => {
Expand Down Expand Up @@ -139,7 +168,7 @@ function DashboardCtrl($rootScope, $routeParams, $location, $timeout, $q, $uibMo
resolve: {
dashboard: () => this.dashboard,
},
});
}).result.then(() => extractGlobalParameters());
};

this.toggleFullscreen = () => {
Expand Down
2 changes: 1 addition & 1 deletion client/app/pages/dashboards/widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</div>
</div>

<parameters parameters="$ctrl.widget.query.getParametersDefs()"></parameters>
<parameters parameters="$ctrl.widget.query.getLocalParametersDefs()"></parameters>

<div ng-switch="$ctrl.queryResult.getStatus()">
<div ng-switch-when="failed">
Expand Down
4 changes: 3 additions & 1 deletion client/app/pages/dashboards/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const EditTextBoxComponent = {
},
};

function DashboardWidgetCtrl($location, $uibModal, $window, Events, currentUser) {
function DashboardWidgetCtrl($location, $uibModal, $window, $scope, Events, currentUser) {
this.canViewQuery = currentUser.hasPermission('view_query');

this.editTextBox = () => {
Expand All @@ -51,6 +51,8 @@ function DashboardWidgetCtrl($location, $uibModal, $window, Events, currentUser)

this.dashboard.layout = response.layout;
this.dashboard.version = response.version;

$scope.$emit('deleteDashboardWidget');
});
};

Expand Down
15 changes: 15 additions & 0 deletions client/app/services/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,21 @@ function QueryResource($resource, $http, $q, $location, currentUser, QueryResult
return this.getParameters().get();
};

Query.prototype.getLocalParametersDefs = function getLocalParametersDefs() {
if (!this.$localParameters) {
this.$localParameters = this.getParametersDefs().filter(p => p.name[0] !== '$');
}

return this.$localParameters;
};

Query.prototype.getGlobalParametersDefs = function getGlobalParametersDefs() {
if (!this.$globalParameters) {
this.$globalParameters = this.getParametersDefs().filter(p => p.name[0] === '$');
}
return this.$globalParameters;
};

return Query;
}

Expand Down