Skip to content

Commit 7a0da6d

Browse files
authored
Adding auto create as an optional field on detectors (#1602)
* adding autocreate optional field Signed-off-by: Amit Galitzky <[email protected]> * only write if autocreated is true Signed-off-by: Amit Galitzky <[email protected]> --------- Signed-off-by: Amit Galitzky <[email protected]>
1 parent a7de806 commit 7a0da6d

20 files changed

+188
-71
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
77
### Features
88
### Enhancements
99
- Adds capability to automatically switch to old access-control if model-group is excluded from protected resources setting ([#1569](https://github.com/opensearch-project/anomaly-detection/pull/1569))
10-
10+
- Adding auto create as an optional field on detectors ([#1602](https://github.com/opensearch-project/anomaly-detection/pull/1602))
1111

1212
### Bug Fixes
1313

src/main/java/org/opensearch/ad/model/ADTask.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ public static ADTask parse(XContentParser parser, String taskId) throws IOExcept
347347
detector.getCustomResultIndexTTL(),
348348
detector.getFlattenResultIndexMapping(),
349349
detector.getLastBreakingUIChangeTime(),
350-
detector.getFrequency()
350+
detector.getFrequency(),
351+
detector.getAutoCreated()
351352
);
352353
return new Builder()
353354
.taskId(parsedTaskId)

src/main/java/org/opensearch/ad/model/AnomalyDetector.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public Integer getShingleSize(Integer customShingleSize) {
156156
* @param lastBreakingUIChangeTime last update time to configuration that can break UI and we have
157157
* to display updates from the changed time
158158
* @param frequency frequency of the detector
159+
* @param autoCreated whether this detector was created automatically
159160
*/
160161
public AnomalyDetector(
161162
String detectorId,
@@ -185,7 +186,8 @@ public AnomalyDetector(
185186
Integer customResultIndexTTL,
186187
Boolean flattenResultIndexMapping,
187188
Instant lastBreakingUIChangeTime,
188-
TimeConfiguration frequency
189+
TimeConfiguration frequency,
190+
Boolean autoCreated
189191
) {
190192
super(
191193
detectorId,
@@ -215,7 +217,8 @@ public AnomalyDetector(
215217
customResultIndexTTL,
216218
flattenResultIndexMapping,
217219
lastBreakingUIChangeTime,
218-
frequency
220+
frequency,
221+
autoCreated
219222
);
220223

221224
checkAndThrowValidationErrors(ValidationAspect.DETECTOR);
@@ -301,6 +304,7 @@ public AnomalyDetector(StreamInput input) throws IOException {
301304
} else {
302305
this.frequency = null;
303306
}
307+
this.autoCreated = input.readOptionalBoolean();
304308
}
305309

306310
public XContentBuilder toXContent(XContentBuilder builder) throws IOException {
@@ -374,6 +378,7 @@ public void writeTo(StreamOutput output) throws IOException {
374378
} else {
375379
output.writeBoolean(false);
376380
}
381+
output.writeOptionalBoolean(autoCreated);
377382
}
378383

379384
@Override
@@ -391,7 +396,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
391396
if (rules != null) {
392397
xContentBuilder.field(RULES_FIELD, rules.toArray());
393398
}
394-
395399
return xContentBuilder.endObject();
396400
}
397401

@@ -474,6 +478,7 @@ public static AnomalyDetector parse(
474478
Instant lastBreakingUIChangeTime = null;
475479
// by default, frequency is the same as interval when not set
476480
TimeConfiguration frequency = null;
481+
Boolean autoCreated = null;
477482

478483
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
479484
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
@@ -628,6 +633,9 @@ public static AnomalyDetector parse(
628633
throw e;
629634
}
630635
break;
636+
case AUTO_CREATED_FIELD:
637+
autoCreated = onlyParseBooleanValue(parser);
638+
break;
631639
default:
632640
parser.skipChildren();
633641
break;
@@ -662,7 +670,8 @@ public static AnomalyDetector parse(
662670
customResultIndexTTL,
663671
flattenResultIndexMapping,
664672
lastBreakingUIChangeTime,
665-
frequency
673+
frequency,
674+
autoCreated
666675
);
667676
detector.setDetectionDateRange(detectionDateRange);
668677
return detector;

src/main/java/org/opensearch/ad/rest/handler/AbstractAnomalyDetectorActionHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ protected AnomalyDetector copyConfig(User user, Config config) {
251251
config.getCustomResultIndexTTL(),
252252
config.getFlattenResultIndexMapping(),
253253
breakingUIChange ? Instant.now() : config.getLastBreakingUIChangeTime(),
254-
config.getFrequency()
254+
config.getFrequency(),
255+
detector.getAutoCreated()
255256
);
256257
}
257258

src/main/java/org/opensearch/forecast/model/ForecastTask.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ public static ForecastTask parse(XContentParser parser, String taskId) throws IO
345345
forecaster.getCustomResultIndexTTL(),
346346
forecaster.getFlattenResultIndexMapping(),
347347
forecaster.getLastBreakingUIChangeTime(),
348-
forecaster.getFrequency()
348+
forecaster.getFrequency(),
349+
forecaster.getAutoCreated()
349350
);
350351
return new Builder()
351352
.taskId(parsedTaskId)

src/main/java/org/opensearch/forecast/model/Forecaster.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ public Forecaster(
137137
Integer customResultIndexTTL,
138138
Boolean flattenResultIndexMapping,
139139
Instant lastBreakingUIChangeTime,
140-
TimeConfiguration frequency
140+
TimeConfiguration frequency,
141+
Boolean autoCreated
141142
) {
142143
super(
143144
forecasterId,
@@ -167,7 +168,8 @@ public Forecaster(
167168
customResultIndexTTL,
168169
flattenResultIndexMapping,
169170
lastBreakingUIChangeTime,
170-
frequency
171+
frequency,
172+
autoCreated
171173
);
172174

173175
checkAndThrowValidationErrors(ValidationAspect.FORECASTER);
@@ -316,6 +318,7 @@ public static Forecaster parse(
316318
Integer customResultIndexMinAge = null;
317319
Integer customResultIndexTTL = null;
318320
Boolean flattenResultIndexMapping = null;
321+
Boolean autoCreated = null;
319322
Instant lastBreakingUIChangeTime = null;
320323
// by default, frequency is the same as interval when not set
321324
TimeConfiguration frequency = null;
@@ -467,6 +470,9 @@ public static Forecaster parse(
467470
throw e;
468471
}
469472
break;
473+
case AUTO_CREATED_FIELD:
474+
autoCreated = onlyParseBooleanValue(parser);
475+
break;
470476
default:
471477
parser.skipChildren();
472478
break;
@@ -500,7 +506,9 @@ public static Forecaster parse(
500506
customResultIndexTTL,
501507
flattenResultIndexMapping,
502508
lastBreakingUIChangeTime,
503-
frequency
509+
frequency,
510+
autoCreated
511+
504512
);
505513
return forecaster;
506514
}

src/main/java/org/opensearch/forecast/rest/handler/AbstractForecasterActionHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ protected Config copyConfig(User user, Config config) {
262262
config.getCustomResultIndexTTL(),
263263
config.getFlattenResultIndexMapping(),
264264
breakingUIChange ? Instant.now() : config.getLastBreakingUIChangeTime(),
265-
config.getFrequency()
265+
config.getFrequency(),
266+
config.getAutoCreated()
266267
);
267268
}
268269

src/main/java/org/opensearch/timeseries/model/Config.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public abstract class Config implements Writeable, ToXContentObject {
8181
public static final String RESULT_INDEX_FIELD_MIN_AGE = "result_index_min_age";
8282
public static final String RESULT_INDEX_FIELD_TTL = "result_index_ttl";
8383
public static final String FLATTEN_CUSTOM_RESULT_INDEX = "flatten_custom_result_index";
84+
public static final String AUTO_CREATED_FIELD = "auto_created";
8485
// Changing categorical field, feature attributes, interval, windowDelay, time field, horizon, indices,
8586
// result index would force us to display results only from the most recent update. Otherwise,
8687
// the UI appear cluttered and unclear.
@@ -129,6 +130,7 @@ public abstract class Config implements Writeable, ToXContentObject {
129130
protected Boolean flattenResultIndexMapping;
130131
protected Instant lastUIBreakingChangeTime;
131132
protected TimeConfiguration frequency;
133+
protected Boolean autoCreated;
132134

133135
public static String INVALID_RESULT_INDEX_NAME_SIZE = "Result index name size must contains less than "
134136
+ MAX_RESULT_INDEX_NAME_SIZE
@@ -162,7 +164,8 @@ protected Config(
162164
Integer customResultIndexTTL,
163165
Boolean flattenResultIndexMapping,
164166
Instant lastBreakingUIChangeTime,
165-
TimeConfiguration frequency
167+
TimeConfiguration frequency,
168+
Boolean autoCreated
166169
) {
167170
if (Strings.isBlank(name)) {
168171
errorMessage = CommonMessages.EMPTY_NAME;
@@ -357,6 +360,7 @@ protected Config(
357360
this.flattenResultIndexMapping = Strings.trimToNull(resultIndex) == null ? null : flattenResultIndexMapping;
358361
this.lastUIBreakingChangeTime = lastBreakingUIChangeTime;
359362
this.frequency = frequency;
363+
this.autoCreated = autoCreated != null ? autoCreated : false;
360364
}
361365

362366
/**
@@ -460,6 +464,7 @@ public Config(StreamInput input) throws IOException {
460464
} else {
461465
this.frequency = null;
462466
}
467+
this.autoCreated = input.readOptionalBoolean();
463468
}
464469

465470
/*
@@ -520,6 +525,7 @@ public void writeTo(StreamOutput output) throws IOException {
520525
} else {
521526
output.writeBoolean(false);
522527
}
528+
output.writeOptionalBoolean(autoCreated);
523529
}
524530

525531
public boolean invalidShingleSizeRange(Integer shingleSizeToTest) {
@@ -578,7 +584,8 @@ public boolean equals(Object o) {
578584
&& Objects.equal(customResultIndexMinAge, config.customResultIndexMinAge)
579585
&& Objects.equal(customResultIndexTTL, config.customResultIndexTTL)
580586
&& Objects.equal(flattenResultIndexMapping, config.flattenResultIndexMapping)
581-
&& Objects.equal(frequency, config.frequency);
587+
&& Objects.equal(frequency, config.frequency)
588+
&& Objects.equal(autoCreated, config.autoCreated);
582589
}
583590

584591
@Generated
@@ -607,7 +614,8 @@ public int hashCode() {
607614
customResultIndexMinAge,
608615
customResultIndexTTL,
609616
flattenResultIndexMapping,
610-
frequency
617+
frequency,
618+
autoCreated
611619
);
612620
}
613621

@@ -665,6 +673,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
665673
if (frequency != null) {
666674
builder.field(FREQUENCY_FIELD, frequency);
667675
}
676+
if (autoCreated != null && autoCreated) {
677+
builder.field(AUTO_CREATED_FIELD, autoCreated);
678+
}
668679
return builder;
669680
}
670681

@@ -898,6 +909,10 @@ public boolean getFlattenResultIndexMapping() {
898909
return flattenResultIndexMapping != null ? flattenResultIndexMapping : false;
899910
}
900911

912+
public boolean getAutoCreated() {
913+
return autoCreated != null ? autoCreated : false;
914+
}
915+
901916
public String getFlattenResultIndexAlias() {
902917
if (getFlattenResultIndexMapping()) {
903918
return (getCustomResultIndexOrAlias() + "_flattened_" + getName()).toLowerCase(Locale.ROOT);
@@ -970,6 +985,7 @@ public String toString() {
970985
.append("customResultIndexTTL", customResultIndexTTL)
971986
.append("flattenResultIndexMapping", flattenResultIndexMapping)
972987
.append("frequency", frequency)
988+
.append("autoCreated", autoCreated)
973989
.toString();
974990
}
975991

src/test/java/org/opensearch/action/admin/indices/mapping/get/IndexAnomalyDetectorActionHandlerTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,8 @@ public <Request extends ActionRequest, Response extends ActionResponse> void doE
878878
detector.getCustomResultIndexTTL(),
879879
false,
880880
Instant.now(),
881-
detector.getFrequency()
881+
detector.getFrequency(),
882+
null
882883
);
883884
try {
884885
listener.onResponse((Response) TestHelpers.createGetResponse(clone, clone.getId(), ADCommonName.CONFIG_INDEX));

src/test/java/org/opensearch/ad/AnomalyDetectorRestTestCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ public ToXContentObject[] getConfig(String detectorId, BasicHeader header, boole
322322
detector.getCustomResultIndexTTL(),
323323
detector.getFlattenResultIndexMapping(),
324324
detector.getLastBreakingUIChangeTime(),
325-
detector.getFrequency()
325+
detector.getFrequency(),
326+
detector.getAutoCreated()
326327
),
327328
detectorJob,
328329
historicalAdTask,
@@ -605,7 +606,8 @@ protected AnomalyDetector cloneDetector(AnomalyDetector anomalyDetector, String
605606
anomalyDetector.getCustomResultIndexTTL(),
606607
anomalyDetector.getFlattenResultIndexMapping(),
607608
Instant.now(),
608-
anomalyDetector.getFrequency()
609+
anomalyDetector.getFrequency(),
610+
anomalyDetector.getAutoCreated()
609611
);
610612
return detector;
611613
}

0 commit comments

Comments
 (0)