-
Notifications
You must be signed in to change notification settings - Fork 2
/
report.clj
147 lines (123 loc) · 3.68 KB
/
report.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
(ns rill.wheel.report
"Tools for reporting on aggregates, events and commands"
(:require [clojure.string :as string]))
(defn- vars-with-meta
[ns k]
(filter #(get (meta %) k)
(vals (ns-publics ns))))
(defn ns-events
[ns]
(vars-with-meta ns :rill.wheel/event-fn))
(defn events
[]
(mapcat ns-events (all-ns)))
(defn ns-commands
[ns]
(vars-with-meta ns :rill.wheel/command-fn))
(defn commands
[]
(mapcat ns-commands (all-ns)))
(defn ns-aggregates
[ns]
(vars-with-meta ns :rill.wheel/descriptor-fn))
(defn aggregates
[]
(mapcat ns-aggregates (all-ns)))
(defn keyword->sym
[k]
(symbol (subs (str k) 1)))
(defn- wheel-type
[m]
(if (var? m)
(wheel-type (meta m))
(keyword (-> m :ns ns-name name) (-> m :name name))))
(defn- report-map
[vars]
(into {} (map (fn [v]
[(wheel-type v) (meta v)])
vars)))
(defn assoc-events
[a-map]
(reduce (fn [a e]
(assoc-in a [(:rill.wheel/aggregate (meta e)) :rill.wheel/events (wheel-type e)]
(meta e)))
a-map
(events)))
(defn assoc-commands
[a-map]
(reduce (fn [a e]
(assoc-in a [(:rill.wheel/aggregate (meta e)) :rill.wheel/commands (wheel-type e)]
(meta e)))
a-map
(commands)))
(defn report-data
"Map with metadata for all aggregates, including events and commands"
[]
(-> (report-map (aggregates))
(assoc-events)
(assoc-commands)))
(defn- key->title
[k]
(let [words (string/split (name k) #"-")]
(string/join " " (map string/capitalize words))))
(defn- line-indent [line]
(count (take-while #{\space} line)))
(defn- find-min-indent [lines]
(if-let [indents (->> lines
(remove string/blank?)
(map line-indent)
seq)]
(reduce min Long/MAX_VALUE indents)
0))
(defn- remove-indent [line indentation]
(subs line (min indentation (count line))))
(defn- remove-extraneous-indentation [doc]
(when doc
(let [[first-line & lines] (string/split doc #"\n")
indentation (find-min-indent lines)]
(->> lines
(map #(remove-indent % indentation))
(cons first-line)
(string/join "\n")))))
(defn- propslist
[s]
(when-let [l (seq (map (fn [p]
(str " - " (key->title p) "\n"))
s))]
(concat ["**Properties**\n\n"]
l
["\n"])))
(defn- msglist
[m]
(->> (sort (keys m))
(mapcat (fn [k]
(let [e (get m k)]
(-> ["### " (key->title k) "\n\n"
" " k "\n\n"
(remove-extraneous-indentation (:doc e))
"\n\n"]
(into (propslist (:rill.wheel/properties e)))
(conj "\n\n")))))
(apply str)))
(defn report-data->markdown
[r]
(->> (sort (keys r))
(mapcat (fn [k]
(let [m (get r k)]
(-> ["# " (key->title k) "\n\n"
" " k "\n\n"
(remove-extraneous-indentation (:doc m))
"\n\n"]
(into (propslist (:rill.wheel/properties m)))
(conj "## Events\n\n")
(into (msglist (:rill.wheel/events m)))
(into ["\n\n"
"## Commands\n\n"])
(into (msglist (:rill.wheel/commands m)))))))
(apply str "# Model overview\n\n")))
(defn report
"Markdown formatted report of all aggregates, events and commands in
the system."
[]
(-> (report-data)
(report-data->markdown)))