Skip to content

Commit

Permalink
Add unit test and change serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
  • Loading branch information
deshsidd committed Jul 2, 2024
1 parent eb8ebd8 commit d419ca5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -175,7 +168,6 @@ public void drainRecords() {
logger.error("Error while trying to categorize the queries.", e);
}
}

}

/**
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}

0 comments on commit d419ca5

Please sign in to comment.