Skip to content

Commit 45dc457

Browse files
committed
update PR based on comments
Signed-off-by: Chenyang Ji <cyji@amazon.com>
1 parent 4fd6b0f commit 45dc457

File tree

7 files changed

+13
-24
lines changed

7 files changed

+13
-24
lines changed

src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020
import java.util.Map;
2121
import java.util.concurrent.LinkedBlockingQueue;
22+
import java.util.stream.Collectors;
2223
import org.apache.logging.log4j.LogManager;
2324
import org.apache.logging.log4j.Logger;
2425
import org.opensearch.client.Client;
@@ -449,10 +450,9 @@ protected void doClose() throws IOException {
449450
* @return QueryInsightsHealthStats
450451
*/
451452
public QueryInsightsHealthStats getHealthStats() {
452-
Map<MetricType, TopQueriesHealthStats> topQueriesHealthStatsMap = new HashMap<>();
453-
for (Map.Entry<MetricType, TopQueriesService> entry : topQueriesServices.entrySet()) {
454-
topQueriesHealthStatsMap.put(entry.getKey(), entry.getValue().getHealthStats());
455-
}
453+
Map<MetricType, TopQueriesHealthStats> topQueriesHealthStatsMap = topQueriesServices.entrySet()
454+
.stream()
455+
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getHealthStats()));
456456
return new QueryInsightsHealthStats(
457457
threadPool.info(QUERY_INSIGHTS_EXECUTOR),
458458
this.queryRecordsQueue.size(),

src/main/java/org/opensearch/plugin/insights/rules/model/healthStats/QueryGrouperHealthStats.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
public class QueryGrouperHealthStats implements ToXContentFragment, Writeable {
2222
private final int queryGroupCount;
2323
private final int queryGroupHeapSize;
24-
private static final String QUERY_GROUP_COUNT = "QueryGroupCount";
25-
private static final String QUERY_GROUP_HEAP_SIZE = "QueryGroupHeapSize";
24+
private static final String QUERY_GROUP_COUNT_TOTAL = "QueryGroupCount_Total";
25+
private static final String QUERY_GROUP_COUNT_MAX_HEAP = "QueryGroupCount_MaxHeap";
2626

2727
/**
2828
* Constructor to read QueryGrouperHealthStats from a StreamInput.
@@ -67,8 +67,8 @@ public void writeTo(StreamOutput out) throws IOException {
6767
*/
6868
@Override
6969
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
70-
builder.field(QUERY_GROUP_COUNT, queryGroupCount);
71-
builder.field(QUERY_GROUP_HEAP_SIZE, queryGroupHeapSize);
70+
builder.field(QUERY_GROUP_COUNT_TOTAL, queryGroupCount);
71+
builder.field(QUERY_GROUP_COUNT_MAX_HEAP, queryGroupHeapSize);
7272
return builder;
7373
}
7474

src/test/java/org/opensearch/plugin/insights/core/service/TopQueriesServiceTests.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ public void testRollingWindowsWithDifferentGroup() {
149149
}
150150

151151
public void testGetHealthStats_EmptyService() {
152-
// Get the health stats from an empty TopQueriesService
153152
TopQueriesHealthStats healthStats = topQueriesService.getHealthStats();
154153
// Validate the health stats
155154
assertNotNull(healthStats);
@@ -160,16 +159,13 @@ public void testGetHealthStats_EmptyService() {
160159
}
161160

162161
public void testGetHealthStats_WithData() {
163-
// Add some mock records to the TopQueriesService
164162
List<SearchQueryRecord> records = QueryInsightsTestUtils.generateQueryInsightRecords(2);
165163
topQueriesService.consumeRecords(records);
166-
// Get the health stats after adding data
167164
TopQueriesHealthStats healthStats = topQueriesService.getHealthStats();
168-
// Validate the health stats
169165
assertNotNull(healthStats);
170166
assertEquals(2, healthStats.getTopQueriesHeapSize()); // Since we added two records
171167
assertNotNull(healthStats.getQueryGrouperHealthStats());
172-
// Assuming no grouping by default, expect QueryGroupCount to be 2
168+
// Assuming no grouping by default, expect QueryGroupCount to be 0
173169
assertEquals(0, healthStats.getQueryGrouperHealthStats().getQueryGroupCount());
174170
}
175171
}

src/test/java/org/opensearch/plugin/insights/core/service/grouper/MinMaxHeapQueryGrouperTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,6 @@ public void testGetHealthStatsWithGroups() {
694694
QueryGrouperHealthStats healthStats = minMaxHeapQueryGrouper.getHealthStats();
695695
// Verify that group count stats reflect the correct number of total groups
696696
assertEquals(2, healthStats.getQueryGroupCount());
697-
// Verify that heap size reflect the correct number of groups in max heap
698697
assertEquals(0, healthStats.getQueryGroupHeapSize());
699698
}
700699

src/test/java/org/opensearch/plugin/insights/rules/model/healthStats/QueryGrouperHealthStatsTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,19 @@ public void testSerialization() throws IOException {
3838
// Read from StreamInput
3939
StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
4040
QueryGrouperHealthStats deserializedStats = new QueryGrouperHealthStats(in);
41-
// Assert equality
4241
assertEquals(stats.getQueryGroupCount(), deserializedStats.getQueryGroupCount());
4342
assertEquals(stats.getQueryGroupHeapSize(), deserializedStats.getQueryGroupHeapSize());
4443
}
4544

4645
public void testToXContent() throws IOException {
4746
QueryGrouperHealthStats stats = new QueryGrouperHealthStats(queryGroupCount, queryGroupHeapSize);
48-
// Write to XContent
4947
XContentBuilder builder = XContentFactory.jsonBuilder();
5048
builder.startObject();
5149
stats.toXContent(builder, ToXContent.EMPTY_PARAMS);
5250
builder.endObject();
5351
String expectedJson = String.format(
5452
Locale.ROOT,
55-
"{\"QueryGroupCount\":%d,\"QueryGroupHeapSize\":%d}",
53+
"{\"QueryGroupCount_Total\":%d,\"QueryGroupCount_MaxHeap\":%d}",
5654
queryGroupCount,
5755
queryGroupHeapSize
5856
);

src/test/java/org/opensearch/plugin/insights/rules/model/healthStats/QueryInsightsHealthStatsTests.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,13 @@ public void testSerialization() throws IOException {
6969
// Read from StreamInput
7070
StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
7171
QueryInsightsHealthStats deserializedHealthStats = new QueryInsightsHealthStats(in);
72-
// Assert equality
7372
assertEquals(healthStats.getQueryRecordsQueueSize(), deserializedHealthStats.getQueryRecordsQueueSize());
7473
assertNotNull(deserializedHealthStats.getThreadPoolInfo());
7574
assertNotNull(deserializedHealthStats.getTopQueriesHealthStats());
7675
}
7776

7877
public void testToXContent() throws IOException {
7978
QueryInsightsHealthStats healthStats = new QueryInsightsHealthStats(threadPoolInfo, queryRecordsQueueSize, topQueriesHealthStats);
80-
// Create XContentBuilder to build JSON
8179
XContentBuilder builder = XContentFactory.jsonBuilder();
8280
builder.startObject();
8381

@@ -99,8 +97,8 @@ public void testToXContent() throws IOException {
9997
+ " \"TopQueriesHealthStats\": {\n"
10098
+ " \"latency\": {\n"
10199
+ " \"TopQueriesHeapSize\": 10,\n"
102-
+ " \"QueryGroupCount\": 20,\n"
103-
+ " \"QueryGroupHeapSize\": 15\n"
100+
+ " \"QueryGroupCount_Total\": 20,\n"
101+
+ " \"QueryGroupCount_MaxHeap\": 15\n"
104102
+ " }\n"
105103
+ " }\n"
106104
+ "}";

src/test/java/org/opensearch/plugin/insights/rules/model/healthStats/TopQueriesHealthStatsTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public void testSerialization() throws IOException {
3838
// Read from StreamInput
3939
StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
4040
TopQueriesHealthStats deserializedHealthStats = new TopQueriesHealthStats(in);
41-
// Assert equality
4241
assertEquals(healthStats.getTopQueriesHeapSize(), deserializedHealthStats.getTopQueriesHeapSize());
4342
assertNotNull(deserializedHealthStats.getQueryGrouperHealthStats());
4443
assertEquals(
@@ -53,14 +52,13 @@ public void testSerialization() throws IOException {
5352

5453
public void testToXContent() throws IOException {
5554
TopQueriesHealthStats healthStats = new TopQueriesHealthStats(topQueriesHeapSize, queryGrouperHealthStats);
56-
// Write to XContent
5755
XContentBuilder builder = XContentFactory.jsonBuilder();
5856
builder.startObject();
5957
healthStats.toXContent(builder, ToXContent.EMPTY_PARAMS);
6058
builder.endObject();
6159
String expectedJson = String.format(
6260
Locale.ROOT,
63-
"{\"TopQueriesHeapSize\":%d,\"QueryGroupCount\":%d,\"QueryGroupHeapSize\":%d}",
61+
"{\"TopQueriesHeapSize\":%d,\"QueryGroupCount_Total\":%d,\"QueryGroupCount_MaxHeap\":%d}",
6462
topQueriesHeapSize,
6563
queryGrouperHealthStats.getQueryGroupCount(),
6664
queryGrouperHealthStats.getQueryGroupHeapSize()

0 commit comments

Comments
 (0)