-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathMLStatsInput.java
316 lines (286 loc) · 12 KB
/
MLStatsInput.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.ml.stats;
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.ml.utils.MLNodeUtils.parseArrayField;
import static org.opensearch.ml.utils.MLNodeUtils.parseField;
import java.io.IOException;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.ml.common.FunctionName;
import lombok.Builder;
import lombok.Getter;
@Getter
public class MLStatsInput implements ToXContentObject, Writeable {
public static final String TARGET_STAT_LEVEL = "target_stat_levels";
public static final String CLUSTER_LEVEL_STATS = "cluster_level_stats";
public static final String NODE_LEVEL_STATS = "node_level_stats";
public static final String ACTION_LEVEL_STATS = "action_level_stats";
public static final String NODE_IDS = "node_ids";
public static final String ALGORITHMS = "algorithms";
public static final String MODELS = "models";
public static final String ACTIONS = "actions";
/**
* Retrieve which stat levels, could be one or multiple stats.
* If no stats set, will not retrieve any stats.
*/
private EnumSet<MLStatLevel> targetStatLevels;
/**
* Which cluster level stats will be retrieved.
*/
private EnumSet<MLClusterLevelStat> clusterLevelStats;
/**
* Which node level stats will be retrieved.
*/
private EnumSet<MLNodeLevelStat> nodeLevelStats;
/**
* Which action level stats will be retrieved.
*/
private EnumSet<MLActionLevelStat> actionLevelStats;
/**
* Which node's stats will be retrieved.
*/
private Set<String> nodeIds;
/**
* Which algorithm's stats will be retrieved.
*/
private EnumSet<FunctionName> algorithms;
/**
* Which model's stats will be retrieved.
*/
private Set<String> models;
/**
* Which action's stats will be retrieved.
*/
private EnumSet<ActionName> actions;
/**
* Constructor
* @param targetStatLevels target stat levels which will be retrieved
* @param clusterLevelStats cluster level stats which will be retrieved
* @param nodeLevelStats node level stats which will be retrieved
* @param actionLevelStats action level stats which will be retrieved
* @param nodeIds retrieve stats on these nodes
* @param algorithms retrieve stats for which algorithms
* @param models retrieve stats for which models
* @param actions retrieve stats for which actions
*/
@Builder
public MLStatsInput(
EnumSet<MLStatLevel> targetStatLevels,
EnumSet<MLClusterLevelStat> clusterLevelStats,
EnumSet<MLNodeLevelStat> nodeLevelStats,
EnumSet<MLActionLevelStat> actionLevelStats,
Set<String> nodeIds,
EnumSet<FunctionName> algorithms,
Set<String> models,
EnumSet<ActionName> actions
) {
this.targetStatLevels = targetStatLevels;
this.clusterLevelStats = clusterLevelStats;
this.nodeLevelStats = nodeLevelStats;
this.actionLevelStats = actionLevelStats;
this.nodeIds = nodeIds;
this.algorithms = algorithms;
this.models = models;
this.actions = actions;
}
public MLStatsInput() {
this.targetStatLevels = EnumSet.noneOf(MLStatLevel.class);
this.clusterLevelStats = EnumSet.noneOf(MLClusterLevelStat.class);
this.nodeLevelStats = EnumSet.noneOf(MLNodeLevelStat.class);
this.actionLevelStats = EnumSet.noneOf(MLActionLevelStat.class);
this.nodeIds = new HashSet<>();
this.algorithms = EnumSet.noneOf(FunctionName.class);
this.models = new HashSet<>();
this.actions = EnumSet.noneOf(ActionName.class);
}
public MLStatsInput(StreamInput input) throws IOException {
targetStatLevels = input.readOptionalEnumSet(MLStatLevel.class);
clusterLevelStats = input.readOptionalEnumSet(MLClusterLevelStat.class);
nodeLevelStats = input.readOptionalEnumSet(MLNodeLevelStat.class);
actionLevelStats = input.readOptionalEnumSet(MLActionLevelStat.class);
nodeIds = input.readBoolean() ? new HashSet<>(input.readStringList()) : new HashSet<>();
models = input.readBoolean() ? new HashSet<>(input.readStringList()) : new HashSet<>();
algorithms = input.readOptionalEnumSet(FunctionName.class);
actions = input.readOptionalEnumSet(ActionName.class);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalEnumSet(targetStatLevels);
out.writeOptionalEnumSet(clusterLevelStats);
out.writeOptionalEnumSet(nodeLevelStats);
out.writeOptionalEnumSet(actionLevelStats);
out.writeOptionalStringCollection(nodeIds);
out.writeOptionalStringCollection(models);
out.writeOptionalEnumSet(algorithms);
out.writeOptionalEnumSet(actions);
}
public static MLStatsInput parse(XContentParser parser) throws IOException {
EnumSet<MLStatLevel> targetStatLevels = EnumSet.noneOf(MLStatLevel.class);
EnumSet<MLClusterLevelStat> clusterLevelStats = EnumSet.noneOf(MLClusterLevelStat.class);
EnumSet<MLNodeLevelStat> nodeLevelStats = EnumSet.noneOf(MLNodeLevelStat.class);
EnumSet<MLActionLevelStat> actionLevelStats = EnumSet.noneOf(MLActionLevelStat.class);
Set<String> nodeIds = new HashSet<>();
Set<String> models = new HashSet<>();
EnumSet<FunctionName> algorithms = EnumSet.noneOf(FunctionName.class);
EnumSet<ActionName> actions = EnumSet.noneOf(ActionName.class);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
switch (fieldName) {
case TARGET_STAT_LEVEL:
parseField(parser, targetStatLevels, input -> MLStatLevel.from(input.toUpperCase(Locale.ROOT)), MLStatLevel.class);
break;
case CLUSTER_LEVEL_STATS:
parseField(
parser,
clusterLevelStats,
input -> MLClusterLevelStat.from(input.toUpperCase(Locale.ROOT)),
MLClusterLevelStat.class
);
break;
case NODE_LEVEL_STATS:
parseField(
parser,
nodeLevelStats,
input -> MLNodeLevelStat.from(input.toUpperCase(Locale.ROOT)),
MLNodeLevelStat.class
);
break;
case ACTION_LEVEL_STATS:
parseField(
parser,
actionLevelStats,
input -> MLActionLevelStat.from(input.toUpperCase(Locale.ROOT)),
MLActionLevelStat.class
);
break;
case NODE_IDS:
parseArrayField(parser, nodeIds);
break;
case ALGORITHMS:
parseField(parser, algorithms, input -> FunctionName.from(input.toUpperCase(Locale.ROOT)), FunctionName.class);
break;
case MODELS:
parseArrayField(parser, models);
break;
case ACTIONS:
parseField(parser, actions, input -> ActionName.from(input.toUpperCase(Locale.ROOT)), ActionName.class);
break;
default:
parser.skipChildren();
break;
}
}
return MLStatsInput
.builder()
.targetStatLevels(targetStatLevels)
.clusterLevelStats(clusterLevelStats)
.nodeLevelStats(nodeLevelStats)
.actionLevelStats(actionLevelStats)
.nodeIds(nodeIds)
.algorithms(algorithms)
.models(models)
.actions(actions)
.build();
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
if (targetStatLevels != null) {
builder.field(TARGET_STAT_LEVEL, targetStatLevels);
}
if (clusterLevelStats != null) {
builder.field(CLUSTER_LEVEL_STATS, clusterLevelStats);
}
if (nodeLevelStats != null) {
builder.field(NODE_LEVEL_STATS, nodeLevelStats);
}
if (actionLevelStats != null) {
builder.field(ACTION_LEVEL_STATS, actionLevelStats);
}
if (nodeIds != null) {
builder.field(NODE_IDS, nodeIds);
}
if (algorithms != null) {
builder.field(ALGORITHMS, algorithms);
}
if (models != null) {
builder.field(MODELS, models);
}
if (actions != null) {
builder.field(ACTIONS, actions);
}
builder.endObject();
return builder;
}
public boolean retrieveAllClusterLevelStats() {
return clusterLevelStats == null || clusterLevelStats.size() == 0;
}
public boolean retrieveAllNodeLevelStats() {
return nodeLevelStats == null || nodeLevelStats.size() == 0;
}
public boolean retrieveAllActionLevelStats() {
return actionLevelStats == null || actionLevelStats.size() == 0;
}
public boolean retrieveStatsOnAllNodes() {
return nodeIds == null || nodeIds.size() == 0;
}
public boolean retrieveStatsForAllAlgos() {
return algorithms == null || algorithms.size() == 0;
}
public boolean retrieveStatsForAllModels() {
return models == null || models.size() == 0;
}
public boolean retrieveStatsForAlgo(FunctionName algoName) {
return retrieveStatsForAllAlgos() || algorithms.contains(algoName);
}
public boolean retrieveStatsForModel(String modelId) {
return retrieveStatsForAllModels() || models.contains(modelId);
}
public boolean retrieveStatsForAction(ActionName actionName) {
return retrieveStatsForAllActions() || actions.contains(actionName);
}
public boolean retrieveStatsForAllActions() {
return actions == null || actions.size() == 0;
}
public boolean retrieveStat(Enum<?> key) {
if (key instanceof MLClusterLevelStat) {
return retrieveAllClusterLevelStats() || clusterLevelStats.contains(key);
}
if (key instanceof MLNodeLevelStat) {
return retrieveAllNodeLevelStats() || nodeLevelStats.contains(key);
}
if (key instanceof MLActionLevelStat) {
return retrieveAllActionLevelStats() || actionLevelStats.contains(key);
}
return false;
}
public boolean onlyRetrieveClusterLevelStats() {
if (targetStatLevels == null || targetStatLevels.size() == 0) {
return false;
}
return !targetStatLevels.contains(MLStatLevel.NODE)
&& !targetStatLevels.contains(MLStatLevel.ALGORITHM)
&& !targetStatLevels.contains(MLStatLevel.MODEL)
&& !targetStatLevels.contains(MLStatLevel.ACTION);
}
public boolean includeAlgoStats() {
return targetStatLevels.contains(MLStatLevel.ALGORITHM) || targetStatLevels.contains(MLStatLevel.ACTION);
}
public boolean includeModelStats() {
return targetStatLevels.contains(MLStatLevel.MODEL) || targetStatLevels.contains(MLStatLevel.ACTION);
}
}