Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Object Detail static viz support (#75386)

Nick Fitzpatrickf8822cdd9c56
diff --git a/e2e/test/scenarios/sharing/subscriptions.cy.spec.js b/e2e/test/scenarios/sharing/subscriptions.cy.spec.js
index 5b0b4b77287..360ba4347d9 100644
--- a/e2e/test/scenarios/sharing/subscriptions.cy.spec.js
+++ b/e2e/test/scenarios/sharing/subscriptions.cy.spec.js
@@ -78,6 +78,35 @@ describe("scenarios > dashboard > subscriptions", () => {
H.setupSMTP();
});
+ it("renders an object detail as a label/value table in a subscription email", () => {
+ const questionDetails = {
+ name: "Object detail static-viz smoke",
+ native: {
+ query: "SELECT 'Hammer' AS product, 19 AS price, NULL AS discount",
+ },
+ display: "object",
+ };
+
+ H.createNativeQuestionAndDashboard({ questionDetails }).then(
+ ({ dashboardId }) => {
+ H.visitDashboard(dashboardId);
+ },
+ );
+
+ H.openAndAddEmailsToSubscriptions([
+ `${admin.first_name} ${admin.last_name}`,
+ ]);
+
+ H.sendEmailAndAssert(({ html }) => {
+ expect(html).not.to.include(
+ "An error occurred while displaying this card.",
+ );
+ expect(html).to.include("Hammer");
+ // "Empty" (the null column) is unique to the :object renderer — a table fallback leaves it blank.
+ expect(html).to.include("Empty");
+ });
+ });
+
describe("with no existing subscriptions", () => {
it("should not enable subscriptions without the recipient (metabase#17657)", () => {
openDashboardSubscriptions();
diff --git a/src/metabase/channel/render/body.clj b/src/metabase/channel/render/body.clj
index 7b133a4fb86..d48fceede95 100644
--- a/src/metabase/channel/render/body.clj
+++ b/src/metabase/channel/render/body.clj
@@ -243,6 +243,67 @@
{:content table-body
:attachments nil}))
+(defn- blank-cell-value?
+ "True when a raw cell value should render as an empty placeholder (nil, or a blank string)."
+ [raw]
+ (or (nil? raw) (and (string? raw) (str/blank? raw))))
+
+(defn- object-detail-pairs
+ "`[label value]` pairs for the prepared object-detail `row`: each column's display name paired with its
+ formatted value; `value` is nil for missing cells (rendered as a muted \"Empty\" placeholder)."
+ [timezone-id card cols row viz-settings]
+ (let [formatters (mapv #(formatter/create-formatter timezone-id % viz-settings) cols)]
+ (into []
+ (map-indexed (fn [idx col]
+ (let [raw (nth row idx nil)]
+ [(column-name card col)
+ (when-not (blank-cell-value? raw)
+ ((nth formatters idx) raw))])))
+ cols)))
+
+(defn- object-detail-row
+ "A single label/value `[:tr ...]` for the object-detail table; `last?` drops the bottom border."
+ [label value label-style value-style last?]
+ (let [border {:border-bottom (if last? 0 style/object-detail-border)}]
+ [:tr
+ [:td {:style (style/style label-style border)} (h label)]
+ [:td {:style (style/style value-style border)}
+ (if (nil? value)
+ ;; Match the live viz: missing values show a muted "Empty" placeholder rather than a blank cell.
+ [:span {:style (style/style (style/object-detail-empty-value-style))} (tru "Empty")]
+ (h value))]]))
+
+(mu/defmethod render :object :- ::RenderedPartCard
+ [_chart-type
+ _render-type
+ timezone-id :- [:maybe :string]
+ card
+ _dashcard
+ {:keys [rows viz-settings] :as unordered-data}]
+ ;; Single-record key/value view: render the first row only (a static email can't paginate).
+ (let [[ordered-cols ordered-rows] (order-data unordered-data viz-settings)
+ {prepared-cols :cols
+ prepared-rows :rows} (table-data/prepare-table-data ordered-cols
+ (take 1 ordered-rows)
+ table-data/show-in-object-detail?)
+ pairs (object-detail-pairs timezone-id card prepared-cols (first prepared-rows) viz-settings)
+ row-count (count rows)
+ last-idx (dec (count pairs))
+ label-style (style/object-detail-label-style)
+ value-style (style/object-detail-value-style)]
+ {:attachments nil
+ :content
+ [:div {:style (style/style (style/section-style))}
+ [:table {:style (style/style (style/object-detail-table-style))
+ :cellpadding "0"
+ :cellspacing "0"}
+ [:tbody
+ (for [[idx [label value]] (m/indexed pairs)]
+ (object-detail-row label value label-style value-style (= idx last-idx)))]]
+ (when (> row-count 1)
+ [:div {:style (style/style (style/object-detail-more-records-style))}
+ (tru "Showing 1 of {0} records." row-count)])]}))
+
(def ^:private default-date-styles
{:year "YYYY"
:quarter "[Q]Q - YYYY"
diff --git a/src/metabase/channel/render/card.clj b/src/metabase/channel/render/card.clj
index 4ee4ea31b69..68335807904 100644
--- a/src/metabase/channel/render/card.clj
+++ b/src/metabase/channel/render/card.clj
@@ -118,13 +118,14 @@
;; for scalar/smartscalar, the display-type might actually be :line, so we can't have line above
(and (= false (render.util/is-visualizer-dashcard? maybe-dashcard))
- (not (contains? #{:progress :gauge} display-type))
+ (not (contains? #{:progress :gauge :object} display-type))
(= @col-sample-count @row-sample-count 1))
(chart-type :scalar "result has one row and one column")
(#{:scalar
:gauge
:table
+ :object
:funnel} display-type)
(chart-type display-type "display-type is %s" display-type)
diff --git a/src/metabase/channel/render/style.clj b/src/metabase/channel/render/style.clj
index d9f5d6a26a6..096457846f8 100644
--- a/src/metabase/channel/render/style.clj
+++ b/src/metabase/channel/render/style.clj
@@ -125,6 +125,66 @@
:font-weight 700
:color (or color color-text-dark)})))
+(def object-detail-border
+ "1px border used for the object-detail table outline and the separators between rows."
+ (str "1px solid " color-border))
+
+(def ^:private object-detail-cell-padding "0.75em 1em")
+
+(defn- object-detail-cell-style
+ "Shared base style for the label and value cells of an object-detail key/value row."
+ []
+ (merge
+ (font-style)
+ {:font-weight 700
+ :vertical-align :top
+ :white-space :normal
+ :padding object-detail-cell-padding}))
+
+(defn object-detail-table-style
+ "Style for the outer table wrapping an object-detail key/value view in a Pulse."
+ []
+ {:max-width "100%"
+ :width "100%"
+ :border object-detail-border
+ :border-radius "6px"
+ :border-collapse "separate"
+ :border-spacing 0})
+
+(defn object-detail-label-style
+ "Style for the label (left) cell of an object-detail key/value row in a Pulse."
+ []
+ (merge
+ (object-detail-cell-style)
+ {:font-size :12.5px
+ :color color-gray-3
+ :text-align :left
+ :width "40%"}))
+
+(defn object-detail-value-style
+ "Style for the value (right) cell of an object-detail key/value row in a Pulse."
+ []
+ (merge
+ (object-detail-cell-style)
+ {:font-size :14px
+ :color color-text-dark
+ :word-break :break-word}))
+
+(defn object-detail-empty-value-style
+ "Style for the muted \"Empty\" placeholder shown for a missing object-detail value."
+ []
+ (merge
+ (font-style)
+ {:color color-gray-3
+ :font-weight 400}))
+
+(defn object-detail-more-records-style
+ "Style for the \"Showing 1 of N records\" note under an object-detail view."
+ []
+ {:color color-gray-2
+ :padding-top :12px
+ :font-size :12px})
+
(defn- register-font! [filename]
(with-open [is (io/input-stream (io/resource filename))]
(.registerFont (GraphicsEnvironment/getLocalGraphicsEnvironment)
diff --git a/src/metabase/channel/render/table_data.clj b/src/metabase/channel/render/table_data.clj
index d13f3d5aecc..46ca5e66a20 100644
--- a/src/metabase/channel/render/table_data.clj
+++ b/src/metabase/channel/render/table_data.clj
@@ -10,6 +10,13 @@
[{:keys [visibility_type] :as _column}]
(not (contains? #{:details-only :retired :sensitive} visibility_type)))
+(defn show-in-object-detail?
+ "Should this column be shown in an object-detail (key/value) view?
+ Like [[show-in-table?]] but keeps `:details-only` columns (the point of object detail);
+ drops only sensitive and retired."
+ [{:keys [visibility_type] :as _column}]
+ (not (contains? #{:retired :sensitive} visibility_type)))
+
(defn create-remapping-lookup
"Creates a map from column names to the index of their remapped column.
Used to handle FK remapping where columns have :remapped_from metadata."
@@ -21,29 +28,31 @@
(defn prepare-table-data
"Prepare query results for table rendering.
- - Filters out columns that shouldn't be shown (sensitive, retired, details-only)
+ - Filters out columns the `visible?` predicate rejects (defaults to [[show-in-table?]])
- Handles FK remapping: removes duplicate columns and substitutes values
Returns {:cols [...] :rows [...]} with the prepared data."
- [cols rows]
- (let [remapping-lookup (create-remapping-lookup cols)
- ;; Build list of columns to keep (visible and not remapped_from)
- ;; and track which source index to read from for each
- col-info (into []
- (comp
- (map-indexed vector)
- (filter (fn [[_ col]] (show-in-table? col)))
- (remove (fn [[_ col]] (:remapped_from col)))
- (map (fn [[idx col]]
- {:source-idx (or (get remapping-lookup (:name col)) idx)
- :col (if-let [remapped-idx (get remapping-lookup (:name col))]
- (nth cols remapped-idx)
- col)})))
- cols)
- output-cols (mapv :col col-info)
- col-indices (mapv :source-idx col-info)
- output-rows (mapv (fn [row]
- (mapv #(nth row % nil) col-indices))
- rows)]
- {:cols output-cols
- :rows output-rows}))
+ ([cols rows]
+ (prepare-table-data cols rows show-in-table?))
+ ([cols rows visible?]
+ (let [remapping-lookup (create-remapping-lookup cols)
+ ;; Build list of columns to keep (visible and not remapped_from)
+ ;; and track which source index to read from for each
+ col-info (into []
+ (comp
+ (map-indexed vector)
+ (filter (fn [[_ col]] (visible? col)))
+ (remove (fn [[_ col]] (:remapped_from col)))
+ (map (fn [[idx col]]
+ {:source-idx (or (get remapping-lookup (:name col)) idx)
+ :col (if-let [remapped-idx (get remapping-lookup (:name col))]
+ (nth cols remapped-idx)
+ col)})))
+ cols)
+ output-cols (mapv :col col-info)
+ col-indices (mapv :source-idx col-info)
+ output-rows (mapv (fn [row]
+ (mapv #(nth row % nil) col-indices))
+ rows)]
+ {:cols output-cols
+ :rows output-rows})))
diff --git a/test/metabase/channel/render/body_test.clj b/test/metabase/channel/render/body_test.clj
index cf8368ff340..29f658ae999 100644
--- a/test/metabase/channel/render/body_test.clj
+++ b/test/metabase/channel/render/body_test.clj
@@ -10,6 +10,7 @@
[metabase.channel.render.body :as body]
[metabase.channel.render.core :as channel.render]
[metabase.channel.render.js.color :as js.color]
+ [metabase.channel.render.style :as style]
[metabase.config.core :as config]
[metabase.formatter.core :as formatter]
[metabase.notification.payload.execute :as notification.execute]
@@ -343,6 +344,68 @@
:render/text "40\nUp 133.33% vs. previous month: 30"}
(body/render :smartscalar nil pacific-tz nil nil results)))))))
+(def ^:private object-card
+ {:name "T" :display :object})
+
+(def ^:private object-id-col
+ {:name "id" :display_name "ID" :base_type :type/BigInteger :semantic_type :type/PK})
+
+(defn- render-object-html [data]
+ (html (:content (body/render :object nil pacific-tz object-card nil data))))
+
+(deftest ^:parallel object-detail-test
+ (let [data {:cols [object-id-col
+ {:name "name" :display_name "Name" :base_type :type/Text}
+ {:name "created" :display_name "Created" :base_type :type/DateTime}]
+ :rows [[1 "Widget" "2014-04-01T08:30:00.0000"]
+ [2 "Gadget" "2015-01-01T00:00:00.0000"]]}
+ html-str (render-object-html data)]
+ (testing "renders each column as a label/value pair for the first row, with per-column formatting"
+ (is (str/includes? html-str "Name"))
+ (is (str/includes? html-str "Widget"))
+ (is (str/includes? html-str "April 1, 2014, 8:30 AM")))
+ (testing "only the first row is rendered (a static email can't paginate)"
+ (is (not (str/includes? html-str "Gadget"))))
+ (testing "notes when more records exist"
+ (is (str/includes? html-str "Showing 1 of 2 records")))))
+
+(deftest ^:parallel object-detail-single-row-test
+ (testing "a single row shows no 'more records' note"
+ (let [data {:cols [{:name "id" :display_name "ID" :base_type :type/Text}]
+ :rows [["only"]]}]
+ (is (not (str/includes? (render-object-html data) "Showing 1 of"))))))
+
+(deftest ^:parallel object-detail-details-only-columns-test
+ (testing "details-only columns (hidden in tables) ARE shown in object detail"
+ (let [data {:cols [object-id-col
+ {:name "notes" :display_name "Notes" :base_type :type/Text :visibility_type :details-only}]
+ :rows [[1 "internal note"]]}
+ html-str (render-object-html data)]
+ (is (str/includes? html-str "Notes"))
+ (is (str/includes? html-str "internal note")))))
+
+(deftest ^:parallel object-detail-hidden-columns-test
+ (testing "sensitive columns are dropped"
+ (let [data {:cols [object-id-col
+ {:name "ssn" :display_name "SSN" :base_type :type/Text :visibility_type :sensitive}]
+ :rows [[1 "999-99-9999"]]}
+ html-str (render-object-html data)]
+ (is (not (str/includes? html-str "999-99-9999")))
+ (is (not (str/includes? html-str "SSN"))))))
+
+(deftest ^:parallel object-detail-empty-values-test
+ (testing "missing values (nil or blank) render as a muted 'Empty' placeholder, like the live viz"
+ (let [data {:cols [object-id-col
+ {:name "name" :display_name "Name" :base_type :type/Text}
+ {:name "note" :display_name "Note" :base_type :type/Text}]
+ :rows [[1 nil " "]]}
+ html-str (render-object-html data)
+ empties (count (re-seq #"Empty" html-str))]
+ (testing "both the nil and the blank-string values are shown as Empty"
+ (is (= 2 empties)))
+ (testing "the placeholder is styled in the secondary/muted color, not left blank"
+ (is (str/includes? html-str style/color-gray-3))))))
+
(defn- replace-style-maps [hiccup-map]
(walk/postwalk (fn [maybe-map]
(if (and (map? maybe-map)
diff --git a/test/metabase/channel/render/card_test.clj b/test/metabase/channel/render/card_test.clj
index 8fe1103fe80..6dd2f88f733 100644
--- a/test/metabase/channel/render/card_test.clj
+++ b/test/metabase/channel/render/card_test.clj
@@ -68,6 +68,17 @@
{:display :pivot} nil
{:cols [{:base_type :type/Text} {:base_type :type/Number}] :rows [["a" 1]]})))))
+(deftest ^:parallel detect-pulse-chart-type-object-detail-test
+ (let [data {:cols [{:base_type :type/Text :name "name"} {:base_type :type/Number :name "n"}]
+ :rows [["a" 1]]}]
+ (testing "an :object display routes to the :object renderer"
+ (is (= :object (channel.render/detect-pulse-chart-type {:display :object} nil data))))
+ (testing "a single-column object detail still routes to :object, not :scalar"
+ (is (= :object (channel.render/detect-pulse-chart-type
+ {:display :object} nil {:cols [{:base_type :type/Text :name "x"}] :rows [["v"]]}))))
+ (testing "empty results still short-circuit to :empty"
+ (is (= :empty (channel.render/detect-pulse-chart-type {:display :object} nil {:cols [] :rows []}))))))
+
(deftest ^:parallel detect-pulse-chart-type-test-2
(testing "Queries resulting in no rows return `:empty`."
(is (= :empty