From 0afb47ade01d021daed7ca7d2c902ef3fae274f3 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 4 Apr 2022 17:09:58 +0200 Subject: [PATCH] :sparkles: Update telemetry task for handle user subscriptions --- backend/src/app/main.clj | 10 ++-- backend/src/app/tasks/telemetry.clj | 59 +++++++++++++++++------ backend/test/app/tasks_telemetry_test.clj | 7 ++- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/backend/src/app/main.clj b/backend/src/app/main.clj index 6a88a10c4..455964fdb 100644 --- a/backend/src/app/main.clj +++ b/backend/src/app/main.clj @@ -209,6 +209,9 @@ {:cron #app/cron "0 0 0 * * ?" ;; daily :task :tasks-gc} + {:cron #app/cron "0 30 */3,23 * * ?" + :task :telemetry} + (when (cf/get :fdata-storage-backed) {:cron #app/cron "0 0 * * * ?" ;; hourly :task :file-offload}) @@ -219,12 +222,7 @@ (when (contains? cf/flags :audit-log-gc) {:cron #app/cron "0 0 0 * * ?" ;; daily - :task :audit-log-gc}) - - (when (or (contains? cf/flags :telemetry) - (cf/get :telemetry-enabled)) - {:cron #app/cron "0 30 */3,23 * * ?" - :task :telemetry})]} + :task :audit-log-gc})]} :app.worker/registry {:metrics (ig/ref :app.metrics/metrics) diff --git a/backend/src/app/tasks/telemetry.clj b/backend/src/app/tasks/telemetry.clj index 11c141157..cf45eeeba 100644 --- a/backend/src/app/tasks/telemetry.clj +++ b/backend/src/app/tasks/telemetry.clj @@ -12,7 +12,7 @@ [app.common.data :as d] [app.common.exceptions :as ex] [app.common.spec :as us] - [app.config :as cfg] + [app.config :as cf] [app.db :as db] [app.util.async :refer [thread-sleep]] [app.util.json :as json] @@ -25,6 +25,7 @@ (declare get-stats) (declare send!) +(declare get-subscriptions) (s/def ::http-client fn?) (s/def ::version ::us/string) @@ -38,18 +39,39 @@ (defmethod ig/init-key ::handler [_ {:keys [pool sprops version] :as cfg}] - (fn [{:keys [send?] :or {send? true}}] - ;; Sleep randomly between 0 to 10s - (when send? - (thread-sleep (rand-int 10000))) + (fn [{:keys [send? enabled?] :or {send? true enabled? false}}] + (let [subs (get-subscriptions pool) + enabled? (or enabled? + (contains? cf/flags :telemetry) + (cf/get :telemetry-enabled)) - (let [instance-id (:instance-id sprops) - stats (-> (get-stats pool version) - (assoc :instance-id instance-id))] - (when send? - (send! cfg stats)) + data {:subscriptions subs + :version version + :instance-id (:instance-id sprops)}] + (cond + ;; If we have telemetry enabled, then proceed the normal + ;; operation. + enabled? + (let [data (merge data (get-stats pool))] + (when send? + (thread-sleep (rand-int 10000)) + (send! cfg data)) + data) - stats))) + ;; If we have telemetry disabled, but there are users that are + ;; explicitly checked the newsletter subscription on the + ;; onboarding dialog or the profile section, then proceed to + ;; send a limited telemetry data, that consists in the list of + ;; subscribed emails and the running penpot version. + (seq subs) + (do + (when send? + (thread-sleep (rand-int 10000)) + (send! cfg data)) + data) + + :else + data)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; IMPL @@ -68,6 +90,12 @@ :response-status (:status response) :response-body (:body response))))) +(defn- get-subscriptions + [conn] + (let [sql "select email from profile where props->>'~:newsletter-subscribed' = 'true'"] + (->> (db/exec! conn [sql]) + (mapv :email)))) + (defn- retrieve-num-teams [conn] (-> (db/exec-one! conn ["select count(*) as count from team;"]) :count)) @@ -166,12 +194,11 @@ :user-tz (System/getProperty "user.timezone")})) (defn get-stats - [conn version] - (let [referer (if (cfg/get :telemetry-with-taiga) + [conn] + (let [referer (if (cf/get :telemetry-with-taiga) "taiga" - (cfg/get :telemetry-referer))] - (-> {:version version - :referer referer + (cf/get :telemetry-referer))] + (-> {:referer referer :total-teams (retrieve-num-teams conn) :total-projects (retrieve-num-projects conn) :total-files (retrieve-num-files conn) diff --git a/backend/test/app/tasks_telemetry_test.clj b/backend/test/app/tasks_telemetry_test.clj index 317875554..60012716f 100644 --- a/backend/test/app/tasks_telemetry_test.clj +++ b/backend/test/app/tasks_telemetry_test.clj @@ -21,13 +21,16 @@ (with-mocks [mock {:target 'app.tasks.telemetry/send! :return nil}] (let [task-fn (-> th/*system* :app.worker/registry :telemetry) - prof (th/create-profile* 1 {:is-active true})] + prof (th/create-profile* 1 {:is-active true + :props {:newsletter-subscribed true}})] ;; run the task - (task-fn nil) + (task-fn {:send? true :enabled? true}) (t/is (:called? @mock)) (let [[_ data] (-> @mock :call-args)] + (t/is (contains? data :subscriptions)) + (t/is (= [(:email prof)] (get data :subscriptions))) (t/is (contains? data :total-fonts)) (t/is (contains? data :total-users)) (t/is (contains? data :total-projects))