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
Fix Metabot's X-ray links for queries and metrics (#84868)
Andrei Simionescua797d1e45d3c
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 850e3e56448..49922d2c0cf 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -1568,7 +1568,8 @@utilutil.configwarehouse-schema-overlay- warehouses}+ warehouses+ xrays}:model-exports #{:model/AiUsageLog:model/Metabot:model/MetabotConversationdiff --git a/resources/metabot/prompts/system/internal.selmer b/resources/metabot/prompts/system/internal.selmerindex 281e09883e7..a31ca252957 100644--- a/resources/metabot/prompts/system/internal.selmer+++ b/resources/metabot/prompts/system/internal.selmer@@ -34,7 +34,7 @@ Metabase is read-only: your SQL is always a `SELECT` (CTEs/`WITH` are fine for tNatural-language querying is your default for analytical questions — build the visualization directly rather than describing how.{% endif %}{% if has_other_tools %}-You can create X-ray auto-generated dashboards from a single source — a table, model, metric, saved question, or a query you built earlier in this conversation — but not custom dashboards with hand-placed cards. Use that tool only when the user explicitly asks for a dashboard.+You can create X-ray auto-generated dashboards from a single source – a table, model, metric, saved question, or a notebook query from this conversation (`query_id`) – but not custom dashboards with hand-placed cards. SQL queries must be saved first. Use that tool only when the user explicitly asks for a dashboard.{% else %}**You cannot create dashboards or documents.** Help users find existing dashboards through search instead.{% endif %}diff --git a/src/metabase/metabot/tools/autogen_dashboard.clj b/src/metabase/metabot/tools/autogen_dashboard.cljindex 98be94511c6..b976fc3a85d 100644--- a/src/metabase/metabot/tools/autogen_dashboard.clj+++ b/src/metabase/metabot/tools/autogen_dashboard.clj@@ -2,14 +2,19 @@"Autogenerated dashboard tool wrappers."(:require[clojure.string :as str]+ [medley.core :as m][metabase.api.common :as api]+ [metabase.lib-be.core :as lib-be]+ [metabase.lib.core :as lib][metabase.metabot.agent.streaming :as streaming][metabase.metabot.db :as metabot.db][metabase.metabot.scope :as scope][metabase.metabot.tools.core :as tools][metabase.metabot.tools.shared :as shared][metabase.util.log :as log]- [metabase.util.malli :as mu]))+ [metabase.util.malli :as mu]+ [metabase.util.malli.registry :as mr]+ [metabase.xrays.core :as xrays]))(set! *warn-on-reflection* true)@@ -35,6 +40,24 @@:recovery [{:uses #{"search"}:text "Call `search` to find another source and use an id from the results."}]})+(tools/defrecoverable incomplete-query!+ "The conversation query is incomplete."+ {:payload [:map {:closed true}+ [:query-id :string]]}+ [{:keys [query-id]}]+ {:message (str "Query " query-id " has no data source yet, so X-ray cannot analyze it.")+ :recovery [{:uses #{"search"}+ :text "Call `search` to find a table, model or metric to X-ray instead."}]})++(tools/defrecoverable unsupported-native-query!+ "X-ray cannot analyze an unsaved SQL query."+ {:payload [:map {:closed true}+ [:query-id :string]]}+ [{:keys [query-id]}]+ {:message (str "Query " query-id " is an unsaved SQL query, which X-ray does not support.")+ :recovery [{:uses #{"search"}+ :text "Call `search` to find a table, model, metric or saved question to X-ray instead."}]})+(defn- readable-source"The `kind` entity with `id` and its X-ray path, if the current user can read it and it is still in use."[kind id]@@ -45,33 +68,49 @@(when (or (:archived entity) (false? (:active entity)))(unavailable-source! {:kind kind :id id})){:entity entity- :path (str "/auto/dashboard/" (name kind) "/" id)}))+ ;; X-ray has no page for metrics, but its question page accepts any card type+ :path (str "/auto/dashboard/" (name (if (= :metric kind) :question kind)) "/" id)}))++(defn- source-value?+ "Return whether `v` identifies a source.++ The model sometimes sends `nil`, `0` or `\"\"` for unused fields instead of leaving them out."+ [v]+ (or (pos-int? v)+ (and (string? v) (not (str/blank? v)))))(defn- resolve-source"Resolve the source entity from the provided source parameters.Returns the entity to analyze and the path for navigation."- [{:keys [table_id model_id metric_id query_id report_id] :as source} memory]- (when-not (= 1 (count (remove nil? (vals source))))- (invalid-source! {}))- (cond- table_id (readable-source :table table_id)- model_id (readable-source :model model_id)- metric_id (readable-source :metric metric_id)- report_id (readable-source :question report_id)-- query_id- ;; For query_id, we need to resolve from memory- (let [queries-state (get-in memory [:state :queries] {})- query (get queries-state query_id)]- (when-not query- (throw (ex-info (str "Query not found in memory with ID: " query_id- ". Available queries: [" (str/join ", " (keys queries-state)) "]")- {:agent-error? true- :query-id query_id- :available-queries (keys queries-state)})))- ;; For queries, we create a transient card-like structure- {:entity {:dataset_query query}- :path (str "/auto/dashboard/adhoc/" query_id)})))+ [source memory]+ (let [{:keys [table_id model_id metric_id query_id report_id] :as source} (m/filter-vals source-value? source)]+ (when-not (= 1 (count source))+ (invalid-source! {}))+ (cond+ table_id (readable-source :table table_id)+ model_id (readable-source :model model_id)+ metric_id (readable-source :metric metric_id)+ report_id (readable-source :question report_id)++ query_id+ (let [queries-state (get-in memory [:state :queries] {})+ query (get queries-state query_id)+ mbql-query (when (and (:database query) (lib/normalized-query-type query))+ (lib-be/normalize-query query))]+ (when-not query+ (throw (ex-info (str "Query not found in memory with ID: " query_id+ ". Available queries: [" (str/join ", " (keys queries-state)) "]")+ {:agent-error? true+ :query-id query_id+ :available-queries (keys queries-state)})))+ (when-not (mr/validate :metabase.lib.schema/query mbql-query)+ (incomplete-query! {:query-id query_id}))+ (let [entity (tools/with-entity {:kind :query :id query_id}+ (xrays/adhoc-query-read-check {:dataset_query query}))]+ (when (lib/native? mbql-query)+ (unsupported-native-query! {:query-id query_id}))+ {:entity entity+ :path (str "/auto/dashboard/adhoc/" (xrays/encode-base64-json query))})))));;; Response Messages@@ -98,7 +137,7 @@- {:table_id 123} - Create dashboard for a table- {:model_id 456} - Create dashboard for a model- {:metric_id 789} - Create dashboard for a metric- - {:query_id \"abc\"} - Create dashboard for query results+ - {:query_id \"abc\"} - Create dashboard for notebook query results (SQL queries must be saved first)- {:report_id 101} - Create dashboard for an existing report/question- memory-atom: Atom containing agent memory (for query resolution)@@ -116,7 +155,7 @@;; Note: The actual automagic analysis happens on navigation to the /auto/dashboard/... URL;; The frontend handles rendering the X-ray dashboard- (log/info "Autogenerated dashboard created" {:path path})+ (log/info "Autogenerated dashboard created" {:source source}){:structured-output {:message response-message:path path}:data-parts [(streaming/dashboard-entity-part {:title title :url path})]@@ -144,7 +183,7 @@- table_id: Create dashboard for a table- model_id: Create dashboard for a model- metric_id: Create dashboard for a metric- - query_id: Create dashboard for query results from this conversation+ - query_id: Create dashboard for notebook queries from this conversation (SQL queries must be saved first)- report_id: Create dashboard for an existing report/question"[{:keys [source]} :- autogen-dashboard-schema](let [result (create-autogenerated-dashboarddiff --git a/src/metabase/metabot/tools/recoverable/common.clj b/src/metabase/metabot/tools/recoverable/common.cljindex 7d99fd21d96..d40022fb564 100644--- a/src/metabase/metabot/tools/recoverable/common.clj+++ b/src/metabase/metabot/tools/recoverable/common.clj@@ -14,7 +14,7 @@"The entity kinds [[not-found!]] can report on. A closed set because the kind reaches the model asa noun in English — an unexpected keyword would read as gibberish, and the payload check turnsthat into an `:internal` error instead."- [:enum :card :chart :collection :dashboard :database :document :field :metric :model :question+ [:enum :card :chart :collection :dashboard :database :document :field :metric :model :query :question:segment :measure :snippet :table :timeline :transform])(defrecoverable not-found!diff --git a/src/metabase/xrays/api/automagic_dashboards.clj b/src/metabase/xrays/api/automagic_dashboards.cljindex c07316de5a3..085196a1d2e 100644--- a/src/metabase/xrays/api/automagic_dashboards.clj+++ b/src/metabase/xrays/api/automagic_dashboards.clj@@ -102,12 +102,11 @@;; ----------------------------------------- API Endpoints for viewing a transient dashboard -----------------(defn- adhoc-query-read-check+(defn adhoc-query-read-check+ "Return `query` if the user has data and source card permissions; otherwise throw a 403."[query](api/check-403- (query-perms/check-data-perms (:dataset_query query)- (query-perms/required-perms-for-query (:dataset_query query))- :throw-exceptions? false))+ (query-perms/can-run-query? (:dataset_query query)))query)(defn- ensure-intdiff --git a/src/metabase/xrays/core.clj b/src/metabase/xrays/core.cljindex d32d7528a4d..5309db3ff7b 100644--- a/src/metabase/xrays/core.clj+++ b/src/metabase/xrays/core.clj@@ -1,16 +1,24 @@(ns metabase.xrays.core"API namespace for X-Ray related stuff."(:require+ [metabase.xrays.api.automagic-dashboards][metabase.xrays.automagic-dashboards.populate]+ [metabase.xrays.automagic-dashboards.util][metabase.xrays.related][potemkin :as p]))(comment+ metabase.xrays.api.automagic-dashboards/keep-memetabase.xrays.automagic-dashboards.populate/keep-me+ metabase.xrays.automagic-dashboards.util/keep-memetabase.xrays.related/keep-me)(p/import-vars+ [metabase.xrays.api.automagic-dashboards+ adhoc-query-read-check][metabase.xrays.automagic-dashboards.populateget-or-create-container-collection]+ [metabase.xrays.automagic-dashboards.util+ encode-base64-json][metabase.xrays.relatedrelated])diff --git a/test/metabase/metabot/tools/autogen_dashboard_test.clj b/test/metabase/metabot/tools/autogen_dashboard_test.cljindex e0763693593..cf97e6b1f44 100644--- a/test/metabase/metabot/tools/autogen_dashboard_test.clj+++ b/test/metabase/metabot/tools/autogen_dashboard_test.clj@@ -2,10 +2,17 @@(:require[clojure.string :as str][clojure.test :refer :all]+ [metabase.api.macros :as api.macros]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.metabot.agent.core :as agent]+ [metabase.metabot.context :as metabot.context][metabase.metabot.test-util :as test-util][metabase.metabot.tools.autogen-dashboard :as autogen-dashboard][metabase.metabot.tools.legacy :as tools.legacy]- [metabase.test :as mt]))+ [metabase.metabot.tools.shared :as shared]+ [metabase.test :as mt]+ [metabase.util.log.capture :as log.capture]));;; Source Resolution Tests (without database)@@ -88,7 +95,7 @@(let [result (autogen-dashboard/create-autogenerated-dashboard{:source {:metric_id (:id metric)}:memory-atom (atom {:state {}})})]- (is (= (str "/auto/dashboard/metric/" (:id metric))+ (is (= (str "/auto/dashboard/question/" (:id metric))(get-in result [:structured-output :path]))))))(testing "report source generates correct path"(mt/with-temp [:model/Card report {:type :question :name "test_report"}]@@ -96,14 +103,54 @@{:source {:report_id (:id report)}:memory-atom (atom {:state {}})})](is (= (str "/auto/dashboard/question/" (:id report))- (get-in result [:structured-output :path]))))))- (testing "query source generates correct path"- (let [query {:lib/type :mbql/query :database 1 :stages []}- memory-atom (atom {:state {:queries {"q123" query}}})]- (is (=? {:structured-output {:path "/auto/dashboard/adhoc/q123"}}- (autogen-dashboard/create-autogenerated-dashboard- {:source {:query_id "q123"}- :memory-atom memory-atom})))))))+ (get-in result [:structured-output :path]))))))))++(defn- xray-page+ "Fetch the dashboard for `path` from the X-ray API as `:rasta`."+ [expected-status path]+ (mt/user-http-request :rasta :get expected-status+ (str "automagic-dashboards/" (str/replace-first path "/auto/dashboard/" ""))))++(defn- venues-query []+ (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/table mp (mt/id :venues)))))++(deftest xray-page-test+ (let [venues (venues-query)]+ (mt/with-temp [:model/Card {model-id :id} {:type :model :dataset_query venues}+ :model/Card {metric-id :id} {:type :metric :dataset_query (lib/aggregate venues (lib/count))}+ :model/Card {question-id :id} {:type :question :dataset_query venues}]+ (binding [shared/*memory-atom* (atom {:state {:queries {"q-1" venues+ "q-legacy" (lib/->legacy-MBQL venues)+ "q-model" (lib/query (mt/metadata-provider)+ (lib.metadata/card+ (mt/metadata-provider) model-id))+ "q-staged" (lib/append-stage venues)}}})]+ (mt/with-current-user (mt/user->id :rasta)+ (doseq [source [{:table_id (mt/id :venues)}+ {:model_id model-id}+ {:metric_id metric-id}+ {:report_id question-id}+ {:query_id "q-1"}+ {:query_id "q-legacy"}+ {:query_id "q-model"}+ {:query_id "q-staged"}]]+ (testing (str "the X-ray link for " (pr-str source) " returns a dashboard from the API")+ (is (=? {:dashcards seq}+ (->> (autogen-dashboard/create-autogenerated-dashboard-tool {:source source})+ :structured-output+ :path+ (xray-page 200)))))))))))++(deftest log-test+ (testing "the log records the source and leaves out the query text"+ (binding [shared/*memory-atom* (atom {:state {:queries {"q-1" (venues-query)}}})]+ (log.capture/with-log-messages-for-level [logs [metabase.metabot.tools.autogen-dashboard :info]]+ (mt/with-current-user (mt/user->id :rasta)+ (autogen-dashboard/create-autogenerated-dashboard-tool {:source {:query_id "q-1"}}))+ (is (= ["Creating autogenerated dashboard {:source {:query_id q-1}}"+ "Autogenerated dashboard created {:source {:query_id q-1}}"]+ (map :message (logs))))))));;; Entity Not Found Tests@@ -178,6 +225,80 @@(mt/with-current-user (mt/user->id :crowberto)(autogen-dashboard/create-autogenerated-dashboard-tool {:source {source-key id}})))))))))))+(deftest unrunnable-query-test+ (testing "a user without query permissions gets no X-ray link"+ (binding [shared/*memory-atom* (atom {:state {:queries {"q-1" (venues-query)}}})]+ (let [path (mt/with-current-user (mt/user->id :crowberto)+ (-> (autogen-dashboard/create-autogenerated-dashboard-tool {:source {:query_id "q-1"}})+ :structured-output+ :path))]+ (mt/with-no-data-perms-for-all-users!+ (is (= "You don't have permissions to do that."+ (xray-page 403 path)))+ (is (= {:class :recoverable+ :code :metabase.metabot.tools.recoverable.common/not-found+ :text (str "Query q-1 was not found. It may not exist, or you may not have access to it.\n"+ "Call `search` to find the entity you want and use an id from the results.")}+ (mt/with-current-user (mt/user->id :rasta)+ (failure {:query_id "q-1"})))))))))++(deftest unreadable-joined-card-test+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-temp [:model/Collection {collection-id :id} {}+ :model/Card {card-id :id} {:collection_id collection-id+ :dataset_query (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/table mp (mt/id :categories))))}]+ (let [mp (mt/metadata-provider)+ query (-> (venues-query)+ (lib/join (lib.metadata/card mp card-id)))]+ (doseq [query [query (lib/->legacy-MBQL query)]]+ (binding [shared/*memory-atom* (atom {:state {:queries {"q-join" query}}})]+ (let [path (mt/with-current-user (mt/user->id :crowberto)+ (-> (autogen-dashboard/create-autogenerated-dashboard-tool {:source {:query_id "q-join"}})+ :structured-output :path))]+ (testing "the X-ray API denies a query joining a card the user cannot read"+ (is (= "You don't have permissions to do that."+ (xray-page 403 path))))+ (testing "the tool refuses the query before returning a link"+ (is (= {:class :recoverable+ :code :metabase.metabot.tools.recoverable.common/not-found+ :text (str "Query q-join was not found. It may not exist, or you may not have access to it.\n"+ "Call `search` to find the entity you want and use an id from the results.")}+ (mt/with-current-user (mt/user->id :rasta)+ (failure {:query_id "q-join"}))))))))))))++(deftest draft-query-test+ (testing "a draft query from the viewing context needs a data source for X-ray"+ (doseq [draft [{} {:stages []} {:database nil :type "query" :query {:source-table nil}}]]+ (testing (pr-str draft)+ (let [context (api.macros/decode-and-validate-params+ :body ::metabot.context/context+ {:user_is_viewing [{:id "q-draft" :type "adhoc" :query draft}]})]+ (binding [shared/*memory-atom* (atom {:state (#'agent/seed-state {} context)})]+ (is (= {:class :recoverable+ :code ::autogen-dashboard/incomplete-query+ :text (str "Query q-draft has no data source yet, so X-ray cannot analyze it.\n"+ "Call `search` to find a table, model or metric to X-ray instead.")}+ (mt/with-current-user (mt/user->id :crowberto)+ (failure {:query_id "q-draft"}))))))))))++(deftest sourceless-query-test+ (testing "selecting a database without a table or card is not enough for an X-ray"+ (doseq [draft [{:database (mt/id)}+ {:database (mt/id) :type "query" :query {:source-table nil}}+ {:database (mt/id) :lib/type :mbql/query :stages []}+ {:database (mt/id) :lib/type :mbql/query :stages [{:lib/type :mbql.stage/mbql}]}+ {:database (mt/id) :lib/type :mbql/query+ :stages [{:lib/type :mbql.stage/mbql :source-table nil}]}]]+ (testing (pr-str draft)+ (binding [shared/*memory-atom* (atom {:state {:queries {"q-draft" draft}}})]+ (is (= {:class :recoverable+ :code ::autogen-dashboard/incomplete-query+ :text (str "Query q-draft has no data source yet, so X-ray cannot analyze it.\n"+ "Call `search` to find a table, model or metric to X-ray instead.")}+ (mt/with-current-user (mt/user->id :crowberto)+ (failure {:query_id "q-draft"})))))))))+(deftest unavailable-source-test(mt/with-temp [:model/Card {model-id :id} {:type :model :archived true}:model/Table {table-id :id} {:name (mt/random-name) :db_id (mt/id) :active false}]@@ -201,9 +322,50 @@:code ::autogen-dashboard/invalid-source:text "Set exactly one of `table_id`, `model_id`, `metric_id`, `query_id` or `report_id` in `source`."}(failure {:table_id 1 :model_id 2}))))- (testing "a null for an unused source is not a second source"- (mt/with-temp [:model/Card {model-id :id} {:type :model}]+ (testing "a placeholder for an unused source is not a second source"+ (mt/with-temp [:model/Table {table-id :id} {:name (mt/random-name) :db_id (mt/id)}+ :model/Card {model-id :id} {:type :model}]+ (mt/with-current-user (mt/user->id :rasta)+ (doseq [[source path] [[{:table_id nil :model_id model-id} (str "/auto/dashboard/model/" model-id)]+ [{:table_id table-id :model_id 0} (str "/auto/dashboard/table/" table-id)]+ [{:table_id 0 :model_id model-id} (str "/auto/dashboard/model/" model-id)]+ [{:query_id "" :table_id table-id} (str "/auto/dashboard/table/" table-id)]]]+ (testing (pr-str source)+ (is (=? {:structured-output {:path path}}+ (autogen-dashboard/create-autogenerated-dashboard-tool {:source source})))))))))++(deftest native-query-test+ (testing "an unsaved SQL query gets a recoverable refusal instead of a broken X-ray link"+ (doseq [query ((juxt identity lib/->legacy-MBQL)+ (lib/native-query (mt/metadata-provider) "SELECT * FROM VENUES"))]+ (testing (pr-str (dissoc query :lib/metadata))+ (binding [shared/*memory-atom* (atom {:state {:queries {"q-sql" query}}})]+ (is (= {:class :recoverable+ :code ::autogen-dashboard/unsupported-native-query+ :text (str "Query q-sql is an unsaved SQL query, which X-ray does not support.\n"+ "Call `search` to find a table, model, metric or saved question to X-ray instead.")}+ (mt/with-current-user (mt/user->id :rasta)+ (failure {:query_id "q-sql"})))))))))++(deftest saved-native-query-test+ (testing "a saved SQL question has a working X-ray link"+ (mt/with-temp [:model/Card {card-id :id} (mt/card-with-metadata+ {:dataset_query (lib/native-query (mt/metadata-provider)+ "SELECT * FROM VENUES")})](mt/with-current-user (mt/user->id :rasta)- (is (=? {:structured-output {:path (str "/auto/dashboard/model/" model-id)}}- (autogen-dashboard/create-autogenerated-dashboard-tool- {:source {:table_id nil :model_id model-id}})))))))+ (is (=? {:dashcards seq}+ (->> (autogen-dashboard/create-autogenerated-dashboard-tool {:source {:report_id card-id}})+ :structured-output :path+ (xray-page 200))))))))++(deftest unreadable-native-query-test+ (testing "a SQL query that the user cannot run returns a not-found error"+ (binding [shared/*memory-atom* (atom {:state {:queries {"q-sql" (lib/native-query (mt/metadata-provider)+ "SELECT * FROM VENUES")}}})]+ (mt/with-no-data-perms-for-all-users!+ (is (= {:class :recoverable+ :code :metabase.metabot.tools.recoverable.common/not-found+ :text (str "Query q-sql was not found. It may not exist, or you may not have access to it.\n"+ "Call `search` to find the entity you want and use an id from the results.")}+ (mt/with-current-user (mt/user->id :rasta)+ (failure {:query_id "q-sql"}))))))))