浏览代码

fix: query performance degration caused by date normalization

Junyi Du 2 年之前
父节点
当前提交
dbf13c87d9

+ 5 - 3
src/main/frontend/components/query_table.cljs

@@ -46,11 +46,11 @@
     (.localeCompare x y (state/sub :preferred-language))
     (< x y)))
 
-(defn- sort-result [result {:keys [sort-by-column sort-desc?]}]
+(defn- sort-result [result {:keys [sort-by-column sort-desc? sort-nlp-date?]}]
   (if (some? sort-by-column)
     (let [comp-fn (if sort-desc? #(locale-compare %2 %1) locale-compare)]
       (sort-by (fn [item]
-                 (block/normalize-block (sort-by-fn sort-by-column item)))
+                 (block/normalize-block (sort-by-fn sort-by-column item) sort-nlp-date?))
                comp-fn
                result))
     result))
@@ -63,12 +63,14 @@
   (let [p-desc? (get-in current-block [:block/properties :query-sort-desc])
         desc? (if (some? p-desc?) p-desc? true)
         p-sort-by (keyword (get-in current-block [:block/properties :query-sort-by]))
+        nlp-date? (get-in current-block [:block/properties :query-nlp-date])
         sort-by-column (or (some-> p-sort-by keyword)
                          (if (query-dsl/query-contains-filter? (:block/content current-block) "sort-by")
                            nil
                            :updated-at))]
     {:sort-desc? desc?
-     :sort-by-column sort-by-column}))
+     :sort-by-column sort-by-column
+     :sort-nlp-date? nlp-date?}))
 
 ;; Components
 ;; ==========

+ 2 - 1
src/main/frontend/date.cljs

@@ -172,7 +172,8 @@
                  :hourCycle "h23"}))))
 
 (defn normalize-date
-  "Given raw date string, return a normalized date string at best effort."
+  "Given raw date string, return a normalized date string at best effort.
+   Warning: this is a function with heavy cost (likely 50ms). Use with caution"
   [s]
   (some
    (fn [formatter]

+ 7 - 3
src/main/frontend/format/block.cljs

@@ -56,9 +56,13 @@ and handles unexpected failure."
             (tf/unparse date/custom-formatter))))
 
 (defn normalize-block
-  "Normalizes supported formats such as dates and percentages."
-  ([block]
-   (->> [normalize-as-percentage normalize-as-date identity]
+  "Normalizes supported formats such as dates and percentages.
+   Be careful, this function may harm query sort performance!
+   - nlp-date? - Enable NLP parsing on date items.
+       Requires heavy computation (see `normalize-as-date` for details)"
+  ([block nlp-date?]
+   (->> [normalize-as-percentage (when nlp-date? normalize-as-date) identity]
+        (remove nil?)
         (map #(% (if (set? block) (first block) block)))
         (remove nil?)
         (first))))

+ 3 - 3
src/test/frontend/format/block_test.cljs

@@ -5,7 +5,7 @@
 
 (deftest test-normalize-date
   (testing "normalize date values"
-    (are [x y] (= (block/normalize-block x) y)
+    (are [x y] (= (block/normalize-block x true) y)
          "Aug 12th, 2022"
          "2022-08-12T00:00:00Z"
 
@@ -20,7 +20,7 @@
 
 (deftest test-normalize-percentage
   (testing "normalize percentages"
-    (are [x y] (= (block/normalize-block x) y)
+    (are [x y] (= (block/normalize-block x false) y)
          "50%"
          0.5
 
@@ -35,7 +35,7 @@
 
 (deftest test-random-values
   (testing "random values should not be processed"
-    (are [x y] (= (block/normalize-block x) y)
+    (are [x y] (= (block/normalize-block x false) y)
          "anreanre"
          "anreanre"