Skip to content

Commit

Permalink
Adding log scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
Silvano Ravotto committed Mar 2, 2021
1 parent c871334 commit 4533320
Show file tree
Hide file tree
Showing 12 changed files with 891 additions and 162 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
samples
24 changes: 24 additions & 0 deletions api/plan-from-log.xqy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import module namespace qputils = "http://marklogic.com/optic/qputils" at "../lib/qputils.xqy";

xdmp:set-response-content-type("application/json"),
let $file := xdmp:get-request-field("file")
let $id := xdmp:get-request-field("id")
let $type := xdmp:get-request-field("type", "estimate")


let $id := "sessionKey=" || $id || " "

let $regex := switch ($type)
case "estimate" return ("Event:id=Optic Plan Trace" || ".*" || $id || "plan=")
case "execution" return ("Event:id=Optic Execution Diagnostics Trace" || ".*" || $id )
case "optimization" return ("Optic Optimization Trace"|| ".*" || $id )
default return fn:error("QV-ARG", "Invalid type")


let $res := xdmp:logfile-scan($file, $regex)
let $res := if ($type = ("optimization"))
then qputils:parseOptimization ($res ! fn:substring-after(., $id))
else qputils:makeGraph(xdmp:unquote(fn:substring-after($res, "
"))/*,"N")


return json:to-array($res)
35 changes: 35 additions & 0 deletions api/scan-for-plans.xqy
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

xdmp:set-response-content-type("application/json"),

array-node {
let $file := xdmp:get-request-field("file")
let $trace := xdmp:get-request-field("trace")
let $regex := xdmp:get-request-field("regex", "( plan=| diagnostic_plan=)")
let $regex :=
if ($trace)
then ("trace=" || $trace || ".*" || $regex )
else $regex
let $start := xdmp:get-request-field("start")
let $start :=
if ($start)
then xs:dateTime (fn:substring(xdmp:logfile-scan($file, (), (), (), (), 1),1,10) || "T" || $start)
else ()
let $end := ()
let $limit := 20
for $e in xdmp:logfile-scan($file, $regex, "s", $start, $end, $limit)
let $type := if (fn:contains($e, "diagnostic_plan")) then "execution" else "estimate"
let $trace :=
if (fn:contains($e, "trace="))
then fn:substring-before(fn:substring-after($e, " trace="), " sessionKey=")
else ""
let $key :=
if ($type eq "estimate")
then fn:substring-before(fn:substring-after($e, " sessionKey="), " plan=")
else fn:substring-before(fn:substring-after($e, " sessionKey="), " diagnostic_plan=")
let $time := fn:substring($e, 1, 23)
return
object-node { "time" : $time, "key" : $key, "trace": $trace }

}


Binary file added favicon.ico
Binary file not shown.
55 changes: 55 additions & 0 deletions lib/qputils.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -866,3 +866,58 @@ declare function makeScripts($in as element())
</script>
)
};


declare function qputils:normalize ($string) {
let $res := fn:normalize-space($string)
return
if (fn:starts-with($res, "("))
then fn:substring($res, 2, fn:string-length($string) -2)
else $res
};

declare function qputils:parseCost ($line, $obj) {
let $line := qputils:normalize ($line)
let $cost := fn:substring-before($line,"crd:")
let $card := fn:substring-before(fn:substring-after($line,"crd:["),"]")
let $_ := map:put($obj, "cardinalities", $card)
let $_ :=
for $token in fn:tokenize($cost,",")
let $t := fn:tokenize($token, ":")
let $name := $t[1]
let $name :=
if ($name = ("mem","dmem","nw","io"))
then ($name|| "-cost")
else if (fn:starts-with($name,"dcpu")) then "dcpu-cost"
else if (fn:starts-with($name,"cpu")) then "cpu-cost"
else if ($name = "m") then "cost"
else if ($name = "c") then "estimated-count"
else if ($name = "r") then "rule-count"
else $name
return map:put($obj,$name,qputils:normalize($t[2]))
return ()
};

declare function qputils:parseOptimization ($lines) {
for $line at $j in $lines
let $tokens := fn:tokenize($line, " ")
let $name := if (fn:contains($line, "initialCost")) then "start"
else if (fn:contains($line, "bestCost")) then "end"
else "iteration " || ( $j - 1)
let $obj := map:new() => map:with("_id", "node_" || $j) => map:with("_name", $name)
let $_ := if ($j gt 1) then map:put($obj,"_parent", "node_" || $j - 1) else ()

let $res :=
for $token in $tokens
let $t := fn:tokenize($token, "=")
let $tagname :=
switch ($t[1])
case "t" return "temperature"
case "r" return "repeats"
case "c" return "cool"
default return $t[1]

where fn:matches($name, "^\w")
return if (fn:starts-with($t[2], "(")) then qputils:parseCost($t[2], $obj) else map:put($obj,$tagname,$t[2])
return ($obj)
};
57 changes: 57 additions & 0 deletions log.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<html>

<head>
<script type="text/javascript" src="ui/lib/d3.v6.min.js">< !-- --></script>
<script type="text/javascript" src="ui/qv.js">< !-- --></script>
<link rel="stylesheet" href="ui/css/w3-theme.css"><!-- -->
<link rel="stylesheet" href="ui/css/w3.css"><!-- -->
<link rel="stylesheet" href="ui/css/style.css"><!-- -->
</link>

<meta charset="UTF-8">


</head>

<body>
<div id="wrapper" class="w3-container w3-theme-l6 full-height" >
<div class="w3-col w3-theme-l1 full-height" style="width:200px">
<div id="form" class="w3-row">
<form class="w3-container w3-tiny">
<label>File</label>
<input class="w3-input w3-border-0" type="text" name="file" id="form_file"
value="">
<label>Start time</label>
<input class="w3-input w3-border-0" type="text" name="start" id="form_start">
<label>Trace id</label>
<input class="w3-input w3-border-0" type="text" name="trace" id="form_trace">
<div class="w3-padding-16 w3-center" >
<button type="button" class="w3-button w3-round-large w3-theme-action"
onclick="qv_scanLogForPlans('#plans','#form_file','#form_start', '#form_trace', '#viewer')">Load
Plans
</button>
</div>
</form>
</div>
<div id="plans" class="w3-row">
<!-- -->
</div>

</div>
<div class="w3-rest">

<div id="viewer">
<div id="tabs" class="qv-tabs w3-row w3-theme-l1">
<!-- -->
</div>
<div id="tooltip" class="tooltip">

</div>
</div>
</div>


</div>
</body>

</html>
29 changes: 29 additions & 0 deletions samples.xqy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
xdmp:set-response-content-type("text/html"),

let $dir := xdmp:modules-root() || "samples/"
return
element html {
element body {
element h2 { "Samples" },
for $group in xdmp:filesystem-directory ($dir)/dir:entry
let $entries := xdmp:filesystem-directory($group/dir:pathname)/dir:entry[fn:ends-with(./dir:filename, ".xml")]
return
(
element h3 { $group/dir:filename},
element table {
attribute id {"box-table-a"},
for $entry in $entries
order by $entry/dir:filename
return
element tr {
element td {
element a { attribute href {"show.xqy?filename=" || $entry/dir:pathname} ,
$entry/dir:filename/fn:string(.)
}
}
}
}
)
}
}

20 changes: 16 additions & 4 deletions show.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,29 @@ declare function local:makeHTML($out)
input = { xdmp:quote(json:to-array($out)), xdmp:log(json:to-array($out)) }
</script>
</head>
<body onload="qv_init('#viewer', input);">
<div id="viewer" >
<div id="tooltip" class="tooltip"> <pre> </pre></div>


<body onload="qv_showPlan('#viewer', input);">
<div id="wrapper" class="full-height">

<div id="viewer">
<div id="tooltip" class="tooltip"> <!-- --></div>
</div>
</div>

</body>
</html>

};


let $in := xdmp:unquote (xdmp:get-request-field("plan") )/*

let $file := xdmp:get-request-field("filename")
let $plan := xdmp:get-request-field("plan")

let $in := if ($file) then xdmp:document-get($file)/*
else if ($plan) then xdmp:unquote (xdmp:get-request-field("plan"))/*
else fn:error(xs:QName("XDMP-ARG"), "Missing argument: filename or plan required")

return (
xdmp:set-response-content-type("text/html"),
Expand Down
90 changes: 85 additions & 5 deletions ui/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
margin-left: 2px;
text-align: left;
color: #505050;
line-height: 1.0;
}

#viewer {
overflow: "auto";
}


body {
background: #F8F8F8;
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0
height: 100%;
}

.tooltip {
Expand All @@ -73,7 +73,87 @@ body {
z-index: 5
}

.tooltip table {
border-collapse: collapse;
white-space: pre-wrap;
font-size: 1em;
}
.tooltip table > tr > td {
border: 1px solid goldenrod;
}
.tooltip table > tr:first-child > td {
border-top: 0;
}
.tooltip table > tr > td:first-child {
border-left: 0;
}
.tooltip table > tr:last-child > td {
border-bottom: 0;
}
.tooltip table > tr > td:last-child {
border-right: 0;
}

pre {
white-space: pre-wrap;
font-size: .80em;
}


table.plantable {
background-color: #FFFFFF;
border-collapse: collapse;
border-width: 1px;
border-color: steelblue;
border-style: solid;
color: #000000;
}

table.plantable td, table.plantable th {
border-width: 1px;
border-color: steelblue;
border-style: solid;
padding: 5px;
}

table.plantable thead {
background-color: whitesmoke;
}



table.criteria td, table.criteria th {
padding: 5px;
}

.full-height {
height: 100%;
}

.qv-tabs {
height: 34px;
}
.qv-banner {
height: 100%;
}

.qv-vertical-center {
margin: 0;
position: absolute;
top: 70px;

}


#viewer {
overflow: auto;
max-width: 100%;
height: 100%;
}

#wrapper {
width: 100%;
margin: 0;
background: #F8F8F8;
}

22 changes: 22 additions & 0 deletions ui/css/w3-theme.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.w3-theme-l5 {color:#000 !important; background-color:#fbfdfe !important}
.w3-theme-l4 {color:#000 !important; background-color:#f3f8fc !important}
.w3-theme-l3 {color:#000 !important; background-color:#e6f1f9 !important}
.w3-theme-l2 {color:#000 !important; background-color:#dae9f6 !important}
.w3-theme-l1 {color:#000 !important; background-color:#cee2f3 !important}
.w3-theme-d1 {color:#000 !important; background-color:#9fc7e8 !important}
.w3-theme-d2 {color:#000 !important; background-color:#7cb2df !important}
.w3-theme-d3 {color:#fff !important; background-color:#599ed7 !important}
.w3-theme-d4 {color:#fff !important; background-color:#368ace !important}
.w3-theme-d5 {color:#fff !important; background-color:#2a73ae !important}

.w3-theme-light {color:#000 !important; background-color:#fbfdfe !important}
.w3-theme-dark {color:#fff !important; background-color:#2a73ae !important}
.w3-theme-action {color:#fff !important; background-color:#2a73ae !important}

.w3-theme {color:#000 !important; background-color:#c2dbf0 !important}
.w3-text-theme {color:#c2dbf0 !important}
.w3-border-theme {border-color:#c2dbf0 !important}

.w3-hover-theme:hover {color:#000 !important; background-color:#c2dbf0 !important}
.w3-hover-text-theme:hover {color:#c2dbf0 !important}
.w3-hover-border-theme:hover {border-color:#c2dbf0 !important}
Loading

0 comments on commit 4533320

Please sign in to comment.