Add wall-clock timestamps to last-events buffer

Each event entry in `last-events` is now wrapped as
`{:name <event-type> :t (app.common.time/now)}` so every event carries a
wall-clock timestamp. A new helper `format-last-events` renders the
buffer as a multi-line string with ISO time and delta-since-previous-
event in ms, replacing the previous pprint dump in error reports.

This lets support/devs tell whether the events leading up to a crash
were spaced out (user action) or jammed together (runaway loop).

AI-assisted-by: minimax-m3
Signed-off-by: Andrey Antukh <niwi@niwi.nz>
This commit is contained in:
Andrey Antukh 2026-07-23 07:31:43 +00:00
parent 52e5e0bec6
commit 66b978c01e
2 changed files with 26 additions and 1 deletions

View File

@ -151,7 +151,7 @@
(println "Last events:")
(println "--------------------")
(pp/pprint @st/last-events {:length 200})
(println (st/format-last-events))
(println)))
(catch :default cause
(.error js/console "error on generating report" cause)

View File

@ -7,6 +7,7 @@
(ns app.main.store
(:require
[app.common.logging :as log]
[app.common.time :as ct]
[app.util.object :as obj]
[app.util.timers :as tm]
[beicon.v2.core :as rx]
@ -94,6 +95,7 @@
(rx/filter #(not (contains? omitset %)))
(rx/map str)
(rx/pipe (rxo/distinct-contiguous))
(rx/map (fn [event] {:name event :t (ct/now)}))
(rx/scan (fn [buffer event]
(cond-> (conj buffer event)
(> (count buffer) 50)
@ -102,6 +104,29 @@
(rx/subs! #(reset! buffer (vec %))))
buffer))
(defn format-last-events
"Render the `last-events` buffer as a multi-line string with the
wall-clock time of each event and the delta (ms) since the previous
entry. The first entry has no delta. Useful for embedding in error
reports."
([] (format-last-events @last-events))
([events]
(let [lines
(loop [prev-t nil
xs (seq events)
out (transient [])]
(if xs
(let [{:keys [name t]} (first xs)
iso (ct/format-inst t :iso)
tail (if prev-t
(str " (+" (ct/diff-ms prev-t t) "ms)")
"")]
(recur t
(next xs)
(conj! out (str iso tail " " name))))
(persistent! out)))]
(str/join "\n" lines))))
(defn emit!
([] nil)
([event]