enhance(rtc): update info in rtc indicator

experiment/tanstack-table
rcmerci 2024-06-20 13:05:43 +08:00
parent eae2156f6c
commit 5fbff9ff8d
3 changed files with 89 additions and 43 deletions

View File

@ -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)))

View File

@ -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})]]]]))))

View File

@ -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/<! (http/put url {:body all-blocks-str :with-credentials? false})))
(rtc-log/rtc-log :rtc.log/upload {:message "requesting upload-graph"})
(rtc-log/rtc-log :rtc.log/upload {:sub-type :request-upload-graph
:message "requesting upload-graph"})
(let [upload-resp
(m/? (ws-util/send&recv get-ws-create-task {:action "upload-graph"
:s3-key key
@ -135,6 +138,8 @@
(op-mem-layer/init-empty-ops-store! repo)
(op-mem-layer/update-graph-uuid! repo graph-uuid)
(m/? (op-mem-layer/new-task--sync-to-idb repo))
(rtc-log/rtc-log :rtc.log/upload {:sub-type :upload-completed
:message "upload-graph completed"})
nil)
(throw (ex-info "upload-graph failed" {:upload-resp upload-resp})))))))
@ -260,7 +265,9 @@
(defn new-task--request-download-graph
[get-ws-create-task graph-uuid]
(rtc-log/rtc-log :rtc.log/download {:message "requesting download graph" :graph-uuid graph-uuid})
(rtc-log/rtc-log :rtc.log/download {:sub-type :request-download-graph
:message "requesting download graph"
:graph-uuid graph-uuid})
(m/join :download-info-uuid
(ws-util/send&recv get-ws-create-task {:action "download-graph"
:graph-uuid graph-uuid})))
@ -275,7 +282,9 @@
[get-ws-create-task download-info-uuid graph-uuid timeout-ms]
(->
(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/<! (http/get s3-url {:with-credentials? false})))
repo (str sqlite-util/db-version-prefix graph-name)]
(if (not= 200 status)
(throw (ex-info "download-graph from s3 failed" {:resp r}))
(do
(rtc-log/rtc-log :rtc.log/download {:message "transacting graph data to local db" :graph-uuid graph-uuid})
(rtc-log/rtc-log :rtc.log/download {:sub-type :transact-graph-data-to-db
:message "transacting graph data to local db"
:graph-uuid graph-uuid})
(let [all-blocks (ldb/read-transit-str body)]
(worker-state/set-rtc-downloading-graph! true)
(op-mem-layer/init-empty-ops-store! repo)
@ -311,5 +324,7 @@
(m/? (op-mem-layer/new-task--sync-to-idb repo))
(m/? (c.m/await-promise (.storeMetadata worker-obj repo (pr-str {:kv/value graph-uuid}))))
(worker-state/set-rtc-downloading-graph! false)
(rtc-log/rtc-log :rtc.log/download {:message "download completed" :graph-uuid graph-uuid})
(rtc-log/rtc-log :rtc.log/download {:sub-type :download-completed
:message "download completed"
:graph-uuid graph-uuid})
nil))))))