-
Notifications
You must be signed in to change notification settings - Fork 5
/
RegressionFunction.java
188 lines (141 loc) · 5.97 KB
/
RegressionFunction.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.takipi.udf.volume;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.joda.time.DateTime;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.takipi.api.client.ApiClient;
import com.takipi.api.client.result.event.EventResult;
import com.takipi.api.client.util.regression.RateRegression;
import com.takipi.api.client.util.regression.RegressionInput;
import com.takipi.api.client.util.regression.RegressionResult;
import com.takipi.api.client.util.regression.RegressionUtil;
import com.takipi.common.util.CollectionUtil;
import com.takipi.udf.ContextArgs;
import com.takipi.udf.input.Input;
import com.takipi.udf.input.TimeInterval;
import com.takipi.udf.util.TestUtil;
public class RegressionFunction {
public static String validateInput(String rawInput) {
return parseRegressionInput(rawInput).toString();
}
static RegressionFunctionInput parseRegressionInput(String rawInput) {
System.out.println("validateInput rawInput:" + rawInput);
if (Strings.isNullOrEmpty(rawInput)) {
throw new IllegalArgumentException("Input is empty");
}
RegressionFunctionInput input;
try {
input = RegressionFunctionInput.of(rawInput);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
if (input.activeTimespan == null) {
throw new IllegalArgumentException("'activeTimespan' must not be empty");
} else if (input.activeTimespan.isNegative()) {
throw new IllegalArgumentException("'activeTimespan' can't be negative time");
}
if (input.baseTimespan == null) {
throw new IllegalArgumentException("'baseTimespan' must not be empty");
} else if (input.baseTimespan.isNegative()) {
throw new IllegalArgumentException("'baseTimespan' can't be negative time");
}
if (input.label == null) {
input.label = "Anomaly";
}
if (input.min_interval == null) {
input.min_interval = TimeInterval.parse("2d");
} else if (input.min_interval.isNegative()) {
throw new IllegalArgumentException("'min_interval' can't be negative time");
}
if (input.max_interval == null) {
input.max_interval = TimeInterval.parse("3d");
} else if (input.max_interval.isNegative()) {
throw new IllegalArgumentException("'max_interval' can't be negative time");
}
if (input.max_interval.asMinutes() <= input.min_interval.asMinutes()) {
throw new IllegalArgumentException("'max_interval' must be greater than 'min_interval'");
}
if (input.regressionDelta <= 0) {
throw new IllegalArgumentException("'regressionDelta' must be positive");
}
return input;
}
public static void execute(String rawContextArgs, String rawInput) {
System.out.println("execute:" + rawContextArgs);
ContextArgs args = (new Gson()).fromJson(rawContextArgs, ContextArgs.class);
ApiClient apiClient = args.apiClient();
if (!args.viewValidate()) {
throw new IllegalArgumentException("Invalid context args - " + rawContextArgs);
}
RegressionFunctionInput input = parseRegressionInput(rawInput);
System.out.println("Calculating regressions\n");
RegressionInput regressionInput = new RegressionInput();
regressionInput.serviceId = args.serviceId;
regressionInput.viewId = args.viewId;
regressionInput.activeTimespan = input.activeTimespan.asMinutes();
regressionInput.baselineTimespan = input.baseTimespan.asMinutes();
regressionInput.minVolumeThreshold = input.minVolumeThreshold;
regressionInput.minErrorRateThreshold = input.minErrorRateThreshold / 100;
regressionInput.regressionDelta = input.regressionDelta / 100;
regressionInput.applySeasonality = true;
if (input.appName != null) {
regressionInput.applictations = Arrays.asList(input.appName.split(","));
}
RateRegression rateRegression = RegressionUtil.calculateRateRegressions(apiClient, regressionInput, System.out,
false);
AnomalyUtil.removeAnomalyLabel(rateRegression.getNonRegressions(), apiClient, args.serviceId,
input.max_interval, input.label);
Collection<RegressionResult> activeRegressions = rateRegression.getAllRegressions().values();
if (activeRegressions.size() == 0) {
System.out.println("No anomalies found");
return;
}
List<EventResult> candidates = Lists.newArrayList();
for (RegressionResult regressionResult : activeRegressions) {
candidates.add(regressionResult.getEvent());
}
Collection<EventResult> contributors = AnomalyUtil.filterAnomalyEvents(candidates, apiClient, args.serviceId,
input.min_interval, input.label, AnomalyUtil.MAX_ANOMALY_CONTRIBUTORS);
if (CollectionUtil.safeIsEmpty(contributors)) {
return;
}
AnomalyUtil.reportAnomaly(apiClient, args.serviceId, args.viewId, contributors, input.label,
rateRegression.getActiveWndowStart(), DateTime.now(), input.toString());
}
static class RegressionFunctionInput extends Input {
public String appName;
public TimeInterval activeTimespan;
public TimeInterval baseTimespan;
public double regressionDelta;
public double minErrorRateThreshold;
public int minVolumeThreshold;
public String label;
public TimeInterval min_interval;
public TimeInterval max_interval;
private RegressionFunctionInput(String raw) {
super(raw);
}
static RegressionFunctionInput of(String raw) {
return new RegressionFunctionInput(raw);
}
@Override
public String toString() {
String result = String.format("Anomaly(last %s vs. prev %s, %s > %.0f%%)", activeTimespan, baseTimespan,
Character.toString((char) 916), regressionDelta);
return result;
}
}
// A sample program on how to programmatically activate RegressionFunction
//
public static void main(String[] args) {
String rawContextArgs = TestUtil.getViewContextArgs(args, "All Events");
// example values
//
String[] sampleValues = new String[] { "activeTimespan=1d", "baseTimespan=14d", "regressionDelta=100",
"minErrorRateThreshold=1", "minVolumeThreshold=100", "label=Anomaly", "min_interval=1d" };
RegressionFunction.execute(rawContextArgs, String.join("\n", sampleValues));
}
}