Skip to content

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

4.9 KB
Raw Blame
(ns metabase-enterprise.content-diagnostics.checkers.duplicated
  "The `duplicated` Content Diagnostics checker - match mode `name`: two or more non-archived entities of
  the same type whose normalized names are equal and non-blank form a duplicate cluster. Every member of
  a cluster of size k gets one `:duplicate_name` finding whose peers are the other k-1 members, with
  `:duplicate-count` = the peer count (→ the native `duplicate_count` column).

  `details` carries `normalized_name` (the normalized name the cluster collided on) and
  `duplicate_entity_ids` (the flagged entity's peer ids - what the serve layer hydrates). Clustering is by
  normalized name within an entity type; cards cluster across all sub-kinds (`type` -
  question/model/metric), so a question and a model sharing a name are duplicates. One lightweight
  (id, name) load per entity type, grouped in memory - no per-entity loop, app-db only."
  (:require
   [clojure.string :as str]
   [metabase-enterprise.content-diagnostics.common :as common]
   [metabase-enterprise.content-diagnostics.db :as cd.db]
   [metabase.util :as u]))

(set! *warn-on-reflection* true)

(defn- normalize-name
  "Normalize a name for duplicate detection: remove diacritical marks, lowercase, remove invisible and collapse whitespace"
  [s]
  (-> (or (u/remove-diacritical-marks s) "")   ; remove-diacritical-marks returns nil on empty input
      u/lower-case-en
      (str/replace #"(?U)\p{Cf}" "")   ; strip Unicode format chars (zero-width joiners, bidi marks, soft hyphens)
      (str/replace #"(?U)\s+" " ")     ; collapse any run of Unicode whitespace to a single space
      str/trim))

(defmulti ^:private candidate-rows
  "Lightweight non-archived `(id, name)` rows for one entity type, for name clustering. card/dashboard/document
  (`::collection-item`) filter on their `archived` column, keep only rows in eligible containers
  (`common/eligible-container-clause`), and add any `common/candidate-cols` (card carries `:card_schema`,
  required by its after-select hook); cards also drop the ones a document owns; transform has no
  `archived` column (hard-deleted), so every row counts; collection narrows to the shared
  collection-subject set. Scan-time and instance-wide - deliberately un-permissioned, unlike the serve
  layer's `read-entity-rows`."
  {:arglists '([entity-type])}
  identity
  :hierarchy #'common/hierarchy)

(defmethod candidate-rows ::common/collection-item
  [entity-type]
  ;; :card_schema (a candidate-col for cards) is required on any Card select - its after-select hook reads it.
  (cd.db/name-rows (common/entity-type->model entity-type)
                   (common/candidate-cols entity-type)
                   [:and
                    [:= :archived false]
                    ;; peers come from this row set, so a document-owned card has to go before
                    ;; clustering, not just from the emitted findings
                    ;; (`common/remove-document-internal-card-findings-xf`)
                    (when (= entity-type :card) [:= :document_id nil])
                    (common/eligible-container-clause :collection_id)]))

(defmethod candidate-rows :transform
  [_]
  (cd.db/name-rows :model/Transform nil nil))

(defmethod candidate-rows :collection
  [_]
  ;; the shared collection-subject definition, so no two checkers can scan divergent collection sets
  (cd.db/name-rows :model/Collection nil common/eligible-collection-where))

(defn- cluster-findings
  "One `:duplicate_name` finding per member of a name cluster; peers are the other members (symmetric: in
  {A,B,C}, A's finding lists {B,C}, B's {A,C}, C's {A,B})."
  [entity-type normalized-name rows]
  (let [ids (mapv :id rows)]
    (for [id ids
          :let [peer-ids (filterv #(not= % id) ids)]]
      {:entity-type     entity-type
       :entity-id       id
       :finding-type    :duplicate_name
       :duplicate-count (count peer-ids)
       :details         {:normalized_name      normalized-name
                         :duplicate_entity_ids peer-ids}})))

(defn- findings-for-type
  "Group one entity type's rows by normalized name and emit findings for every cluster of ≥ 2 (cards cluster
  across all sub-kinds - grouping is by name alone). Clusters with a blank normalized name are skipped:
  `name` is NOT NULL on every covered model but can be whitespace-only, and unknown is not duplicate."
  [entity-type]
  (for [[norm-name rows] (group-by (comp normalize-name :name) (candidate-rows entity-type))
        :when   (and (not (str/blank? norm-name)) (>= (count rows) 2))
        finding (cluster-findings entity-type norm-name rows)]
    finding))

(defn checker
  "Instance-wide `:duplicate_name` finding maps across all covered entity types. The denormalized display
  attrs are stamped by `common/attach-entity-attrs`; `:duration-ms`/`:last-active-at` are left unset
  (those columns stay NULL on duplicated findings)."
  []
  (common/attach-entity-attrs
   (into [] (mapcat findings-for-type) (keys common/entity-type->model))))