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

[explore] set working default for MetricsControl #4803

Merged
merged 1 commit into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 10 additions & 7 deletions superset/assets/javascripts/explore/stores/controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,17 @@ export const controls = {
label: t('Metrics'),
validators: [v.nonEmpty],
default: (c) => {
const metric = mainMetric(c.options);
const metric = mainMetric(c.savedMetrics);
return metric ? [metric] : null;
},
mapStateToProps: state => ({
columns: state.datasource ? state.datasource.columns : [],
savedMetrics: state.datasource ? state.datasource.metrics : [],
datasourceType: state.datasource && state.datasource.type,
}),
mapStateToProps: (state) => {
const datasource = state.datasource;
return {
columns: datasource ? datasource.columns : [],
savedMetrics: datasource ? datasource.metrics : [],
datasourceType: datasource && datasource.type,
};
},
description: t('One or many metrics to display'),
},

Expand Down Expand Up @@ -264,7 +267,7 @@ export const controls = {
label: t('Metric'),
clearable: false,
validators: [v.nonEmpty],
default: c => mainMetric(c.options),
default: props => mainMetric(props.savedMetrics),
mapStateToProps: state => ({
columns: state.datasource ? state.datasource.columns : [],
savedMetrics: state.datasource ? state.datasource.metrics : [],
Expand Down
8 changes: 4 additions & 4 deletions superset/assets/javascripts/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,17 @@ export function getParam(name) {
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

export function mainMetric(metricOptions) {
export function mainMetric(savedMetrics) {
// Using 'count' as default metric if it exists, otherwise using whatever one shows up first
let metric;
if (metricOptions && metricOptions.length > 0) {
metricOptions.forEach((m) => {
if (savedMetrics && savedMetrics.length > 0) {
savedMetrics.forEach((m) => {
if (m.metric_name === 'count') {
metric = 'count';
}
});
if (!metric) {
metric = metricOptions[0].metric_name;
metric = savedMetrics[0].metric_name;
}
}
return metric;
Expand Down