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.data-apps.generate.schema.source
"Data access for a data app's schema, reified as a protocol.
[[SchemaSource]] names every read that [[metabase-enterprise.data-apps.generate.schemas/fetch-items]] performs, so
the data-access surface is one definition. [[app-db-source]] is the production implementation; tests reify the
protocol with literal values. All methods return the shaped schema entities of the
`metabase-enterprise.data-apps.generate.schema.*` builders, except [[library-tables]], which returns raw table rows
(only `:id` is consumed)."
(:require
[clojure.set :as set]
[metabase-enterprise.data-apps.db :as data-apps.db]
[metabase-enterprise.data-apps.generate.schema.metric :as schemas.metric]
[metabase-enterprise.data-apps.generate.schema.table :as schemas.table]
[metabase.collections.core :as collections]
[metabase.collections.models.collection :as collection]))
(set! *warn-on-reflection* true)
(def LibraryScope
"The library collection tree, classified by collection type: `:data-collection-ids` hold the published tables,
`:metric-collection-ids` the metrics."
[:map {:closed true}
[:data-collection-ids [:set :int]]
[:metric-collection-ids [:set :int]]])
(defn- app-db-library-scope
"The [[LibraryScope]] of the root libraries and their descendants, empty where the instance has no library."
[]
(let [library (collections/library-collection)
descendants (when (and library (not (:archived library)))
(collection/descendants-flat-for [library]))
collections (data-apps.db/unarchived-collections (into #{} (map :id) descendants))
ids-of-type (fn [collection-type]
(into #{} (comp (filter #(= (:type %) collection-type)) (map :id)) collections))]
{:data-collection-ids (ids-of-type collection/library-data-collection-type)
:metric-collection-ids (ids-of-type collection/library-metrics-collection-type)}))
(defprotocol SchemaSource
"The data-access patterns behind schema generation."
(library-scope [source]
"The [[LibraryScope]] of the root libraries.")
(metrics [source collection-ids]
"Schema entities for the metrics in `collection-ids`.")
(tables [source table-ids]
"Schema entities for the tables with `table-ids`.")
(library-tables [source collection-ids]
"Published table rows in `collection-ids`."))
(defn- curated-library-ids
[scope-key requested-ids]
(set/intersection (scope-key (app-db-library-scope)) requested-ids))
(def app-db-source
"The production [[SchemaSource]], backed by the application database."
(reify SchemaSource
(library-scope [_]
(app-db-library-scope))
(metrics [_ collection-ids]
(vec (schemas.metric/metric-schemas
(curated-library-ids :metric-collection-ids collection-ids))))
(tables [_ table-ids]
(vec (schemas.table/table-schemas (schemas.table/select-tables table-ids))))
(library-tables [_ collection-ids]
(vec (schemas.table/select-library-tables
(curated-library-ids :data-collection-ids collection-ids))))))