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 5 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/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().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 = () => {
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.widget.query.getLocalParametersDefs()"></parameters>

<div ng-switch="$ctrl.queryResult.getStatus()">
<div ng-switch-when="failed">
Expand Down
5 changes: 5 additions & 0 deletions client/app/pages/dashboards/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 +92,7 @@ export default function (ngModule) {
widget: '<',
public: '<',
dashboard: '<',
deleted: '&onDelete',
},
});
}
16 changes: 16 additions & 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 Expand Up @@ -293,6 +294,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.global);
Copy link
Member

Choose a reason for hiding this comment

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

It's safe to cache because it's used only in dashboards, where the query won't change - right?

Maybe we should move these two functions to the dashboard code, to avoid someone using it "by accident" in another context?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It must be cached or angular can't detect that the state isn't changing. The normal parameters are cached, so why is this case any different?

https://github.com/getredash/redash/pull/1504/files/cb2e6fddf44d32fc22d54e5f2a2adffcbb44dce1#diff-35fc04aab5b4f115145224036f087c3cR285

Copy link
Member

Choose a reason for hiding this comment

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

Normal parameters aren't cached -- whenever we call Parameters.get it calls Parameters.updateParameters (which is a no-op in case the query text didn't change). Or are we talking about different things?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I see. The parameters object is cached, but I didn't look into what get did. I'll move it to dashboard.

}

return this.$localParameters;
};

Query.prototype.getGlobalParametersDefs = function getGlobalParametersDefs() {
if (!this.$globalParameters) {
this.$globalParameters = this.getParametersDefs().filter(p => p.global);
}
return this.$globalParameters;
};

return Query;
}

Expand Down