0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-16 03:58:20 -05:00

🐛 Fix minor issues on event instumentation module

This commit is contained in:
Andrey Antukh 2022-03-14 13:56:32 +01:00
parent e6d6b60b63
commit 7e8d8eef5a
2 changed files with 86 additions and 70 deletions

View file

@ -53,7 +53,7 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ stream] (watch [_ _ stream]
(rx/merge (rx/merge
(rx/of (ptk/event ::ev/initialize) (rx/of (ev/initialize)
(du/initialize-profile)) (du/initialize-profile))
(->> stream (->> stream
(rx/filter du/profile-fetched?) (rx/filter du/profile-fetched?)
@ -79,6 +79,7 @@
[] []
(mf/unmount (dom/get-element "app")) (mf/unmount (dom/get-element "app"))
(mf/unmount (dom/get-element "modal")) (mf/unmount (dom/get-element "modal"))
(st/emit! (ev/initialize))
(init-ui)) (init-ui))
(defn ^:dev/after-load after-load (defn ^:dev/after-load after-load
@ -91,4 +92,3 @@
(fn [_ _ old-value current-value] (fn [_ _ old-value current-value]
(when (not= old-value current-value) (when (not= old-value current-value)
(reinit)))) (reinit))))

View file

@ -8,6 +8,7 @@
(:require (:require
["ua-parser-js" :as UAParser] ["ua-parser-js" :as UAParser]
[app.common.data :as d] [app.common.data :as d]
[app.common.logging :as l]
[app.config :as cf] [app.config :as cf]
[app.main.repo :as rp] [app.main.repo :as rp]
[app.util.globals :as g] [app.util.globals :as g]
@ -20,6 +21,8 @@
[lambdaisland.uri :as u] [lambdaisland.uri :as u]
[potok.core :as ptk])) [potok.core :as ptk]))
(l/set-level! :info)
;; Defines the maximum buffer size, after events start discarding. ;; Defines the maximum buffer size, after events start discarding.
(def max-buffer-size 1024) (def max-buffer-size 1024)
@ -94,11 +97,13 @@
(derive :app.main.data.fonts/add-font ::generic-action) (derive :app.main.data.fonts/add-font ::generic-action)
(derive :app.main.data.fonts/delete-font ::generic-action) (derive :app.main.data.fonts/delete-font ::generic-action)
(derive :app.main.data.fonts/delete-font-variant ::generic-action) (derive :app.main.data.fonts/delete-font-variant ::generic-action)
(derive :app.main.data.modal/show-modal ::generic-action)
(derive :app.main.data.users/logout ::generic-action) (derive :app.main.data.users/logout ::generic-action)
(derive :app.main.data.users/request-email-change ::generic-action) (derive :app.main.data.users/request-email-change ::generic-action)
(derive :app.main.data.users/update-password ::generic-action) (derive :app.main.data.users/update-password ::generic-action)
(derive :app.main.data.users/update-photo ::generic-action) (derive :app.main.data.users/update-photo ::generic-action)
(derive :app.main.data.workspace.comments/open-comment-thread ::generic-action) (derive :app.main.data.workspace.comments/open-comment-thread ::generic-action)
(derive :app.main.data.workspace.guides/update-guides ::generic-action)
(derive :app.main.data.workspace.libraries/add-color ::generic-action) (derive :app.main.data.workspace.libraries/add-color ::generic-action)
(derive :app.main.data.workspace.libraries/add-media ::generic-action) (derive :app.main.data.workspace.libraries/add-media ::generic-action)
(derive :app.main.data.workspace.libraries/add-typography ::generic-action) (derive :app.main.data.workspace.libraries/add-typography ::generic-action)
@ -111,8 +116,6 @@
(derive :app.main.data.workspace/create-page ::generic-action) (derive :app.main.data.workspace/create-page ::generic-action)
(derive :app.main.data.workspace/set-workspace-layout ::generic-action) (derive :app.main.data.workspace/set-workspace-layout ::generic-action)
(derive :app.main.data.workspace/toggle-layout-flag ::generic-action) (derive :app.main.data.workspace/toggle-layout-flag ::generic-action)
(derive :app.main.data.modal/show-modal ::generic-action)
(derive :app.main.data.workspace.guides/update-guides ::generic-action)
(defmulti process-event ptk/type) (defmulti process-event ptk/type)
(defmethod process-event :default [_] nil) (defmethod process-event :default [_] nil)
@ -133,17 +136,20 @@
(dissoc ::context) (dissoc ::context)
(cond-> origin (assoc :origin origin)))})))) (cond-> origin (assoc :origin origin)))}))))
(defn- filter-props (defn- normalize-props
"Removes complex data types from props." "Removes complex data types from props."
[data] [data]
(into {} (into {}
(comp
(remove (fn [[_ v]] (nil? v)))
(map (fn [[k v :as kv]] (map (fn [[k v :as kv]]
(cond (cond
(map? v) [k :placeholder/map] (map? v) [k :placeholder/map]
(vector? v) [k :placeholder/vec] (vector? v) [k :placeholder/vec]
(set? v) [k :placeholder/set] (set? v) [k :placeholder/set]
(coll? v) [k :placeholder/coll] (coll? v) [k :placeholder/coll]
:else kv))) (fn? v) [k :placeholder/fn]
:else kv))))
data)) data))
(defmethod process-event ::generic-action (defmethod process-event ::generic-action
@ -156,9 +162,8 @@
{:type "action" {:type "action"
:name (or (::name mdata) (name type)) :name (or (::name mdata) (name type))
:props (-> (merge (d/without-nils data) :props (-> (merge data (::props mdata))
(d/without-nils (::props mdata))) (normalize-props))
(filter-props))
:context (d/without-nils :context (d/without-nils
{:event-origin (::origin mdata) {:event-origin (::origin mdata)
:event-namespace (namespace type) :event-namespace (namespace type)
@ -174,8 +179,7 @@
:project-id (get-in match [:path-params :project-id])}] :project-id (get-in match [:path-params :project-id])}]
{:name "navigate" {:name "navigate"
:type "action" :type "action"
:timestamp (dt/now) :props (normalize-props props)}))
:props (d/without-nils props)}))
(defmethod process-event :app.main.data.users/logged-in (defmethod process-event :app.main.data.users/logged-in
[event] [event]
@ -191,7 +195,7 @@
{:name "signin" {:name "signin"
:type "identify" :type "identify"
:profile-id (:id data) :profile-id (:id data)
:props (d/without-nils props)})) :props (normalize-props props)}))
;; --- MAIN LOOP ;; --- MAIN LOOP
@ -218,16 +222,20 @@
(defn initialize (defn initialize
[] []
(when (contains? @cf/flags :audit-log)
(ptk/reify ::initialize (ptk/reify ::initialize
ptk/EffectEvent ptk/EffectEvent
(effect [_ _ stream] (effect [_ _ stream]
(let [session (atom nil) (let [session (atom nil)
stoper (rx/filter (ptk/type? ::initialize) stream)
buffer (atom #queue []) buffer (atom #queue [])
profile (->> (rx/from-atom storage {:emit-current-value? true}) profile (->> (rx/from-atom storage {:emit-current-value? true})
(rx/map :profile) (rx/map :profile)
(rx/map :id) (rx/map :id)
(rx/dedupe))] (rx/dedupe))]
(l/debug :hint "event instrumentation initialized")
(->> (rx/merge (->> (rx/merge
(->> (rx/from-atom buffer) (->> (rx/from-atom buffer)
(rx/filter #(pos? (count %))) (rx/filter #(pos? (count %)))
@ -241,10 +249,16 @@
(rx/mapcat (fn [[chunk profile-id]] (rx/mapcat (fn [[chunk profile-id]]
(let [events (filterv #(= profile-id (:profile-id %)) chunk)] (let [events (filterv #(= profile-id (:profile-id %)) chunk)]
(->> (persist-events events) (->> (persist-events events)
(rx/tap (fn [_]
(l/debug :hint "events chunk persisted" :total (count chunk))))
(rx/map (constantly chunk)))))) (rx/map (constantly chunk))))))
(rx/take-until stoper)
(rx/subs (fn [chunk] (rx/subs (fn [chunk]
(swap! buffer remove-from-buffer (count chunk))))) (swap! buffer remove-from-buffer (count chunk)))
(fn [cause]
(l/error :hint "unexpected error on audit persistence" :cause cause))
(fn []
(l/debug :hint "audit persistence terminated"))))
(->> stream (->> stream
(rx/with-latest-from profile) (rx/with-latest-from profile)
@ -265,13 +279,15 @@
(assoc :context context))))) (assoc :context context)))))
(rx/tap (fn [event] (rx/tap (fn [event]
(l/debug :hint "event enqueued")
(swap! buffer append-to-buffer event))) (swap! buffer append-to-buffer event)))
(rx/switch-map #(rx/timer (inst-ms session-timeout))) (rx/switch-map #(rx/timer (inst-ms session-timeout)))
(rx/subs #(reset! session nil))))))) (rx/take-until stoper)
(rx/subs (fn [_]
(defmethod ptk/resolve ::initialize (l/debug :hint "session reinitialized")
[_ params] (reset! session nil))
(if (contains? @cf/flags :audit-log) (fn [cause]
(initialize) (l/error :hint "error on event batching stream" :cause cause))
(ptk/data-event ::initialize params))) (fn []
(l/debug :hitn "events batching stream terminated")))))))))