Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EQK-29] Added Query Statistic Page #37

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.exadel.etoolbox.querykit.core.models.query;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class QueryInfo {

private static final Pattern PROPERTY_INDEX_PATTERN = Pattern.compile("/\\*\\sproperty\\s([^\\s=]+)[=\\s]");
private static final Pattern LUCENE_INDEX_PATTERN = Pattern.compile("/\\*\\slucene:([^\\s*]+)[\\s*]");

private QueryManager queryManager;
@Setter
@Getter
private String statement;
@Setter
@Getter
private String language;
@Setter
@Getter
private String threadName;
@Setter
@Getter
private String lastExecuted;
@Setter
@Getter
private int executeCount;
private Set<String> indexes;

public QueryInfo(QueryManager queryManager) {
this.queryManager = queryManager;
}

public Set<String> getIndexes() throws IOException {
if (indexes != null) {
return indexes;
}
try {
indexes = new HashSet<>();
Query query = queryManager.createQuery("explain " + statement, language);
QueryResult queryResult = query.execute();
Row row = queryResult.getRows().nextRow();
String queryPlan = row.getValue("plan").getString();

Stream.of(PROPERTY_INDEX_PATTERN, LUCENE_INDEX_PATTERN).forEach(pattern -> {
Matcher matcher = pattern.matcher(queryPlan);
while (matcher.find()) {
indexes.add(matcher.group(1).trim().replaceAll("\\(.+\\)", StringUtils.EMPTY));
}
});
} catch (Exception e) {
indexes = Collections.emptySet();
}
return indexes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package com.exadel.etoolbox.querykit.core.models.query;

import com.exadel.etoolbox.querykit.core.utils.Constants;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

import javax.annotation.PostConstruct;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

@Model(adaptables = SlingHttpServletRequest.class)
@Slf4j
public class QueryStatisticsModel {

private static final Pattern PROPERTY_INDEX_PATTERN = Pattern.compile("/\\*\\sproperty\\s([^\\s=]+)[=\\s]");
private static final Pattern LUCENE_INDEX_PATTERN = Pattern.compile("/\\*\\slucene:([^\\s*]+)[\\s*]");

private static final String OAK_INDEX_PREFIX = "/oak:index";
private static final String STRIP_CHARS = " /";

@SlingObject
private ResourceResolver resourceResolver;

@Getter
private List<QueryInfo> allQueries;
@Getter
private Map<String, Integer> indexesByUsage = new LinkedHashMap<>();
@Getter
private Map<String, List<QueryInfo>> queriesByIndex = new HashMap<>();
@Getter
private LinkedHashMap<String, List<QueryInfo>> queriesByIndexSorted = new LinkedHashMap<>();

@PostConstruct
private void init() throws IOException {

QueryManager queryManager = getQueryManager(resourceResolver);
MBeanServerConnection server = ManagementFactory.getPlatformMBeanServer();

allQueries = getQueryInfo(server, queryManager);

for (QueryInfo queryInfo : allQueries) {
if (queryInfo.getIndexes().isEmpty()) {
continue;
}
queryInfo.getIndexes().forEach(index -> queriesByIndex.computeIfAbsent(index, k -> new ArrayList<>()).add(queryInfo));
}

queriesByIndex.entrySet().stream()
.sorted((a, b) -> Integer.compare(a.getValue().size(), b.getValue().size()) * -1)
.forEach(entry -> queriesByIndexSorted.put(entry.getKey(), entry.getValue()));

getIndexes(resourceResolver).forEach(index -> indexesByUsage.put(
index,
queriesByIndex.entrySet().stream()
.filter(entry -> {
String indexId = getIndexId(index);
return entry.getKey().equals(indexId) || entry.getKey().endsWith(indexId + Constants.CLOSING_BRACKET);
})
.map(entry -> entry.getValue().size())
.findFirst().orElse(0)));
}

private QueryManager getQueryManager(ResourceResolver resourceResolver) {
Session session = resourceResolver.adaptTo(Session.class);
if (session == null) {
return null;
}
try {
return session.getWorkspace().getQueryManager();
} catch (RepositoryException e) {
log.error("Can't get QueryManager from the session", e);
return null;
}
}

private List<QueryInfo> getQueryInfo(MBeanServerConnection server, QueryManager queryManager) {
ObjectName queryStatMbean = getQueryStatMBean(server);
if (queryStatMbean == null || queryManager == null) {
return Collections.emptyList();
}
String jsonRaw;
List<QueryInfo> result = new ArrayList<>();

try {
jsonRaw = server.invoke(queryStatMbean, "asJson", null, null).toString();

} catch (Exception e) {
return Collections.emptyList();
}

JsonParser jsonParser = new JsonParser();
JsonArray queries = jsonParser.parse(jsonRaw).getAsJsonArray();

for(int i = 0; i < queries.size(); i++) {
JsonObject jsonObject = queries.get(i).getAsJsonObject();
QueryInfo queryInfo = getQueryInfo(queryManager, jsonObject);
if (queryInfo != null) {
result.add(queryInfo);
}
}
result.sort((a, b) -> Integer.compare(a.getExecuteCount(), b.getExecuteCount()) * -1);
return result;
}

private QueryInfo getQueryInfo(QueryManager queryManager, JsonObject jsonObject) {
String statement = getAsString(jsonObject, "query");
if (StringUtils.contains(statement, "oak-internal") || StringUtils.startsWithIgnoreCase(statement, "explain ")) {
return null;
}
String language = getAsString(jsonObject, "language");
String thread = getAsString(jsonObject, "lastThreadName");
String lastExecuted = getAsString(jsonObject, "lastExecutedMillis");
int executeCount = getAsInt(jsonObject, "executeCount");
QueryInfo queryInfo = new QueryInfo(queryManager);
queryInfo.setLanguage(language);
queryInfo.setStatement(statement);
queryInfo.setThreadName(thread);
queryInfo.setExecuteCount(executeCount);
queryInfo.setLastExecuted(lastExecuted);
return queryInfo;
}

private ObjectName getQueryStatMBean(MBeanServerConnection server) {
try {
Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.jackrabbit.oak:type=QueryStats,*"), null);
return names.iterator().next();
} catch (IOException | MalformedObjectNameException | NoSuchElementException e) {
log.warn("Can't get 'org.apache.jackrabbit.oak:type=QueryStats,*'", e);
return null;
}
}

private List<String> getIndexes(ResourceResolver resourceResolver) {
Resource oakIndex = resourceResolver.getResource(OAK_INDEX_PREFIX);
if (oakIndex == null || !oakIndex.hasChildren()) {
return Collections.emptyList();
}
return StreamSupport.stream(oakIndex.getChildren().spliterator(), false)
.map(Resource::getPath)
.sorted()
.collect(Collectors.toList());
}

private String getIndexId(String value) {
if (value.contains(Constants.OPENING_BRACKET) && value.contains(Constants.CLOSING_BRACKET)) {
return StringUtils.substringBefore(value, Constants.OPENING_BRACKET).trim();
} else if (StringUtils.containsIgnoreCase(value, OAK_INDEX_PREFIX)) {
return StringUtils.strip(StringUtils.substringAfter(value, OAK_INDEX_PREFIX), STRIP_CHARS);
}
return StringUtils.strip(value, STRIP_CHARS);
}

private String getAsString(JsonObject object, String property) {
try {
return object.get(property).getAsString();
} catch (UnsupportedOperationException e) {
return StringUtils.EMPTY;
}
}

private int getAsInt(JsonObject object, String property) {
try {
return object.get(property).getAsInt();
} catch (UnsupportedOperationException e) {
return 0;
}
}

public Map<String, Integer> getIndexesByUsage() {
Map<String,Integer> map = new LinkedHashMap<>();
indexesByUsage.forEach((key, value) -> map.put(StringUtils.removeStart(key, "/oak:index/"), value));
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="cq:Page">
<jcr:content
jcr:mixinTypes="[sling:VanityPath]"
jcr:primaryType="nt:unstructured"
jcr:title="Oak Query Index Usage"
sling:vanityPath="/etoolbox-query-kit/oak-query-index-usage"
sling:resourceType="granite/ui/components/shell/page">
<head jcr:primaryType="nt:unstructured">
<clientlibs
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/includeclientlibs"
categories="[etoolbox-query-kit.queryStatistics]"/>
</head>
<title
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/text"
text="Oak Query Index Usage"/>
<actions jcr:primaryType="nt:unstructured">
<primary jcr:primaryType="nt:unstructured">
<back
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/anchorbutton"
href="/etoolbox-query-kit.html"
text="Back"
variant="primary"/>
</primary>
</actions>
<content
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container"
margin="{Boolean}true">
<items jcr:primaryType="nt:unstructured">
<result
jcr:primaryType="nt:unstructured"
sling:resourceType="/apps/etoolbox-query-kit/components/console/queryStatistics"/>
</items>
</content>
</jcr:content>
</jcr:root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:ClientLibraryFolder"
categories="[etoolbox-query-kit.queryStatistics]"
dependencies="[coralui2,moment]"
jsProcessor="[min:gcc;languageIn=ECMASCRIPT_2015,compilationLevel=advanced,languageOut=ECMASCRIPT5]"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#base=css
main-layout.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
.foundation-layout-panel-content {
padding-left: 20px;
padding-right: 20px;
}

.query-statistics-headlines {
text-align: center;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#base=js
query-statistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function (document, $, ns) {
'use strict';

$(document).on('click', '.active-index', function (index) {
$('#tab-queries-by-index')[0].click();
let indexId = this.getAttribute('value');
setTimeout((function() {
$(`#${indexId}`)[0].scrollIntoView();
}), 0)
});
})(document, Granite.$, Granite.Eqk = (Granite.Eqk || {}));
Loading