The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Correctly detect funnel chart config in static-viz (#68158)
Nathan Voxland19d6d374c837
diff --git a/src/metabase/channel/render/body.clj b/src/metabase/channel/render/body.cljindex 4dbfa4c5df0..e2bc0ef4be7 100644--- a/src/metabase/channel/render/body.clj+++ b/src/metabase/channel/render/body.clj@@ -535,25 +535,81 @@[k value])) funnel-viz raw-rows)](remove nil? rows-data))))-(defn- get-funnel-axis-fns- "Return [x-axis-fn y-axis-fn] tuple for indexing into the funnel data for the appropriate axis' data"+(defn- summable-col?+ "Check if a column is summable (numeric but not temporal, location, or entity).+ Works with snake_case column keys from result metadata."+ [col]+ (let [effective-type (or (:effective_type col) (:base_type col))+ semantic-type (:semantic_type col)]+ (and (isa? effective-type :type/Number)+ (not (isa? effective-type :type/Temporal))+ (not (isa? semantic-type :type/Address))+ (not (isa? semantic-type :type/FK))+ (not (isa? semantic-type :type/PK))+ (not (isa? semantic-type :type/Name)))))++(defn- metric-col?+ "Check if a column should be treated as a metric, matching frontend isMetric logic.+ A metric is summable, not from breakout, not named like an ID, and not binned."+ [col]+ (and (not= (:source col) "breakout")+ (summable-col? col)+ (not (:binning_info col))+ (let [col-name (some-> (:name col) u/lower-case-en)]+ (not (or (= col-name "id")+ (str/ends-with? col-name "_id")+ (str/ends-with? col-name "-id"))))))++(defn- reorder-cols-for-funnel+ "Reorder :cols and :rows so that the dimension column is first and metric column is second.+ Finds columns by name, mirroring the frontend FunnelNormal.tsx findIndex logic."+ [data dim-col-name metric-col-name]+ (let [cols (:cols data)+ dim-idx (first (keep-indexed (fn [i c] (when (= (:name c) dim-col-name) i)) cols))+ met-idx (first (keep-indexed (fn [i c] (when (= (:name c) metric-col-name) i)) cols))]+ (if (and dim-idx met-idx)+ (-> data+ (assoc :cols [(nth cols dim-idx) (nth cols met-idx)])+ (update :rows (fn [rs] (mapv (fn [r] (let [v (vec r)] [(v dim-idx) (v met-idx)])) rs))))+ data)))++(defn- swap-first-two-cols+ "Swap the first two columns in data, reordering both :cols and :rows."+ [data]+ (-> data+ (update :cols (fn [cs] (vec (cons (second cs) (cons (first cs) (drop 2 cs))))))+ (update :rows (fn [rs] (mapv (fn [r] (let [v (vec r)] (into [(v 1) (v 0)] (subvec v 2)))) rs)))))++(defn- normalize-funnel-data+ "Ensure funnel data has dimension column first and metric column second.+ Reorders both :cols and :rows if needed. When explicit funnel.dimension and+ funnel.metric settings are present, finds columns by name (like the frontend)."[card dashcard data]- (if (render.util/is-visualizer-dashcard? dashcard)- ;; x-axis looks for :funnel.dimension- ;; y-axis looks for :funnel.metric- (let [x-axis-is-first (= (:name (first (:cols data))) (get-in data [:viz-settings :funnel.dimension]))]- (if x-axis-is-first- [first second]- [second first]))- (formatter/graphing-column-row-fns card data)))+ (let [cols (:cols data)]+ (if (or (< (count cols) 2)+ (and (not= :funnel (:display card))+ (not (render.util/is-visualizer-dashcard? dashcard))))+ data+ (let [viz-settings (if (render.util/is-visualizer-dashcard? dashcard)+ (:viz-settings data)+ (:visualization_settings card))+ dimension-col-name (get viz-settings :funnel.dimension)+ metric-col-name (get viz-settings :funnel.metric)]+ (if (and dimension-col-name metric-col-name)+ ;; Both dimension and metric specified: find by name regardless of position+ (reorder-cols-for-funnel data dimension-col-name metric-col-name)+ ;; Auto-detect: metric col should be second, non-metric col should be first+ (let [[col1 col2] cols]+ (if (and (metric-col? col1) (not (metric-col? col2)))+ (swap-first-two-cols data)+ data)))))))(mu/defmethod render :funnel_normal :- ::RenderedPartCard- [_chart-type render-type _timezone-id card dashcard {:keys [rows cols viz-settings] :as data}]- (let [[x-axis-rowfn- y-axis-rowfn] (get-funnel-axis-fns card dashcard data)+ [_chart-type render-type _timezone-id card dashcard data]+ (let [{:keys [rows cols viz-settings]} (normalize-funnel-data card dashcard data)funnel-viz (:funnel.rows viz-settings)- raw-rows (map (juxt x-axis-rowfn y-axis-rowfn)- (formatter/row-preprocess x-axis-rowfn y-axis-rowfn rows))+ raw-rows (map (juxt first second)+ (formatter/row-preprocess first second rows))rows (if (and funnel-viz (all-unique? funnel-viz))(funnel-rows funnel-viz raw-rows)raw-rows)diff --git a/test/metabase/channel/render/body_test.clj b/test/metabase/channel/render/body_test.cljindex 4e4cab3682c..6bd26727ec9 100644--- a/test/metabase/channel/render/body_test.clj+++ b/test/metabase/channel/render/body_test.clj@@ -541,6 +541,106 @@(is (= (map :key (get-in funnel-card [:visualization_settings :funnel.rows]))section-labels))))))))+;; Test data with numeric first column and text second column+;; This matches the bug scenario: SELECT 100 as val, 'step 1' as step+(def ^:private funnel-numeric-first-rows+ [[100 "homepage"]+ [50 "cart"]+ [25 "checkout"]+ [10 "purchase"]])++(tx/defdataset funnel-numeric-first-data+ [["stages"+ [{:field-name "val", :base-type :type/Integer}+ {:field-name "step", :base-type :type/Text}]+ funnel-numeric-first-rows]])++(deftest render-funnel-with-numeric-first-column-test+ (testing "Static-viz Funnel Chart auto-detects dimension/metric when metric column is first (#28568)"+ (mt/dataset funnel-numeric-first-data+ (let [funnel-query {:database (mt/id)+ :type :query+ :query+ {:source-table (mt/id :stages)+ :fields [[:field (mt/id :stages :val)]+ [:field (mt/id :stages :step)]]}}+ funnel-card {:display :funnel+ :dataset_query funnel-query+ :visualization_settings {}}]+ (mt/with-temp [:model/Card {card-id :id} funnel-card]+ (let [row-names (into #{} (map second funnel-numeric-first-rows))+ doc (render.tu/render-card-as-hickory! card-id)+ pulse-body (hik.s/select (hik.s/class "pulse-body") doc)+ labels (->> doc+ (hik.s/select (hik.s/tag :tspan))+ (mapv (comp first :content))+ (filter row-names))]+ (is (not (render-error? pulse-body)))+ (is (seq labels) "Dimension labels (step names) should appear in the rendered funnel"))))))+ (testing "Explicit funnel.dimension setting is honored even when metric is first"+ (mt/dataset funnel-numeric-first-data+ (let [funnel-query {:database (mt/id)+ :type :query+ :query+ {:source-table (mt/id :stages)+ :fields [[:field (mt/id :stages :val)]+ [:field (mt/id :stages :step)]]}}+ funnel-card {:display :funnel+ :dataset_query funnel-query+ :visualization_settings {:funnel.dimension "STEP"}}]+ (mt/with-temp [:model/Card {card-id :id} funnel-card]+ (let [row-names (into #{} (map second funnel-numeric-first-rows))+ doc (render.tu/render-card-as-hickory! card-id)+ pulse-body (hik.s/select (hik.s/class "pulse-body") doc)+ labels (->> doc+ (hik.s/select (hik.s/tag :tspan))+ (mapv (comp first :content))+ (filter row-names))]+ (is (not (render-error? pulse-body)))+ (is (seq labels) "Dimension labels should appear when funnel.dimension is set")))))))++;; Test data with 3 columns where dimension is at index 2+;; This matches the bug scenario: SELECT 'foo' as col, 100 as val, 'step 1' as step+(def ^:private funnel-three-col-rows+ [["foo" 100 "homepage"]+ ["bar" 50 "cart"]+ ["baz" 25 "checkout"]+ ["qux" 10 "purchase"]])++(tx/defdataset funnel-three-col-data+ [["stages"+ [{:field-name "col", :base-type :type/Text}+ {:field-name "val", :base-type :type/Integer}+ {:field-name "step", :base-type :type/Text}]+ funnel-three-col-rows]])++(deftest render-funnel-with-three-columns-explicit-settings-test+ (testing "Static-viz Funnel Chart works when dimension is at index 2 with explicit funnel.dimension and funnel.metric"+ (mt/dataset funnel-three-col-data+ (let [funnel-query {:database (mt/id)+ :type :query+ :query+ {:source-table (mt/id :stages)+ :fields [[:field (mt/id :stages :col)]+ [:field (mt/id :stages :val)]+ [:field (mt/id :stages :step)]]}}+ funnel-card {:display :funnel+ :dataset_query funnel-query+ :visualization_settings {:funnel.dimension "STEP"+ :funnel.metric "VAL"}}]+ (mt/with-temp [:model/Card {card-id :id} funnel-card]+ (let [row-names (into #{} (map #(nth % 2) funnel-three-col-rows))+ doc (render.tu/render-card-as-hickory! card-id)+ pulse-body (hik.s/select (hik.s/class "pulse-body") doc)+ labels (->> doc+ (hik.s/select (hik.s/tag :tspan))+ (mapv (comp first :content))+ (filter row-names))]+ (is (not (render-error? pulse-body))+ "3-column funnel with explicit dimension/metric should not error")+ (is (seq labels)+ "Dimension labels (step names) should appear in the rendered funnel")))))))+(deftest render-pie-chart-test(testing "The static-viz pie chart renders correctly."(mt/dataset test-data