-
Notifications
You must be signed in to change notification settings - Fork 36
Prevent creating detector with duplicate name. Issue:#118 #134
Prevent creating detector with duplicate name. Issue:#118 #134
Conversation
private void checkADNameExists(String detectorId) throws IOException { | ||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() | ||
// src/main/resources/mappings/anomaly-detectors.json#L14 | ||
.query(QueryBuilders.termQuery("name.keyword", anomalyDetector.getName())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor. can this query also include a condition like not this detector id to utilize es search instead of additional postprocessing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a good point. will change to use must_not to exclude this detector id
String errorMsg = String.format("Cannot create anomaly detector with name[%s] used by detectorId %s", name, existingDetectorId); | ||
logger.error(errorMsg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor. i would use a warning for this case since it's a harmless user error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure. will do
// src/main/resources/mappings/anomaly-detectors.json#L14 | ||
boolQueryBuilder.must(QueryBuilders.termQuery("name.keyword", anomalyDetector.getName())); | ||
// _id field does not allow "", but allows " " | ||
boolQueryBuilder.mustNot(QueryBuilders.termQuery(RestHandlerUtils._ID, StringUtils.isBlank(detectorId) ? " " : detectorId)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we add mustNot
condition only when detectorId is not blank?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea. Thanks!
if (response.getHits().getTotalHits().value > 0) { | ||
String errorMsg = String | ||
.format( | ||
"Cannot create anomaly detector with name[%s] used by detectorId %s", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may show this error message on Kibana. How about we make it more readable?
Cannot create anomaly detector with name [%s] as it's already used by detector [%s]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure. will change that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for the change.
Issue #, if available:
#118
Description of changes:
Add logic to prevent user from creating detector with duplicate name
Testing:
Manual testing:
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.