From beff2d326cac87a4125d14b82fa9a26e65ae2187 Mon Sep 17 00:00:00 2001
From: Simon Belak <simon@metabase.com>
Date: Wed, 28 Aug 2019 18:07:24 +0100
Subject: [PATCH] Insights: include unit in response (#10740)

---
 .../visualizations/SmartScalar.jsx            |  8 +-
 .../sync/analyze/fingerprint/insights.clj     |  3 +-
 .../analyze/fingerprint/insights_test.clj     | 81 +++++++++----------
 3 files changed, 40 insertions(+), 52 deletions(-)

diff --git a/frontend/src/metabase/visualizations/visualizations/SmartScalar.jsx b/frontend/src/metabase/visualizations/visualizations/SmartScalar.jsx
index bb01561390b..cc1de87f982 100644
--- a/frontend/src/metabase/visualizations/visualizations/SmartScalar.jsx
+++ b/frontend/src/metabase/visualizations/visualizations/SmartScalar.jsx
@@ -92,12 +92,6 @@ export default class Smart extends React.Component {
     const lastRow = rows[rows.length - 1];
     const value = lastRow && lastRow[metricIndex];
     const column = cols[metricIndex];
-    const dimensionColumn = cols[dimensionIndex];
-
-    const granularity =
-      dimensionColumn && dimensionColumn.unit
-        ? formatBucketing(dimensionColumn.unit).toLowerCase()
-        : null;
 
     const insights =
       rawSeries && rawSeries[0].data && rawSeries[0].data.insights;
@@ -106,6 +100,8 @@ export default class Smart extends React.Component {
       return null;
     }
 
+    const granularity = formatBucketing(insight["unit"]).toLowerCase();
+
     const lastChange = insight["last-change"];
     const previousValue = insight["previous-value"];
 
diff --git a/src/metabase/sync/analyze/fingerprint/insights.clj b/src/metabase/sync/analyze/fingerprint/insights.clj
index dc66149ea74..d13a918beef 100644
--- a/src/metabase/sync/analyze/fingerprint/insights.clj
+++ b/src/metabase/sync/analyze/fingerprint/insights.clj
@@ -203,7 +203,8 @@
                    :slope          slope
                    :offset         offset
                    :best-fit       best-fit
-                   :col            (:name number-col)})))))))
+                   :col            (:name number-col)
+                   :unit           unit})))))))
 
 (defn- datetime-truncated-to-year?
   "This is hackish as hell, but we change datetimes with year granularity to strings upstream and
diff --git a/test/metabase/sync/analyze/fingerprint/insights_test.clj b/test/metabase/sync/analyze/fingerprint/insights_test.clj
index 83077efb160..d607fdba5bd 100644
--- a/test/metabase/sync/analyze/fingerprint/insights_test.clj
+++ b/test/metabase/sync/analyze/fingerprint/insights_test.clj
@@ -84,56 +84,47 @@
 
 ;; Make sure we don't return nosense results like infinitiy coeficients
 ;; Fixes https://github.com/metabase/metabase/issues/9070
-(def ^:private ts [["2018-11-01",2960,10875]
-                   ["2018-11-02",2574,11762]
-                   ["2018-11-03",2761,13101]
-                   ["2018-11-04",2405,12931]
-                   ["2018-11-05",1726,10890]
-                   ["2018-11-06",1669,10829]
-                   ["2018-11-07",3661,10098]
-                   ["2018-11-08",5760,12935]
-                   ["2018-11-09",5251,30183]
-                   ["2018-11-10",5757,36148]
-                   ["2018-11-11",5244,32264]
-                   ["2018-11-12",4190,25583]
-                   ["2018-11-13",2343,21411]
-                   ["2018-11-14",2109,21848]
-                   ["2018-11-15",1865,19892]
-                   ["2018-11-16",2130,14942]
-                   ["2018-11-17",5037,15690]
-                   ["2018-11-18",5029,14506]
-                   ["2018-11-19",2335,10714]
-                   ["2018-11-20",1745,9545]
-                   ["2018-11-21",1784,7516]
-                   ["2018-11-22",1717,6460]
-                   ["2018-11-23",1796,4901]
-                   ["2018-11-24",2039,5217]
-                   ["2018-11-25",1781,4477]
-                   ["2018-11-26",1330,3263]
-                   ["2018-11-27",1296,2994]
-                   ["2018-11-28",1278,3238]
-                   ["2018-11-29",1377,3120]
-                   ["2018-11-30",1553,2984]
-                   ["2018-12-01",1805,3732]
-                   ["2018-12-02",1796,3311]
-                   ["2018-12-03",1444,2525]])
 
-;; disabled for now
-#_(expect
-  [{:last-value     1444,
-    :previous-value 1796,
-    :last-change    -0.19599109131403117,
-    :slope          -73.10260695187168,
-    :offset         1307680.6786987525,
-    :best-fit       [:+ 1307680.6786987525 [:* -73.10260695187168 :x]],
+;; Keep the size of this dataset below `i/validation-set-size` else result might depend on which
+;; data points are included in the sample, producing intermittent test failures
+(def ^:private ts [["2018-11-01",296,10875]
+                   ["2018-11-02",257,11762]
+                   ["2018-11-03",276,13101]
+                   ["2018-11-05",172,10890]
+                   ["2018-11-08",576,12935]
+                   ["2018-11-09",525,30183]
+                   ["2018-11-10",575,36148]
+                   ["2018-11-16",213,14942]
+                   ["2018-11-17",503,15690]
+                   ["2018-11-18",502,14506]
+                   ["2018-11-19",233,10714]
+                   ["2018-11-20",174,9545]
+                   ["2018-11-22",171,6460]
+                   ["2018-11-24",203,5217]
+                   ["2018-11-26",133,3263]
+                   ["2018-11-28",127,3238]
+                   ["2018-11-29",137,3120]
+                   ["2018-12-01",180,3732]
+                   ["2018-12-02",179,3311]
+                   ["2018-12-03",144,2525]])
+
+(expect
+  [{:last-value     144,
+    :previous-value 179,
+    :last-change    -0.19553072625698323,
+    :slope          -7.671473413418271,
+    :offset         137234.92983406168,
+    :best-fit       [:* 1.5672560913548484E227 [:exp [:* -0.02899533549378612 :x]]],
+    :unit           :day
     :col            nil}
    {:last-value     2525,
     :previous-value 3311,
     :last-change    -0.2373905164602839,
-    :slope          -551.1062834224598,
-    :offset         9850467.098930478,
-    :best-fit       [:+ 9850467.098930478 [:* -551.1062834224598 :x]],
-    :col            nil}]
+    :slope          -498.764272733624,
+    :offset         8915371.843617931,
+    :best-fit       [:+ 8915371.843617931 [:* -498.764272733624 :x]],
+    :col            nil
+    :unit           :day}]
   (transduce identity
              (insights [{:base_type :type/DateTime} {:base_type :type/Number} {:base_type :type/Number}])
              ts))
-- 
GitLab