瀏覽代碼

debug: add posthog capture for failed writes

Tienson Qin 4 年之前
父節點
當前提交
b05e53d371

+ 1 - 0
src/main/frontend/fs/node.cljs

@@ -72,6 +72,7 @@
                  mtime (gobj/get result "mtime")]
            (prn "[DEBUG] 5. The file was saved successfully!" {:path path})
            (when (util/electron?)
+             (state/set-ack-step! path :saved-successfully)
              (state/ack-file-write! path))
            (db/set-file-last-modified-at! repo path mtime)
            (p/let [content (if (encrypt/encrypted-db? (state/get-current-repo))

+ 5 - 1
src/main/frontend/handler/events.cljs

@@ -23,7 +23,8 @@
             [frontend.util :as util]
             [rum.core :as rum]
             ["semver" :as semver]
-            [clojure.string :as string]))
+            [clojure.string :as string]
+            [frontend.modules.instrumentation.posthog :as posthog]))
 
 ;; TODO: should we move all events here?
 
@@ -182,6 +183,9 @@
   (when (util/electron?)
     (state/set-modal! shell/shell)))
 
+(defmethod handle :instrument [[_ {:keys [type payload]}]]
+  (posthog/capture type payload))
+
 (defn run!
   []
   (let [chan (state/get-events-chan)]

+ 2 - 0
src/main/frontend/modules/file/core.cljs

@@ -96,6 +96,8 @@
     (let [chan-callback (:chan-callback opts)]
       (async/put! chan [repo files opts])
       (prn "[DEBUG] 4. Pushed to the write channel")
+      (doseq [file (map first files)]
+        (state/set-ack-step! file :pushed-to-channel))
       (when chan-callback
         (chan-callback)))))
 

+ 5 - 1
src/main/frontend/modules/instrumentation/posthog.cljs

@@ -2,7 +2,8 @@
   (:require [frontend.config :as cfg]
             [frontend.util :as util]
             [frontend.version :refer [version]]
-            ["posthog-js" :as posthog]))
+            ["posthog-js" :as posthog]
+            [cljs-bean.core :as bean]))
 
 (def ^:const token "qUumrWobEk2dKiKt1b32CMEZy8fgNS94rb_Bq4WutPA")
 (def ^:const masked "masked")
@@ -37,5 +38,8 @@
       (init)
       (posthog/opt_in_capturing))))
 
+(defn capture [id data]
+  (posthog/capture (str id) (bean/->js data)))
+
 (comment
   (posthog/debug))

+ 2 - 0
src/main/frontend/modules/outliner/file.cljs

@@ -27,6 +27,8 @@
                    (nil? (:block/file page-block)))
       (let [tree (tree/blocks->vec-tree blocks (:block/name page-block))]
         (prn "[DEBUG] 3. Block tree built")
+        (let [path (:file/path (:block/file (db/entity page-db-id)))]
+          (state/set-ack-step! path :start-writing))
         (file/save-tree page-block tree)))))
 
 (defn write-files!

+ 1 - 0
src/main/frontend/modules/outliner/pipeline.cljs

@@ -19,6 +19,7 @@
           :page (:block/name page)
           :file path})
     (when (util/electron?)
+      (state/set-ack-step! path :start-writing)
       (state/wait-for-write-ack! page-title path)))
   (file/sync-to-file page))
 

+ 14 - 5
src/main/frontend/state.cljs

@@ -1499,11 +1499,16 @@
                                      (let [last-ack-at (get-in @state [:debug/write-acks file-path :last-ack-at])]
                                        (when-not (and last-ack-at
                                                       (< requested-at last-ack-at (+ requested-at 3000)))
-                                         (js/alert (str "Logseq failed to save the page "
-                                                        page-title
-                                                        " to the file: "
-                                                        file-path
-                                                        ". Stop editing this page anymore, and copy all the blocks of this page to another editor to avoid any data-loss.")))))
+                                         (let [step (get-in @state [:debug/write-acks file-path :step])]
+                                           (pub-event! [:instrument {:type :debug/write-failed
+                                                                     :payload {:step step}}])
+                                           (js/alert (str "Logseq failed to save the page "
+                                                         page-title
+                                                         " to the file: "
+                                                         file-path
+                                                         ". Stop editing this page anymore, and copy all the blocks of this page to another editor to avoid any data-loss.\n"
+                                                         "Last step: "
+                                                         step))))))
                                    3000)]
         (swap! ack-wait-timeouts assoc file-path timeout)))))
 
@@ -1511,3 +1516,7 @@
   [file-path]
   (let [ack-at (util/time-ms)]
     (set-state! [:debug/write-acks file-path :last-ack-at] ack-at)))
+
+(defn set-ack-step!
+  [file-path step]
+  (set-state! [:debug/write-acks file-path :step] step))