-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug-app.lisp
284 lines (272 loc) · 16.6 KB
/
debug-app.lisp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
(in-package :weblocks-utils)
(defwebapp debug-app
:prefix "/debug-app"
:init-user-session 'init-user-session
:description "Debugging app for weblocks"
:js-backend :jquery
:autostart nil ;; have to start the app manually
:debug t)
#+this-causes-performance-troubles-and-errors(defparameter weblocks-util:*process-html-parts-p*
(lambda ()
(and
(webapp-debug)
(ignore-errors (weblocks:current-webapp))
(not (typep (weblocks:current-webapp) 'debug-app)))))
(defun remove-session-action (&rest args &key id action)
(hunchentoot:remove-session (hunchentoot::get-stored-session (parse-integer id)))
(redirect (weblocks::weblocks-webapp-prefix (weblocks::current-webapp))))
(defstore
*list-store*
:custom
:classes
(list
'webapp-cls
(list
:object-id (lambda (item item-data)
(weblocks::weblocks-webapp-name item-data))
:slots (list
(list
'title
(lambda (item item-data)
(weblocks::weblocks-webapp-name item-data))))
:find-all-objects (lambda ()
(remove-if #'null (loop for i in weblocks::*registered-webapps* collect (ignore-errors (weblocks::find-app i))))))
'session-cls
(list
:object-id (lambda (item item-data)
(format nil "~A-~A"
(weblocks::weblocks-webapp-name (slot-value (getf item-data :app) 'weblocks-custom::data))
(hunchentoot:session-id (getf item-data :session))))
:slots (list
(list
'string-id
(lambda (item item-data)
(format nil "~A-session-~A"
(weblocks::weblocks-webapp-name (slot-value (getf item-data :app) 'weblocks-custom::data))
(hunchentoot:session-id (getf item-data :session)))))
(list 'hunchentoot-session-id
(lambda (item item-data)
(hunchentoot:session-id (getf item-data :session))))
(list
'webapp
(lambda (item item-data)
(getf item-data :app)))
(list 'session
(lambda (item item-data)
(getf item-data :session))))
:find-all-objects (lambda ()
(loop for i in (all-of 'webapp-cls :store *list-store*) append
(with-webapp (slot-value i 'weblocks-custom::data)
(loop for j in (weblocks::active-sessions)
collect (list :session j :app i))))))
'session-data-cls
(list
:object-id (lambda (item item-data)
(format nil "~A-~A-~A"
(weblocks::weblocks-webapp-name (slot-value (slot-value (getf item-data :session) 'webapp) 'weblocks-custom::data))
(hunchentoot:session-id (slot-value (getf item-data :session) 'session))
(getf item-data :key)))
:slots (list
(list
'key
(lambda (item item-data)
(getf item-data :key)))
(list
'value
(lambda (item item-data)
(getf item-data :value)))
(list
'session
(lambda (item item-data)
(getf item-data :session))))
:find-all-objects (lambda ()
(loop for i in (all-of 'session-cls :store *list-store*)
append (with-webapp
(slot-value (slot-value i 'webapp) 'weblocks-custom::data)
(let ((session-data (weblocks::webapp-session-hash (slot-value i 'session))))
(when session-data
(loop for j from 1
for key being the hash-key of session-data
for value being the hash-value of session-data
collect
(list :key key :value value :session i))))))))))
(defun tree-title (obj)
(typecase obj
(webapp-cls (object-id obj))
(session-cls (arnesi:escape-as-html (format nil " session #~A" (slot-value obj 'hunchentoot-session-id))))
(session-data-cls (arnesi:escape-as-html (format nil " key: ~A, value: - ~A" (slot-value obj 'key)
(truncate-string (prin1-to-string (slot-value obj 'value)) :length 20))))))
(defun memory-html-info ()
(nl2br
(let ((*standard-output* (make-string-output-stream)))
(room)
(get-output-stream-string *standard-output*))))
(defun get-system-free-memory ()
"Returns bytes of system free memory"
(with-open-file (in "/proc/meminfo" :direction :input :if-does-not-exist nil)
(when in
(loop for i = (read-line in nil)
while i do
(when (ppcre:scan "MemFree" i)
(return-from
get-system-free-memory
(* 1024
(parse-integer
(ppcre:scan-to-strings "\\d+" i)
:junk-allowed t))))))))
(defun bytes-as-pretty-megabytes (bytes-number)
(format nil "~A mb" (floor (/ bytes-number 1024 1024))))
(defun init-user-session (root)
(make-action #'remove-session-action "remove-session")
(let ((tree-grid)
(view)
(summary-widget))
(setf view (defview nil (:type tree)
(weblocks-custom::data
:present-as (tree-branches :straight-column-captions nil)
:reader (lambda (item)
(tree-title item)))
(action-buttons
:present-as html
:reader (lambda (item)
(format nil "~A ~A"
(if (typep item 'session-data-cls)
""
(funcall (weblocks-tree-widget::action-links-reader tree-grid view
:adding-allowed-p nil
:editing-allowed-p nil
:deleting-allowed-p nil) item))
(if (typep item 'session-cls)
(weblocks::with-html-to-string
(:str "| ")
(:a :href (add-get-param-to-url
(make-action-url "remove-session")
"id"
(write-to-string (slot-value item 'hunchentoot-session-id)))
:onclick (format nil "initiateActionWithArgs(\"~A\", \"~A\", {id: '~A'}); return false;"
"remove-session" (session-name-string-pair) (slot-value item 'hunchentoot-session-id))
"remove session"))
"")
)))))
(setf tree-grid (make-instance 'tree-widget
:expand-all-items-p nil
:class-store *list-store*
:allow-add-p nil
:view view
:data-class 'webapp-cls))
(setf summary-widget
(make-instance 'composite
:widgets (list
(make-widget
(lambda (&rest args)
(with-html
(:h1 "Application Summary")
(:h2 "Memory")
(str "System free memory ")
(:b (str (bytes-as-pretty-megabytes (get-system-free-memory))))
(:br)
(str "Application used memory ")
(:b (str (bytes-as-pretty-megabytes (sb-kernel:dynamic-usage))))
(str ", ")
(render-link (lambda (&rest args)
(do-information (memory-html-info)))
"more info"
)
(str ",")
(render-link
(lambda (&rest args)
(if (find-package :tg)
(progn
(funcall (intern "GC" "TG") :full t)
(mark-dirty summary-widget)
(do-information "Success"))
(do-information "trivial-garbage package not found")))
"free unused memory")
(:br)))))))
(setf (widget-children root)
(make-navigation
"toplevel"
(list "Application summary"
summary-widget nil)
(list "Debug sessions"
(make-instance 'composite
:widgets (list
(make-widget
(lambda (&rest args)
(with-html
(:h1 "Debug sessions")
(:span :style "font-family:monospace"
"[ℹ] "
(:i "This is debug tree with applications (1st level), application sessions (2nd level) and application session values (3rd level)"))
(:br)
(:br))))
tree-grid)) "debug-sessions")
(list "Debug page"
(eval
`(make-navigation
"debug page"
,@(loop for i in weblocks::*active-webapps*
unless (string= "debug-app" (weblocks::weblocks-webapp-name i))
collect `(list (weblocks::weblocks-webapp-name ,i)
(make-instance
'composite
:widgets (list
(make-widget
(lambda (&rest args)
(with-html
(:h1 "Debug page"))
(let ((*html-parts-debug-app* ,(alexandria:make-keyword
(string-upcase
(weblocks::weblocks-webapp-name i)))))
(if (with-debugged-app-md5-hash
(ignore-errors (weblocks-util:get-html-parts-root-hash)))
(progn
(with-html
(:span :style "font-family:monospace"
"[ℹ] "
(:i "This is debug tree with rendered page parts from last page render."))
(:div :id "eval-message" :style "display:none;font-family:monospace"
(:br)
"[ℹ] "
(:i "Evaluate "
(:br)
" "
(:b (str (ps:ps
((ps:@ window open)
(ps:LISP ,(format nil "/debug-app/debug-page/~A" (weblocks::webapp-name i)))))))
(:br)
(cl-who:fmt
"on your Weblocks-powered page of application ~A to receive debugging page mirror below (useful for visual debugging). This page then can be closed." ,(weblocks::webapp-name i))))
(:br)
(:br))
(render-debug-page-tree))
(with-html
(:span :style "font-family:monospace"
"[ℹ] "
(:i "It seems "
(:b (str (weblocks::weblocks-webapp-name ,i)))
" is not started yet. Please open its url in browser")))))))))
(attributize-name (weblocks::weblocks-webapp-name ,i))))))
"debug-page")))))
(defun start-debug-app (&rest args)
(apply #'start-weblocks args)
(start-webapp 'debug-app))
(defmethod tree-data ((obj tree-widget))
(loop for i in (all-of 'webapp-cls :store *list-store*)
collect (list
:item i
:children (loop for j in
(find-by-values 'session-cls
:webapp (cons i (lambda (item1 item2)
(string= (object-id item1) (object-id item2))))
:store *list-store*) append
(let ((item (list :item j
:children
(loop for i in
(find-by-values
'session-data-cls
:session (cons j (lambda (item1 item2)
(string= (object-id item1) (object-id item2))))
:store *list-store*)
collect (list :item i)))))
(list item))))))