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.impl
(:require
[metabase-enterprise.stale.db :as stale.db]
[metabase.staleness.core :as staleness]
[metabase.util.malli :as mu]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
(def ^:private FindStaleContentArgs
[:map {:closed true}
[:collection-ids [:or
{:doc "Collection IDs to search: a set (a nil member means the root), or :all for instance-wide."}
[:= :all]
[:set [:maybe :int]]]]
[:cutoff-date [:time/local-date {:doc "The cutoff date for stale content."}]]
[:limit [:maybe {:doc "The limit for pagination."} :int]]
[:offset [:maybe {:doc "The offset for pagination."} :int]]
[:sort-column [:enum {:doc "The column to sort by."} :name :last_used_at]]
[:sort-direction [:enum {:doc "The direction to sort by."} :asc :desc]]
[:models {:optional true} [:set {:doc "The set of models to search for stale content."} :keyword]]
[:include-columns {:optional true}
[:set {:doc "Extra union columns to return on each row (rows carry only :id and :model by default)."}
[:enum :name :last_used_at :collection_id]]]])
(defn- queries [{:keys [models] :or {models #{:model/Card :model/Dashboard}} :as args}]
;; Ensure each model's namespace is loaded so its `find-stale-query` method is registered before we
;; dispatch — passing the `:model/X` keyword alone does not trigger Toucan model resolution.
(run! t2/resolve-model models)
(when-let [unsupported (seq (remove #(get-method staleness/find-stale-query %) models))]
(throw (ex-info (str "No staleness method registered for: " (vec unsupported)) {})))
(map #(staleness/find-stale-query % args) models))
(mu/defn find-candidates :- [:map
[:rows [:sequential [:map
[:id pos-int?]
[:model keyword?]]]]
[:total :int]]
"Find stale content in the given collections.
Arguments are defined by [[FindStaleContentArgs]]:
- `collection-ids`: the set of collection IDs to look for stale content in (a nil member means root-level
content), or `:all` to search the whole instance. Non-recursive, the exact set you pass in will be searched
- `cutoff-date`: if something was last accessed before this date, it is 'stale'
- `limit` / `offset`: to support pagination
- `sort-column`: one of `:name` or `:last_used_at` (column to sort on)
- `sort-direction`: `:asc` or `:desc`
- `include-columns`: extra union columns (`:name`, `:last_used_at`, `:collection_id`) to return on each
row; rows carry only `:id` and `:model` by default
Returns a map containing two keys,
- `:rows` (a collection of maps containing an `:id` and `:model` field, like `{:id 1 :model :model/Card}`), and
- `:total` (the total count of stale elements that could be found if you iterated through all pages)
"
[{:keys [collection-ids limit offset sort-column sort-direction include-columns] :as args} :- FindStaleContentArgs]
(when (and (set? collection-ids) (contains? collection-ids :root))
(throw (ex-info "not implemented." {:collection-ids collection-ids})))
(let [union-queries (queries args)]
{:rows (into []
(comp
(map #(select-keys % (into [:id :model] include-columns)))
(map (fn [v] (update v :model #(keyword "model" %)))))
(stale.db/stale-content-rows union-queries
(into [:id :model] (sort include-columns))
sort-column sort-direction limit offset))
:total (stale.db/stale-content-count union-queries)}))