Skip to content

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

Fix :view_count not returned for relevant entities in the dependencies graph (#68370)

Alexander Polyankin6165e707188d
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/api.clj b/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
index c2bb86dfb66..87055ee5685 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
@@ -282,15 +282,15 @@
;; (See CardDependencyNodeData, DashboardDependencyNodeData, etc.)
;;
;; Note: Some fields (like :creator, :collection) are added via t2/hydrate,
-;; and others (like :last-edit-info, :view_count) are computed/added separately.
+;; and others (like :last-edit-info) are computed/added separately.
;; This map only lists the base database columns to SELECT.
(def ^:private entity-select-fields
{:card [:id :name :description :type :display :database_id :query_type :collection_id :dashboard_id :document_id :result_metadata
- :created_at :creator_id
+ :created_at :creator_id :view_count
;; :card_schema always has to be selected
:card_schema]
- :dashboard [:id :name :description :created_at :creator_id :collection_id]
- :document [:id :name :created_at :creator_id :collection_id]
+ :dashboard [:id :name :description :created_at :creator_id :collection_id :view_count]
+ :document [:id :name :created_at :creator_id :collection_id :view_count]
:table [:id :name :description :display_name :db_id :schema]
:transform [:id :name :description :creator_id
;; :source has to be selected otherwise the BE won't know what DB it belongs to
diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
index aa3974c4a31..c1fccf973b3 100644
--- a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
@@ -989,7 +989,7 @@
(is (= (:id card) (:id (first response))))))))))))
;; TODO (AlexP 01/15/26) -- fix and unskip this test
-#_(deftest graph-archived-measure-in-chain-test
+#_(deftest ^:sequential graph-archived-measure-in-chain-test
(testing "GET /api/ee/dependencies/graph when a measure in the chain is archived"
(mt/with-premium-features #{:dependencies}
(mt/with-model-cleanup [:model/Measure]
@@ -1061,6 +1061,41 @@
(is (contains? node-ids measure-c-id) "measure C should appear")
(is (contains? node-ids products-id) "products table should appear"))))))))
+(deftest ^:sequential graph-view-count-test
+ (testing "GET /api/ee/dependencies/graph should return :view_count for :card"
+ (mt/with-premium-features #{:dependencies}
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/Card {card-id :id} {}]
+ (events/publish-event! :event/card-read {:object-id card-id :user-id user-id :context :question})
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph"
+ :id card-id
+ :type "card")]
+ (is (=? {:nodes [{:id card-id
+ :data {:view_count 1}}]}
+ response))))))
+ (testing "GET /api/ee/dependencies/graph should return :view_count for :dashboard"
+ (mt/with-premium-features #{:dependencies}
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/Dashboard {dashboard-id :id} {}]
+ (events/publish-event! :event/dashboard-read {:object-id dashboard-id :user-id user-id})
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph"
+ :id dashboard-id
+ :type "dashboard")]
+ (is (=? {:nodes [{:id dashboard-id
+ :data {:view_count 1}}]}
+ response))))))
+ (testing "GET /api/ee/dependencies/graph should return :view_count for :document"
+ (mt/with-premium-features #{:dependencies}
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/Document {document-id :id} {}]
+ (events/publish-event! :event/document-read {:object-id document-id :user-id user-id})
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph"
+ :id document-id
+ :type "document")]
+ (is (=? {:nodes [{:id document-id
+ :data {:view_count 1}}]}
+ response)))))))
+
(deftest ^:sequential unreferenced-questions-test
(testing "GET /api/ee/dependencies/unreferenced - only unreferenced questions are returned"
(mt/with-premium-features #{:dependencies}