Skip to content

Commit

Permalink
[Kibana] Tweak Pagination (#735)
Browse files Browse the repository at this point in the history
This introduces Lombok to the codebase and removes most of `RestEntry` due to now-unnecessary getters and setters.
  • Loading branch information
pickypg authored Sep 8, 2024
1 parent eccc36c commit 70b6b7a
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 201 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,37 +99,39 @@ private List<String> getKibanaSpacesIds(DiagnosticContext context) throws Diagno
* @return Number of HTTP request that will be executed.
*/
public int runBasicQueries(RestClient client, DiagnosticContext context, List<String> spacesIds) throws DiagnosticException {
int totalRetries = 0;
List<RestEntry> queries = new ArrayList<>();

for (Map.Entry<String, RestEntry> entry : context.elasticRestCalls.entrySet()) {
RestEntry current = entry.getValue();
if(current.isSpaceAware()) {
for(String spaceId : spacesIds) {
RestEntry tmp = new RestEntry(current);
if (current.isSpaceAware()) {
for (String spaceId : spacesIds) {
RestEntry restEntry = current;

// The calls made for the default Space will be written without a subpath
if(!spaceId.equals("default")) {
tmp.url = String.format("/s/%s%s", spaceId, tmp.getUrl());
// Sanitizing the spaceId to make it "safe" for a filepath
tmp.subdir = Paths.get(tmp.subdir, "space_" + spaceId.replaceAll("[^a-zA-Z0-9-_]", "_")).normalize().toString();
if (!"default".equals(spaceId)) {
String url = String.format("/s/%s%s", spaceId, restEntry.getUrl());
String subdir = Paths.get(
restEntry.getSubdir(),
"space_" + spaceId.replaceAll("[^a-zA-Z0-9-_]", "_")
).normalize().toString();

restEntry = current.copyWithNewUrl(url, subdir);
}
if(current.isPageable()) {
getAllPages(client, queries, context.perPage, tmp, current.getPageableFieldName());

if (restEntry.isPageable()) {
getAllPages(client, queries, context.perPage, restEntry, current.getPageableFieldName());
} else {
queries.add(tmp);
queries.add(restEntry);
}
}
} else if (current.isPageable()) {
getAllPages(client, queries, context.perPage, entry.getValue(), current.getPageableFieldName());
} else {
if(current.isPageable()) {
getAllPages(client, queries, context.perPage, entry.getValue(), current.getPageableFieldName());
} else {
queries.add(entry.getValue());
}
queries.add(entry.getValue());
}
}
totalRetries = runQueries(client, queries, context.tempDir, 0, 0);

return totalRetries;
return runQueries(client, queries, context.tempDir, 0, 0);
}

private String getPageUrl(RestEntry action, int page, int perPage, String perPageField) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void execExtract(MonitoringExportInputs inputs) throws DiagnosticExceptio
RestEntryConfig builder = new RestEntryConfig(version);
Map restCalls = JsonYamlUtils.readYamlFromClasspath(Constants.MONITORING_REST, true);
Map<String, RestEntry> versionedRestCalls = builder.buildEntryMap(restCalls);
monitoringUri = versionedRestCalls.get("monitoring-uri").url;
monitoringUri = versionedRestCalls.get("monitoring-uri").getUrl();

if (inputs.listClusters) {
logger.info(Constants.CONSOLE, "Displaying a list of available clusters.");
Expand Down Expand Up @@ -226,9 +226,9 @@ private void runExportQueries(String tempDir, RestClient client, MonitoringExpor
List<String> statsFields = config.getStatsByType(inputs.type);

String monitoringScroll = Long.toString(config.monitoringScrollSize);
String monitoringStartUri = restCalls.get("monitoring-start-scroll-uri").url;
String metricbeatStartUri = restCalls.get("metricbeat-start-scroll-uri").url;
String monitoringScrollUri = restCalls.get("monitoring-scroll-uri").url;
String monitoringStartUri = restCalls.get("monitoring-start-scroll-uri").getUrl();
String metricbeatStartUri = restCalls.get("metricbeat-start-scroll-uri").getUrl();
String monitoringScrollUri = restCalls.get("monitoring-scroll-uri").getUrl();

for (String stat : statsFields) {
String startUri;
Expand Down
108 changes: 30 additions & 78 deletions src/main/java/co/elastic/support/rest/RestEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,96 +6,48 @@
*/
package co.elastic.support.rest;

import co.elastic.support.util.SystemProperties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lombok.Getter;

@Getter
public class RestEntry {

private static final Logger logger = LogManager.getLogger(RestEntry.class);

public static final String MISSING = "missing";

private final String name;
private final String url;
private final String subdir;
private final String extension;
private final boolean retry;
private final boolean showErrors;
private final String pageableFieldName;
private final boolean pageable;
private final boolean spaceAware;

public RestEntry(String name, String subdir, String extension, boolean retry, String url, boolean showErrors) {
this(name, subdir, extension, retry, url, showErrors, null, false);
}

public RestEntry(
String name,
String subdir,
String extension,
boolean retry,
String url,
boolean showErrors,
String pageableFieldName,
boolean spaceAware
) {
this.name = name;
this.subdir = subdir;
this.extension = extension;
this.retry = retry;
this.url = url;
this.showErrors = showErrors;
this.pageableFieldName = pageableFieldName;
this.pageable = pageableFieldName != null;
this.spaceAware = spaceAware;
}

// Copy constructor
public RestEntry(RestEntry other) {
this.name = other.name;
this.subdir = other.subdir;
this.extension = other.extension;
this.retry = other.retry;
this.url = other.url;
this.showErrors = other.showErrors;
this.isPageable = other.isPageable;
this.pageableFieldName = other.pageableFieldName;
this.isSpaceAware = other.isSpaceAware;
}

public String name;

public String getName() {
return name;
}

public String url;

public String getUrl() {
return url;
}

public String subdir = SystemProperties.fileSeparator;

public String getSubdir() {
return subdir;
}

public String extension = "json";

public String getExtension() {
return extension;
}

public boolean retry = false;

public boolean isRetry() {
return retry;
}

public boolean showErrors = true;

private String pageableFieldName = null;

private boolean isPageable = false;

private boolean isSpaceAware = false;

public boolean isSpaceAware() {
return isSpaceAware;
}

public boolean isPageable() {
return isPageable;
}

public String getPageableFieldName() {
return pageableFieldName;
}

public void setSpaceAware(boolean isSpaceAware) {
this.isSpaceAware = isSpaceAware;
}

public void setPageableFieldName(String pageableFieldName) {
if(pageableFieldName != null) {
this.pageableFieldName = pageableFieldName;
this.isPageable = true;
}
public RestEntry copyWithNewUrl(String url, String subdir) {
return new RestEntry(name, subdir, extension, retry, url, showErrors, pageableFieldName, spaceAware);
}
}
58 changes: 31 additions & 27 deletions src/main/java/co/elastic/support/rest/RestEntryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,51 @@ public Map<String, RestEntry> buildEntryMap(Map<String, Object> config) {
return entries;
}

@SuppressWarnings("unchecked")
private RestEntry build(Map.Entry<String, Object> entry) {
String name = entry.getKey();
Map<String, Object> values = (Map) entry.getValue();
Map<String, Object> values = (Map<String, Object>) entry.getValue();

// only some diagnostics provide a mode (currently only Elasticsearch)
// currently "tags" is a simple string, but if we ever need it to be an
// array, then naturally this will need to change
if ("full".equals(mode) == false && mode.equals(values.get("tags")) == false) {
return null;
}

RestEntry tmp = new RestEntry(
name,
(String) ObjectUtils.defaultIfNull(values.get("subdir"), ""),
(String) ObjectUtils.defaultIfNull(values.get("extension"), ".json"),
(Boolean) ObjectUtils.defaultIfNull(values.get("retry"), false),
RestEntry.MISSING,
(Boolean) ObjectUtils.defaultIfNull(values.get("showErrors"), true)
);
Map<String, Object> versions = (Map) values.get("versions");
populateVersionSpecificUrlAndModifiers(versions, tmp);

return tmp;

return buildRestEntryForVersion(entry.getKey(), values);
}

private void populateVersionSpecificUrlAndModifiers(Map<String, Object> versions, RestEntry entry) {
@SuppressWarnings("unchecked")
private RestEntry buildRestEntryForVersion(String name, Map<String, Object> entry) {
String subdir = (String) ObjectUtils.defaultIfNull(entry.get("subdir"), "");
String extension = (String) ObjectUtils.defaultIfNull(entry.get("extension"), ".json");
boolean retry = (boolean) ObjectUtils.defaultIfNull(entry.get("retry"), false);
boolean showErrors = (boolean) ObjectUtils.defaultIfNull(entry.get("showErrors"), true);

Map<String, Object> versions = (Map<String, Object>) entry.get("versions");

for (Map.Entry<String, Object> urlVersion : versions.entrySet()) {
if (semver.satisfies(urlVersion.getKey())) {
// We allow it to be String,String or String,Map(url,paginate,spaceaware)
if(urlVersion.getValue() instanceof Map) {
Map<String, Object> info = (Map) urlVersion.getValue();
entry.url = (String) ObjectUtils.defaultIfNull(info.get("url"), RestEntry.MISSING);
entry.setPageableFieldName((String)ObjectUtils.defaultIfNull(info.get("paginate"), null));
entry.setSpaceAware((boolean)ObjectUtils.defaultIfNull(info.get("spaceaware"), false));
return;
} else if (urlVersion.getValue() instanceof String){
entry.url = (String) urlVersion.getValue();
return;
if (urlVersion.getValue() instanceof String) {
return new RestEntry(name, subdir, extension, retry, (String) urlVersion.getValue(), showErrors);
// We allow it to be String,String or String,Map(url,paginate,spaceaware)
} else if (urlVersion.getValue() instanceof Map) {
Map<String, Object> info = (Map<String, Object>) urlVersion.getValue();

String url = (String) ObjectUtils.defaultIfNull(info.get("url"), null);

if (url == null) {
throw new RuntimeException("Undefined URL for REST entry (route)");
}

String pageableFieldName = (String) ObjectUtils.defaultIfNull(info.get("paginate"), null);
boolean spaceAware = (boolean) ObjectUtils.defaultIfNull(info.get("spaceaware"), false);

return new RestEntry(name, subdir, extension, retry, url, showErrors, pageableFieldName, spaceAware);
}
}
}
}

return null;
}
}
Loading

0 comments on commit 70b6b7a

Please sign in to comment.