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
Move the creation of the "card_view" tracking from the card APIs to query execution level (#64115)
Nathan Voxlandb7bf8b6eccb1
diff --git a/e2e/test/scenarios/dashboard/tabs.cy.spec.js b/e2e/test/scenarios/dashboard/tabs.cy.spec.jsindex ab635bbc74d..c860f64b219 100644--- a/e2e/test/scenarios/dashboard/tabs.cy.spec.js+++ b/e2e/test/scenarios/dashboard/tabs.cy.spec.js@@ -428,10 +428,10 @@ describe("scenarios > dashboard > tabs", () => {cy.get("@secondTabQuerySpy").should("not.have.been.called");cy.wait("@firstTabQuery").then((r) => {firstQuestion().then((r) => {- expect(r.view_count).to.equal(3); // 1 (previously) + 1 (firstQuestion) + 1 (firstTabQuery)+ expect(r.view_count).to.equal(2); // 1 (previously) + 1 (firstQuestion)});secondQuestion().then((r) => {- expect(r.view_count).to.equal(2); // 1 (previously) + 1 (secondQuestion)+ expect(r.view_count).to.equal(1); // 1 (previously)});});@@ -441,10 +441,10 @@ describe("scenarios > dashboard > tabs", () => {cy.get("@firstTabQuerySpy").should("have.been.calledOnce");cy.wait("@secondTabQuery").then((r) => {firstQuestion().then((r) => {- expect(r.view_count).to.equal(4); // 3 (previously) + 1 (firstQuestion)+ expect(r.view_count).to.equal(2); // 2 (previously)});secondQuestion().then((r) => {- expect(r.view_count).to.equal(4); // 2 (previously) + 1 (secondQuestion) + 1 (secondTabQuery)+ expect(r.view_count).to.equal(2); // 1(previously) + 1 (secondTabQuery)});});@@ -455,10 +455,10 @@ describe("scenarios > dashboard > tabs", () => {cy.get("@secondTabQuerySpy").should("have.been.calledOnce");firstQuestion().then((r) => {- expect(r.view_count).to.equal(5); // 4 (previously) + 1 (firstQuestion)+ expect(r.view_count).to.equal(2); // 2 (previously)});secondQuestion().then((r) => {- expect(r.view_count).to.equal(5); // 4 (previously) + 1 (secondQuestion)+ expect(r.view_count).to.equal(2); // 2 (previously)});// Go to public dashboard@@ -488,10 +488,10 @@ describe("scenarios > dashboard > tabs", () => {cy.get("@publicSecondTabQuerySpy").should("not.have.been.called");cy.wait("@publicFirstTabQuery").then((r) => {firstQuestion().then((r) => {- expect(r.view_count).to.equal(7); // 5 (previously) + 1 (firstQuestion) + 1 (publicFirstTabQuery)+ expect(r.view_count).to.equal(3); // 2 (previously) + 1 (publicFirstTabQuery)});secondQuestion().then((r) => {- expect(r.view_count).to.equal(6); // 5 (previously) + 1 (secondQuestion)+ expect(r.view_count).to.equal(2); // 2 (previously)});});@@ -501,10 +501,10 @@ describe("scenarios > dashboard > tabs", () => {cy.get("@publicFirstTabQuerySpy").should("have.been.calledOnce");cy.wait("@publicSecondTabQuery").then((r) => {firstQuestion().then((r) => {- expect(r.view_count).to.equal(8); // 7 (previously) + 1 (firstQuestion)+ expect(r.view_count).to.equal(3); // 3 (previously)});secondQuestion().then((r) => {- expect(r.view_count).to.equal(8); // 6 (previously) + 1 (secondQuestion) + 1 (publicSecondTabQuery)+ expect(r.view_count).to.equal(3); // 2 (previously) + 1 (publicSecondTabQuery)});});@@ -543,13 +543,6 @@ describe("scenarios > dashboard > tabs", () => {.its("body");};- firstQuestion().then((r) => {- expect(r.view_count).to.equal(1); // 1 (firstQuestion)- });- secondQuestion().then((r) => {- expect(r.view_count).to.equal(1); // 1 (secondQuestion)- });-cy.intercept("GET",`/api/embed/dashboard/*/dashcard/*/card/${ORDERS_QUESTION_ID}*`,@@ -578,7 +571,7 @@ describe("scenarios > dashboard > tabs", () => {expect(r.view_count).to.equal(3); // 1 (previously) + 1 (firstQuestion) + 1 (first tab query)});secondQuestion().then((r) => {- expect(r.view_count).to.equal(2); // 1 (previously) + 1 (secondQuestion)+ expect(r.view_count).to.equal(1); // 1 (previously)});});@@ -587,10 +580,10 @@ describe("scenarios > dashboard > tabs", () => {cy.get("@firstTabQuerySpy").should("have.been.calledOnce");cy.wait("@secondTabQuery").then((r) => {firstQuestion().then((r) => {- expect(r.view_count).to.equal(4); // 3 (previously) + 1 (firstQuestion)+ expect(r.view_count).to.equal(3); // 3 (previously)});secondQuestion().then((r) => {- expect(r.view_count).to.equal(4); // 2 (previously) + 1 (secondQuestion) + 1 (second tab query)+ expect(r.view_count).to.equal(2); // 1 (previously) + 1 (second tab query)});});diff --git a/e2e/test/scenarios/documents/documents.cy.spec.ts b/e2e/test/scenarios/documents/documents.cy.spec.tsindex 7aedde92c4c..27e1ca1039f 100644--- a/e2e/test/scenarios/documents/documents.cy.spec.ts+++ b/e2e/test/scenarios/documents/documents.cy.spec.ts@@ -508,7 +508,7 @@ H.describeWithSnowplowEE("documents", () => {H.createQuestion(ACCOUNTS_COUNT_BY_CREATED_AT);// Need to get this one to simulate recent activityH.createQuestion(PRODUCTS_COUNT_BY_CATEGORY_PIE).then(- ({ body: { id } }) => cy.request(`/api/card/${id}`),+ ({ body: { id } }) => cy.request("POST", `/api/card/${id}/query`),);H.createDashboard({name: "Fancy Dashboard",diff --git a/src/metabase/dashboards/api.clj b/src/metabase/dashboards/api.cljindex 78c3616f084..cf41713790a 100644--- a/src/metabase/dashboards/api.clj+++ b/src/metabase/dashboards/api.clj@@ -1252,13 +1252,12 @@[:dashboard_load_id {:optional true} [:maybe ms/NonBlankString]][:parameters {:optional true} [:maybe [:sequential ParameterWithID]]]]](with-dashboard-load-id dashboard_load_id- (u/prog1 (m/mapply qp.dashboard/process-query-for-dashcard- (merge- body- {:dashboard-id dashboard-id- :card-id card-id- :dashcard-id dashcard-id}))- (events/publish-event! :event/card-read {:object-id card-id, :user-id api/*current-user-id*, :context :dashboard}))))+ (m/mapply qp.dashboard/process-query-for-dashcard+ (merge+ body+ {:dashboard-id dashboard-id+ :card-id card-id+ :dashcard-id dashcard-id}))))(api.macros/defendpoint :post "/:dashboard-id/dashcard/:dashcard-id/card/:card-id/query/:export-format""Run the query associated with a Saved Question (`Card`) in the context of a `Dashboard` that includes it, and return@@ -1307,15 +1306,14 @@[{:keys [dashboard-id dashcard-id card-id]} :- [:map[:dashboard-id ms/PositiveInt][:dashcard-id ms/PositiveInt]- [:card-id ms/PositiveInt]]+ [:card-id ms/PositiveInt]]_query-paramsbody :- [:map[:parameters {:optional true} [:maybe [:sequential ParameterWithID]]]]]- (u/prog1 (m/mapply qp.dashboard/process-query-for-dashcard- (merge- body- {:dashboard-id dashboard-id- :card-id card-id- :dashcard-id dashcard-id- :qp qp.pivot/run-pivot-query}))- (events/publish-event! :event/card-read {:object-id card-id, :user-id api/*current-user-id*, :context :dashboard})))+ (m/mapply qp.dashboard/process-query-for-dashcard+ (merge+ body+ {:dashboard-id dashboard-id+ :card-id card-id+ :dashcard-id dashcard-id+ :qp qp.pivot/run-pivot-query})))diff --git a/src/metabase/embedding/api/embed.clj b/src/metabase/embedding/api/embed.cljindex 69e5fa7ef1e..6a6a1c987ff 100644--- a/src/metabase/embedding/api/embed.clj+++ b/src/metabase/embedding/api/embed.clj@@ -75,8 +75,7 @@[:token string?]]](let [unsigned (unsign-and-translate-ids token)](api.embed.common/check-embedding-enabled-for-card (embedding.jwt/get-in-unsigned-token-or-throw unsigned [:resource :question]))- (u/prog1 (api.embed.common/card-for-unsigned-token unsigned, :constraints [:enable_embedding true])- (events/publish-event! :event/card-read {:object-id (:id <>), :user-id api/*current-user-id*, :context :question}))))+ (api.embed.common/card-for-unsigned-token unsigned, :constraints [:enable_embedding true])))(defn ^:private run-query-for-unsigned-token-async"Run the query belonging to Card identified by `unsigned-token`. Checks that embedding is enabled both globally and@@ -183,9 +182,8 @@[:dashcard-id ms/PositiveInt][:card-id ms/PositiveInt]]query-params :- :map]- (u/prog1 (process-query-for-dashcard-with-signed-token token dashcard-id card-id :api- (api.embed.common/parse-query-params query-params))- (events/publish-event! :event/card-read {:object-id card-id, :user-id api/*current-user-id*, :context :dashboard})))+ (process-query-for-dashcard-with-signed-token token dashcard-id card-id :api+ (api.embed.common/parse-query-params query-params)));;; +----------------------------------------------------------------------------------------------------------------+;;; | FieldValues, Search, Remappings |@@ -312,10 +310,9 @@[:dashcard-id ms/PositiveInt][:card-id ms/PositiveInt]]query-params :- :map]- (u/prog1 (process-query-for-dashcard-with-signed-token token dashcard-id card-id- :api (api.embed.common/parse-query-params query-params)- :qp qp.pivot/run-pivot-query)- (events/publish-event! :event/card-read {:object-id card-id, :user-id api/*current-user-id*, :context :dashboard})))+ (process-query-for-dashcard-with-signed-token token dashcard-id card-id+ :api (api.embed.common/parse-query-params query-params)+ :qp qp.pivot/run-pivot-query))(api.macros/defendpoint :get "/tiles/card/:token/:zoom/:x/:y""Generates a single tile image for an embedded Card using the map visualization."diff --git a/src/metabase/public_sharing/api.clj b/src/metabase/public_sharing/api.cljindex bb8c4feca40..2ad23cdef08 100644--- a/src/metabase/public_sharing/api.clj+++ b/src/metabase/public_sharing/api.clj@@ -92,8 +92,7 @@[{:keys [uuid]} :- [:map[:uuid ms/UUIDString]]](public-sharing.validation/check-public-sharing-enabled)- (u/prog1 (card-with-uuid uuid)- (events/publish-event! :event/card-read {:object-id (:id <>), :user-id api/*current-user-id*, :context :question})))+ (card-with-uuid uuid))(defmulti ^:private transform-qp-result"Transform results to be suitable for a public endpoint"@@ -294,13 +293,12 @@(public-sharing.validation/check-public-sharing-enabled)(api/check-404 (t2/select-one-pk :model/Card :id card-id :archived false))(let [dashboard-id (api/check-404 (t2/select-one-pk :model/Dashboard :public_uuid uuid, :archived false))]- (u/prog1 (process-query-for-dashcard- :dashboard-id dashboard-id- :card-id card-id- :dashcard-id dashcard-id- :export-format :api- :parameters parameters)- (events/publish-event! :event/card-read {:object-id card-id, :user-id api/*current-user-id*, :context :dashboard}))))+ (process-query-for-dashcard+ :dashboard-id dashboard-id+ :card-id card-id+ :dashcard-id dashcard-id+ :export-format :api+ :parameters parameters)))(api.macros/defendpoint :post ["/dashboard/:uuid/dashcard/:dashcard-id/card/:card-id/:export-format":export-format qp.schema/export-formats-regex]@@ -516,14 +514,13 @@(public-sharing.validation/check-public-sharing-enabled)(api/check-404 (t2/select-one-pk :model/Card :id card-id :archived false))(let [dashboard-id (api/check-404 (t2/select-one-pk :model/Dashboard :public_uuid uuid, :archived false))]- (u/prog1 (process-query-for-dashcard- :dashboard-id dashboard-id- :card-id card-id- :dashcard-id dashcard-id- :export-format :api- :parameters parameters- :qp qp.pivot/run-pivot-query)- (events/publish-event! :event/card-read {:object-id card-id, :user-id api/*current-user-id*, :context :dashboard}))))+ (process-query-for-dashcard+ :dashboard-id dashboard-id+ :card-id card-id+ :dashcard-id dashcard-id+ :export-format :api+ :parameters parameters+ :qp qp.pivot/run-pivot-query)))(def ^:private action-execution-throttle"Rate limit at 10 actions per 1000 ms on a per action basis.diff --git a/src/metabase/queries/api/card.clj b/src/metabase/queries/api/card.cljindex 1de5b6156a1..8571500da2c 100644--- a/src/metabase/queries/api/card.clj+++ b/src/metabase/queries/api/card.clj@@ -227,26 +227,16 @@instead, you can specify `?legacy-mbql=true`."[{:keys [id]} :- [:map[:id [:or ms/PositiveInt ms/NanoIdString]]]- {ignore-view? :ignore_view- legacy-mbql? :legacy-mbql- :keys [context]} :- [:map- [:ignore_view {:optional true} [:maybe :boolean]]- [:context {:optional true} [:maybe [:enum :collection]]]- [:legacy-mbql {:optional true, :default false} [:maybe :boolean]]]]+ {legacy-mbql? :legacy-mbql+ :keys []} :- [:map [:legacy-mbql {:optional true, :default false} [:maybe :boolean]]]](let [resolved-id (eid-translation/->id-or-404 :card id)- card (get-card resolved-id)- response (cond-> card- legacy-mbql?- (update :dataset_query (fn [query]- #_{:clj-kondo/ignore [:discouraged-var]}- (cond-> query- (seq query) lib/->legacy-MBQL))))]- (u/prog1 response- (when-not ignore-view?- (events/publish-event! :event/card-read- {:object-id (:id <>)- :user-id api/*current-user-id*- :context (or context :question)})))))+ card (get-card resolved-id)]+ (cond-> card+ legacy-mbql?+ (update :dataset_query (fn [query]+ #_{:clj-kondo/ignore [:discouraged-var]}+ (cond-> query+ (seq query) lib/->legacy-MBQL))))))(defn- check-allowed-to-remove-from-existing-dashboards [card](let [dashboards (or (:in_dashboards card)@@ -820,10 +810,10 @@(let [resolved-card-id (eid-translation/->id-or-404 :card card-id)](qp.card/process-query-for-cardresolved-card-id :api- :parameters parameters+ :parameters parameters:ignore-cache ignore_cache:dashboard-id dashboard_id- :context (if collection_preview :collection :question)+ :context (if collection_preview :collection :question):middleware {:process-viz-settings? false})))(api.macros/defendpoint :post "/:card-id/query/:export-format"diff --git a/src/metabase/query_processor/card.clj b/src/metabase/query_processor/card.cljindex 20e14e08053..cf0bae16aa4 100644--- a/src/metabase/query_processor/card.clj+++ b/src/metabase/query_processor/card.clj@@ -6,6 +6,7 @@[medley.core :as m][metabase.api.common :as api][metabase.cache.core :as cache]+ [metabase.events.core :as events];; legacy usages -- don't use Legacy MBQL utils in QP code going forward, prefer Lib. This will be updated to use;; Lib soon^{:clj-kondo/ignore [:discouraged-namespace]}@@ -284,6 +285,28 @@(m/index-by :id))](mapv #(merge (-> % :id id->card-param) %) parameters)))+(defn- card-read-context+ "The context to use for tracking the view. Return nil if the view should not be tracked"+ [{:keys [context]}]+ (case context+ :public-dashboard :dashboard+ :public-question :question+ :embedded-dashboard :dashboard+ :embedded-question :question+ :csv-download nil+ :public-csv-download nil+ :embedded-csv-download nil+ :json-download nil+ :public-json-download nil+ :embedded-json-download nil+ :xlsx-download nil+ :public-xlsx-download nil+ :embedded-xlsx-download nil+ :dashboard-subscription nil+ :pulse nil+ :map-tiles nil+ context))+(mu/defn process-query-for-card"Run the query for Card with `parameters` and `constraints`. By default, returns results in a`metabase.server.streaming_response.StreamingResponse` (see [[metabase.server.streaming-response]]) that should be@@ -346,6 +369,10 @@(log/tracef "Running query for Card %d:\n%s" card-id(u/pprint-to-str query))(binding [qp.perms/*card-id* card-id]+ (when-let [context (card-read-context info)]+ (events/publish-event! :event/card-read {:object-id card-id+ :user-id (:executed-by info)+ :context context}))(qp.store/with-metadata-provider (:database_id card)(qp.results-metadata/store-previous-result-metadata! card)(runner query info)))))diff --git a/test/metabase/queries/api/card_test.clj b/test/metabase/queries/api/card_test.cljindex a6e2e59f87d..0627414d49e 100644--- a/test/metabase/queries/api/card_test.clj+++ b/test/metabase/queries/api/card_test.clj@@ -298,14 +298,14 @@"The set of Card returned with f=archived should be equal to the set of archived cards")))))(deftest embedding-sdk-info-saves-view-log- (testing "GET /api/card with embedding headers set"+ (testing "GET /api/card/query with embedding headers set"(let [;; any strings will work here (must be shorter than 254 chars), but these are semi-relaistic:client-string (mt/random-name)version-string (str "1." (rand-int 1000) "." (rand-int 1000))](mt/with-temp [:model/Database {database-id :id} {}:model/Card card-1 {:name "Card 1" :database_id database-id}](mt/with-premium-features #{:audit-app}- (mt/user-http-request :crowberto :get 200 (str "card/" (u/the-id card-1))+ (mt/user-http-request :crowberto :post 202 (str "card/" (u/the-id card-1) "/query"){:request-options {:headers {"x-metabase-client" client-string"x-metabase-client-version" version-string}}}))(is (= {:embedding_client client-string, :embedding_version version-string}diff --git a/test/metabase/query_processor/dashboard_test.clj b/test/metabase/query_processor/dashboard_test.cljindex 4ae0bc221d3..615269a4bc9 100644--- a/test/metabase/query_processor/dashboard_test.clj+++ b/test/metabase/query_processor/dashboard_test.clj@@ -116,7 +116,7 @@#"Not found"(run-query-for-dashcard dashboard-id card-id-2 dashcard-id-3))))))-(deftest ^:parallel default-value-precedence-test-field-filters+(deftest default-value-precedence-test-field-filters(testing "If both Dashboard and Card have default values for a Field filter parameter, Card defaults should take precedence\n"(mt/dataset test-data(mt/with-tempdiff --git a/test/metabase/view_log/events/view_log_test.clj b/test/metabase/view_log/events/view_log_test.cljindex 23258e074d0..c48d8d46d94 100644--- a/test/metabase/view_log/events/view_log_test.clj+++ b/test/metabase/view_log/events/view_log_test.clj@@ -219,8 +219,8 @@(mt/with-premium-features #{:audit-app}(testing "Card reads (views) via the API are recorded in the view_log"(mt/with-temp [:model/Card card {:name "My Cool Card" :type :question}]- (testing "GET /api/card/:id"- (mt/user-http-request :crowberto :get 200 (format "card/%s" (u/id card)))+ (testing "POST /api/card/:id/query"+ (mt/user-http-request :crowberto :post 202 (format "card/%s/query" (u/id card)))(is (partial= {:user_id (mt/user->id :crowberto), :model "card", :model_id (u/id card), :context :question}(latest-view (mt/user->id :crowberto) (u/id card)))))))))@@ -251,8 +251,8 @@(testing "Viewing a public card logs the correct view log event."(mt/with-temporary-setting-values [enable-public-sharing true](public-test/with-temp-public-card [card]- (testing "GET /api/public/card/:uuid"- (client/client :get 200 (str "public/card/" (:public_uuid card)))+ (testing "GET /api/public/card/:uuid/query"+ (client/client :get 202 (str "public/card/" (:public_uuid card) "/query"))(is (partial= {:model "card", :model_id (:id card), :has_access true, :context :question}(latest-view nil (:id card))))))))))@@ -261,7 +261,7 @@(testing "Running a query for a card in a public dashboard logs the correct view log event."(mt/with-temporary-setting-values [enable-public-sharing true](public-test/with-temp-public-dashboard-and-card [dash card dashcard]- (testing "GET /api/public/dashboard/:uuid/card/:card-id"+ (testing "GET /api/public/dashboard/:uuid/card/:card-id/query"(client/client :get 202 (public-test/dashcard-url dash card dashcard))(is (partial= {:model "card", :model_id (:id card), :has_access true, :context :dashboard}(latest-view nil (:id card))))))))))@@ -281,8 +281,8 @@(testing "Viewing an embedding logs the correct view log event."(embed-test/with-embedding-enabled-and-new-secret-key!(mt/with-temp [:model/Card card {:enable_embedding true}]- (testing "GET /api/embed/card/:token"- (client/client :get 200 (embed-test/card-url card))+ (testing "GET /api/embed/card/:token/query"+ (client/client :get 202 (str (embed-test/card-url card) "/query"))(is (partial= {:model "card", :model_id (:id card), :has_access true}(latest-view nil (:id card))))))))))