From d419ca5d20c50e199cb4da893471350024924198 Mon Sep 17 00:00:00 2001 From: Siddhant Deshmukh Date: Tue, 2 Jul 2024 12:56:50 -0700 Subject: [PATCH] Add unit test and change serialization Signed-off-by: Siddhant Deshmukh --- .../core/service/QueryInsightsService.java | 27 +++++++++++++------ .../rules/model/SearchQueryRecord.java | 6 +---- .../service/QueryInsightsServiceTests.java | 21 +++++++++++++++ 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java index da30d37e557b7..b0afd66968b1f 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java @@ -122,13 +122,6 @@ public QueryInsightsService( this.metricsRegistry = metricsRegistry; } - private void setSearchQueryMetricsEnabled(boolean searchQueryMetricsEnabled) { - this.searchQueryMetricsEnabled = searchQueryMetricsEnabled; - if ((this.searchQueryMetricsEnabled == true) && this.searchQueryCategorizer == null) { - this.searchQueryCategorizer = new SearchQueryCategorizer(metricsRegistry); - } - } - /** * Ingest the query data into in-memory stores * @@ -175,7 +168,6 @@ public void drainRecords() { logger.error("Error while trying to categorize the queries.", e); } } - } /** @@ -282,6 +274,25 @@ public void setExporter(final MetricType type, final Settings settings) { } } + /** + * Set search query metrics enabled to enable collection of search query categorization metrics + * @param searchQueryMetricsEnabled boolean flag + */ + public void setSearchQueryMetricsEnabled(boolean searchQueryMetricsEnabled) { + this.searchQueryMetricsEnabled = searchQueryMetricsEnabled; + if ((this.searchQueryMetricsEnabled == true) && this.searchQueryCategorizer == null) { + this.searchQueryCategorizer = new SearchQueryCategorizer(metricsRegistry); + } + } + + public boolean isSearchQueryMetricsEnabled() { + return this.searchQueryMetricsEnabled; + } + + public SearchQueryCategorizer getSearchQueryCategorizer() { + return this.searchQueryCategorizer; + } + /** * Validate the exporter config for a metricType * diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/SearchQueryRecord.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/SearchQueryRecord.java index a6e6b4a9051f0..1d8d931ad9866 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/SearchQueryRecord.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/SearchQueryRecord.java @@ -134,11 +134,7 @@ public XContentBuilder toXContent(final XContentBuilder builder, final ToXConten public void writeTo(final StreamOutput out) throws IOException { out.writeLong(timestamp); out.writeMap(measurements, (stream, metricType) -> MetricType.writeTo(out, metricType), StreamOutput::writeGenericValue); - out.writeMap( - attributes, - (stream, attribute) -> Attribute.writeTo(out, attribute), - (stream, attributeValue) -> Attribute.writeValueTo(out, attributeValue) - ); + out.writeMap(attributes, (stream, attribute) -> Attribute.writeTo(out, attribute), StreamOutput::writeGenericValue); } /** diff --git a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java index 1c81d516a0f97..68be62148e4ce 100644 --- a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java +++ b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java @@ -63,4 +63,25 @@ public void testClose() { fail("No exception expected when closing query insights service"); } } + + public void testSearchQueryMetricsEnabled() { + // Initially, searchQueryMetricsEnabled should be false and searchQueryCategorizer should be null + assertFalse(queryInsightsService.isSearchQueryMetricsEnabled()); + assertNull(queryInsightsService.getSearchQueryCategorizer()); + + // Enable search query metrics + queryInsightsService.setSearchQueryMetricsEnabled(true); + + // Assert that searchQueryMetricsEnabled is true and searchQueryCategorizer is initialized + assertTrue(queryInsightsService.isSearchQueryMetricsEnabled()); + assertNotNull(queryInsightsService.getSearchQueryCategorizer()); + + // Disable search query metrics + queryInsightsService.setSearchQueryMetricsEnabled(false); + + // Assert that searchQueryMetricsEnabled is false and searchQueryCategorizer is not null + assertFalse(queryInsightsService.isSearchQueryMetricsEnabled()); + assertNotNull(queryInsightsService.getSearchQueryCategorizer()); + + } }