From 4c41f848baf3c6ae20a96cea40b776f5f3cde34b Mon Sep 17 00:00:00 2001
From: Jerry Huang <34140255+qwef@users.noreply.github.com>
Date: Thu, 3 Aug 2023 16:14:22 -0700
Subject: [PATCH] Fix nils in subscriptions for values in series (#32813)

* replace nils initial code

* add test
---
 src/metabase/pulse/render/body.clj       | 13 +++++++++++--
 test/metabase/pulse/render/body_test.clj | 21 +++++++++++++--------
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/src/metabase/pulse/render/body.clj b/src/metabase/pulse/render/body.clj
index c51c35e9f54..6ba87d85889 100644
--- a/src/metabase/pulse/render/body.clj
+++ b/src/metabase/pulse/render/body.clj
@@ -452,9 +452,17 @@
                             (table-fn some-entries)])
                          (split-at (/ (count legend-entries) 2) legend-entries)))])))
 
+(defn- replace-nils [rows]
+  (mapv (fn [row]
+          (if (nil? (first row))
+            (assoc row 0 "(empty)")
+            row))
+        rows))
+
 (s/defmethod render :categorical/donut :- common/RenderedPulseCard
   [_ render-type timezone-id :- (s/maybe s/Str) card dashcard {:keys [rows cols viz-settings] :as data}]
-  (let [viz-settings                (merge viz-settings (:visualization_settings dashcard))
+  (let [rows                        (replace-nils rows)
+        viz-settings                (merge viz-settings (:visualization_settings dashcard))
         [x-axis-rowfn y-axis-rowfn] (common/graphing-column-row-fns card data)
         rows                        (map (juxt (comp str x-axis-rowfn) y-axis-rowfn)
                                          (common/row-preprocess x-axis-rowfn y-axis-rowfn rows))
@@ -792,7 +800,8 @@
 
   Use the combo charts for every chart-type in line area bar because we get multiple chart series for cheaper this way."
   [chart-type render-type _timezone-id card dashcard {:keys [cols rows viz-settings] :as data}]
-  (let [viz-settings    (merge viz-settings (:visualization_settings dashcard))
+  (let [rows            (replace-nils rows)
+        viz-settings    (merge viz-settings (:visualization_settings dashcard))
         x-axis-rowfn    (or (ui-logic/mult-x-axis-rowfn card data) #(vector (first %)))
         y-axis-rowfn    (or (ui-logic/mult-y-axis-rowfn card data) #(vector (second %)))
         x-rows          (filter some? (map x-axis-rowfn rows))
diff --git a/test/metabase/pulse/render/body_test.clj b/test/metabase/pulse/render/body_test.clj
index f40ecef5923..4887ce64c56 100644
--- a/test/metabase/pulse/render/body_test.clj
+++ b/test/metabase/pulse/render/body_test.clj
@@ -421,11 +421,15 @@
          (render-bar-graph {:cols         default-columns
                             :rows         [[10.0 1] [5.0 10] [2.50 20] [1.25 nil]]
                             :viz-settings {:graph.metrics ["NumPurchased"]}}))))
-  (testing "Check to make sure we allow nil values for the y-axis"
-    (is (has-inline-image?
-         (render-bar-graph {:cols         default-columns
-                            :rows         [[10.0 1] [5.0 10] [2.50 20] [nil 30]]
-                            :viz-settings {:graph.metrics ["NumPurchased"]}}))))
+  (testing "Check to make sure we allow nil values for the x-axis"
+    (let [graph (render-bar-graph {:cols         default-columns
+                                   :rows         [[10.0 1] [5.0 10] [2.50 20] [nil 30]]
+                                   :viz-settings {:graph.metrics ["NumPurchased"]}})]
+      (is (has-inline-image? graph))
+      (is (= graph
+             (render-bar-graph {:cols         default-columns
+                                :rows         [[10.0 1] [5.0 10] [2.50 20] ["(empty)" 30]]
+                                :viz-settings {:graph.metrics ["NumPurchased"]}})))))
   (testing "Check to make sure we allow nil values for both x and y on different rows"
     (is (has-inline-image?
          (render-bar-graph {:cols         default-columns
@@ -576,15 +580,16 @@
                                     x))
                                 html-tree))]
     (testing "Renders without error"
-      (let [rendered-info (render [["Doohickey" 75] ["Widget" 25]] {:show_values true})]
+      (let [rendered-info (render [[nil 10] ["Doohickey" 65] ["Widget" 25]] {:show_values true})]
         (is (has-inline-image? rendered-info))))
     (testing "Includes percentages"
       (is (= [:div
               [:img]
               [:table
-               [:tr [:td [:span "•"]] [:td "Doohickey"] [:td "75%"]]
+               [:tr [:td [:span "•"]] [:td "(empty)"] [:td "10%"]]
+               [:tr [:td [:span "•"]] [:td "Doohickey"] [:td "65%"]]
                [:tr [:td [:span "•"]] [:td "Widget"] [:td "25%"]]]]
-             (prune (:content (render [["Doohickey" 75] ["Widget" 25]]))))))))
+             (prune (:content (render [[nil 10] ["Doohickey" 65] ["Widget" 25]]))))))))
 
 (deftest render-progress
   (let [col [{:name          "NumPurchased",
-- 
GitLab