Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore cruft fields #9687

Merged
merged 5 commits into from Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/metabase/automagic_dashboards/core.clj
Expand Up @@ -731,6 +731,7 @@
(comp (->> (db/select Field
:table_id [:in (map u/get-id tables)]
:visibility_type "normal"
:preview_display true
:active true)
field/with-targets
(map #(assoc % :engine engine))
Expand Down
17 changes: 11 additions & 6 deletions src/metabase/sync/analyze/classifiers/no_preview_display.clj
Expand Up @@ -5,16 +5,21 @@
(:require [metabase.sync.interface :as i]
[schema.core :as s]))

(def ^:private ^:const ^Integer average-length-no-preview-threshold
(def ^:private ^:const ^Long average-length-no-preview-threshold
"Fields whose values' average length is greater than this amount should be marked as `preview_display = false`."
50)

(defn- long-plain-text-field?
[{:keys [base_type special_type]} fingerprint]
(and (isa? base_type :type/Text)
(contains? #{nil :type/SerializedJSON} special_type)
(some-> fingerprint
(get-in [:type :type/Text :average-length])
(> average-length-no-preview-threshold))))

(s/defn infer-no-preview-display :- (s/maybe i/FieldInstance)
"Classifier that determines whether FIELD should be marked 'No Preview Display'.
If FIELD is textual and its average length is too great, mark it so it isn't displayed in the UI."
[field :- i/FieldInstance, fingerprint :- (s/maybe i/Fingerprint)]
(when (isa? (:base_type field) :type/Text)
(when-let [average-length (get-in fingerprint [:type :type/Text :average-length])]
(when (> average-length average-length-no-preview-threshold)
(assoc field
:preview_display false)))))
(when (long-plain-text-field? field fingerprint)
(assoc field :preview_display false)))
44 changes: 44 additions & 0 deletions test/metabase/sync/analyze/classifiers/no_preview_display_test.clj
@@ -0,0 +1,44 @@
(ns metabase.sync.analyze.classifiers.no-preview-display-test
"Tests for the category classifier."
(:require [expectations :refer :all]
[metabase.models.field :as field]
[metabase.sync.analyze.classifiers.no-preview-display :refer :all]))

(def ^:private long-text-field
(field/map->FieldInstance
{:database_type "VARCHAR"
:special_type nil
:name "longfield"
:fingerprint_version 1
:has_field_values nil
:active true
:visibility_type :normal
:preview_display true
:display_name "Mr. Long"
:fingerprint {:global {:distinct-count 42}
:type
{:type/Text
{:percent-json 0.0
:percent-url 0.0
:percent-email 0.0
:average-length 130.516}}}
:base_type :type/Text}))

;; Leave short text fields intact
(expect
nil
(:preview_display (infer-no-preview-display long-text-field
(-> long-text-field
:fingerprint
(assoc-in [:type :type/Text :average-length] 2)))))

;; Don't preview generic long text fields
(expect
false
(:preview_display (infer-no-preview-display long-text-field (:fingerprint long-text-field))))

;; If the field has a special type, show it regardless of it's length
(expect
nil
(:preview_display (infer-no-preview-display (assoc long-text-field :special_type :type/Name)
(:fingerprint long-text-field))))