From c70a17d8fbe97c444c6abb0f21744d1b042f928e Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 30 Oct 2023 09:24:31 +0000 Subject: [PATCH] Update backend docs on Mon Oct 30 09:24:31 UTC 2023 --- index.html | 84 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 29 deletions(-) diff --git a/index.html b/index.html index a007447..f8ba3f8 100644 --- a/index.html +++ b/index.html @@ -2866,7 +2866,7 @@ }; })(SyntaxHighlighter); Metabase -- Marginalia

Metabase


The simplest, fastest way to get business intelligence and analytics to everyone in your company 😋

-



(this space intentionally left almost blank)

namespaces

 

Metabase Backend Developer Documentation

+



(this space intentionally left almost blank)

namespaces

 

Metabase Backend Developer Documentation

Welcome to Metabase! Here are links to useful resources.

@@ -2913,6 +2913,7 @@

Important Libraries

[clojure.string :as str] [dev.debug-qp :as debug-qp] [dev.model-tracking :as model-tracking] + [dev.explain :as dev.explain] [honeysql.core :as hsql] [malli.dev :as malli-dev] [metabase.api.common :as api] @@ -2949,6 +2950,8 @@

Important Libraries

[debug-qp process-query-debug pprint-sql] + [dev.explain + explain-query] [model-tracking track! untrack! @@ -3772,7 +3775,23 @@

Important Libraries

[:field (mt/id :categories :id) {:join-alias "CATEGORIES__via__CATEGORY_ID"}]] :source-table (mt/id :categories) :fk-field-id (mt/id :venues :category_id)}] - "venues")))))
 
+ "venues")))))
 
+
(ns dev.explain
+  (:require
+   [clojure.string :as str]
+   [honey.sql :as sql]
+   [toucan2.core :as t2]))

Explain a sql query or a honeysql query with option to analyze the query.

+
(defn explain-query
+  ([queryable]
+   (explain-query queryable false))
+  ([queryable analyze?]
+   (->> (t2/query
+         (str/join
+          " "
+          (remove nil? ["EXPLAIN"
+                        (when analyze? "ANALYZE")
+                        "(" (if (map? queryable) (first (sql/format queryable {:inline true})) queryable) ")"])))
+        (map #(get % (keyword "query plan"))))))
 
(ns dev.h2-shell
   (:require [environ.core :as env]
             [metabase.db.data-source :as mdb.data-source]
@@ -16387,22 +16406,23 @@ 

Important Libraries

(sql.helpers/where query [:= id :database_id]) query))
(mu/defn ^:private replace-select :- :map
-  "Replace a select from query that has alias is `target-alias` with the `with` column, throw an error if
+  "Replace a select from query that has alias is `target-alias` with [`with` `target-alias`] column, throw an error if
   can't find the target select.
   This works with the assumption that `query` contains a list of select from [[select-clause-for-model]],
   and some of them are dummy column casted to the correct type.
   This function then will replace the dummy column with alias is `target-alias` with the `with` column."
   [query        :- :map
    target-alias :- :keyword
-   with         :- [:or :keyword [:sequential :any]]]
-  (let [selects (:select query)
-        idx     (first (keep-indexed (fn [index item]
-                                       (when (and (coll? item)
-                                                  (= (last item) target-alias))
-                                         index))
-                                     selects))]
+   with         :- :keyword]
+  (let [selects     (:select query)
+        idx         (first (keep-indexed (fn [index item]
+                                           (when (and (coll? item)
+                                                      (= (last item) target-alias))
+                                             index))
+                                         selects))
+        with-select [with target-alias]]
     (if (some? idx)
-      (assoc query :select (m/replace-nth idx with selects))
+      (assoc query :select (m/replace-nth idx with-select selects))
       (throw (ex-info "Failed to replace selector" {:status-code  400
                                                     :target-alias target-alias
                                                     :with         with})))))
@@ -16410,12 +16430,22 @@

Important Libraries

[query :- :map model :- [:enum "card" "dataset" "dashboard" "metric"]] (-> query - (replace-select :last_editor_id [:r.user_id :last_editor_id]) - (replace-select :last_edited_at [:r.timestamp :last_edited_at]) + (replace-select :last_editor_id :r.user_id) + (replace-select :last_edited_at :r.timestamp) (sql.helpers/left-join [:revision :r] [:and [:= :r.model_id (search.config/column-with-model-alias model :id)] [:= :r.most_recent true] - [:= :r.model (search.config/search-model->revision-model model)]])))

+----------------------------------------------------------------------------------------------------------------+ + [:= :r.model (search.config/search-model->revision-model model)]])))

+
(mu/defn ^:private with-moderated-status :- :map
+  [query :- :map
+   model :- [:enum "card" "dataset"]]
+  (-> query
+      (replace-select :moderated_status :mr.status)
+      (sql.helpers/left-join [:moderation_review :mr]
+                             [:and
+                              [:= :mr.moderated_item_type "card"]
+                              [:= :mr.moderated_item_id (search.config/column-with-model-alias model :id)]
+                              [:= :mr.most_recent true]])))

+----------------------------------------------------------------------------------------------------------------+ | Search Queries for each Toucan Model | +----------------------------------------------------------------------------------------------------------------+

@@ -16433,7 +16463,8 @@

Important Libraries

[:= :bookmark.user_id api/*current-user-id*]]) (add-collection-join-and-where-clauses :card.collection_id search-ctx) (add-card-db-id-clause (:table-db-id search-ctx)) - (with-last-editing-info model)))
+ (with-last-editing-info model) + (with-moderated-status model)))
(defmethod search-query-for-model "action"
   [model search-ctx]
   (-> (base-query-for-model model search-ctx)
@@ -79688,7 +79719,8 @@ 

`::position`

[:table.db_id :database_id] [:table.schema :table_schema] [:table.name :table_name] - [:table.description :table_description]])

The columns that will be returned by the query for model, excluding :model, which is added automatically.

+ [:table.description :table_description]])

The columns that will be returned by the query for model, excluding :model, which is added automatically. +This is not guaranteed to be the final list of columns, new columns can be added by calling [[api.search/replace-select]]

(defmulti columns-for-model
   {:arglists '([model])}
   (fn [model] model))
@@ -79706,17 +79738,6 @@

`::position`

(conj default-columns :collection_id :collection_position :dataset_query :creator_id [:collection.name :collection_name] [:collection.authority_level :collection_authority_level] - [{:select [:status] - :from [:moderation_review] - :where [:and - [:= :moderated_item_type "card"] - [:= :moderated_item_id :card.id] - [:= :most_recent true]] - ;; order by and limit just in case a bug violates the invariant of only one most_recent. We don't want to - ;; error in this query - :order-by [[:id :desc]] - :limit 1} - :moderated_status] bookmark-col dashboardcard-count-col))
(defmethod columns-for-model "indexed-entity" [_]
   [[:model-index-value.name     :name]
@@ -79928,7 +79949,12 @@ 

`::position`

;; => true

(defn- joined-with-table?
   [query join-type table]
-  (->> (get query join-type) (partition 2) (map first) (some #(= % table)) boolean))
+ (->> (get query join-type) (partition 2) (map first) (some #(= % table)) boolean))

Return the apporpriate revision model given a search model.

+
(defn search-model->revision-model
+  [model]
+  (case model
+    "dataset" (recur "card")
+    (str/capitalize model)))
(doseq [model ["dashboard" "card" "dataset" "metric"]]
   (defmethod build-optional-filter-query [:last-edited-by model]
     [_filter model query editor-ids]
@@ -109409,7 +109435,7 @@ 

Java system property user.dir

min-retention-days) (do (truncate-audit-log.i/log-minimum-value-warning env-var-value) min-retention-days) - :else env-var-value))))