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.stale.db
"Application database queries for the stale module. Every function here is a direct Toucan 2 call with no
additional logic, so the rest of the module only touches `toucan2.core` for hydration."
(:require
[metabase.lib.schema.id :as lib.schema.id]
[metabase.staleness.core :as staleness]
[metabase.util.malli :as mu]
[toucan2.core :as t2]))
(mu/defn collection
"The Collection with `collection-id`, or nil."
[collection-id :- ::lib.schema.id/collection]
(t2/select-one :model/Collection :id [:auto/param collection-id]))
(mu/defn collections-by-id
"A map of ID to Collection for `collection-ids`."
[collection-ids :- [:set ::lib.schema.id/collection]]
(t2/select-pk->fn identity :model/Collection :id [:in collection-ids]))
(mu/defn stale-cards
"The listing columns of the Cards with `card-ids`, with their latest moderation status."
[card-ids :- [:set ::lib.schema.id/card]]
(t2/select [:model/Card
:id
:dashboard_id
:description
:collection_id
:name
:entity_id
:archived
:collection_position
:display
:collection_preview
:database_id
[nil :location]
:dataset_query
:card_schema
;; :type is read by `queries/model?` when labelling the row; the rest are here only
;; because :dataset_query is schema-governed and the Card schema upgrade needs them.
:type
:entity_id
:result_metadata
:dimensions
:dimension_mappings
:last_used_at
[^:allow-subquery
{:select [:status]
:from [:moderation_review]
:where [:and
[:= :moderated_item_type "card"]
[:= :moderated_item_id :report_card.id]
[:= :most_recent true]]
;; limit 1 to ensure that there is only one result but this invariant should hold true, just
;; protecting against potential bugs
:order-by [[:id :desc]]
:limit 1}
:moderated_status]]
:id [:in card-ids]))
(mu/defn stale-dashboards
"The listing columns of the Dashboards with `dashboard-ids`."
[dashboard-ids :- [:set ::lib.schema.id/dashboard]]
(t2/select [:model/Dashboard
:id
:description
:collection_id
:name
:entity_id
:archived
:collection_position
[:last_viewed_at :last_used_at]
["dashboard" :model]
[nil :dashboard_id]
[nil :location]
[nil :database_id]]
:id [:in dashboard-ids]))
(defn- stale-content-union
"The union of `union-queries` (per-model SELECTs from `metabase.staleness.core/find-stale-query`),
aliased for use as a `:from` source."
[union-queries]
[[^:allow-subquery {:union-all union-queries} :dummy_alias]])
(mu/defn stale-content-rows
"A page of `select-columns` rows from the union of `union-queries`, sorted by `sort-column` (`:name` or
`:last_used_at`) in `sort-direction`, skipping `offset` and returning up to `limit` (either may be nil for no
restriction)."
[union-queries :- [:sequential ::staleness/query]
select-columns :- [:sequential :keyword]
sort-column :- [:enum :name :last_used_at]
sort-direction :- [:enum :asc :desc]
limit :- [:maybe :int]
offset :- [:maybe :int]]
(t2/query (cond-> {:select select-columns
:from (stale-content-union union-queries)
:order-by [[(case sort-column
:name :%lower.name
:last_used_at :last_used_at)
sort-direction]]}
(some? limit) (assoc :limit limit)
(some? offset) (assoc :offset offset))))
(mu/defn stale-content-count
"The total count of rows across every page [[stale-content-rows]] would return for `union-queries`."
[union-queries :- [:sequential ::staleness/query]]
(:count (t2/query-one {:select [[:%count.* :count]]
:from (stale-content-union union-queries)})))