Skip to content

Commit

Permalink
Merge pull request getredash#1504 from rockwotj/rockwood/global-params
Browse files Browse the repository at this point in the history
Add: global parameters for dashboards
  • Loading branch information
arikfr authored Jan 26, 2017
2 parents 25ca061 + 11faabe commit a5d7351
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 3 deletions.
4 changes: 4 additions & 0 deletions client/app/components/parameter-settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ <h4 class="modal-title">{{$ctrl.parameter.name}}</h4>
<option value="datetime-with-seconds">Date and Time (with seconds)</option>
</select>
</div>
<div class="form-group">
<label>Global</label>
<input type="checkbox" class="form-inline" ng-model="$ctrl.parameter.global">
</div>
</div>
</div>
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: '=?',
changed: '&onChange',
},
template,
link(scope) {
// is this the correct location for this logic?
if (scope.syncValues !== false) {
scope.$watch('parameters', () => {
if (scope.changed) {
scope.changed({});
}
scope.parameters.forEach((param) => {
if (param.value !== null || param.value !== '') {
$location.search(`p_${param.name}`, param.value);
Expand Down
6 changes: 5 additions & 1 deletion client/app/pages/dashboards/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ <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>

<div ng-repeat="row in $ctrl.dashboard.widgets" class="row">
<dashboard-widget ng-repeat="widget in row" widget="widget" dashboard="$ctrl.dashboard"></dashboard-widget>
<dashboard-widget ng-repeat="widget in row" widget="widget" dashboard="$ctrl.dashboard" on-delete="$ctrl.extractGlobalParameters()"></dashboard-widget>
</div>
</div>
29 changes: 28 additions & 1 deletion client/app/pages/dashboards/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function DashboardCtrl($rootScope, $routeParams, $location, $timeout, $q, $uibMo
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,30 @@ function DashboardCtrl($rootScope, $routeParams, $location, $timeout, $q, $uibMo
}
};

this.extractGlobalParameters = () => {
let globalParams = {};
this.dashboard.widgets.forEach(row =>
row.forEach((widget) => {
widget.getQuery().getParametersDefs().filter(p => p.global).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 = () => {
this.globalParameters.forEach((global) => {
global.locals.forEach((local) => {
local.value = global.value;
});
});
};

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

this.extractGlobalParameters();

$q.all(promises).then((queryResults) => {
const filters = {};
queryResults.forEach((queryResult) => {
Expand Down Expand Up @@ -139,7 +166,7 @@ function DashboardCtrl($rootScope, $routeParams, $location, $timeout, $q, $uibMo
resolve: {
dashboard: () => this.dashboard,
},
});
}).result.then(() => this.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.localParametersDefs()"></parameters>

<div ng-switch="$ctrl.queryResult.getStatus()">
<div ng-switch-when="failed">
Expand Down
12 changes: 12 additions & 0 deletions client/app/pages/dashboards/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ function DashboardWidgetCtrl($location, $uibModal, $window, Events, currentUser)
});
};

this.localParametersDefs = () => {
if (!this.localParameters) {
this.localParameters = this.widget.query.getParametersDefs().filter(p => !p.global);
}
return this.localParameters;
};

this.deleteWidget = () => {
if (!$window.confirm(`Are you sure you want to remove "${this.widget.getName()}" from the dashboard?`)) {
return;
Expand All @@ -51,6 +58,10 @@ function DashboardWidgetCtrl($location, $uibModal, $window, Events, currentUser)

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

if (this.deleted) {
this.deleted({});
}
});
};

Expand Down Expand Up @@ -88,6 +99,7 @@ export default function (ngModule) {
widget: '<',
public: '<',
dashboard: '<',
deleted: '&onDelete',
},
});
}
1 change: 1 addition & 0 deletions client/app/services/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Parameters {
name: param,
type: 'text',
value: null,
global: false,
});
}
});
Expand Down

0 comments on commit a5d7351

Please sign in to comment.