Skip to content

Commit

Permalink
minimal: refactor dev section similar to other templates
Browse files Browse the repository at this point in the history
- add portal.clj to start portal data inspector
- add `mulog_events.clj` to define tap publisher and start publishing events to portal
- refactor `user.clj` namespace, mulog and portal sections
  • Loading branch information
practicalli-johnny committed Jul 26, 2023
1 parent ae33c07 commit 36cfc4a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 17 deletions.
55 changes: 55 additions & 0 deletions resources/practicalli/minimal/dev/mulog_events.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
;; ---------------------------------------------------------
;; Mulog Global Context and Custom Publisher
;;
;; - set event log global context
;; - tap publisher for use with Portal and other tap sources
;; - publish all mulog events to Portal tap source
;; ---------------------------------------------------------

(ns mulog-events
(:require
[com.brunobonacci.mulog :as mulog]
[com.brunobonacci.mulog.buffer :as mulog-buffer]))

;; ---------------------------------------------------------
;; Set event global context
;; - information added to every event for REPL workflow
(mulog/set-global-context! {:app-name "{{main/ns}} Service",
:version "0.1.0", :env "dev"})
;; ---------------------------------------------------------

;; ---------------------------------------------------------
;; Mulog event publishing

(deftype TapPublisher
[buffer transform]
com.brunobonacci.mulog.publisher.PPublisher
(agent-buffer [_] buffer)
(publish-delay [_] 200)
(publish [_ buffer]
(doseq [item (transform (map second (mulog-buffer/items buffer)))]
(tap> item))
(mulog-buffer/clear buffer)))

#_{:clj-kondo/ignore [:unused-private-var]}
(defn ^:private tap-events
[{:keys [transform] :as _config}]
(TapPublisher. (mulog-buffer/agent-buffer 10000) (or transform identity)))

(def tap-publisher
"Start mulog custom tap publisher to send all events to Portal
and other tap sources
`mulog-tap-publisher` to stop publisher"
(mulog/start-publisher!
{:type :custom, :fqn-function "mulog-events/tap-events"}))

#_{:clj-kondo/ignore [:unused-public-var]}
(defn stop
"Stop mulog tap publisher to ensure multiple publishers are not started
Recommended before using `(restart)` or evaluating the `user` namespace"
[]
tap-publisher)

;; Example mulog event message
;; (mulog/log ::dev-user-ns :message "Example event message" :ns (ns-publics *ns*))
;; ---------------------------------------------------------
24 changes: 24 additions & 0 deletions resources/practicalli/minimal/dev/portal.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns portal
(:require
;; Data inspector
[portal.api :as inspect]))


;; ---------------------------------------------------------
;; Start Portal and capture all evaluation results

;; Open Portal window in browser with dark theme
;; https://cljdoc.org/d/djblue/portal/0.37.1/doc/ui-concepts/themes
;; Portal options:
;; - light theme {:portal.colors/theme :portal.colors/solarized-light}
;; - dark theme {:portal.colors/theme :portal.colors/gruvbox}

(def instance
"Open portal window if no portal sessions have been created.
A portal session is created when opening a portal window"
(or (seq (inspect/sessions))
(inspect/open {:portal.colors/theme :portal.colors/gruvbox})))

;; Add portal as tapsource (add to clojure.core/tapset)
(add-tap #'portal.api/submit)
;; ---------------------------------------------------------
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"Tools for REPL Driven Development"
(:require
;; REPL Workflow
[mulog-events] ; Event Logging
[com.brunobonacci.mulog :as mulog] ; Global context & Tap publisher
[portal]
[portal.api :as inspect] ; Data inspector
[clojure.tools.namespace.repl :as namespace]))

Expand Down Expand Up @@ -61,23 +64,14 @@
;; ---------------------------------------------------------

;; ---------------------------------------------------------
;; Start Portal and capture all evaluation results

;; Open Portal window in browser with dark theme
;; https://cljdoc.org/d/djblue/portal/0.37.1/doc/ui-concepts/themes
;; Portal options:
;; - light theme {:portal.colors/theme :portal.colors/solarized-light}
;; - dark theme {:portal.colors/theme :portal.colors/gruvbox}

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(def portal-instance
"Open portal window if no portal sessions have been created.
A portal session is created when opening a portal window"
(or (seq (inspect/sessions))
(inspect/open {:portal.colors/theme :portal.colors/gruvbox})))

;; Add portal as tapsource (add to clojure.core/tapset)
(add-tap #'portal.api/submit)
;; Mulog event logging
;; `mulog-publisher` namespace used to launch tap> events to tap-source (portal)
;; and set global context for all events

;; Example mulog event message
(mulog/log ::dev-user-ns
:message "Example event from user namespace"
:ns (ns-publics *ns*))
;; ---------------------------------------------------------

;; ---------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions resources/practicalli/minimal/template.edn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
[["build" ""
{"build.clj.template" "build.clj"
"deps.edn.template" "deps.edn"}]
["dev" "dev"
{"mulog_events.clj" "mulog_events.clj"
"portal.clj" "portal.clj"
"user.clj" "user.clj"}]
["src" "src/{{top/file}}"
{"app.clj.template" "{{main/file}}.clj"}]
["test" "test/{{top/file}}"
Expand Down

0 comments on commit 36cfc4a

Please sign in to comment.