Optimize how peer stats are processed

- Debounce peer stats signals by 1s because they may happen 30 times during
login and the payload conversion using js->clj is not cheap.

- Change the order of case macro checks: put more frequent signals at the top.

- Better format code
This commit is contained in:
Icaro Motta 2024-05-27 08:29:20 -03:00 committed by GitHub
parent 366fb7b174
commit 00a9555633
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 74 additions and 55 deletions

View File

@ -3,12 +3,14 @@
[legacy.status-im.chat.models.message :as models.message] [legacy.status-im.chat.models.message :as models.message]
[legacy.status-im.mailserver.core :as mailserver] [legacy.status-im.mailserver.core :as mailserver]
[legacy.status-im.visibility-status-updates.core :as visibility-status-updates] [legacy.status-im.visibility-status-updates.core :as visibility-status-updates]
[oops.core :as oops]
[status-im.common.pairing.events :as pairing] [status-im.common.pairing.events :as pairing]
[status-im.contexts.chat.messenger.messages.link-preview.events :as link-preview] [status-im.contexts.chat.messenger.messages.link-preview.events :as link-preview]
[status-im.contexts.chat.messenger.messages.transport.events :as messages.transport] [status-im.contexts.chat.messenger.messages.transport.events :as messages.transport]
[status-im.contexts.communities.discover.events] [status-im.contexts.communities.discover.events]
[status-im.contexts.profile.push-notifications.local.events :as local-notifications] [status-im.contexts.profile.push-notifications.local.events :as local-notifications]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[utils.debounce :as debounce]
[utils.re-frame :as rf] [utils.re-frame :as rf]
[utils.transforms :as transforms])) [utils.transforms :as transforms]))
@ -22,9 +24,12 @@
:peers-count peers-count)} :peers-count peers-count)}
(visibility-status-updates/peers-summary-change peers-count)))) (visibility-status-updates/peers-summary-change peers-count))))
(rf/defn wakuv2-peer-stats (rf/reg-event-fx :wakuv2-peer-stats
[{:keys [db]} peer-stats] (fn [{:keys [db]} [^js peer-stats-js]]
{:db (assoc db :peer-stats peer-stats :peers-count (count (:peers peer-stats)))}) (let [peer-stats (transforms/js->clj peer-stats-js)]
{:db (assoc db
:peer-stats peer-stats
:peers-count (count (:peers peer-stats)))})))
(rf/defn process (rf/defn process
{:events [:signals/signal-received]} {:events [:signals/signal-received]}
@ -38,57 +43,71 @@
(log/debug "Signal received" {:type type}) (log/debug "Signal received" {:type type})
(log/trace "Signal received" {:payload event-str}) (log/trace "Signal received" {:payload event-str})
(case type (case type
"node.login" {:fx [[:dispatch "wallet"
[:profile.login/login-node-signal (rf/dispatch [:wallet/signal-received event-js])
(transforms/js->clj event-js)]]]}
"backup.performed" {:db (assoc-in db "wakuv2.peerstats"
[:profile/profile :last-backup] (debounce/debounce-and-dispatch [:wakuv2-peer-stats event-js] 1000)
(.-lastBackup event-js))}
"envelope.sent" (messages.transport/update-envelopes-status cofx "envelope.sent"
(:ids (messages.transport/update-envelopes-status
(js->clj event-js cofx
:keywordize-keys (:ids (transforms/js->clj event-js))
true)) :sent)
:sent)
"envelope.expired" (messages.transport/update-envelopes-status cofx "envelope.expired"
(:ids (messages.transport/update-envelopes-status
(js->clj event-js cofx
:keywordize-keys (:ids (transforms/js->clj event-js))
true)) :not-sent)
:not-sent)
"message.delivered" (let [{:keys [chatID messageID]} (js->clj event-js "message.delivered"
:keywordize-keys (let [{:keys [chatID messageID]} (transforms/js->clj event-js)]
true)] (models.message/update-message-status cofx chatID messageID :delivered))
(models.message/update-message-status cofx
chatID "messages.new"
messageID (messages.transport/sanitize-messages-and-process-response cofx event-js true)
:delivered))
"mailserver.changed" (mailserver/handle-mailserver-changed cofx (.-id event-js)) "mailserver.changed"
"mailserver.available" (mailserver/handle-mailserver-available cofx (.-id event-js)) (mailserver/handle-mailserver-changed cofx (oops/oget event-js :id))
"mailserver.not.working" (mailserver/handle-mailserver-not-working cofx)
"discovery.summary" (summary cofx (js->clj event-js :keywordize-keys true)) "mailserver.available"
"mediaserver.started" {:db (assoc db :mediaserver/port (.-port event-js))} (mailserver/handle-mailserver-available cofx (oops/oget event-js :id))
"wakuv2.peerstats" (wakuv2-peer-stats cofx (js->clj event-js :keywordize-keys true))
"messages.new" (messages.transport/sanitize-messages-and-process-response cofx "mailserver.not.working"
event-js (mailserver/handle-mailserver-not-working cofx)
true)
"wallet" (rf/dispatch [:wallet/signal-received event-js]) "discovery.summary"
"local-notifications" (local-notifications/process cofx (summary cofx (transforms/js->clj event-js))
(js->clj event-js :keywordize-keys true))
"community.found" (link-preview/cache-community-preview-data (js->clj event-js "local-notifications"
:keywordize-keys (local-notifications/process cofx (transforms/js->clj event-js))
true))
"status.updates.timedout" (visibility-status-updates/handle-visibility-status-updates "community.found"
cofx (link-preview/cache-community-preview-data (transforms/js->clj event-js))
(js->clj event-js :keywordize-keys true))
"localPairing" (pairing/handle-local-pairing-signals "status.updates.timedout"
cofx (visibility-status-updates/handle-visibility-status-updates cofx (transforms/js->clj event-js))
(js->clj event-js :keywordize-keys true))
"curated.communities.update" (rf/dispatch [:fetched-contract-communities "localPairing"
(js->clj event-js :keywordize-keys true)]) (pairing/handle-local-pairing-signals cofx (transforms/js->clj event-js))
"waku.backedup.profile" (rf/dispatch [:profile/update-profile-from-backup
(js->clj event-js :keywordize-keys true)]) "curated.communities.update"
"waku.backedup.settings" (rf/dispatch [:profile/update-setting-from-backup (rf/dispatch [:fetched-contract-communities (transforms/js->clj event-js)])
(js->clj event-js :keywordize-keys true)])
"waku.backedup.profile"
(rf/dispatch [:profile/update-profile-from-backup (transforms/js->clj event-js)])
"waku.backedup.settings"
(rf/dispatch [:profile/update-setting-from-backup (transforms/js->clj event-js)])
"mediaserver.started"
{:db (assoc db :mediaserver/port (oops/oget event-js :port))}
"node.login"
{:fx [[:dispatch [:profile.login/login-node-signal (transforms/js->clj event-js)]]]}
"backup.performed"
{:db (assoc-in db [:profile/profile :last-backup] (oops/oget event-js :lastBackup))}
(log/debug "Event " type " not handled")))) (log/debug "Event " type " not handled"))))