Skip to content

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

Fix metrics stats in anonymous tracking (#63507)

Alexander Polyankin8c1582e60150
diff --git a/src/metabase/analytics/stats.clj b/src/metabase/analytics/stats.clj
index 428356f8e6a..3bb2e4f34a8 100644
--- a/src/metabase/analytics/stats.clj
+++ b/src/metabase/analytics/stats.clj
@@ -350,6 +350,11 @@
[]
{:segments (t2/count :model/Segment)})
+(defn- metric-metrics
+ "Get metrics based on Metrics."
+ []
+ {:metrics (t2/count :model/Card :type :metric :archived false)})
+
;;; Execution Metrics
(defn- execution-metrics-sql []
@@ -477,6 +482,7 @@
:execution (execution-metrics)
:field (field-metrics)
:group (group-metrics)
+ :metric (metric-metrics)
:pulse (pulse-metrics)
:alert (alert-metrics)
:question (question-metrics)
diff --git a/test/metabase/analytics/stats_test.clj b/test/metabase/analytics/stats_test.clj
index 039b9a322ed..048f6f4f377 100644
--- a/test/metabase/analytics/stats_test.clj
+++ b/test/metabase/analytics/stats_test.clj
@@ -179,6 +179,20 @@
(into #{} (map #(contains? system-stats %) [:java_version :java_runtime_name :max_memory]))))
"Spot checking a few system stats to ensure conversion from property names and presence in the anonymous-usage-stats"))
+(deftest metrics-anonymous-usage-stats-test
+ (testing "should report the metric count"
+ (mt/with-temp [:model/Card _ {:type :metric}
+ :model/Card _ {:type :metric :archived true}]
+ (is (=?
+ {:stats {:metric {:metrics 1}}}
+ (legacy-anonymous-usage-stats)))))
+ (testing "should correctly check for the card type"
+ (mt/with-temp [:model/Card _ {:type :question}
+ :model/Card _ {:type :model}]
+ (is (=?
+ {:stats {:metric {:metrics 0}}}
+ (legacy-anonymous-usage-stats))))))
+
(defn- bin-large-number
"Return large bin number. Assumes positive inputs."
[x]