@@ -34,7 +34,7 @@ declare variable $FORMAT-JSON := "json";
3434declare %private variable $current-trace-settings := map:map ();
3535
3636declare %private variable $current-trace := map:new ((
37- map:entry ("trace-id " , xdmp:random ()),
37+ map:entry ("traceId " , xdmp:random ()),
3838 map:entry ("created" , fn:current-dateTime ())
3939));
4040
@@ -74,16 +74,17 @@ declare function trace:write-trace()
7474 xdmp:to-json ($current-trace)
7575 else
7676 element trace {
77- element trace-id { map:get ($current-trace, "trace-id" ) },
77+ element format { map:get ($current-trace, "format" ) },
78+ element traceId { map:get ($current-trace, "traceId" ) },
7879 element created { map:get ($current-trace, "created" ) },
7980 element identifier { map:get ($current-trace, "identifier" ) },
80- element flow- type { map:get ($current-trace, "flow-type " ) },
81- for $key in ("collector-plugin " , "content-plugin " , "headers-plugin " , "triples-plugin " , "writer-plugin " )
81+ element flowType { map:get ($current-trace, "flowType " ) },
82+ for $key in ("collectorPlugin " , "contentPlugin " , "headersPlugin " , "triplesPlugin " , "writerPlugin " )
8283 let $m := map:get ($current-trace, $key)
8384 return
8485 if (fn:exists ($m)) then
8586 element { $key } {
86- element plugin-module-uri { map:get ($m, "plugin-module-uri " ) },
87+ element pluginModuleUri { map:get ($m, "pluginModuleUri " ) },
8788 element input { map:get ($m, "input" ) },
8889 element output { map:get ($m, "output" ) },
8990 element duration { map:get ($m, "duration" ) }
@@ -99,10 +100,10 @@ declare function trace:write-trace()
99100 declare variable $trace external;
100101
101102 xdmp:document-insert(
102- "/" || $trace/trace-id ,
103+ "/" || $trace/traceId ,
103104 $trace,
104105 xdmp:default-permissions(),
105- ($trace/*:type)
106+ ("trace", $trace/*:type)
106107 )
107108 ' ,
108109 map:new ((
@@ -119,60 +120,74 @@ declare function trace:plugin-trace(
119120 $identifier,
120121 $module-uri,
121122 $plugin-type as xs:string,
122- $flow-type as xs:string,
123+ $flowType as xs:string,
123124 $input,
124125 $output,
125126 $duration as xs:dayTimeDuration)
126127{
128+ let $format :=
129+ let $o :=
130+ if ($input instance of document-node ()) then
131+ $input/node ()
132+ else
133+ $input
134+ return
135+ typeswitch ($o)
136+ case element () return "xml"
137+ case object-node () return "json"
138+ case array-node () return "json"
139+ default return "xml"
140+ return
141+ map:put ($current-trace, "format" , $format),
127142 map:put ($current-trace, "identifier" , $identifier),
128- map:put ($current-trace, "flow-type " , $flow-type ),
143+ map:put ($current-trace, "flowType " , $flowType ),
129144 let $plugin-map := map:new ((
130- map:entry ("plugin-module-uri " , $module-uri),
145+ map:entry ("pluginModuleUri " , $module-uri),
131146 map:entry ("input" , $input),
132147 map:entry ("output" , $output),
133148 map:entry ("duration" , $duration)
134149 ))
135150 return
136- map:put ($current-trace, $plugin-type || "-plugin " , $plugin-map)
151+ map:put ($current-trace, $plugin-type || "Plugin " , $plugin-map)
137152};
138153
139154declare function trace:error-trace (
140155 $identifier as xs:string?,
141156 $module-uri as xs:string,
142157 $plugin-type as xs:string,
143- $flow-type as xs:string,
158+ $flowType as xs:string,
144159 $input,
145160 $error as element (error:error),
146161 $duration as xs:dayTimeDuration)
147162{
163+ let $format :=
164+ let $o :=
165+ if ($input instance of document-node ()) then
166+ $input/node ()
167+ else
168+ $input
169+ return
170+ typeswitch ($o)
171+ case element () return "xml"
172+ case object-node () return "json"
173+ case array-node () return "json"
174+ default return "xml"
175+ return
176+ map:put ($current-trace, "format" , $format),
148177 map:put ($current-trace, "identifier" , $identifier),
149- map:put ($current-trace, "flow-type " , $flow-type ),
178+ map:put ($current-trace, "flowType " , $flowType ),
150179 map:put ($current-trace-settings, "_has_errors" , fn:true ()),
151180 let $plugin-map := map:new ((
152- map:entry ("plugin-module-uri " , $module-uri),
181+ map:entry ("pluginModuleUri " , $module-uri),
153182 map:entry ("input" , $input),
154183 map:entry ("error" , $error),
155184 map:entry ("duration" , $duration)
156185 ))
157- let $_ := map:put ($current-trace, $plugin-type || "-plugin " , $plugin-map)
186+ let $_ := map:put ($current-trace, $plugin-type || "Plugin " , $plugin-map)
158187 let $_ := trace:write-trace ()
159188 return ()
160189};
161190
162- declare function trace:camel-case ( $str as xs:string ) as xs:string
163- {
164- if ( fn:contains ($str , "-" ) ) then
165- let $subs := fn:tokenize ($str,"-" )
166- return
167- fn:string-join ((
168- $subs[1 ],
169- for $s in $subs[ fn:position () gt 1 ]
170- return (
171- fn:upper-case ( fn:substring ($s , 1 , 1 )) , fn:lower-case (fn:substring ($s,2 )))),"" )
172- else $str
173-
174- };
175-
176191declare function trace:_walk_json ($nodes as node ()* ,$o)
177192{
178193 let $quote-options :=
@@ -184,26 +199,56 @@ declare function trace:_walk_json($nodes as node()* ,$o)
184199 for $n in $nodes
185200 return
186201 typeswitch ($n)
202+ case array-node () return
203+ let $name as xs:string := fn:string (fn:node-name ($n))
204+ return
205+ map:put ($o, $name, xdmp:quote ($n))
206+ case object-node () return
207+ let $oo := json:object ()
208+ let $name as xs:string := fn:string (fn:node-name ($n))
209+ return
210+ if ($name = "input" ) then
211+ if ($n/node ()) then
212+ let $_ :=
213+ for $x in $n/node ()
214+ let $nn := fn:string (fn:node-name ($x))
215+ return
216+ map:put ($oo, $nn, xdmp:quote ($x, $quote-options))
217+ return
218+ map:put ($o, $name, $oo)
219+ else
220+ map:put ($o, $name, null-node {})
221+ else if ($name = "output" ) then
222+ map:put ($o, $name, xdmp:quote ($n, $quote-options))
223+ else
224+ let $_ := trace:_walk_json ($n/node (), $oo)
225+ return
226+ map:put ($o, $name, $oo)
227+ case number-node () |
228+ boolean-node () |
229+ null-node () |
230+ text () return
231+ map:put ($o, fn:string (fn:node-name ($n)), fn:data ($n))
187232 case element (input) return
188233 let $oo := json:object ()
189234 let $_ :=
190235 for $x in $n/*
191236 return
192- map:put ($oo, trace:camel-case ( fn:local-name ($x) ), xdmp:quote ($x, $quote-options))
237+ map:put ($oo, fn:local-name ($x), xdmp:quote ($x, $quote-options))
193238 return
194239 map:put ($o, "input" , $oo)
195240 case element (output) return
196- map:put ($o, trace:camel-case ( fn:local-name ($n) ), xdmp:quote ($n/node (), $quote-options))
241+ map:put ($o, fn:local-name ($n), xdmp:quote ($n/node (), $quote-options))
197242 case element (duration) return
198243 map:put ($o, "duration" , fn:seconds-from-duration (xs:dayTimeDuration ($n)))
199244 case element () return
200245 if ($n/*) then
201246 let $oo := json:object ()
202247 let $_ := trace:_walk_json ($n/*, $oo)
203248 return
204- map:put ($o, trace:camel-case ( fn:local-name ($n) ), $oo)
249+ map:put ($o, fn:local-name ($n), $oo)
205250 else
206- map:put ($o, trace:camel-case ( fn:local-name ($n) ), fn:data ($n))
251+ map:put ($o, fn:local-name ($n), fn:data ($n))
207252 default return
208253 $n
209254};
@@ -212,7 +257,14 @@ declare function trace:trace-to-json($trace)
212257{
213258 if ($trace instance of element ()) then
214259 let $o := json:object ()
215- let $_ := trace:_walk_json ($trace/*, $o)
260+ let $walk-me :=
261+ let $n := $trace/node ()
262+ return
263+ if ($n instance of object-node ()) then
264+ $n/node ()
265+ else
266+ $n
267+ let $_ := trace:_walk_json ($walk-me, $o)
216268 return
217269 $o
218270 else
@@ -221,15 +273,26 @@ declare function trace:trace-to-json($trace)
221273
222274declare function trace:trace-to-json-slim ($trace)
223275{
224- let $o := json:object ()
225- let $_ := (
226- map:put ($o, "traceId" , $trace/trace-id/string ()),
227- map:put ($o, "created" , $trace/created/string ()),
228- map:put ($o, "identifier" , $trace/identifier/string ()),
229- map:put ($o, "flowType" , $trace/flow-type /string ())
230- )
231- return
232- $o
276+ (:if ($trace instance of element()) then:)
277+ let $o := json:object ()
278+ let $_ := (
279+ map:put ($o, "traceId" , $trace/traceId/string ()),
280+ map:put ($o, "created" , $trace/created/string ()),
281+ map:put ($o, "identifier" , $trace/identifier/string ()),
282+ map:put ($o, "flowType" , $trace/flowType/string ())
283+ )
284+ return
285+ $o
286+ (: else
287+ let $o := json:object()
288+ let $_ := (
289+ map:put($o, "traceId", $trace/traceId),
290+ map:put($o, "created", $trace/created),
291+ map:put($o, "identifier", $trace/identifier),
292+ map:put($o, "flowType", $trace/flowType)
293+ )
294+ return
295+ $o:)
233296};
234297
235298declare function trace:find-traces (
@@ -239,11 +302,16 @@ declare function trace:find-traces(
239302{
240303 let $options :=
241304 <options xmlns = "http://marklogic.com/appservices/search" >
305+ <additional-query>{cts:collection-query ("trace" )}</additional-query>
242306 <return-results>false </return-results>
243307 <return-facets>true </return-facets>
308+ <sort-order type = "xs:dateTime"
309+ direction = "descending" >
310+ <element ns = "" name = "created" />
311+ </sort-order>
244312 </options>
245313 let $query := search:parse ($q)
246- let $count := xdmp :estimate (cts:search ( fn:doc (), cts:query ($query)) )
314+ let $count := search :estimate ($query, $options )
247315 let $start := ($page - 1 ) * $page-length + 1
248316 let $end := fn:min (($start + $page-length - 1 , $count))
249317 let $results :=
@@ -283,19 +351,24 @@ declare function trace:get-traces($page as xs:int, $page-length as xs:int)
283351
284352declare function trace:get-trace ($id as xs:string)
285353{
286- trace:trace-to-json (/trace[trace-id = $id])
354+ let $query :=
355+ cts:or-query ((
356+ cts:element-range-query (xs:QName ("traceId" ), "=" , $id, ("collation=http://marklogic.com/collation/codepoint" )),
357+ cts:json-property-range-query ("traceId" , "=" , $id, ("collation=http://marklogic.com/collation/codepoint" ))
358+ ))
359+ return
360+ trace:trace-to-json (cts:search (fn:doc (), $query)[1 ])
287361};
288362
289- declare function trace:get-trace-ids ($q as xs:string?)
363+ declare function trace:get-traceIds ($q as xs:string?)
290364{
291365 let $query :=
292366 if ($q) then
293367 cts:element-value-query (xs:QName ("identifier" ), fn:lower-case ($q) || "*" , "wildcarded" )
294368 else ()
295- let $_ := xdmp:log ($query)
296369 let $results :=
297370 cts:element-value-co-occurrences (
298- xs:QName ("trace-id " ),
371+ xs:QName ("traceId " ),
299372 xs:QName ("identifier" ),
300373 (
301374 "limit=10" ,
0 commit comments