From f572f5ef5e69724e991f4726faa28d9cd92400d2 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 9 Mar 2021 13:40:16 +0200 Subject: [PATCH] Add RPC usage stats screen --- src/status_im/ethereum/json_rpc.cljs | 2 + .../ui/screens/advanced_settings/views.cljs | 7 + .../ui/screens/routing/profile_stack.cljs | 3 + src/status_im/ui/screens/rpc_usage_info.cljs | 135 ++++++++++++++++++ status-go-version.json | 6 +- translations/en.json | 7 +- 6 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 src/status_im/ui/screens/rpc_usage_info.cljs diff --git a/src/status_im/ethereum/json_rpc.cljs b/src/status_im/ethereum/json_rpc.cljs index 47e273e381..017144a24b 100644 --- a/src/status_im/ethereum/json_rpc.cljs +++ b/src/status_im/ethereum/json_rpc.cljs @@ -131,6 +131,8 @@ "wakuext_exportCommunity" {} "wakuext_ensVerified" {} "status_chats" {} + "rpcstats_getStats" {} + "rpcstats_reset" {} "localnotifications_switchWalletNotifications" {} "localnotifications_notificationPreferences" {} "wallet_getTransfers" {} diff --git a/src/status_im/ui/screens/advanced_settings/views.cljs b/src/status_im/ui/screens/advanced_settings/views.cljs index cce62d4ef7..a0dbb7ad81 100644 --- a/src/status_im/ui/screens/advanced_settings/views.cljs +++ b/src/status_im/ui/screens/advanced_settings/views.cljs @@ -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) diff --git a/src/status_im/ui/screens/routing/profile_stack.cljs b/src/status_im/ui/screens/routing/profile_stack.cljs index 4abf7f1fdc..0ff74e03bb 100644 --- a/src/status_im/ui/screens/routing/profile_stack.cljs +++ b/src/status_im/ui/screens/routing/profile_stack.cljs @@ -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 diff --git a/src/status_im/ui/screens/rpc_usage_info.cljs b/src/status_im/ui/screens/rpc_usage_info.cljs new file mode 100644 index 0000000000..901b358941 --- /dev/null +++ b/src/status_im/ui/screens/rpc_usage_info.cljs @@ -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]])) + + diff --git a/status-go-version.json b/status-go-version.json index f0187c175c..0a8e7d151b 100644 --- a/status-go-version.json +++ b/status-go-version.json @@ -2,7 +2,7 @@ "_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh ' 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" } diff --git a/translations/en.json b/translations/en.json index 8fbf5d4a0e..4f8d279404 100644 --- a/translations/en.json +++ b/translations/en.json @@ -1481,5 +1481,10 @@ "you-can-fetch": "You can fetch chat history", "youre-on-mobile-network": "You’re 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" }