From 97f5ed892298f044afb50a8feff7a7cc0a61a070 Mon Sep 17 00:00:00 2001
From: Tom Robinson <tlrobinson@gmail.com>
Date: Thu, 10 May 2018 11:01:30 -0700
Subject: [PATCH] Distribution drill

---
 frontend/src/metabase-lib/lib/Question.js     |  4 +++
 .../qb/components/drill/DistributionDrill.jsx | 33 +++++++++++++++++++
 .../qb/components/modes/SegmentMode.jsx       |  2 ++
 frontend/src/metabase/qb/lib/actions.js       | 18 ++++++++++
 4 files changed, 57 insertions(+)
 create mode 100644 frontend/src/metabase/qb/components/drill/DistributionDrill.jsx

diff --git a/frontend/src/metabase-lib/lib/Question.js b/frontend/src/metabase-lib/lib/Question.js
index c319b89fec0..18d9a4e6535 100644
--- a/frontend/src/metabase-lib/lib/Question.js
+++ b/frontend/src/metabase-lib/lib/Question.js
@@ -21,6 +21,7 @@ import {
   pivot,
   filter,
   breakout,
+  distribution,
   toUnderlyingRecords,
   drillUnderlyingRecords,
 } from "metabase/qb/lib/actions";
@@ -298,6 +299,9 @@ export default class Question {
   toUnderlyingData(): Question {
     return this.setDisplay("table");
   }
+  distribution(column) {
+    return this.setCard(distribution(this.card(), column));
+  }
 
   composeThisQuery(): ?Question {
     const SAVED_QUESTIONS_FAUX_DATABASE = -1337;
diff --git a/frontend/src/metabase/qb/components/drill/DistributionDrill.jsx b/frontend/src/metabase/qb/components/drill/DistributionDrill.jsx
new file mode 100644
index 00000000000..9f849f122b7
--- /dev/null
+++ b/frontend/src/metabase/qb/components/drill/DistributionDrill.jsx
@@ -0,0 +1,33 @@
+/* @flow */
+
+import { t } from "c-3po";
+import type {
+  ClickAction,
+  ClickActionProps,
+} from "metabase/meta/types/Visualization";
+import { isPK } from "metabase/lib/schema_metadata";
+
+export default ({ question, clicked }: ClickActionProps): ClickAction[] => {
+  if (
+    !clicked ||
+    !clicked.column ||
+    clicked.value !== undefined ||
+    clicked.column.source !== "fields"
+  ) {
+    return [];
+  }
+  const { column } = clicked;
+
+  if (!isPK(column)) {
+    return [
+      {
+        name: "distribution",
+        title: t`Distribution`,
+        section: "averages",
+        question: () => question.distribution(column),
+      },
+    ];
+  } else {
+    return [];
+  }
+};
diff --git a/frontend/src/metabase/qb/components/modes/SegmentMode.jsx b/frontend/src/metabase/qb/components/modes/SegmentMode.jsx
index 8ea44ec54df..08ab4259a8d 100644
--- a/frontend/src/metabase/qb/components/modes/SegmentMode.jsx
+++ b/frontend/src/metabase/qb/components/modes/SegmentMode.jsx
@@ -9,6 +9,7 @@ import CountByTimeAction from "../actions/CountByTimeAction";
 import SummarizeColumnDrill from "../drill/SummarizeColumnDrill";
 import SummarizeColumnByTimeDrill from "../drill/SummarizeColumnByTimeDrill";
 import CountByColumnDrill from "../drill/CountByColumnDrill";
+import DistributionDrill from "../drill/DistributionDrill";
 // import PlotSegmentField from "../actions/PlotSegmentField";
 
 import type { QueryMode } from "metabase/meta/types/Visualization";
@@ -28,6 +29,7 @@ const SegmentMode: QueryMode = {
     SummarizeColumnDrill,
     SummarizeColumnByTimeDrill,
     CountByColumnDrill,
+    DistributionDrill,
   ],
 };
 
diff --git a/frontend/src/metabase/qb/lib/actions.js b/frontend/src/metabase/qb/lib/actions.js
index 678956f2ee6..1e2fcdbad07 100644
--- a/frontend/src/metabase/qb/lib/actions.js
+++ b/frontend/src/metabase/qb/lib/actions.js
@@ -16,6 +16,7 @@ import {
   isState,
   isCountry,
   isCoordinate,
+  isNumber,
 } from "metabase/lib/schema_metadata";
 import Utils from "metabase/lib/utils";
 
@@ -232,6 +233,23 @@ export const breakout = (card, breakout, tableMetadata) => {
   return newCard;
 };
 
+export const distribution = (card, column) => {
+  const breakout = isDate(column)
+    ? ["datetime-field", getFieldRefFromColumn(column), "month"]
+    : isNumber(column)
+      ? ["binning-strategy", getFieldRefFromColumn(column), "default"]
+      : getFieldRefFromColumn(column);
+
+  const newCard = startNewCard("query");
+  newCard.dataset_query = Utils.copy(card.dataset_query);
+  newCard.dataset_query.query.aggregation = [["count"]];
+  newCard.dataset_query.query.breakout = [breakout];
+  delete newCard.dataset_query.query.order_by;
+  delete newCard.dataset_query.query.fields;
+  newCard.display = "bar";
+  return newCard;
+};
+
 // min number of points when switching units
 const MIN_INTERVALS = 4;
 
-- 
GitLab