Skip to content

Commit

Permalink
test: test revert ToXContent serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranshu-S committed Jul 11, 2024
1 parent 6362613 commit 9c984a5
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions server/src/main/java/org/opensearch/indices/NodeIndicesStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,33 +299,27 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.startObject(Fields.INDICES);
stats.toXContent(builder, params);

if (Fields.INDICES.equals(level)) {
if (statsByIndex == null && statsByShard != null) {
statsByIndex = createStatsByIndex(statsByShard);
}
if ("indices".equals(level)) {
Map<Index, CommonStats> indexStats = createStatsByIndex();
builder.startObject(Fields.INDICES);
if (statsByIndex != null) {
for (Map.Entry<Index, CommonStats> entry : statsByIndex.entrySet()) {
builder.startObject(entry.getKey().getName());
entry.getValue().toXContent(builder, params);
builder.endObject();
}
for (Map.Entry<Index, CommonStats> entry : indexStats.entrySet()) {
builder.startObject(entry.getKey().getName());
entry.getValue().toXContent(builder, params);
builder.endObject();
}
builder.endObject();
} else if (Fields.SHARDS.equals(level)) {
} else if ("shards".equals(level)) {
builder.startObject("shards");
if (statsByShard != null) {
for (Map.Entry<Index, List<IndexShardStats>> entry : statsByShard.entrySet()) {
builder.startArray(entry.getKey().getName());
for (IndexShardStats indexShardStats : entry.getValue()) {
builder.startObject().startObject(String.valueOf(indexShardStats.getShardId().getId()));
for (ShardStats shardStats : indexShardStats.getShards()) {
shardStats.toXContent(builder, params);
}
builder.endObject().endObject();
for (Map.Entry<Index, List<IndexShardStats>> entry : statsByShard.entrySet()) {
builder.startArray(entry.getKey().getName());
for (IndexShardStats indexShardStats : entry.getValue()) {
builder.startObject().startObject(String.valueOf(indexShardStats.getShardId().getId()));
for (ShardStats shardStats : indexShardStats.getShards()) {
shardStats.toXContent(builder, params);
}
builder.endArray();
builder.endObject().endObject();
}
builder.endArray();
}
builder.endObject();
}
Expand All @@ -351,6 +345,23 @@ private Map<Index, CommonStats> createStatsByIndex(Map<Index, List<IndexShardSta
return statsMap;
}

private Map<Index, CommonStats> createStatsByIndex() {
Map<Index, CommonStats> statsMap = new HashMap<>();
for (Map.Entry<Index, List<IndexShardStats>> entry : statsByShard.entrySet()) {
if (!statsMap.containsKey(entry.getKey())) {
statsMap.put(entry.getKey(), new CommonStats());
}

for (IndexShardStats indexShardStats : entry.getValue()) {
for (ShardStats shardStats : indexShardStats.getShards()) {
statsMap.get(entry.getKey()).add(shardStats.getStats());
}
}
}

return statsMap;
}

public List<IndexShardStats> getShardStats(Index index) {
if (statsByShard == null) {
return null;
Expand Down

0 comments on commit 9c984a5

Please sign in to comment.