A Query to track Specific Gravity Refractometer Readings Beer and Winemakers

I wanted to learn how to use advanced queries so I wrote a query that would look at my refractometer logs and automatically convert them to S.G.

If you have a page for your wine, e.g. titled [[Pinot Noir - June 2022]], then in your journal page have a log entry like:

* log [[Pinot Noir - June 2022]]
** bx: 22

Place the following query on the wine page and it will collect all the bx references, convert them to specific gravity, and calculate the ABV. It sorts the Brix values and assumes it always goes down (you’ll need something more advanced if you add stuff during fermentation). It looks like this:

Gravity Log

3 results

Original Gravity: 1.0919540676449373

10 1.0091 8.3604
15 1.0405 4.8769
22 1.08446 0

(The SG for the last line will be off due to how the conversion formula works.)

Here is the query. Enjoy!

{:title [:h2 "Gravity Log"]
 :query [:find ?ct
       :in $ ?current-page
       [?p :block/name ?current-page]
       [?b :block/path-refs ?p]
       [?ch :block/parent ?b]
       [?ch :block/content ?ct]
       [(clojure.string/includes? ?ct "bx:")]
  :inputs [:current-page]
  :result-transform (fn [result] 
( let [values (map (fn [x] (clojure.string/replace x (re-pattern "bx: (\\w+)") "$1"))
      ] values )
:view (fn [bxs]
      (let [fbxs (sort (map parseFloat (flatten bxs)))
            bx-to-sg (fn [bx] (+ 1 (/ bx (- 258.6 (/  (* 227.1 bx) 258.2))))  )
            og (apply max fbxs)
            get-sg (fn [ogr cgr]  (- (* cgr 0.00628) (* ogr 0.0025) -1.0013  ))
            get-abv (fn [ogr cgr] (* 0.6967 (- ogr cgr)))
        [:b "Original Gravity: " (str (bx-to-sg og))]
            [:tr [:th "Brix"] [:th "SG"] [:th "ABV"]]
            (for [cgr fbxs]
               [:tr [:td (str cgr)] [:td (str (get-sg og cgr))] [:td (str (get-abv og cgr))] ]

Gravity Log

3 results

Original Gravity: 1.0039191087608061

12 1.074135 -7.656733
22 1.136935 -14.623732999999998