Skip to content

Commit 2e0c119

Browse files
committed
Clean up files
Signed-off-by: Andy Qin <[email protected]>
1 parent 7b532a5 commit 2e0c119

File tree

8 files changed

+34
-16
lines changed

8 files changed

+34
-16
lines changed

src/main/java/org/opensearch/neuralsearch/rest/RestNeuralStatsAction.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ public class RestNeuralStatsAction extends BaseRestHandler {
8181

8282
private static final Set<String> RESPONSE_PARAMS = ImmutableSet.of("nodeId", "stat", INCLUDE_METADATA_PARAM, FLATTEN_PARAM);
8383

84-
public static boolean isValidParamString(String name) {
85-
return name.matches(PARAM_REGEX) && name.length() < MAX_PARAM_LENGTH;
84+
/**
85+
* Validates a param string if its under the max length and matches simple string pattern
86+
* @param param the string to validate
87+
* @return whether it's valid
88+
*/
89+
public static boolean isValidParamString(String param) {
90+
return param.matches(PARAM_REGEX) && param.length() < MAX_PARAM_LENGTH;
8691
}
8792

8893
private NeuralSearchSettingsAccessor settingsAccessor;
@@ -134,7 +139,7 @@ private NeuralStatsRequest createNeuralStatsRequest(RestRequest request) {
134139
return neuralStatsRequest;
135140
}
136141

137-
NeuralStatsInput createNeuralStatsInputFromRequestParams(RestRequest request) {
142+
private NeuralStatsInput createNeuralStatsInputFromRequestParams(RestRequest request) {
138143
NeuralStatsInput neuralStatsInput = new NeuralStatsInput();
139144

140145
// Parse specified nodes

src/main/java/org/opensearch/neuralsearch/stats/events/EventStatName.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
@Getter
2020
public enum EventStatName implements StatName {
21-
TEXT_EMBEDDING_PROCESSOR_EXECUTIONS("text_embedding_executions", "processors.ingest", EventStatType.TIMESTAMPED_COUNTER);
21+
TEXT_EMBEDDING_PROCESSOR_EXECUTIONS("text_embedding_executions", "processors.ingest", EventStatType.TIMESTAMPED_EVENT_COUNTER);
2222

2323
private final String nameString;
2424
private final String path;
@@ -43,7 +43,7 @@ public enum EventStatName implements StatName {
4343
this.statType = statType;
4444

4545
switch (statType) {
46-
case EventStatType.TIMESTAMPED_COUNTER:
46+
case EventStatType.TIMESTAMPED_EVENT_COUNTER:
4747
eventStat = new TimestampedEventStat(this);
4848
break;
4949
}

src/main/java/org/opensearch/neuralsearch/stats/events/EventStatType.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Enum for different kinds of event stat types to track
1313
*/
1414
public enum EventStatType implements StatType {
15-
TIMESTAMPED_COUNTER;
15+
TIMESTAMPED_EVENT_COUNTER;
1616

1717
/**
1818
* Gets the name of the stat type, the enum name in lowercase

src/main/java/org/opensearch/neuralsearch/stats/events/EventStatsManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Map<EventStatName, TimestampedEventStatSnapshot> getTimestampedEventStatS
7272
// Filter stats based on passed in collection
7373
Map<EventStatName, TimestampedEventStatSnapshot> eventStatsDataMap = new HashMap<>();
7474
for (EventStatName statName : statsToRetrieve) {
75-
if (statName.getStatType() == EventStatType.TIMESTAMPED_COUNTER) {
75+
if (statName.getStatType() == EventStatType.TIMESTAMPED_EVENT_COUNTER) {
7676
StatSnapshot<?> snapshot = statName.getEventStat().getStatSnapshot();
7777
if (snapshot instanceof TimestampedEventStatSnapshot) {
7878
// Get event data snapshot

src/main/java/org/opensearch/neuralsearch/stats/info/InfoStatName.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
@Getter
2020
public enum InfoStatName implements StatName {
2121
// Cluster info
22-
CLUSTER_VERSION("cluster_version", "", InfoStatType.SETTABLE_STRING),
23-
TEXT_EMBEDDING_PROCESSORS("text_embedding_processors_in_pipelines", "processors.ingest", InfoStatType.INFO_COUNTABLE);
22+
CLUSTER_VERSION("cluster_version", "", InfoStatType.INFO_STRING),
23+
TEXT_EMBEDDING_PROCESSORS("text_embedding_processors_in_pipelines", "processors.ingest", InfoStatType.INFO_COUNTER);
2424

2525
private final String nameString;
2626
private final String path;

src/main/java/org/opensearch/neuralsearch/stats/info/InfoStatType.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
* Enum for different kinds of info stat types to track
1313
*/
1414
public enum InfoStatType implements StatType {
15-
INFO_COUNTABLE,
16-
SETTABLE_STRING,
17-
SETTABLE_BOOLEAN;
15+
INFO_COUNTER,
16+
INFO_STRING,
17+
INFO_BOOLEAN;
1818

1919
/**
2020
* Gets the name of the stat type, the enum name in lowercase

src/main/java/org/opensearch/neuralsearch/stats/info/InfoStatsManager.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private Map<InfoStatName, CountableInfoStatSnapshot> getCountableStats() {
7272
// Initialize empty map with keys so stat names are visible in JSON even if the value is not counted
7373
Map<InfoStatName, CountableInfoStatSnapshot> countableInfoStats = new HashMap<>();
7474
for (InfoStatName stat : EnumSet.allOf(InfoStatName.class)) {
75-
if (stat.getStatType() == InfoStatType.INFO_COUNTABLE) {
75+
if (stat.getStatType() == InfoStatType.INFO_COUNTER) {
7676
countableInfoStats.put(stat, new CountableInfoStatSnapshot(stat));
7777
}
7878
}
@@ -92,8 +92,8 @@ private Map<InfoStatName, SettableInfoStatSnapshot<?>> getSettableStats() {
9292
Map<InfoStatName, SettableInfoStatSnapshot<?>> settableInfoStats = new HashMap<>();
9393
for (InfoStatName statName : EnumSet.allOf(InfoStatName.class)) {
9494
switch (statName.getStatType()) {
95-
case InfoStatType.SETTABLE_BOOLEAN -> settableInfoStats.put(statName, new SettableInfoStatSnapshot<Boolean>(statName));
96-
case InfoStatType.SETTABLE_STRING -> settableInfoStats.put(statName, new SettableInfoStatSnapshot<String>(statName));
95+
case InfoStatType.INFO_BOOLEAN -> settableInfoStats.put(statName, new SettableInfoStatSnapshot<Boolean>(statName));
96+
case InfoStatType.INFO_STRING -> settableInfoStats.put(statName, new SettableInfoStatSnapshot<String>(statName));
9797
}
9898
}
9999

src/test/java/org/opensearch/neuralsearch/rest/RestNeuralStatsActionIT.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
import lombok.extern.log4j.Log4j2;
88
import org.junit.After;
99
import org.junit.Before;
10+
import org.opensearch.client.Request;
11+
import org.opensearch.client.Response;
12+
import org.opensearch.client.ResponseException;
13+
import org.opensearch.core.rest.RestStatus;
1014
import org.opensearch.neuralsearch.BaseNeuralSearchIT;
15+
import org.opensearch.neuralsearch.plugin.NeuralSearch;
1116
import org.opensearch.neuralsearch.settings.NeuralSearchSettings;
1217
import org.opensearch.neuralsearch.stats.common.StatSnapshot;
1318
import org.opensearch.neuralsearch.stats.info.InfoStatName;
@@ -57,6 +62,14 @@ public void tearDown() throws Exception {
5762
updateClusterSettings(NeuralSearchSettings.NEURAL_STATS_ENABLED.getKey(), false);
5863
}
5964

65+
public void test_statsDisabledIsForbidden() throws Exception {
66+
updateClusterSettings("plugins.neural_search.stats_enabled", false);
67+
Request request = new Request("GET", NeuralSearch.NEURAL_BASE_URI + "/stats");
68+
69+
ResponseException response = expectThrows(ResponseException.class, () -> client().performRequest(request));
70+
assertEquals(RestStatus.FORBIDDEN, RestStatus.fromCode(response.getResponse().getStatusLine().getStatusCode()));
71+
}
72+
6073
public void test_textEmbedding() throws Exception {
6174
// Setup processors
6275
String modelId = uploadTextEmbeddingModel();
@@ -138,7 +151,7 @@ public void test_includeMetadata() throws Exception {
138151
String valueWithMetadata = ((Map<String, String>) clusterVersionStatMetadata).get(StatSnapshot.VALUE_FIELD);
139152

140153
// Stat type metadata should match
141-
assertEquals(InfoStatType.SETTABLE_STRING.getTypeString(), statType);
154+
assertEquals(InfoStatType.INFO_STRING.getTypeString(), statType);
142155

143156
// Fetch Without metadata
144157
params.put(RestNeuralStatsAction.INCLUDE_METADATA_PARAM, "false");

0 commit comments

Comments
 (0)