From 2abd430e029a8c6c16adde7b7ec8cca7fbf3a961 Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Wed, 10 Mar 2021 13:11:02 +0000 Subject: [PATCH] Anon metrics transfer Saving my initial noodling Saving my initial noodling Filtering working much better, data collection working better Added jsonrpc call params Added transformations ns and funcs whitelisting the new rpc method Addressed PR feedback Fix lint Add reasons to why txf ns exists Transform event instead of the contexts Signed-off-by: Shivek Khurana --- src/status_im/anon_metrics/interceptors.cljs | 27 ++++++++++++++++++++ src/status_im/anon_metrics/transformers.cljs | 17 ++++++++++++ src/status_im/ethereum/json_rpc.cljs | 3 ++- src/status_im/navigation.cljs | 9 ++++--- 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 src/status_im/anon_metrics/interceptors.cljs create mode 100644 src/status_im/anon_metrics/transformers.cljs diff --git a/src/status_im/anon_metrics/interceptors.cljs b/src/status_im/anon_metrics/interceptors.cljs new file mode 100644 index 0000000000..20d13566a6 --- /dev/null +++ b/src/status_im/anon_metrics/interceptors.cljs @@ -0,0 +1,27 @@ +(ns status-im.anon-metrics.interceptors + (:require [status-im.ethereum.json-rpc :as json-rpc] + [taoensso.timbre :as log] + [re-frame.interceptor :refer [->interceptor]] + [status-im.utils.platform :as platform] + [status-im.utils.build :as build] + [status-im.anon-metrics.transformers :as txf])) + +(defn transform-and-log [context] + (log/info :catch-event-fn (get-in context [:coeffects :event])) + (when-let [transformed-payload (txf/transform context)] + (json-rpc/call {:method "appmetrics_saveAppMetrics" + :params [[{:event (-> context :coeffects :event first) + :value transformed-payload + :app_version build/version + :os platform/os}]] + :on-failure log/error}))) + +(defn catch-events-before [context] + (log/info "catch-events/interceptor fired") + (transform-and-log context) + context) + +(def catch-events + (->interceptor + :id :catch-events + :before catch-events-before)) diff --git a/src/status_im/anon_metrics/transformers.cljs b/src/status_im/anon_metrics/transformers.cljs new file mode 100644 index 0000000000..b68c3774bc --- /dev/null +++ b/src/status_im/anon_metrics/transformers.cljs @@ -0,0 +1,17 @@ +(ns status-im.anon-metrics.transformers + "`status-go` defines the shape of expected events so that we don't overcollect data (by mistake or intentionally). + `transformers` ns transform the event payload to match the expected shape on `status-go` side.") + +(defn navigate-to-txf [event] + {:view-id (second event) + :params (-> event + (nth 2) + (select-keys [:screen]))}) + +(def transformations + {:navigate-to navigate-to-txf}) + +(defn transform [ctx] + (let [event (-> ctx :coeffects :event)] + (when-let [txf (-> event first transformations)] + (txf event)))) diff --git a/src/status_im/ethereum/json_rpc.cljs b/src/status_im/ethereum/json_rpc.cljs index 2b04db899e..b810c0b503 100644 --- a/src/status_im/ethereum/json_rpc.cljs +++ b/src/status_im/ethereum/json_rpc.cljs @@ -178,7 +178,8 @@ "mailservers_addChatRequestRange" {} "mailservers_addChatRequestRanges" {} "mailservers_getChatRequestRanges" {} - "mailservers_deleteChatRequestRange" {}}) + "mailservers_deleteChatRequestRange" {} + "appmetrics_saveAppMetrics" {}}) (defn on-error-retry [call-method {:keys [method number-of-retries delay on-error] :as arg}] diff --git a/src/status_im/navigation.cljs b/src/status_im/navigation.cljs index 5393842304..b872bdab30 100644 --- a/src/status_im/navigation.cljs +++ b/src/status_im/navigation.cljs @@ -2,7 +2,8 @@ (:require [re-frame.core :as re-frame] [status-im.ui.screens.routing.core :as navigation] [taoensso.timbre :as log] - [status-im.utils.fx :as fx])) + [status-im.utils.fx :as fx] + [status-im.anon-metrics.interceptors :as anon-metrics])) (re-frame/reg-fx ::navigate-to @@ -44,7 +45,8 @@ ::navigate-to [go-to-view-id screen-params]}) (fx/defn navigate-to - {:events [:navigate-to]} + {:events [:navigate-to] + :interceptors [anon-metrics/catch-events]} [cofx go-to-view-id screen-params] (navigate-to-cofx cofx go-to-view-id screen-params)) @@ -59,7 +61,8 @@ {::navigate-reset config}) (fx/defn navigate-replace - {:events [:navigate-replace]} + {:events [:navigate-replace] + :interceptors [anon-metrics/catch-events]} [{:keys [db]} go-to-view-id screen-params] (let [db (cond-> (assoc db :view-id go-to-view-id) (seq screen-params)