From 5fbff9ff8d616c1abf78b65a3ef9b87771afd702 Mon Sep 17 00:00:00 2001 From: rcmerci Date: Thu, 20 Jun 2024 13:05:43 +0800 Subject: [PATCH] enhance(rtc): update info in rtc indicator --- src/main/frontend/components/rtc/flows.cljs | 3 + .../frontend/components/rtc/indicator.cljs | 98 ++++++++++++------- .../rtc/full_upload_download_graph.cljs | 31 ++++-- 3 files changed, 89 insertions(+), 43 deletions(-) diff --git a/src/main/frontend/components/rtc/flows.cljs b/src/main/frontend/components/rtc/flows.cljs index 43ff5f3906..a57e5c35f1 100644 --- a/src/main/frontend/components/rtc/flows.cljs +++ b/src/main/frontend/components/rtc/flows.cljs @@ -19,3 +19,6 @@ (m/eduction (remove #(contains? #{:rtc.log/download :rtc.log/upload} (:type %))) rtc-log-flow)) + +(def rtc-state-flow + (m/watch (:rtc/state @state/state))) diff --git a/src/main/frontend/components/rtc/indicator.cljs b/src/main/frontend/components/rtc/indicator.cljs index 7ea5047031..615f5c1c4f 100644 --- a/src/main/frontend/components/rtc/indicator.cljs +++ b/src/main/frontend/components/rtc/indicator.cljs @@ -1,26 +1,25 @@ (ns frontend.components.rtc.indicator "RTC state indicator" - (:require [fipp.edn :as fipp] + (:require [cljs-time.core :as t] + [fipp.edn :as fipp] [frontend.common.missionary-util :as c.m] [frontend.components.rtc.flows :as rtc-flows] - [frontend.db :as db] [frontend.state :as state] [frontend.ui :as ui] [frontend.util :as util] - [logseq.db :as ldb] [logseq.shui.ui :as shui] [missionary.core :as m] [rum.core :as rum])) (comment (def rtc-state-schema - [:enum :downloading :uploading :open :error])) + [:enum :open :close])) (defonce ^:private *detail-info - (atom {:pending-local-ops 0 ;TODO: mock now, will update later - :graph-uuid #uuid "c9424ea4-5aab-4957-a2bf-423631862259" ;TODO: mock for now - :local-tx 233 ;TODO: mock for now - :rtc-state :open ;TODO: mock for now, `rtc-state-schema` + (atom {:pending-local-ops 0 + :graph-uuid nil + :local-tx nil + :rtc-state :open :download-logs nil :upload-logs nil :misc-logs nil})) @@ -42,49 +41,78 @@ (constantly nil) (update-log-task rtc-flows/rtc-download-log-flow :download-logs) (update-log-task rtc-flows/rtc-upload-log-flow :upload-logs) - (update-log-task rtc-flows/rtc-misc-log-flow :misc-logs)) + (update-log-task rtc-flows/rtc-misc-log-flow :misc-logs) + (m/reduce (fn [_ state] + (swap! *detail-info assoc + :pending-local-ops (:unpushed-block-update-count state) + :graph-uuid (:graph-uuid state) + :local-tx (:local-tx state) + :rtc-state (if (:rtc-lock state) :open :close))) + rtc-flows/rtc-state-flow)) ::update-detail-info)] (reset! *update-detail-info-canceler canceler)))) (run-task--update-detail-info) (rum/defc details < rum/reactive [] - (let [{:keys [download-logs upload-logs misc-logs]} (rum/react *detail-info)] + (let [{:keys [graph-uuid local-tx rtc-state download-logs upload-logs misc-logs]} (rum/react *detail-info)] [:pre.select-text (-> (cond-> {} download-logs (assoc :download download-logs) upload-logs (assoc :upload upload-logs) - misc-logs (assoc :misc misc-logs)) + misc-logs (assoc :misc misc-logs) + graph-uuid (assoc :graph-uuid graph-uuid) + local-tx (assoc :local-tx local-tx) + rtc-state (assoc :rtc-state rtc-state)) (fipp/pprint {:width 20}) with-out-str)])) +(defn- downloading? + [detail-info] + (when-let [{:keys [created-at sub-type]} (first (:download-logs detail-info))] + (and (not= :download-completed sub-type) + (> 600 ;; 10min + (/ (- (t/now) created-at) 1000))))) + +(defn- uploading? + [detail-info] + (when-let [{:keys [created-at sub-type]} (first (:upload-logs detail-info))] + (and (not= :upload-completed sub-type) + (> 600 + (/ (- (t/now) created-at) 1000))))) + (rum/defc indicator < rum/reactive [] (let [detail-info (rum/react *detail-info) _ (state/sub :auth/id-token) online? (state/sub :network/online?) - uploading? (= :uploading (:rtc-state detail-info)) - downloading? (= :downloading (:rtc-state detail-info)) + uploading? (uploading? detail-info) + downloading? (downloading? detail-info) rtc-state (:rtc-state detail-info) - unpushed-block-update-count (:pending-local-ops detail-info) - {:keys [graph-uuid]} (state/sub :rtc/state)] - (when (or graph-uuid downloading?) - (if downloading? - (shui/button - {:variant :ghost - :size :sm} - "Downloading...") - (when (and graph-uuid (= graph-uuid (ldb/get-graph-rtc-uuid (db/get-db)))) - [:div.cp__rtc-sync - [:div.cp__rtc-sync-indicator - [:a.button.cloud - {:on-click #(shui/popup-show! (.-target %) - (details) - {:align "end"}) - :class (util/classnames [{:on (and online? (= :open rtc-state)) - :idle (and online? (= :open rtc-state) (zero? unpushed-block-update-count) - (not uploading?) - (not downloading?)) - :queuing (or uploading? downloading? (pos? unpushed-block-update-count))}])} - [:span.flex.items-center - (ui/icon "cloud" {:size ui/icon-size})]]]]))))) + unpushed-block-update-count (:pending-local-ops detail-info)] + (cond-> [:div] + downloading? + (conj (shui/button + {:variant :ghost + :size :sm} + "Downloading...")) + uploading? + (conj (shui/button + {:variant :ghost + :size :sm} + "Uploading...")) + ;; (and graph-uuid + ;; (= graph-uuid (ldb/get-graph-rtc-uuid (db/get-db)))) + true + (conj + [:div.cp__rtc-sync + [:div.cp__rtc-sync-indicator + [:a.button.cloud + {:on-click #(shui/popup-show! (.-target %) + (details) + {:align "end"}) + :class (util/classnames [{:on (and online? (= :open rtc-state)) + :idle (and online? (= :open rtc-state) (zero? unpushed-block-update-count)) + :queuing (pos? unpushed-block-update-count)}])} + [:span.flex.items-center + (ui/icon "cloud" {:size ui/icon-size})]]]])))) diff --git a/src/main/frontend/worker/rtc/full_upload_download_graph.cljs b/src/main/frontend/worker/rtc/full_upload_download_graph.cljs index 1b52caef4e..05ad7969d4 100644 --- a/src/main/frontend/worker/rtc/full_upload_download_graph.cljs +++ b/src/main/frontend/worker/rtc/full_upload_download_graph.cljs @@ -110,7 +110,8 @@ (defn new-task--upload-graph [get-ws-create-task repo conn remote-graph-name] (m/sp - (rtc-log/rtc-log :rtc.log/upload {:message "fetching presigned put-url"}) + (rtc-log/rtc-log :rtc.log/upload {:sub-type :fetch-presigned-put-url + :message "fetching presigned put-url"}) (let [[{:keys [url key]} all-blocks-str] (m/? (m/join @@ -119,9 +120,11 @@ (m/sp (let [all-blocks (export-as-blocks @conn)] (ldb/write-transit-str all-blocks)))))] - (rtc-log/rtc-log :rtc.log/upload {:message "uploading data"}) + (rtc-log/rtc-log :rtc.log/upload {:sub-type :upload-data + :message "uploading data"}) (m/? (c.m/ (m/sp - (rtc-log/rtc-log :rtc.log/download {:message "waiting for the remote to prepare the data" :graph-uuid graph-uuid}) + (rtc-log/rtc-log :rtc.log/download {:sub-type :wait-remote-graph-data-ready + :message "waiting for the remote to prepare the data" + :graph-uuid graph-uuid}) (loop [] (m/? (m/sleep 3000)) (let [{:keys [download-info-list]} @@ -295,14 +304,18 @@ (defn new-task--download-graph-from-s3 [graph-uuid graph-name s3-url] (m/sp - (rtc-log/rtc-log :rtc.log/download {:message "downloading graph data" :graph-uuid graph-uuid}) + (rtc-log/rtc-log :rtc.log/download {:sub-type :downloading-graph-data + :message "downloading graph data" + :graph-uuid graph-uuid}) (let [^js worker-obj (:worker/object @worker-state/*state) {:keys [status body] :as r} (m/? (c.m/