0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-14 19:19:09 -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 {:cron #app/cron "0 0 0 * * ?" ;; daily
:task :tasks-gc} :task :tasks-gc}
{:cron #app/cron "0 30 */3,23 * * ?"
:task :telemetry}
(when (cf/get :fdata-storage-backed) (when (cf/get :fdata-storage-backed)
{:cron #app/cron "0 0 * * * ?" ;; hourly {:cron #app/cron "0 0 * * * ?" ;; hourly
:task :file-offload}) :task :file-offload})
@ -219,12 +222,7 @@
(when (contains? cf/flags :audit-log-gc) (when (contains? cf/flags :audit-log-gc)
{:cron #app/cron "0 0 0 * * ?" ;; daily {:cron #app/cron "0 0 0 * * ?" ;; daily
:task :audit-log-gc}) :task :audit-log-gc})]}
(when (or (contains? cf/flags :telemetry)
(cf/get :telemetry-enabled))
{:cron #app/cron "0 30 */3,23 * * ?"
:task :telemetry})]}
:app.worker/registry :app.worker/registry
{:metrics (ig/ref :app.metrics/metrics) {:metrics (ig/ref :app.metrics/metrics)

View file

@ -12,7 +12,7 @@
[app.common.data :as d] [app.common.data :as d]
[app.common.exceptions :as ex] [app.common.exceptions :as ex]
[app.common.spec :as us] [app.common.spec :as us]
[app.config :as cfg] [app.config :as cf]
[app.db :as db] [app.db :as db]
[app.util.async :refer [thread-sleep]] [app.util.async :refer [thread-sleep]]
[app.util.json :as json] [app.util.json :as json]
@ -25,6 +25,7 @@
(declare get-stats) (declare get-stats)
(declare send!) (declare send!)
(declare get-subscriptions)
(s/def ::http-client fn?) (s/def ::http-client fn?)
(s/def ::version ::us/string) (s/def ::version ::us/string)
@ -38,18 +39,39 @@
(defmethod ig/init-key ::handler (defmethod ig/init-key ::handler
[_ {:keys [pool sprops version] :as cfg}] [_ {:keys [pool sprops version] :as cfg}]
(fn [{:keys [send?] :or {send? true}}] (fn [{:keys [send? enabled?] :or {send? true enabled? false}}]
;; Sleep randomly between 0 to 10s (let [subs (get-subscriptions pool)
(when send? enabled? (or enabled?
(thread-sleep (rand-int 10000))) (contains? cf/flags :telemetry)
(cf/get :telemetry-enabled))
(let [instance-id (:instance-id sprops) data {:subscriptions subs
stats (-> (get-stats pool version) :version version
(assoc :instance-id instance-id))] :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? (when send?
(send! cfg stats)) (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 ;; IMPL
@ -68,6 +90,12 @@
:response-status (:status response) :response-status (:status response)
:response-body (:body 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 (defn- retrieve-num-teams
[conn] [conn]
(-> (db/exec-one! conn ["select count(*) as count from team;"]) :count)) (-> (db/exec-one! conn ["select count(*) as count from team;"]) :count))
@ -166,12 +194,11 @@
:user-tz (System/getProperty "user.timezone")})) :user-tz (System/getProperty "user.timezone")}))
(defn get-stats (defn get-stats
[conn version] [conn]
(let [referer (if (cfg/get :telemetry-with-taiga) (let [referer (if (cf/get :telemetry-with-taiga)
"taiga" "taiga"
(cfg/get :telemetry-referer))] (cf/get :telemetry-referer))]
(-> {:version version (-> {:referer referer
:referer referer
:total-teams (retrieve-num-teams conn) :total-teams (retrieve-num-teams conn)
:total-projects (retrieve-num-projects conn) :total-projects (retrieve-num-projects conn)
:total-files (retrieve-num-files conn) :total-files (retrieve-num-files conn)

View file

@ -21,13 +21,16 @@
(with-mocks [mock {:target 'app.tasks.telemetry/send! (with-mocks [mock {:target 'app.tasks.telemetry/send!
:return nil}] :return nil}]
(let [task-fn (-> th/*system* :app.worker/registry :telemetry) (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 ;; run the task
(task-fn nil) (task-fn {:send? true :enabled? true})
(t/is (:called? @mock)) (t/is (:called? @mock))
(let [[_ data] (-> @mock :call-args)] (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-fonts))
(t/is (contains? data :total-users)) (t/is (contains? data :total-users))
(t/is (contains? data :total-projects)) (t/is (contains? data :total-projects))