Skip to content

Commit

Permalink
Refactor the getRequest method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rabi Panda committed May 29, 2020
1 parent dc42e60 commit 6e22177
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions src/main/java/com/o19s/es/ltr/rest/RestLTRStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.o19s.es.ltr.action.LTRStatsAction.LTRStatsNodesRequest;
import com.o19s.es.ltr.stats.LTRStats;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestActions;
Expand All @@ -14,7 +13,10 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
Expand Down Expand Up @@ -57,48 +59,52 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
}

private LTRStatsNodesRequest getRequest(RestRequest request) {
Set<String> validStats = ltrStats.getStats().keySet();
LTRStatsNodesRequest ltrStatsRequest =
new LTRStatsNodesRequest(splitCommaSeparatedParam(request, "nodeId"));
LTRStatsNodesRequest ltrStatsRequest = new LTRStatsNodesRequest(
splitCommaSeparatedParam(request, "nodeId").orElse(null));
ltrStatsRequest.timeout(request.param("timeout"));

String[] stats = splitCommaSeparatedParam(request, "stat");
Set<String> statsSet = stats != null ? new HashSet<>(Arrays.asList(stats)) : Collections.emptySet();
List<String> requestedStats =
splitCommaSeparatedParam(request, "stat")
.map(Arrays::asList)
.orElseGet(Collections::emptyList);

if (isAllStatsRequested(statsSet)) {
Set<String> validStats = ltrStats.getStats().keySet();
if (isAllStatsRequested(requestedStats)) {
ltrStatsRequest.addAll(validStats);
} else {
if (statsSet.contains(LTRStatsNodesRequest.ALL_STATS_KEY)) {
throw new IllegalArgumentException(
"Request " + request.path() + " contains " + LTRStatsNodesRequest.ALL_STATS_KEY + " and individual stats");
}
Set<String> invalidStats = new HashSet<>();
for (String stat : statsSet) {
if (validStats.contains(stat)) {
ltrStatsRequest.addStat(stat);
} else {
invalidStats.add(stat);
}
}
if (!invalidStats.isEmpty()) {
throw new IllegalArgumentException(
unrecognized(request, invalidStats, ltrStatsRequest.getStatsToBeRetrieved(), "stat"));
}
ltrStatsRequest.addAll(getStatsToBeRetrieved(request, validStats, requestedStats));
}

return ltrStatsRequest;
}

private boolean isAllStatsRequested(Set<String> statsSet) {
private Set<String> getStatsToBeRetrieved(
RestRequest request, Set<String> validStats, List<String> requestedStats) {
if (requestedStats.contains(LTRStatsNodesRequest.ALL_STATS_KEY)) {
throw new IllegalArgumentException(
String.format(Locale.getDefault(), "Request %s contains both %s and individual stats",
request.path(), LTRStatsNodesRequest.ALL_STATS_KEY));
}

Set<String> invalidStats =
requestedStats.stream()
.filter(s -> !validStats.contains(s))
.collect(Collectors.toSet());

if (!invalidStats.isEmpty()) {
throw new IllegalArgumentException(
unrecognized(request, invalidStats, new HashSet<>(requestedStats), "stat"));
}
return new HashSet<>(requestedStats);
}

private boolean isAllStatsRequested(List<String> statsSet) {
return statsSet.isEmpty()
|| (statsSet.size() == 1 && statsSet.contains(LTRStatsNodesRequest.ALL_STATS_KEY));
}

private String[] splitCommaSeparatedParam(RestRequest request, String paramName) {
String[] arr = null;
String str = request.param(paramName);
if (!Strings.isEmpty(str)) {
arr = str.split(",");
}
return arr;
private Optional<String[]> splitCommaSeparatedParam(RestRequest request, String paramName) {
return Optional.ofNullable(request.param(paramName))
.map(s -> s.split(","));
}
}

0 comments on commit 6e22177

Please sign in to comment.