Refactor SearchEngine logging for the Developer Panel
This commit is contained in:
parent
6fae3550a2
commit
4ffb7bce87
1 changed files with 65 additions and 34 deletions
|
@ -2,10 +2,13 @@
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.searchengine;
|
package edu.cornell.mannlib.vitro.webapp.searchengine;
|
||||||
|
|
||||||
|
import static edu.cornell.mannlib.vitro.webapp.utils.developer.Key.SEARCH_DELETIONS_ENABLE;
|
||||||
|
import static edu.cornell.mannlib.vitro.webapp.utils.developer.Key.SEARCH_ENGINE_ENABLE;
|
||||||
|
import static edu.cornell.mannlib.vitro.webapp.utils.developer.Key.SEARCH_INDEX_ENABLE;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@ -30,28 +33,58 @@ public abstract class SearchEngineLogger implements AutoCloseable {
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
// Factory
|
// Factory
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
public static SearchEngineLogger doAdd(SearchInputDocument[] docs) {
|
public static SearchEngineLogger doAdd(SearchInputDocument[] docs) {
|
||||||
|
if (isEnabled(SEARCH_INDEX_ENABLE)) {
|
||||||
return new AddLogger(Arrays.asList(docs));
|
return new AddLogger(Arrays.asList(docs));
|
||||||
|
} else {
|
||||||
|
return new DisabledLogger();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SearchEngineLogger doAdd(Collection<SearchInputDocument> docs) {
|
public static SearchEngineLogger doAdd(Collection<SearchInputDocument> docs) {
|
||||||
|
if (isEnabled(SEARCH_INDEX_ENABLE)) {
|
||||||
return new AddLogger(docs);
|
return new AddLogger(docs);
|
||||||
|
} else {
|
||||||
|
return new DisabledLogger();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SearchEngineLogger doDeleteById(String[] ids) {
|
public static SearchEngineLogger doDeleteById(String[] ids) {
|
||||||
|
if (isEnabled(SEARCH_DELETIONS_ENABLE)) {
|
||||||
return new DeleteIdsLogger(Arrays.asList(ids));
|
return new DeleteIdsLogger(Arrays.asList(ids));
|
||||||
|
} else {
|
||||||
|
return new DisabledLogger();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SearchEngineLogger doDeleteById(Collection<String> ids) {
|
public static SearchEngineLogger doDeleteById(Collection<String> ids) {
|
||||||
|
if (isEnabled(SEARCH_DELETIONS_ENABLE)) {
|
||||||
return new DeleteIdsLogger(ids);
|
return new DeleteIdsLogger(ids);
|
||||||
|
} else {
|
||||||
|
return new DisabledLogger();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SearchEngineLogger doDeleteByQuery(String query) {
|
public static SearchEngineLogger doDeleteByQuery(String query) {
|
||||||
|
if (isEnabled(SEARCH_DELETIONS_ENABLE)) {
|
||||||
return new DeleteQueryLogger(query);
|
return new DeleteQueryLogger(query);
|
||||||
|
} else {
|
||||||
|
return new DisabledLogger();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SearchEngineLogger doQuery(SearchQuery query) {
|
public static SearchEngineLogger doQuery(SearchQuery query) {
|
||||||
|
if (isEnabled(SEARCH_ENGINE_ENABLE)) {
|
||||||
return new QueryLogger(query);
|
return new QueryLogger(query);
|
||||||
|
} else {
|
||||||
|
return new DisabledLogger();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isEnabled(Key enableKey) {
|
||||||
|
return log.isInfoEnabled()
|
||||||
|
&& DeveloperSettings.getInstance().getBoolean(enableKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
@ -59,12 +92,9 @@ public abstract class SearchEngineLogger implements AutoCloseable {
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
private final long startTime;
|
private final long startTime;
|
||||||
protected final boolean enabled;
|
|
||||||
|
|
||||||
public SearchEngineLogger(Key enableKey) {
|
public SearchEngineLogger() {
|
||||||
this.startTime = System.currentTimeMillis();
|
this.startTime = System.currentTimeMillis();
|
||||||
this.enabled = log.isInfoEnabled()
|
|
||||||
&& DeveloperSettings.getInstance().getBoolean(enableKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected float elapsedSeconds() {
|
protected float elapsedSeconds() {
|
||||||
|
@ -74,7 +104,6 @@ public abstract class SearchEngineLogger implements AutoCloseable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
if (enabled) {
|
|
||||||
try {
|
try {
|
||||||
writeToLog();
|
writeToLog();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -82,8 +111,7 @@ public abstract class SearchEngineLogger implements AutoCloseable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@SuppressWarnings("unused")
|
||||||
|
|
||||||
public void setSearchResponse(SearchResponse response) {
|
public void setSearchResponse(SearchResponse response) {
|
||||||
throw new UnsupportedOperationException(this.getClass().getSimpleName()
|
throw new UnsupportedOperationException(this.getClass().getSimpleName()
|
||||||
+ " does not support setSearchResponse()");
|
+ " does not support setSearchResponse()");
|
||||||
|
@ -100,20 +128,13 @@ public abstract class SearchEngineLogger implements AutoCloseable {
|
||||||
private final boolean passesRestrictions;
|
private final boolean passesRestrictions;
|
||||||
|
|
||||||
AddLogger(Collection<SearchInputDocument> docs) {
|
AddLogger(Collection<SearchInputDocument> docs) {
|
||||||
super(Key.SEARCH_INDEX_ENABLE);
|
|
||||||
|
|
||||||
this.docs = restrictDocsByUriOrName(docs);
|
this.docs = restrictDocsByUriOrName(docs);
|
||||||
|
this.passesRestrictions = passesDocumentRestriction()
|
||||||
this.passesRestrictions = enabled && passesDocumentRestriction()
|
|
||||||
&& this.docs.size() > 0;
|
&& this.docs.size() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<SearchInputDocument> restrictDocsByUriOrName(
|
private List<SearchInputDocument> restrictDocsByUriOrName(
|
||||||
Collection<SearchInputDocument> rawDocs) {
|
Collection<SearchInputDocument> rawDocs) {
|
||||||
if (!enabled) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
|
|
||||||
String restriction = DeveloperSettings.getInstance().getString(
|
String restriction = DeveloperSettings.getInstance().getString(
|
||||||
Key.SEARCH_INDEX_URI_OR_NAME_RESTRICTION);
|
Key.SEARCH_INDEX_URI_OR_NAME_RESTRICTION);
|
||||||
if (restriction.isEmpty()) {
|
if (restriction.isEmpty()) {
|
||||||
|
@ -198,14 +219,13 @@ public abstract class SearchEngineLogger implements AutoCloseable {
|
||||||
private final List<String> ids;
|
private final List<String> ids;
|
||||||
|
|
||||||
DeleteIdsLogger(Collection<String> ids) {
|
DeleteIdsLogger(Collection<String> ids) {
|
||||||
super(Key.SEARCH_DELETIONS_ENABLE);
|
|
||||||
this.ids = new ArrayList<>(ids);
|
this.ids = new ArrayList<>(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeToLog() {
|
public void writeToLog() {
|
||||||
log.info(String.format(
|
log.info(String.format(
|
||||||
"%8.3f deleted these %d search documents: %s\n",
|
"%8.3f deleted these %d search documents: %s",
|
||||||
elapsedSeconds(), ids.size(), StringUtils.join(ids, ", ")));
|
elapsedSeconds(), ids.size(), StringUtils.join(ids, ", ")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -214,7 +234,6 @@ public abstract class SearchEngineLogger implements AutoCloseable {
|
||||||
private final String query;
|
private final String query;
|
||||||
|
|
||||||
DeleteQueryLogger(String query) {
|
DeleteQueryLogger(String query) {
|
||||||
super(Key.SEARCH_DELETIONS_ENABLE);
|
|
||||||
this.query = query;
|
this.query = query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,14 +253,13 @@ public abstract class SearchEngineLogger implements AutoCloseable {
|
||||||
private SearchResponse response;
|
private SearchResponse response;
|
||||||
|
|
||||||
QueryLogger(SearchQuery query) {
|
QueryLogger(SearchQuery query) {
|
||||||
super(Key.SEARCH_ENGINE_ENABLE);
|
|
||||||
this.query = query;
|
this.query = query;
|
||||||
this.stackTrace = new StackTraceUtility(SearchEngineWrapper.class,
|
this.stackTrace = new StackTraceUtility(SearchEngineWrapper.class,
|
||||||
enabled);
|
true);
|
||||||
this.passesRestrictions = enabled && passesQueryRestriction()
|
this.passesRestrictions = passesQueryRestriction()
|
||||||
&& passesStackRestriction();
|
&& passesStackRestriction();
|
||||||
log.debug("QueryLogger: enabled=" + enabled + ", query=" + query
|
log.debug("QueryLogger: query=" + query + ", passes="
|
||||||
+ ", passes=" + passesRestrictions);
|
+ passesRestrictions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean passesStackRestriction() {
|
private boolean passesStackRestriction() {
|
||||||
|
@ -294,4 +312,17 @@ public abstract class SearchEngineLogger implements AutoCloseable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class DisabledLogger extends SearchEngineLogger {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSearchResponse(SearchResponse response) {
|
||||||
|
// Does nothing.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void writeToLog() {
|
||||||
|
// Does nothing.
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue