Skip to content

Commit 468b15e

Browse files
authored
[ML][HLRC] adds new running_state field to datafeed stats (#73926) (#74025)
Adds the new running_state field to the High Level REST client. The new field is introduced in 7.14 through PR: #73926
1 parent d697f01 commit 468b15e

File tree

5 files changed

+153
-5
lines changed

5 files changed

+153
-5
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedStats.java

+22-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ public class DatafeedStats implements ToXContentObject {
3232
private final String assignmentExplanation;
3333
@Nullable
3434
private final DatafeedTimingStats timingStats;
35+
@Nullable
36+
private final RunningState runningState;
3537

3638
public static final ParseField ASSIGNMENT_EXPLANATION = new ParseField("assignment_explanation");
3739
public static final ParseField NODE = new ParseField("node");
3840
public static final ParseField TIMING_STATS = new ParseField("timing_stats");
41+
public static final ParseField RUNNING_STATE = new ParseField("running_state");
3942

4043
public static final ConstructingObjectParser<DatafeedStats, Void> PARSER = new ConstructingObjectParser<>("datafeed_stats",
4144
true,
@@ -45,7 +48,8 @@ public class DatafeedStats implements ToXContentObject {
4548
NodeAttributes nodeAttributes = (NodeAttributes)a[2];
4649
String assignmentExplanation = (String)a[3];
4750
DatafeedTimingStats timingStats = (DatafeedTimingStats)a[4];
48-
return new DatafeedStats(datafeedId, datafeedState, nodeAttributes, assignmentExplanation, timingStats);
51+
RunningState runningState = (RunningState) a[5];
52+
return new DatafeedStats(datafeedId, datafeedState, nodeAttributes, assignmentExplanation, timingStats, runningState);
4953
} );
5054

5155
static {
@@ -54,15 +58,21 @@ public class DatafeedStats implements ToXContentObject {
5458
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), NodeAttributes.PARSER, NODE);
5559
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), ASSIGNMENT_EXPLANATION);
5660
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), DatafeedTimingStats.PARSER, TIMING_STATS);
61+
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), RunningState.PARSER, RUNNING_STATE);
5762
}
5863

59-
public DatafeedStats(String datafeedId, DatafeedState datafeedState, @Nullable NodeAttributes node,
60-
@Nullable String assignmentExplanation, @Nullable DatafeedTimingStats timingStats) {
64+
public DatafeedStats(String datafeedId,
65+
DatafeedState datafeedState,
66+
@Nullable NodeAttributes node,
67+
@Nullable String assignmentExplanation,
68+
@Nullable DatafeedTimingStats timingStats,
69+
@Nullable RunningState runningState) {
6170
this.datafeedId = Objects.requireNonNull(datafeedId);
6271
this.datafeedState = Objects.requireNonNull(datafeedState);
6372
this.node = node;
6473
this.assignmentExplanation = assignmentExplanation;
6574
this.timingStats = timingStats;
75+
this.runningState = runningState;
6676
}
6777

6878
public String getDatafeedId() {
@@ -85,6 +95,10 @@ public DatafeedTimingStats getDatafeedTimingStats() {
8595
return timingStats;
8696
}
8797

98+
public RunningState getRunningState() {
99+
return runningState;
100+
}
101+
88102
@Override
89103
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
90104
builder.startObject();
@@ -112,13 +126,16 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par
112126
if (timingStats != null) {
113127
builder.field(TIMING_STATS.getPreferredName(), timingStats);
114128
}
129+
if (runningState != null) {
130+
builder.field(RUNNING_STATE.getPreferredName(), runningState);
131+
}
115132
builder.endObject();
116133
return builder;
117134
}
118135

119136
@Override
120137
public int hashCode() {
121-
return Objects.hash(datafeedId, datafeedState.toString(), node, assignmentExplanation, timingStats);
138+
return Objects.hash(datafeedId, datafeedState.toString(), node, assignmentExplanation, timingStats, runningState);
122139
}
123140

124141
@Override
@@ -134,6 +151,7 @@ public boolean equals(Object obj) {
134151
Objects.equals(this.datafeedState, other.datafeedState) &&
135152
Objects.equals(this.node, other.node) &&
136153
Objects.equals(this.assignmentExplanation, other.assignmentExplanation) &&
154+
Objects.equals(this.runningState, other.runningState) &&
137155
Objects.equals(this.timingStats, other.timingStats);
138156
}
139157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.client.ml.datafeed;
10+
11+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
12+
import org.elasticsearch.common.xcontent.ParseField;
13+
import org.elasticsearch.common.xcontent.ToXContentObject;
14+
import org.elasticsearch.common.xcontent.XContentBuilder;
15+
16+
import java.io.IOException;
17+
import java.util.Objects;
18+
19+
public class RunningState implements ToXContentObject {
20+
21+
private static final ParseField REAL_TIME_CONFIGURED = new ParseField("real_time_configured");
22+
private static final ParseField REAL_TIME_RUNNING = new ParseField("real_time_running");
23+
24+
public static final ConstructingObjectParser<RunningState, Void> PARSER = new ConstructingObjectParser<>(
25+
"datafeed_running_state",
26+
true,
27+
a -> new RunningState((Boolean)a[0], (Boolean)a[1])
28+
);
29+
30+
static {
31+
PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), REAL_TIME_CONFIGURED);
32+
PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), REAL_TIME_RUNNING);
33+
}
34+
35+
// Is the datafeed a "realtime" datafeed, meaning it was started without an end_time
36+
private final boolean realTimeConfigured;
37+
// Has the reading historical data has finished and are we now running on "real-time" data
38+
private final boolean realTimeRunning;
39+
40+
public RunningState(boolean realTimeConfigured, boolean realTimeRunning) {
41+
this.realTimeConfigured = realTimeConfigured;
42+
this.realTimeRunning = realTimeRunning;
43+
}
44+
45+
/**
46+
* Indicates if the datafeed is configured to run in real time
47+
*
48+
* @return true if the datafeed is configured to run in real time.
49+
*/
50+
public boolean isRealTimeConfigured() {
51+
return realTimeConfigured;
52+
}
53+
54+
/**
55+
* Indicates if the datafeed has processed all historical data available at the start time and is now processing "real-time" data.
56+
* @return true if the datafeed is now running in real-time
57+
*/
58+
public boolean isRealTimeRunning() {
59+
return realTimeRunning;
60+
}
61+
62+
@Override
63+
public boolean equals(Object o) {
64+
if (this == o) return true;
65+
if (o == null || getClass() != o.getClass()) return false;
66+
RunningState that = (RunningState) o;
67+
return realTimeConfigured == that.realTimeConfigured && realTimeRunning == that.realTimeRunning;
68+
}
69+
70+
@Override
71+
public int hashCode() {
72+
return Objects.hash(realTimeConfigured, realTimeRunning);
73+
}
74+
75+
@Override
76+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
77+
builder.startObject();
78+
builder.field(REAL_TIME_CONFIGURED.getPreferredName(), realTimeConfigured);
79+
builder.field(REAL_TIME_RUNNING.getPreferredName(), realTimeRunning);
80+
builder.endObject();
81+
return builder;
82+
}
83+
84+
85+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java

+2
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,8 @@ public void testGetDatafeedStats() throws Exception {
814814
assertThat(response.datafeedStats(), hasSize(1));
815815
assertThat(response.datafeedStats().get(0).getDatafeedId(), equalTo(datafeedId1));
816816
assertThat(response.datafeedStats().get(0).getDatafeedState().toString(), equalTo(DatafeedState.STARTED.toString()));
817+
assertThat(response.datafeedStats().get(0).getRunningState(), is(notNullValue()));
818+
assertThat(response.datafeedStats().get(0).getRunningState().isRealTimeConfigured(), is(true));
817819

818820
// Test getting all explicitly
819821
request = GetDatafeedStatsRequest.getAllDatafeedStatsRequest();

client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedStatsTests.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ public static DatafeedStats createRandomInstance() {
4040
}
4141
String assignmentReason = randomBoolean() ? randomAlphaOfLength(10) : null;
4242
DatafeedTimingStats timingStats = DatafeedTimingStatsTests.createRandomInstance();
43-
return new DatafeedStats(datafeedId, datafeedState, nodeAttributes, assignmentReason, timingStats);
43+
return new DatafeedStats(
44+
datafeedId,
45+
datafeedState,
46+
nodeAttributes,
47+
assignmentReason,
48+
timingStats,
49+
randomBoolean() ? null : RunningStateTests.createRandomInstance()
50+
);
4451
}
4552

4653
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
package org.elasticsearch.client.ml.datafeed;
9+
10+
import org.elasticsearch.common.xcontent.XContentParser;
11+
import org.elasticsearch.test.AbstractXContentTestCase;
12+
13+
import java.io.IOException;
14+
15+
16+
public class RunningStateTests extends AbstractXContentTestCase<RunningState> {
17+
18+
public static RunningState createRandomInstance() {
19+
return new RunningState(randomBoolean(), randomBoolean());
20+
}
21+
22+
@Override
23+
protected RunningState createTestInstance() {
24+
return createRandomInstance();
25+
}
26+
27+
@Override
28+
protected RunningState doParseInstance(XContentParser parser) throws IOException {
29+
return RunningState.PARSER.apply(parser, null);
30+
}
31+
32+
@Override
33+
protected boolean supportsUnknownFields() {
34+
return true;
35+
}
36+
}

0 commit comments

Comments
 (0)