-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
ee50076
d912dd6
5fde987
7f377c5
5d88c3d
66028db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
package com.exadel.etoolbox.querykit.core.models.query; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import lombok.Getter; | ||
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) | ||
@Getter | ||
public class QueryStatisticModel { | ||
|
||
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; | ||
|
||
private List<QueryStatisticModel.QueryInfo> allQueries; | ||
private Map<String, Integer> indexesByUsage = new LinkedHashMap<>(); | ||
private Map<String, List<QueryStatisticModel.QueryInfo>> queriesByIndex = new HashMap<>(); | ||
private LinkedHashMap<String, List<QueryStatisticModel.QueryInfo>> queriesByIndexSorted = new LinkedHashMap<>(); | ||
|
||
@PostConstruct | ||
protected void init() throws IOException { | ||
|
||
QueryManager queryManager = getQueryManager(resourceResolver); | ||
MBeanServerConnection server = ManagementFactory.getPlatformMBeanServer(); | ||
|
||
allQueries = getQueryInfo(server, queryManager); | ||
|
||
for (QueryStatisticModel.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 + ")"); | ||
}) | ||
.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) { | ||
return null; | ||
} | ||
} | ||
|
||
private List<QueryStatisticModel.QueryInfo> getQueryInfo(MBeanServerConnection server, QueryManager queryManager) { | ||
ObjectName queryStatMbean = getQueryStatMBean(server); | ||
if (queryStatMbean == null || queryManager == null) { | ||
return Collections.emptyList(); | ||
} | ||
String jsonRaw; | ||
List<QueryStatisticModel.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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The content of this loop is kind of asking for extracting into a separate method) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted) |
||
String statement = getAsString(jsonObject, "query"); | ||
if (StringUtils.contains(statement, "oak-internal") || StringUtils.startsWithIgnoreCase(statement, "explain ")) { | ||
continue; | ||
} | ||
String language = getAsString(jsonObject, "language"); | ||
String thread = getAsString(jsonObject, "lastThreadName"); | ||
String lastExecuted = getAsString(jsonObject, "lastExecutedMillis"); | ||
int executeCount = getAsInt(jsonObject, "executeCount"); | ||
|
||
QueryStatisticModel.QueryInfo queryInfo = new QueryStatisticModel.QueryInfo(queryManager); | ||
queryInfo.setLanguage(language); | ||
queryInfo.setStatement(statement); | ||
queryInfo.setTheadName(thread); | ||
queryInfo.setExecuteCount(executeCount); | ||
queryInfo.setLastExecuted(lastExecuted); | ||
result.add(queryInfo); | ||
} | ||
result.sort((a, b) -> Integer.compare(a.getExecuteCount(), b.getExecuteCount()) * -1); | ||
return result; | ||
} | ||
|
||
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) { | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to add logging here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
} | ||
} | ||
|
||
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("(") && value.contains(")")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use Constants.OPENING_BRACKET, etc. (and please review other places when existing constants could be used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
return StringUtils.substringBefore(value, "(").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 static class QueryInfo { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
private QueryManager queryManager; | ||
private String statement; | ||
private String language; | ||
private String threadName; | ||
private String lastExecuted; | ||
private int executeCount; | ||
private Set<String> indexes; | ||
|
||
private QueryInfo(QueryManager queryManager) { | ||
this.queryManager = queryManager; | ||
} | ||
|
||
public String getStatement() { | ||
return statement; | ||
} | ||
private void setStatement(String value) { | ||
statement = value; | ||
} | ||
|
||
public String getLanguage() { | ||
return language; | ||
} | ||
private void setLanguage(String value) { | ||
language = value; | ||
} | ||
|
||
public String getThreadName() { | ||
return threadName; | ||
} | ||
private void setTheadName(String value) { | ||
threadName = value; | ||
} | ||
|
||
public String getLastExecuted() { | ||
return lastExecuted; | ||
} | ||
private void setLastExecuted(String value) { | ||
lastExecuted = value; | ||
} | ||
|
||
public int getExecuteCount() { | ||
return executeCount; | ||
} | ||
private void setExecuteCount(int value) { | ||
executeCount = value; | ||
} | ||
|
||
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; | ||
} | ||
} | ||
|
||
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,31 @@ | ||
<?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="[cq.common.wcm,cq.listview.coral.columns.personalization,cq.siteadmin.admin.page.row,cq.gui.coral.common.admin.references,cq.screens.dcc,etoolbox-query-kit.queryStatistic]"/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need all these categories here? They look like too much There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimized) |
||
</head> | ||
<title | ||
jcr:primaryType="nt:unstructured" | ||
sling:resourceType="granite/ui/components/coral/foundation/text" | ||
text="Oak Query Index Usage"/> | ||
<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/queryStatistic"/> | ||
</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"?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let us move to the main clientlibs folder. The rest of css/js in one place. I frankly see no big reason fo these two rules to be in another |
||
<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.queryStatistic]" | ||
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-statistic-headlines { | ||
text-align: center; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed