Add RPC usage stats screen

This commit is contained in:
Roman Volosovskyi 2021-03-09 13:40:16 +02:00
parent 51178ab1b2
commit f572f5ef5e
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
6 changed files with 156 additions and 4 deletions

View File

@ -131,6 +131,8 @@
"wakuext_exportCommunity" {}
"wakuext_ensVerified" {}
"status_chats" {}
"rpcstats_getStats" {}
"rpcstats_reset" {}
"localnotifications_switchWalletNotifications" {}
"localnotifications_notificationPreferences" {}
"wallet_getTransfers" {}

View File

@ -51,6 +51,13 @@
:on-press
#(re-frame/dispatch [:navigate-to :bootnodes-settings])
:chevron true}
{:size :small
:title (i18n/label :t/rpc-usage-info)
:accessibility-label :rpc-usage-info
:container-margin-top 8
:on-press
#(re-frame/dispatch [:navigate-to :rpc-usage-info])
:chevron true}
(when platform/ios?
{:size :small
:title (i18n/label :t/notification-settings)

View File

@ -28,6 +28,7 @@
[status-im.ui.screens.network.views :as network]
[status-im.ui.screens.network.network-details.views :as network-details]
[status-im.ui.screens.network-info.views :as network-info]
[status-im.ui.screens.rpc-usage-info :as rpc-usage-info]
[status-im.ui.screens.log-level-settings.views :as log-level-settings]
[status-im.ui.screens.fleet-settings.views :as fleet-settings]
[status-im.ui.screens.profile.seed.views :as profile.seed]
@ -106,6 +107,8 @@
:component network-details/network-details}
{:name :network-info
:component network-info/network-info}
{:name :rpc-usage-info
:component rpc-usage-info/usage-info}
{:name :edit-network
:component edit-network/edit-network}
{:name :log-level-settings

View File

@ -0,0 +1,135 @@
(ns status-im.ui.screens.rpc-usage-info
(:require [status-im.ui.components.topbar :as topbar]
[status-im.ui.components.react :as react]
[status-im.i18n.i18n :as i18n]
[quo.core :as quo.core]
[quo.react-native :as quo.react-native]
[re-frame.core :as re-frame]
[status-im.utils.fx :as fx]
[status-im.ethereum.json-rpc :as json-rpc]
[taoensso.timbre :as log]
[clojure.string :as clojure.string]))
(re-frame/reg-sub :rpc-usage/raw-data (fn [db] (get db :rpc-usage/data)))
(re-frame/reg-sub :rpc-usage/filter (fn [db] (get db :rpc-usage/filter)))
(re-frame/reg-sub
:rpc-usage/data
:<- [:rpc-usage/raw-data]
:<- [:rpc-usage/filter]
(fn [[{:keys [total methods]} method-filter]]
(let [data
(->> methods
(map (fn [[k v]]
[(name k) v]))
(filter (fn [[k]]
(clojure.string/includes? k method-filter)))
(sort-by second >))
filtered-total (reduce + (map second data))]
{:stats data
:filtered-total filtered-total
:total total})))
(re-frame/reg-fx
::get-stats
(fn []
(status-im.ethereum.json-rpc/call
{:method "rpcstats_getStats"
:params []
:on-success #(re-frame/dispatch [::handle-stats %])})))
(re-frame/reg-fx
::reset
(fn []
(status-im.ethereum.json-rpc/call
{:method "rpcstats_reset"
:params []
:on-success #(log/debug "rpcstats_reset success")})))
(fx/defn handle-stats
{:events [::handle-stats]}
[{:keys [db]} data]
{:db (assoc db :rpc-usage/data data)})
(fx/defn get-stats
{:events [::get-stats]}
[{:keys [db]}]
(let [method-filter (get db :rpc-usage/filter "eth_")]
{:db (assoc db :rpc-usage/filter method-filter)
::get-stats nil}))
(fx/defn reset
{:events [::reset]}
[{:keys [db]}]
{:db (dissoc db :rpc-usage/data)
::reset nil})
(fx/defn copy
{:events [::copy]}
[{:keys [db]}]
{:db (dissoc db :rpc-usage/data)
::reset nil})
(fx/defn set-filter
{:events [::set-filter]}
[{:keys [db]} method-filter]
{:db (assoc db :rpc-usage/filter method-filter)})
(defn stats-table [{:keys [total filtered-total stats]}]
[quo.react-native/scroll-view
{:style {:padding-horizontal 8}}
[quo.react-native/view
{:style {:flex-direction :row
:justify-content :space-between}}
[quo.core/text "TOTAL"]
[quo.core/text (str filtered-total " of " total)]]
(when (seq stats)
(for [[k v] stats]
^{:key (str k v)}
[quo.react-native/view
{:style {:flex-direction :row
:justify-content :space-between}}
[quo.core/text k]
[quo.core/text v]]))])
(defn prepare-stats [{:keys [stats]}]
(clojure.string/join
"\n"
(map (fn [[k v]]
(str k " " v))
stats)))
(defn usage-info []
(let [stats @(re-frame/subscribe [:rpc-usage/data])
methods-filter @(re-frame/subscribe [:rpc-usage/filter])]
[react/view {:flex 1
:margin-horizontal 8}
[topbar/topbar
{:title (i18n/label :t/rpc-usage-info)}]
[quo.react-native/view
{:style {:flex-direction :row
:margin-top 8
:justify-content :space-between}}
[quo.core/button
{:on-press #(re-frame/dispatch [::get-stats])
:accessibility-label :rpc-usage-get-stats}
(i18n/label :t/rpc-usage-get-stats)]
[quo.core/button
{:on-press #(re-frame/dispatch [::reset])
:accessibility-label :rpc-usage-reset}
(i18n/label :t/rpc-usage-reset)]
[quo.core/button
{:on-press
#(react/copy-to-clipboard (prepare-stats stats))
:accessibility-label :rpc-usage-copy}
(i18n/label :t/rpc-usage-copy)]]
[quo.core/text-input
{:on-change-text #(re-frame/dispatch [::set-filter %])
:label (i18n/label :t/rpc-usage-filter)
:default-value methods-filter
:auto-capitalize :none
:show-cancel false
:auto-focus false}]
[stats-table stats]]))

View File

@ -2,7 +2,7 @@
"_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh <tag>' instead",
"owner": "status-im",
"repo": "status-go",
"version": "v0.73.2",
"commit-sha1": "e29cca667a4afc4872d2f03bc76e46c0296a7348",
"src-sha256": "03hsjgryrf187akbah5c9f8vkdn6ngp14221gqwcqkbaaj779000"
"version": "v0.73.3",
"commit-sha1": "3a408135d82b4deaffb1dff1f7ab67da18322353",
"src-sha256": "0p36hdwrbg7zb6j5iv9277wm3jfbjdmw947yqw2q3ksh3vws3n7n"
}

View File

@ -1481,5 +1481,10 @@
"you-can-fetch": "You can fetch chat history",
"youre-on-mobile-network": "Youre on mobile network",
"status-mobile-descr": "Status tends to use a lot of data when syncing chats. You can choose not to sync when on mobile network",
"restore-defaults": "Restore Defaults"
"restore-defaults": "Restore Defaults",
"rpc-usage-info": "RPC usage stats",
"rpc-usage-get-stats": "Refresh",
"rpc-usage-reset": "Reset",
"rpc-usage-filter": "Filter methods",
"rpc-usage-copy": "Copy"
}