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
(ns metabase-enterprise.content-diagnostics.checkers.imbalanced.empty
"The `empty` imbalanced checker: content with nothing in it, across collections, cards, dashboards, and
documents. Runs independently of `sparse`/`crowded`, so an entity can be flagged by more than one (a
many-tab dashboard with 0 dashcards is both `crowded` and `empty`).
What counts as empty:
- Collection: no direct items, the same count `sparse`/`crowded` use. Items are exactly the covered
kinds: child collections, cards, dashboards, documents, transforms; empty items still count (a
folder of only-empty dashboards is not `empty` - the dashboards are).
- Card: its latest clean run (no parameters, sandbox, cache, or error) within
`content-diagnostics-empty-card-lookback-days` returned 0 rows; a card with no clean run in that window
is left alone. `as_of` is that run's start.
- Dashboard: no dashcards.
- Document: no text and no embedded content.
Transforms are never flagged - the app DB has no reliable row count for a transform's target table.
Every finding records a count of 0 (there is no threshold - 0 is definitionally empty). Set-based,
reads only the app DB."
(:require
[clojure.string :as str]
[java-time.api :as t]
[metabase-enterprise.content-diagnostics.checkers.imbalanced.common :as shared]
[metabase-enterprise.content-diagnostics.common :as common]
[metabase-enterprise.content-diagnostics.db :as cd.db]
[metabase-enterprise.content-diagnostics.settings :as cd.settings]
[metabase.documents.prose-mirror :as prose-mirror]
[metabase.util :as u]))
(set! *warn-on-reflection* true)
(defn- lookback-cutoff
"The earliest clean run the card arm will draw a verdict from, per
`content-diagnostics-empty-card-lookback-days`. A card with no clean run since then is left alone rather
than called empty."
[]
(t/minus (t/offset-date-time) (t/days (cd.settings/content-diagnostics-empty-card-lookback-days))))
(defn- empty-card-id->as-of
"`{card-id -> started_at}` for every non-archived card in an eligible container whose latest clean run
within the [[lookback-cutoff]] window returned 0 rows. Clean means unparameterized, unsandboxed, not a
cache hit, and error-free - anything else is not instance-wide evidence of emptiness (a sandbox filters
rows per user, and an errored run means broken, not empty)."
[]
(u/index-by :card_id :started_at
(cd.db/cards-with-empty-latest-run (lookback-cutoff)
(common/eligible-container-clause :c.collection_id))))
(def ^:private structural-node-types
"Prose-mirror node types that are pure structure or layout - a document built only from these, with no
non-blank text or reference label, has no content. `text` is here because a text node's substance is
its `:text`, checked separately. The list is intentionally not exhaustive: an unknown node type (e.g.
an image) counts as content, so the check never wrongly calls a document empty."
#{"doc" "paragraph" "heading" "text" "bulletList" "orderedList" "listItem" "blockquote"
"codeBlock" "flexContainer" "resizeNode" "hardBreak"})
(defn- document-empty?
"True when a prose-mirror document has **no content of any kind**: no non-blank text, no non-blank
reference label (smart links, mentions), and no node outside [[structural-node-types]] (card embeds
and unknown future node types count as content)."
[doc]
(empty? (prose-mirror/collect-ast
doc
(fn [{:keys [type text] :as node}]
(when (or (not (str/blank? text))
(not (str/blank? (get-in node [:attrs :label])))
(and (some? type) (not (structural-node-types type))))
node)))))
(defn checker
"Instance-wide `empty` findings across collections, cards, dashboards, and documents. Collections are
flagged on their direct-item count alone - the per-item verdicts never roll up."
[]
(let [empty-card-as-of (empty-card-id->as-of)
dashboards (shared/active-dashboards)
dashcard-totals (shared/dashboard-dashcard-totals)
documents (shared/active-documents)
collections (shared/eligible-collections)
item-counts (shared/direct-item-counts collections)
empty-dashboards (into #{}
(keep #(when (zero? (long (get dashcard-totals (:id %) 0))) (:id %)))
dashboards)
;; only a parseable (prose-mirror) document can be judged empty - any other content type is
;; unknown, so it is never flagged
empty-documents (into #{}
(keep #(when (and (= (:content_type %) prose-mirror/prose-mirror-content-type)
(document-empty? %))
(:id %)))
documents)]
(common/attach-entity-attrs
(concat
(for [{:keys [id]} collections
:when (zero? (long (get item-counts id 0)))]
(shared/finding :collection id :empty 0 {:threshold 0 :unit "items"}))
(for [[card-id as-of] empty-card-as-of]
(shared/finding :card card-id :empty 0 {:threshold 0 :unit "rows" :as_of as-of}))
(for [id empty-dashboards]
(shared/finding :dashboard id :empty 0 {:threshold 0 :unit "dashcards"}))
(for [id empty-documents]
(shared/finding :document id :empty 0 {:threshold 0 :unit "cards"}))))))