Skip to content

Instantly share code, notes, and snippets.

@ableasdale
Last active July 28, 2016 09:21
Show Gist options
  • Select an option

  • Save ableasdale/466e247858f88cad3fbe to your computer and use it in GitHub Desktop.

Select an option

Save ableasdale/466e247858f88cad3fbe to your computer and use it in GitHub Desktop.
MarkLogic Forest Counts with Rebalancer preview
xquery version "1.0-ml";
declare namespace f = "http://marklogic.com/xdmp/status/forest";
declare variable $DATABASE as xs:string := xdmp:get-request-field("db", "Documents");
declare function local:create-bootstrap-page($title as xs:string, $content as element()){
element html { attribute lang {"en"},
element head {
element meta { attribute charset {"utf-8"}},
element meta { attribute http-equiv {"X-UA-Compatible"}, attribute content {"IE=edge"} },
element meta { attribute name {"viewport"}, attribute content {"width=device-width, initial-scale=1"} },
element title {$title},
element link {
attribute rel {"stylesheet"},
attribute href {"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"},
attribute integrity {"sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"},
attribute crossorigin {"anonymous"}
},
element link {
attribute rel {"stylesheet"},
attribute href {"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"},
attribute integrity {"sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r"},
attribute crossorigin {"anonymous"}
}
},
element body {
$content,
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">{" "}</script>,
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous">{" "}</script>
}
}
};
declare function local:page-header(){
element div {attribute class {"row"},
element div {attribute class {"col-md-9"}, element h3 {"Forest counts (rebalancer preview) ", element small {$DATABASE}}},
element div {attribute class {"col-md-3"}, attribute style {"padding-top:1em;"}, local:database-select()}
}
};
declare function local:database-select() {
element div { attribute class {"dropdown"},
element button {
attribute class {"btn btn-default dropdown-toggle pull-right"},
attribute type {"button"},
attribute id {"database-select"},
attribute data-toggle {"dropdown"},
attribute aria-haspopup { "true" },
attribute aria-expanded {"true"},
"Choose database ", element span {attribute class {"caret"}}
},
element ul { attribute class {"dropdown-menu"}, attribute aria-labelledby {"database-select"},
element li { attribute class {"dropdown-header"}, "Available Databases:"},
for $x in xdmp:database-name(xdmp:databases())
return element li { element a { attribute href {concat("?db=",$x)}, $x}}
}
}
};
declare function local:rebalancer-preview() {
element div { attribute class {"container"}, element div { attribute class {"row"},
local:page-header(),
element table { attribute class {"table table-striped table-bordered"},
element thead {
element tr {
for $i in ("Source Forest", "Active Fragments", "Deleted Fragments", "Target Forests", "Total to be moved") return element th {$i}
}
},
element tbody {
for $f in xdmp:forest-counts(xdmp:database-forests(xdmp:database($DATABASE)), (), ("preview-rebalancer"))
return
element tr {
element td {$f/f:forest-name/fn:string(.)},
element td {fn:sum ($f//f:active-fragment-count)},
element td {fn:sum ($f//f:deleted-fragment-count)},
element td {
element dl { attribute class { "dl-horizontal" },
for $c in $f//f:rebalance-fragment-count[f:fragment-count != 0]
order by number($c/f:fragment-count) descending
return (element dt {$c/f:rebalance-destination}, element dd {$c/f:fragment-count})
}
},
element td {fn:sum($f//f:rebalance-fragment-count[*:fragment-count != 0]/f:fragment-count)}}
}
}
}
}
};
xdmp:set-response-content-type("text/html; charset=utf-8"),
'<!DOCTYPE html>',
local:create-bootstrap-page("Rebalancer preview", local:rebalancer-preview())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment