Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: Kaituo Li <[email protected]>
  • Loading branch information
kaituo committed Jun 29, 2023
1 parent 99749df commit d13cf6a
Show file tree
Hide file tree
Showing 29 changed files with 1,227 additions and 439 deletions.
15 changes: 8 additions & 7 deletions src/main/java/org/opensearch/ad/AnomalyDetectorRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -168,20 +169,21 @@ private List<AnomalyResult> parsePreviewResult(
AnomalyResult result;
if (results != null && results.size() > i) {
ThresholdingResult thresholdingResult = results.get(i);
result = thresholdingResult
.toAnomalyResult(
thresholdingResult
.toIndexableResult(
detector,
Instant.ofEpochMilli(timeRange.getKey()),
Instant.ofEpochMilli(timeRange.getValue()),
null,
null,
featureDatas,
entity,
Optional.ofNullable(entity),
CommonValue.NO_SCHEMA_VERSION,
null,
null,
null
);
)
.ifPresent(r -> anomalyResults.add((AnomalyResult) r));
} else {
result = new AnomalyResult(
detector.getId(),
Expand All @@ -192,14 +194,13 @@ private List<AnomalyResult> parsePreviewResult(
null,
null,
null,
entity,
Optional.ofNullable(entity),
detector.getUser(),
CommonValue.NO_SCHEMA_VERSION,
null
);
anomalyResults.add(result);
}

anomalyResults.add(result);
}
}
return anomalyResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -282,7 +283,7 @@ public void indexAnomalyResultException(
executionStartTime,
Instant.now(),
errorMessage,
null, // single-stream detectors have no entity
Optional.empty(), // single-stream detectors have no entity
user,
anomalyDetectionIndices.getSchemaVersion(ADIndex.RESULT),
null // no model id
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/opensearch/ad/ProfileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static SearchRequest createRealtimeInittedEverRequest(String detectorId,
filterQuery.filter(QueryBuilders.rangeQuery(AnomalyResult.ANOMALY_SCORE_FIELD).gt(0));
// Historical analysis result also stored in result index, which has non-null task_id.
// For realtime detection result, we should filter task_id == null
ExistsQueryBuilder taskIdExistsFilter = QueryBuilders.existsQuery(AnomalyResult.TASK_ID_FIELD);
ExistsQueryBuilder taskIdExistsFilter = QueryBuilders.existsQuery(CommonName.TASK_ID_FIELD);
filterQuery.mustNot(taskIdExistsFilter);

SearchSourceBuilder source = new SearchSourceBuilder().query(filterQuery).size(1);
Expand Down
105 changes: 42 additions & 63 deletions src/main/java/org/opensearch/ad/ml/ThresholdingResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.opensearch.ad.model.AnomalyDetector;
import org.opensearch.ad.model.AnomalyResult;
import org.opensearch.timeseries.ml.IntermediateResult;
import org.opensearch.timeseries.model.Config;
import org.opensearch.timeseries.model.Entity;
import org.opensearch.timeseries.model.FeatureData;
import org.opensearch.timeseries.model.IndexableResult;

/**
* Data object containing thresholding results.
*/
public class ThresholdingResult {
public class ThresholdingResult extends IntermediateResult {

private final double grade;
private final double confidence;
private final double rcfScore;
private long totalUpdates;

/**
* position of the anomaly vis a vis the current time (can be -ve) if anomaly is
* detected late, which can and should happen sometime; for shingle size 1; this
Expand Down Expand Up @@ -163,10 +162,9 @@ public ThresholdingResult(
double threshold,
int forestSize
) {
super(confidence, totalUpdates, rcfScore);
this.grade = grade;
this.confidence = confidence;
this.rcfScore = rcfScore;
this.totalUpdates = totalUpdates;

this.relativeIndex = relativeIndex;
this.relevantAttribution = relevantAttribution;
this.pastValues = pastValues;
Expand All @@ -185,23 +183,6 @@ public double getGrade() {
return grade;
}

/**
* Returns the confidence for the grade.
*
* @return confidence for the grade
*/
public double getConfidence() {
return confidence;
}

public double getRcfScore() {
return rcfScore;
}

public long getTotalUpdates() {
return totalUpdates;
}

public int getRelativeIndex() {
return relativeIndex;
}
Expand Down Expand Up @@ -237,10 +218,8 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
ThresholdingResult that = (ThresholdingResult) o;
return this.grade == that.grade
&& this.confidence == that.confidence
&& this.rcfScore == that.rcfScore
&& this.totalUpdates == that.totalUpdates
return super.equals(o)
&& this.grade == that.grade
&& this.relativeIndex == that.relativeIndex
&& Arrays.equals(relevantAttribution, that.relevantAttribution)
&& Arrays.equals(pastValues, that.pastValues)
Expand All @@ -254,10 +233,8 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects
.hash(
super.hashCode(),
grade,
confidence,
rcfScore,
totalUpdates,
relativeIndex,
Arrays.hashCode(relevantAttribution),
Arrays.hashCode(pastValues),
Expand All @@ -271,10 +248,8 @@ public int hashCode() {
@Override
public String toString() {
return new ToStringBuilder(this)
.append(super.toString())
.append("grade", grade)
.append("confidence", confidence)
.append("rcfScore", rcfScore)
.append("totalUpdates", totalUpdates)
.append("relativeIndex", relativeIndex)
.append("relevantAttribution", Arrays.toString(relevantAttribution))
.append("pastValues", Arrays.toString(pastValues))
Expand Down Expand Up @@ -302,43 +277,47 @@ public String toString() {
* @param error Error
* @return converted AnomalyResult
*/
public AnomalyResult toAnomalyResult(
AnomalyDetector detector,
@Override
public Optional<IndexableResult> toIndexableResult(
Config detector,
Instant dataStartInstant,
Instant dataEndInstant,
Instant executionStartInstant,
Instant executionEndInstant,
List<FeatureData> featureData,
Entity entity,
Optional<Entity> entity,
Integer schemaVersion,
String modelId,
String taskId,
String error
) {
return AnomalyResult
.fromRawTRCFResult(
detector.getId(),
detector.getIntervalInMilliseconds(),
taskId,
rcfScore,
grade,
confidence,
featureData,
dataStartInstant,
dataEndInstant,
executionStartInstant,
executionEndInstant,
error,
entity,
detector.getUser(),
schemaVersion,
modelId,
relevantAttribution,
relativeIndex,
pastValues,
expectedValuesList,
likelihoodOfValues,
threshold
return Optional
.of(
AnomalyResult
.fromRawTRCFResult(
detector.getId(),
detector.getIntervalInMilliseconds(),
taskId,
rcfScore,
grade,
confidence,
featureData,
dataStartInstant,
dataEndInstant,
executionStartInstant,
executionEndInstant,
error,
entity,
detector.getUser(),
schemaVersion,
modelId,
relevantAttribution,
relativeIndex,
pastValues,
expectedValuesList,
likelihoodOfValues,
threshold
)
);
}
}
Loading

0 comments on commit d13cf6a

Please sign in to comment.