Skip to content

Commit

Permalink
fix: Update elin.interceptor.tap/initialize to call tap-handler for i…
Browse files Browse the repository at this point in the history
…ntercepting tapped values
  • Loading branch information
liquidz committed Dec 28, 2024
1 parent 37dddda commit 6cd211f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 21 deletions.
4 changes: 2 additions & 2 deletions resources/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
elin.interceptor.nrepl/normalize-path {}
elin.interceptor.nrepl/progress {}
elin.interceptor.output/print-output {}
elin.interceptor.tap/initialize {}
elin.interceptor.tap/access-tapped-values {}]
elin.interceptor.tap/initialize {:max-store-size 10}
elin.interceptor.tap/access-tapped-values {:var-name "*tapped*"}]

:config-map {elin.interceptor.evaluate/set-eval-result-to-virtual-text {:format "=> {{{result}}}"
:highlight "DiffText"
Expand Down
94 changes: 75 additions & 19 deletions src/elin/interceptor/tap.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
[clojure.string :as str]
[elin.constant.interceptor :as e.c.interceptor]
[elin.function.evaluate :as e.f.evaluate]
[exoscale.interceptor :as ix])
(:import
(java.time LocalDateTime)))
[elin.util.interceptor :as e.u.interceptor]
[exoscale.interceptor :as ix]))

(def ^:private ns-sym
'elin.interceptor.tap)
(gensym "elin.interceptor.tap."))

(def ^:private tap-fn-sym
'catch-tapped!)
Expand All @@ -17,10 +16,39 @@
(gensym "elin-tapped"))

(defn- initialize-code
[]
[{:keys [http-server-port max-store-size]}]
(str
`(do (in-ns '~ns-sym)
(refer-clojure)

(defn datafy#
[x#]
(clojure.walk/prewalk
(fn [v#]
(cond
(map? v#) (update-vals v# datafy#)
(list? v#) (map datafy# v#)
(vector? v#) (mapv datafy# v#)
:else (clojure.core.protocols/datafy v#)))
x#))

(defn tap-handler-request#
[value#]
(try
(let [uri# (java.net.URI/create
(str "http://localhost:" ~http-server-port "/api/v1"))
body# (format "{\"method\": \"elin.handler.tap/tapped\", \"params\": [%s]}"
(pr-str (pr-str value#)))
client# (java.net.http.HttpClient/newHttpClient)
req# (-> (java.net.http.HttpRequest/newBuilder)
(.uri uri#)
(.header "Content-Type" "application/json")
(.POST (java.net.http.HttpRequest$BodyPublishers/ofString body#))
(.build))]

(.send client# req# (java.net.http.HttpResponse$BodyHandlers/ofString)))
(catch Exception _# nil)))

(def ~tapped-atom-sym (atom []))

(try
Expand All @@ -29,26 +57,54 @@

(defn ~tap-fn-sym
[x#]
(swap! ~tapped-atom-sym conj {:id (str (random-uuid))
:time (str (LocalDateTime/now))
:value (clojure.core.protocols/datafy x#)}))
(let [value# (datafy# x#)]
(tap-handler-request# value#)
(when (<= ~max-store-size (count (deref ~tapped-atom-sym)))
(swap! ~tapped-atom-sym (fn [v#] (drop-last 1 v#))))
(swap! ~tapped-atom-sym (fn [v#]
(cons {:id (str (random-uuid))
:time (str (java.time.LocalDateTime/now))
:value value#}
v#)))))
(try
(add-tap ~tap-fn-sym)
(catch Exception _# nil)))))

(def initialize
"Defines a tap function and adds it."
"Defines a tap function and adds it.
.Configuration
[%autowidth.stretch]
|===
| key | type | description
| max-store-size | integer | The maximum number of values to store when tapped. Defautl value is `10`.
|==="
{:kind e.c.interceptor/connect
:leave (-> (fn [ctx]
(e.f.evaluate/evaluate-code ctx (initialize-code)))
:leave (-> (fn [{:as ctx :component/keys [handler]}]
(let [config (e.u.interceptor/config ctx #'initialize)
http-server-port (get-in handler [:initialize :export "g:elin_http_server_port"])
code (initialize-code {:max-store-size (:max-store-size config)
:http-server-port http-server-port})]
(e.f.evaluate/evaluate-code ctx code)))
(ix/discard))})

(def access-tapped-values
"Enables access to tapped values from a specific symbol."
{:kind e.c.interceptor/evaluate
:enter (-> (fn [ctx]
(let [var-sym (symbol (name ns-sym)
(name tapped-atom-sym))
var-code (str `(deref ~var-sym))]
(update ctx :code #(str/replace % #"\*tapped\*" var-code))))
(ix/when #(str/includes? (:code %) "*tapped*")))})
"Enables access to tapped values from a specific symbol.
.Configuration
[%autowidth.stretch]
|===
| key | type | description
| var-name | string | The var name to access tapped values. Default value is `\\*tapped*`.
|==="
(let [get-target-name #(-> (e.u.interceptor/config % #'access-tapped-values)
(:var-name))]
{:kind e.c.interceptor/evaluate
:enter (-> (fn [ctx]
(let [target-name (get-target-name ctx)
var-code (str `(deref ~(symbol (name ns-sym)
(name tapped-atom-sym))))]
(update ctx :code #(str/replace % target-name var-code))))
(ix/when #(str/includes? (:code %) (get-target-name %))))}))

0 comments on commit 6cd211f

Please sign in to comment.