forked from opensearch-project/anomaly-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADSaveResultStrategy.java
95 lines (86 loc) · 3.25 KB
/
ADSaveResultStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.ad.ratelimit;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import org.opensearch.ad.ml.ThresholdingResult;
import org.opensearch.ad.model.AnomalyResult;
import org.opensearch.timeseries.model.Config;
import org.opensearch.timeseries.model.Entity;
import org.opensearch.timeseries.ratelimit.FeatureRequest;
import org.opensearch.timeseries.ratelimit.RequestPriority;
import org.opensearch.timeseries.ratelimit.SaveResultStrategy;
import org.opensearch.timeseries.util.ParseUtils;
public class ADSaveResultStrategy implements SaveResultStrategy<AnomalyResult, ThresholdingResult> {
private int resultMappingVersion;
private ADResultWriteWorker resultWriteWorker;
public ADSaveResultStrategy(int resultMappingVersion, ADResultWriteWorker resultWriteWorker) {
this.resultMappingVersion = resultMappingVersion;
this.resultWriteWorker = resultWriteWorker;
}
@Override
public void saveResult(ThresholdingResult result, Config config, FeatureRequest origRequest, String modelId) {
// result.getRcfScore() = 0 means the model is not initialized
// result.getGrade() = 0 means it is not an anomaly
saveResult(
result,
config,
Instant.ofEpochMilli(origRequest.getDataStartTimeMillis()),
Instant.ofEpochMilli(origRequest.getDataStartTimeMillis() + config.getIntervalInMilliseconds()),
modelId,
origRequest.getCurrentFeature(),
origRequest.getEntity(),
origRequest.getTaskId()
);
}
@Override
public void saveResult(
ThresholdingResult result,
Config config,
Instant dataStart,
Instant dataEnd,
String modelId,
double[] currentData,
Optional<Entity> entity,
String taskId
) {
// result.getRcfScore() = 0 means the model is not initialized
// result.getGrade() = 0 means it is not an anomaly
if (result != null && result.getRcfScore() > 0) {
List<AnomalyResult> indexableResults = result
.toIndexableResults(
config,
dataStart,
dataEnd,
Instant.now(),
Instant.now(),
ParseUtils.getFeatureData(currentData, config),
entity,
resultMappingVersion,
modelId,
taskId,
null
);
for (AnomalyResult r : indexableResults) {
saveResult(r, config);
}
}
}
@Override
public void saveResult(AnomalyResult result, Config config) {
resultWriteWorker
.put(
new ADResultWriteRequest(
System.currentTimeMillis() + config.getIntervalInMilliseconds(),
config.getId(),
result.getAnomalyGrade() > 0 ? RequestPriority.HIGH : RequestPriority.MEDIUM,
result,
config.getCustomResultIndexOrAlias(),
config.getFlattenResultIndexMapping()
)
);
}
}