From 23338803f9f73deca148919edf50a4e4a9c1b47e Mon Sep 17 00:00:00 2001
From: bryan <bryan.maass@gmail.com>
Date: Tue, 22 Oct 2024 12:50:23 -0600
Subject: [PATCH] fix grouped metrics + add some safety to it (#48995)

---
 src/metabase/analytics/stats.clj | 53 ++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/src/metabase/analytics/stats.clj b/src/metabase/analytics/stats.clj
index 9489f071899..b070d7fb2ca 100644
--- a/src/metabase/analytics/stats.clj
+++ b/src/metabase/analytics/stats.clj
@@ -626,22 +626,36 @@
                                 (update-vals count)))
      :eid-translations-24h (get-translation-count)}))
 
-(defn- snowplow-grouped-metrics [{:keys [eid-translations-24h
-                                         query-executions
-                                         query-executions-24h]
-                                  :as _snowplow-grouped-metric-info}]
-  [{:name :query_executions_by_source
-    :values (mapv (fn [qe-group]
-                    {:group qe-group :value (get query-executions qe-group)})
-                  ["interactive_embed" "internal" "public_link" "sdk_embed" "static_embed"])
-    :tags ["embedding"]}
-   {:name :query_executions_by_source_24h
-    :values (mapv (fn [qe-group] {:group qe-group :value (get query-executions-24h qe-group)})
-                  ["interactive_embed" "internal" "public_link" "sdk_embed" "static_embed"])
-    :tags ["embedding"]}
-   {:name :entity_id_translations_last_24h
-    :values eid-translations-24h
-    :tags ["embedding"]}])
+(defn- deep-string-keywords
+  "Snowplow data will not work if you pass in keywords, but this will let use use keywords all over."
+  [data]
+  (walk/postwalk
+   (fn [x] (if (keyword? x) (-> x u/->snake_case_en name) x))
+   data))
+
+(mu/defn- snowplow-grouped-metrics
+  :- [:sequential
+      [:map
+       ["name" :string]
+       ["values" [:sequential [:map ["group" :string] ["value" :int]]]]
+       ["tags" [:sequential :string]]]]
+  [{:keys [eid-translations-24h
+           query-executions
+           query-executions-24h]
+    :as _snowplow-grouped-metric-info}]
+  (deep-string-keywords
+   [{:name :query_executions_by_source
+     :values (mapv (fn [qe-group]
+                     {:group qe-group :value (get query-executions qe-group)})
+                   ["interactive_embed" "internal" "public_link" "sdk_embed" "static_embed"])
+     :tags ["embedding"]}
+    {:name :query_executions_by_source_24h
+     :values (mapv (fn [qe-group] {:group qe-group :value (get query-executions-24h qe-group)})
+                   ["interactive_embed" "internal" "public_link" "sdk_embed" "static_embed"])
+     :tags ["embedding"]}
+    {:name :entity_id_translations_last_24h
+     :values (mapv (fn [[k v]] {:group k :value v}) eid-translations-24h)
+     :tags ["embedding"]}]))
 
 (defn- ->snowplow-metric-info
   "Collects Snowplow metrics data that is not in the legacy stats format. Also clears entity id translation count."
@@ -888,13 +902,6 @@
                 (update-in [:stats :cache] dissoc :num_queries_cached_unbinned))
      :snowplow-stats (snowplow-anonymous-usage-stats stats)}))
 
-(defn- deep-string-keywords
-  "Snowplow data will not work if you pass in keywords, but this will let use use keywords all over."
-  [data]
-  (walk/postwalk
-   (fn [x] (if (keyword? x) (-> x u/->snake_case_en name) x))
-   data))
-
 (defn- stats-post-cleanup []
   (clear-translation-count!))
 
-- 
GitLab