Skip to content
Snippets Groups Projects
Commit 81229983 authored by Atte Keinänen's avatar Atte Keinänen
Browse files

Add 'Add a metric' action which converts question to multiquery

parent d0b78617
No related branches found
No related tags found
No related merge requests found
......@@ -40,10 +40,10 @@ function createChildQuery(question: Question, datasetQuery: ChildDatasetQuery):
* Because each query contained by MultiDatasetQuery should have just a single aggregation, StructuredQueries
* with two or more aggregations are broken into queries with one of those aggregations in each.
*/
export function convertToMultiDatasetQuery(datasetQuery: DatasetQuery) {
export function convertToMultiDatasetQuery(question: Question, datasetQuery: DatasetQuery) {
const getConvertedQueries = () => {
if (isStructuredDatasetQuery(datasetQuery)) {
const structuredQuery: StructuredQuery = new StructuredQuery(null, datasetQuery);
const structuredQuery: StructuredQuery = new StructuredQuery(question, datasetQuery);
const aggregations = structuredQuery.aggregations();
const isMultiAggregationQuery = aggregations.length > 1;
......@@ -56,10 +56,11 @@ export function convertToMultiDatasetQuery(datasetQuery: DatasetQuery) {
return aggregations.map((aggregation) => (
structuredQuery.clearAggregations().addAggregation(aggregation).datasetQuery()
));
} else {
return [datasetQuery];
}
} else {
// Just bypass the native/structured query
return [datasetQuery];
throw new Error("Native queries can't yet be converted to MultiDatasetQuery")
}
}
......
......@@ -32,6 +32,11 @@ export default class Query {
return false;
}
// TODO: Decide the behavior of isEditable for multimetric questions
isEditable(): boolean {
return true;
}
/**
* Returns the dataset_query object underlying this Query
*/
......
......@@ -184,11 +184,12 @@ export default class Question {
return this.query().isMulti();
}
canConvertToMultiQuery(): boolean {
return true;
const query = this.query();
return query instanceof StructuredQuery && !query.isBareRows();
}
convertToMultiQuery(): Question {
// TODO Atte Keinänen 6/6/17: I want to be 99% sure that this doesn't corrupt the question in any scenario
const multiDatasetQuery = convertToMultiDatasetQuery(this._card.dataset_query);
const multiDatasetQuery = convertToMultiDatasetQuery(this, this._card.dataset_query);
return this.setCard(assoc(this._card, "dataset_query", multiDatasetQuery));
}
......
/* @flow */
import StructuredQuery from "metabase-lib/lib/StructuredQuery";
import type {
ClickAction,
ClickActionProps
} from "metabase/meta/types/Visualization";
export default ({ question }: ClickActionProps): ClickAction[] => {
const isBareRows = question.query() instanceof StructuredQuery && question.query().isBareRows()
if (question.canConvertToMultiQuery()) {
return [
{
name: "add-metric",
title: "Add a metric",
icon: "add",
question: () => question.convertToMultiQuery()
}
];
}
return [];
};
/* @flow */
import AddMetricAction from "./AddMetricAction";
import UnderlyingDataAction from "./UnderlyingDataAction";
import UnderlyingRecordsAction from "./UnderlyingRecordsAction";
export const DEFAULT_ACTIONS = [UnderlyingDataAction, UnderlyingRecordsAction];
export const DEFAULT_ACTIONS = [AddMetricAction, UnderlyingDataAction, UnderlyingRecordsAction];
......@@ -15,7 +15,7 @@ const MetricList = ({...props}) => {
const metrics = question.singleQueries();
const metricColors = getCardColors(question.card());
const showAddMetricButton = !hideAddButton && !question.query().isBareRows();
const showAddMetricButton = !hideAddButton;
const canAddMetricToVisualization = _.contains(["line", "area", "bar"], question.display());
const metricsAreRemovable = !hideClearButton && question.canRemoveMetric();
......
......@@ -186,12 +186,12 @@ export default class QuestionHeader extends Component {
onSetCardAttribute
} = this.props;
const database = question && question.query().database();
const card = question.card();
// TODO Atte Keinänen 5/20/17 Add multi-query support to all components that need metadata
// This only fetches the table metadata of first available query
const tableMetadata = question.query().tableMetadata();
// These only fetch the database and table metadata of first available query
const tableMetadata = question.singleQueries()[0].tableMetadata();
const database = question && question.singleQueries()[0].database();
const SaveNewCardButton = () =>
<ModalWithTrigger
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment