Skip to content

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

1.9 KB
Raw Blame
(ns metabase-enterprise.audit-app.interface
  (:require
   [metabase.classloader.core :as classloader]
   [metabase.util.i18n :refer [tru]]
   [metabase.util.malli.schema :as ms]))

(def ResultsMetadata
  "Schema for the expected format for `:metadata` returned by an internal query function."
  [:sequential
   {:min 1}
   [:tuple
    ms/KeywordOrString
    [:map {:closed true}
     [:base_type      ms/FieldType]
     [:display_name   ms/NonBlankString]
     [:remapped_to    {:optional true} :keyword]
     [:remapped_from  {:optional true} :keyword]
     [:code           {:optional true} :boolean]]]])

(defmulti internal-query
  "Define a new internal query type. Conventionally `query-type` should be a namespaced keyword with the namespace in
  which the method is defined. See docstring
  for [[metabase-enterprise.audit-app.query-processor.middleware.handle-audit-queries]] for a description of what this
  method should return."
  {:arglists '([query-type & args])}
  (fn [query-type & _]
    (keyword query-type)))

(defmethod internal-query :default
  [query-type & _]
  (throw (ex-info (str (tru "Unable to run internal query function: cannot resolve {0}" query-type))
                  {:status-code 400})))

(def ^:private loadable-namespaces
  "Namespaces that register [[internal-query]] methods on load, and so may be loaded on demand to resolve an
  `:internal` query. Add to this when you add an audit page."
  #{"metabase-enterprise.audit-app.pages.queries"})

(defn resolve-internal-query
  "Invoke the internal query with `query-type` (invokes the corresponding implementation of [[internal-query]])."
  [query-type & args]
  (let [query-type (keyword query-type)
        ns-str     (namespace query-type)]
    (when (contains? loadable-namespaces ns-str)
      ;; The namespace comes from the fixed allowlist above.
      #_{:clj-kondo/ignore [:metabase/modules]}
      (classloader/require (symbol ns-str)))
    (apply internal-query query-type args)))