Skip to content

Commit

Permalink
Implement ToXContent for UploadStats
Browse files Browse the repository at this point in the history
UploadStats will contain request count and metrics as
response. This will be part of StatsNodeResponse.

Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
  • Loading branch information
VijayanB committed May 13, 2022
1 parent 316c64a commit f2360e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,34 @@
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;

import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;
import org.opensearch.common.metrics.CounterMetric;
import org.opensearch.common.xcontent.ToXContentObject;
import org.opensearch.common.xcontent.XContentBuilder;

/**
* Contains the total upload stats
*/
public final class UploadStats implements Writeable {
public final class UploadStats implements Writeable, ToXContentObject {

private static final UploadStats instance = new UploadStats();

public enum FIELDS {
METRICS,
REQUEST_COUNT;

@Override
public String toString() {
return name().toLowerCase(Locale.getDefault());
}
}

private final Set<UploadMetric> metrics;
private final CounterMetric totalAPICount;

Expand Down Expand Up @@ -94,4 +107,17 @@ public void writeTo(StreamOutput output) throws IOException {
output.writeVLong(getTotalAPICount());
output.writeCollection(metrics);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(FIELDS.REQUEST_COUNT.toString(), getTotalAPICount());
builder.startArray(FIELDS.METRICS.toString());
for (UploadMetric metric : metrics) {
builder.startObject();
metric.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
package org.opensearch.geospatial.stats.upload;

import static org.opensearch.geospatial.GeospatialTestHelper.GEOJSON;
import static org.opensearch.geospatial.GeospatialTestHelper.buildFieldNameValuePair;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.IntStream;

import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.xcontent.ToXContent;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.geospatial.GeospatialTestHelper;
import org.opensearch.test.OpenSearchTestCase;

Expand Down Expand Up @@ -82,4 +87,19 @@ public void testStreams() throws IOException {
assertEquals("api count is ", stats.getTotalAPICount(), serializedStats.getTotalAPICount());
assertEquals("failed to serialize metrics", stats.getMetrics().size(), serializedStats.getMetrics().size());
}

public void testToXContent() throws IOException {
UploadStats stats = UploadStatsBuilder.randomUploadStats();
XContentBuilder statsContent = XContentFactory.jsonBuilder().startObject();
String statsAsString = Strings.toString(stats.toXContent(statsContent, ToXContent.EMPTY_PARAMS).endObject());
assertNotNull(statsAsString);
assertTrue(statsAsString.contains(buildFieldNameValuePair(UploadStats.FIELDS.REQUEST_COUNT.toString(), stats.getTotalAPICount())));
stats.getMetrics().forEach(uploadMetric -> {
assertTrue(statsAsString.contains(buildFieldNameValuePair(UploadMetric.FIELDS.TYPE, GEOJSON)));
assertTrue(statsAsString.contains(buildFieldNameValuePair(UploadMetric.FIELDS.UPLOAD, uploadMetric.getUploadCount())));
assertTrue(statsAsString.contains(buildFieldNameValuePair(UploadMetric.FIELDS.DURATION, uploadMetric.getDuration())));
assertTrue(statsAsString.contains(buildFieldNameValuePair(UploadMetric.FIELDS.FAILED, uploadMetric.getFailedCount())));
assertTrue(statsAsString.contains(buildFieldNameValuePair(UploadMetric.FIELDS.SUCCESS, uploadMetric.getSuccessCount())));
});
}
}

0 comments on commit f2360e5

Please sign in to comment.