0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-12 01:58:44 -05:00

Update telemetry task for handle user subscriptions

This commit is contained in:
Andrey Antukh 2022-04-04 17:09:58 +02:00
parent 88292f2f3b
commit 0afb47ade0
3 changed files with 52 additions and 24 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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))