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
Record model views in the view log (#77091)
Ngoc Khuatfe4a72d76fe2
diff --git a/src/metabase/view_log/events/view_log.clj b/src/metabase/view_log/events/view_log.cljindex 471232d6b5e..c7d53fcc933 100644--- a/src/metabase/view_log/events/view_log.clj+++ b/src/metabase/view_log/events/view_log.clj@@ -139,6 +139,28 @@(catch Throwable e(log/warnf e "Failed to process view event. %s" topic)))))+(derive ::card-query-model-view :metabase/event)+(derive :event/card-query ::card-query-model-view)++(m/defmethod events/publish-event! ::card-query-model-view+ "Log a view for models, which are opened as ad-hoc `card__id` queries that fire :event/card-query but never+ :event/card-read. The :ad-hoc + model guards keep regular cards (logged via card-read) and downloads out."+ [topic {:keys [card-id user-id context]}]+ (span/with-span!+ {:name "view-log-model-query"+ :topic topic+ :user-id user-id}+ (try+ (when (and (= context :ad-hoc)+ (= :model (t2/select-one-fn :type :model/Card :id card-id)))+ (increment-view-counts! :model/Card card-id)+ (record-views! (generate-view :model :model/Card+ :object-id card-id+ :user-id user-id+ :context :question)))+ (catch Throwable e+ (log/warnf e "Failed to process card query view event. %s" topic)))))+(derive ::dashboard-queried :metabase/event)(derive :event/dashboard-queried ::dashboard-queried)diff --git a/test/metabase/view_log/events/view_log_test.clj b/test/metabase/view_log/events/view_log_test.cljindex 3d38461c21f..7bdd2818bf5 100644--- a/test/metabase/view_log/events/view_log_test.clj+++ b/test/metabase/view_log/events/view_log_test.clj@@ -71,6 +71,23 @@(is (= nil (latest-view (u/id user) (u/id card)))"view log entries should not be made in OSS")))))+(deftest card-query-model-read-ee-test+ (mt/with-premium-features #{:audit-app}+ (mt/with-temp [:model/User user {}+ :model/Card model {:type :model :creator_id (u/id user)}+ :model/Card question {:type :question :creator_id (u/id user)}]+ (testing "An ad-hoc card-query on a model is recorded as a card view (GDGT-2693)"+ (events/publish-event! :event/card-query {:card-id (u/id model) :user-id (u/id user) :context :ad-hoc})+ (is (partial= {:user_id (u/id user) :model "card" :model_id (u/id model) :context :question :has_access true}+ (latest-view (u/id user) (u/id model)))))+ (testing "An ad-hoc card-query on a non-model card is not recorded (card-read handles questions)"+ (events/publish-event! :event/card-query {:card-id (u/id question) :user-id (u/id user) :context :ad-hoc})+ (is (nil? (latest-view (u/id user) (u/id question)))))+ (testing "A non-ad-hoc card-query on a model is not recorded (downloads and card-read paths are excluded)"+ (events/publish-event! :event/card-query {:card-id (u/id model) :user-id (u/id user) :context :csv-download})+ (is (= 1 (t2/count :model/ViewLog :user_id (u/id user) :model_id (u/id model)))+ "only the ad-hoc view is recorded")))))+(deftest collection-read-ee-test(mt/with-premium-features #{:audit-app}(mt/with-temp [:model/Collection coll {}]@@ -167,6 +184,19 @@(events/publish-event! :event/card-read {:object-id (:id card) :user-id (u/the-id user) :context :question})(is (= 2 (t2/select-one-fn :view_count :model/Card (:id card)))))))))+(deftest card-query-model-view-count-test+ (mt/test-helpers-set-global-values!+ (mt/with-temporary-setting-values [synchronous-batch-updates true]+ (mt/with-temp [:model/User user {}+ :model/Card model {:type :model :creator_id (u/id user)}]+ (testing "An ad-hoc card-query on a model increments its view_count (GDGT-2693)"+ (is (= 0 (:view_count model))+ "view_count should be 0 before the event is published")+ (events/publish-event! :event/card-query {:card-id (u/id model) :user-id (u/id user) :context :ad-hoc})+ (is (= 1 (t2/select-one-fn :view_count :model/Card (u/id model))))+ (events/publish-event! :event/card-query {:card-id (u/id model) :user-id (u/id user) :context :ad-hoc})+ (is (= 2 (t2/select-one-fn :view_count :model/Card (u/id model)))))))))+(deftest dashboard-read-view-count-test(mt/test-helpers-set-global-values!(mt/with-temporary-setting-values [synchronous-batch-updates true]@@ -246,6 +276,20 @@(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)))))))))+(deftest model-dataset-query-view-log-test+ (mt/with-premium-features #{:audit-app}+ (testing "Opening a model runs an ad-hoc card__<id> query that is recorded in the view_log (GDGT-2693)"+ (mt/with-temp [:model/Card model {:name "My Model"+ :type :model+ :dataset_query (mt/mbql-query venues {:limit 1})}]+ (testing "POST /api/dataset with a card__<model-id> source records a card view"+ (mt/user-http-request :crowberto :post 202 "dataset"+ {:database (mt/id)+ :type :query+ :query {:source-table (str "card__" (u/id model))}})+ (is (partial= {:user_id (mt/user->id :crowberto) :model "card" :model_id (u/id model) :context :question}+ (latest-view (mt/user->id :crowberto) (u/id model)))))))))+(deftest get-dashboard-view-log-test(mt/with-premium-features #{:audit-app}(testing "Dashboard reads (views) via the API are recorded in the view_log"