Skip to content

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

Add library stats to anonymous daily stats ping (#68071)

Gabriel Morenobb66df77e19a
diff --git a/src/metabase/analytics/stats.clj b/src/metabase/analytics/stats.clj
index ac7dbdf7b7d..fb4482cf1f4 100644
--- a/src/metabase/analytics/stats.clj
+++ b/src/metabase/analytics/stats.clj
@@ -180,6 +180,31 @@
{:total 1
:archived (true? (:archived document))}))})
+(defn- library-stats
+ "Get metrics for Library usage."
+ []
+ (letfn [(collection-and-descendant-ids [type]
+ ;; Get collection and build location prefix for descendants (location like "<parent-location><id>/%")
+ (when-let [{:keys [id location]} (t2/select-one [:model/Collection :id :location] :type type)]
+ (let [children-location (str location id "/")
+ descendant-ids (t2/select-pks-set :model/Collection :location [:like (str children-location "%")])]
+ (conj (or descendant-ids #{}) id))))]
+ (let [library-data-ids (collection-and-descendant-ids "library-data")
+ library-metrics-ids (collection-and-descendant-ids "library-metrics")]
+ {:library_data (if (seq library-data-ids)
+ (t2/count :model/Table
+ {:where [:and
+ [:= :is_published true]
+ [:in :collection_id library-data-ids]]})
+ 0)
+ :library_metrics (if (seq library-metrics-ids)
+ (t2/count :model/Card
+ {:where [:and
+ [:= :type "metric"]
+ [:= :archived false]
+ [:in :collection_id library-metrics-ids]]})
+ 0)})))
+
(defn- group-metrics
"Get metrics based on groups:
TODO characterize by # w/ sql access, # of users, no self-serve data access"
@@ -497,7 +522,8 @@
:system (system-metrics)
:table (table-metrics)
:user (user-metrics)
- :document (document-metrics)}}))
+ :document (document-metrics)
+ :library (library-stats)}}))
(defn- ^:deprecated send-stats-deprecated!
"Send stats to Metabase tracking server."
@@ -711,6 +737,8 @@
[:embedded_questions (get-in stats [:stats :question :embedded :total] 0) #{"questions" "embedding"}]
[:entity_id_translations_last_24h (:entity_id_translations_last_24h metric-info 0) #{"embedding"}]
[:first_time_only_alerts (get-in stats [:stats :alert :first_time_only] 0) #{"alerts"}]
+ [:library_data (get-in stats [:stats :library :library_data] 0) #{"library"}]
+ [:library_metrics (get-in stats [:stats :library :library_metrics] 0) #{"library"}]
[:metabase_fields (get-in stats [:stats :field :fields] 0) #{"fields"}]
[:metrics (get-in stats [:stats :metric :metrics] 0) #{"metrics"}]
[:models (:models metric-info 0) #{}]
diff --git a/test/metabase/analytics/stats_test.clj b/test/metabase/analytics/stats_test.clj
index ed8c52377a4..11ddeaba5a1 100644
--- a/test/metabase/analytics/stats_test.clj
+++ b/test/metabase/analytics/stats_test.clj
@@ -640,6 +640,72 @@
:archived 1}}
(#'stats/document-metrics)))))))
+(deftest library-stats-test
+ (mt/with-empty-h2-app-db!
+ (testing "with no library collections"
+ (is (= {:library_data 0
+ :library_metrics 0}
+ (#'stats/library-stats))))
+
+ (testing "with library collections and data"
+ (mt/with-temp [:model/User {user-id :id} {:email "[email protected]"
+ :first_name "Test"
+ :last_name "User"}
+ :model/Database {db-id :id} {:name "Test DB"
+ :engine :h2}
+ :model/Collection {library-id :id} {:name "Library"
+ :type "library"
+ :location "/"}
+ :model/Collection {data-coll-id :id} {:name "Data"
+ :type "library-data"
+ :location (format "/%d/" library-id)}
+ :model/Collection {metrics-coll-id :id} {:name "Metrics"
+ :type "library-metrics"
+ :location (format "/%d/" library-id)}
+ :model/Collection {sub-coll-id :id} {:name "Sub folder"
+ :location (format "/%d/%d/" library-id metrics-coll-id)}
+ ;; Published table in library-data collection
+ :model/Table _ {:db_id db-id
+ :name "published_table"
+ :active true
+ :is_published true
+ :collection_id data-coll-id}
+ ;; Unpublished table (should not count)
+ :model/Table _ {:db_id db-id
+ :name "unpublished_table"
+ :active true
+ :is_published false}
+ ;; Metric in library-metrics collection
+ :model/Card _ {:name "Metric 1"
+ :type :metric
+ :collection_id metrics-coll-id
+ :creator_id user-id
+ :database_id db-id
+ :dataset_query {}
+ :display :table
+ :visualization_settings {}}
+ ;; Metric in sub-collection of library-metrics (should count)
+ :model/Card _ {:name "Metric 2"
+ :type :metric
+ :collection_id sub-coll-id
+ :creator_id user-id
+ :database_id db-id
+ :dataset_query {}
+ :display :table
+ :visualization_settings {}}
+ ;; Metric outside library (should not count)
+ :model/Card _ {:name "Metric 3"
+ :type :metric
+ :collection_id nil
+ :creator_id user-id
+ :database_id db-id
+ :dataset_query {}
+ :display :table
+ :visualization_settings {}}]
+ (is (= {:library_data 1
+ :library_metrics 2}
+ (#'stats/library-stats)))))))
+
(deftest transform-metrics-test
(testing "ee-transform-metrics should return zeros for OSS"
(mt/with-empty-h2-app-db!