Skip to content

Commit 1355f4f

Browse files
committed
Apply review comments
1 parent e92a8b4 commit 1355f4f

File tree

7 files changed

+30
-34
lines changed

7 files changed

+30
-34
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedsStatsAction.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import org.elasticsearch.xpack.core.ml.datafeed.DatafeedState;
2525
import org.elasticsearch.xpack.core.ml.datafeed.DatafeedTimingStats;
2626
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
27+
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
2728

2829
import java.io.IOException;
30+
import java.util.Collections;
2931
import java.util.Map;
3032
import java.util.Objects;
3133

@@ -38,6 +40,9 @@ public class GetDatafeedsStatsAction extends StreamableResponseActionType<GetDat
3840

3941
public static final String ALL = "_all";
4042
private static final String STATE = "state";
43+
private static final String NODE = "node";
44+
private static final String ASSIGNMENT_EXPLANATION = "assignment_explanation";
45+
private static final String TIMING_STATS = "timing_stats";
4146

4247
private GetDatafeedsStatsAction() {
4348
super(NAME);
@@ -181,7 +186,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
181186
builder.field(DatafeedConfig.ID.getPreferredName(), datafeedId);
182187
builder.field(STATE, datafeedState.toString());
183188
if (node != null) {
184-
builder.startObject("node");
189+
builder.startObject(NODE);
185190
builder.field("id", node.getId());
186191
builder.field("name", node.getName());
187192
builder.field("ephemeral_id", node.getEphemeralId());
@@ -197,11 +202,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
197202
builder.endObject();
198203
}
199204
if (assignmentExplanation != null) {
200-
builder.field("assignment_explanation", assignmentExplanation);
205+
builder.field(ASSIGNMENT_EXPLANATION, assignmentExplanation);
201206
}
202207
if (timingStats != null) {
203-
builder.field("timing_stats");
204-
timingStats.toXContentWithCalculatedFields(builder);
208+
builder.field(
209+
TIMING_STATS,
210+
timingStats,
211+
new MapParams(Collections.singletonMap(ToXContentParams.INCLUDE_CALCULATED_FIELDS, "true")));
205212
}
206213
builder.endObject();
207214
return builder;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetJobsStatsAction.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.TimingStats;
3333
import org.elasticsearch.xpack.core.ml.stats.ForecastStats;
3434
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
35+
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
3536

3637
import java.io.IOException;
3738
import java.util.Collections;
@@ -51,6 +52,8 @@ public class GetJobsStatsAction extends ActionType<GetJobsStatsAction.Response>
5152
private static final String FORECASTS_STATS = "forecasts_stats";
5253
private static final String STATE = "state";
5354
private static final String NODE = "node";
55+
private static final String ASSIGNMENT_EXPLANATION = "assignment_explanation";
56+
private static final String OPEN_TIME = "open_time";
5457
private static final String TIMING_STATS = "timing_stats";
5558

5659
private GetJobsStatsAction() {
@@ -266,14 +269,16 @@ public XContentBuilder toUnwrappedXContent(XContentBuilder builder) throws IOExc
266269
builder.endObject();
267270
}
268271
if (assignmentExplanation != null) {
269-
builder.field("assignment_explanation", assignmentExplanation);
272+
builder.field(ASSIGNMENT_EXPLANATION, assignmentExplanation);
270273
}
271274
if (openTime != null) {
272-
builder.field("open_time", openTime.getStringRep());
275+
builder.field(OPEN_TIME, openTime.getStringRep());
273276
}
274277
if (timingStats != null) {
275-
builder.field(TIMING_STATS);
276-
timingStats.toXContentWithCalculatedFields(builder);
278+
builder.field(
279+
TIMING_STATS,
280+
timingStats,
281+
new MapParams(Collections.singletonMap(ToXContentParams.INCLUDE_CALCULATED_FIELDS, "true")));
277282
}
278283
return builder;
279284
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedTimingStats.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.common.xcontent.ToXContent;
1616
import org.elasticsearch.common.xcontent.ToXContentObject;
1717
import org.elasticsearch.common.xcontent.XContentBuilder;
18+
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
1819

1920
import java.io.IOException;
2021
import java.util.Objects;
@@ -126,20 +127,12 @@ public void writeTo(StreamOutput out) throws IOException {
126127

127128
@Override
128129
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
129-
return toXContent(builder, false);
130-
}
131-
132-
public XContentBuilder toXContentWithCalculatedFields(XContentBuilder builder) throws IOException {
133-
return toXContent(builder, true);
134-
}
135-
136-
private XContentBuilder toXContent(XContentBuilder builder, boolean includeCalculatedFields) throws IOException {
137130
builder.startObject();
138131
builder.field(JOB_ID.getPreferredName(), jobId);
139132
builder.field(SEARCH_COUNT.getPreferredName(), searchCount);
140133
builder.field(BUCKET_COUNT.getPreferredName(), bucketCount);
141134
builder.field(TOTAL_SEARCH_TIME_MS.getPreferredName(), totalSearchTimeMs);
142-
if (includeCalculatedFields) {
135+
if (params.paramAsBoolean(ToXContentParams.INCLUDE_CALCULATED_FIELDS, false)) {
143136
Double avgSearchTimePerBucket = getAvgSearchTimePerBucketMs();
144137
if (avgSearchTimePerBucket != null) {
145138
builder.field(AVG_SEARCH_TIME_PER_BUCKET_MS.getPreferredName(), getAvgSearchTimePerBucketMs());

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappings.java

-6
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,6 @@ private static void addDataCountsMapping(XContentBuilder builder) throws IOExcep
920920
private static void addTimingStatsExceptBucketCountMapping(XContentBuilder builder) throws IOException {
921921
builder
922922
// re-used: BUCKET_COUNT
923-
.startObject(TimingStats.TOTAL_BUCKET_PROCESSING_TIME_MS.getPreferredName())
924-
.field(TYPE, DOUBLE)
925-
.endObject()
926923
.startObject(TimingStats.MIN_BUCKET_PROCESSING_TIME_MS.getPreferredName())
927924
.field(TYPE, DOUBLE)
928925
.endObject()
@@ -951,9 +948,6 @@ private static void addDatafeedTimingStats(XContentBuilder builder) throws IOExc
951948
// re-used: BUCKET_COUNT
952949
.startObject(DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName())
953950
.field(TYPE, DOUBLE)
954-
.endObject()
955-
.startObject(DatafeedTimingStats.AVG_SEARCH_TIME_PER_BUCKET_MS.getPreferredName())
956-
.field(TYPE, DOUBLE)
957951
.endObject();
958952
}
959953

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/TimingStats.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.common.xcontent.ToXContentObject;
1616
import org.elasticsearch.common.xcontent.XContentBuilder;
1717
import org.elasticsearch.xpack.core.ml.job.config.Job;
18+
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
1819

1920
import java.io.IOException;
2021
import java.util.Objects;
@@ -193,18 +194,10 @@ public void writeTo(StreamOutput out) throws IOException {
193194

194195
@Override
195196
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
196-
return toXContent(builder, false);
197-
}
198-
199-
public XContentBuilder toXContentWithCalculatedFields(XContentBuilder builder) throws IOException {
200-
return toXContent(builder, true);
201-
}
202-
203-
private XContentBuilder toXContent(XContentBuilder builder, boolean includeCalculatedFields) throws IOException {
204197
builder.startObject();
205198
builder.field(Job.ID.getPreferredName(), jobId);
206199
builder.field(BUCKET_COUNT.getPreferredName(), bucketCount);
207-
if (includeCalculatedFields) {
200+
if (params.paramAsBoolean(ToXContentParams.INCLUDE_CALCULATED_FIELDS, false)) {
208201
builder.field(TOTAL_BUCKET_PROCESSING_TIME_MS.getPreferredName(), getTotalBucketProcessingTimeMs());
209202
}
210203
if (minBucketProcessingTimeMs != null) {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ReservedFieldNames.java

-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ public final class ReservedFieldNames {
181181
Result.IS_INTERIM.getPreferredName(),
182182

183183
TimingStats.BUCKET_COUNT.getPreferredName(),
184-
TimingStats.TOTAL_BUCKET_PROCESSING_TIME_MS.getPreferredName(),
185184
TimingStats.MIN_BUCKET_PROCESSING_TIME_MS.getPreferredName(),
186185
TimingStats.MAX_BUCKET_PROCESSING_TIME_MS.getPreferredName(),
187186
TimingStats.AVG_BUCKET_PROCESSING_TIME_MS.getPreferredName(),
@@ -190,7 +189,6 @@ public final class ReservedFieldNames {
190189
DatafeedTimingStats.SEARCH_COUNT.getPreferredName(),
191190
DatafeedTimingStats.BUCKET_COUNT.getPreferredName(),
192191
DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName(),
193-
DatafeedTimingStats.AVG_SEARCH_TIME_PER_BUCKET_MS.getPreferredName(),
194192

195193
GetResult._ID,
196194
GetResult._INDEX,

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ToXContentParams.java

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public final class ToXContentParams {
2424
*/
2525
public static final String INCLUDE_TYPE = "include_type";
2626

27+
/**
28+
* When serialising POJOs to X Content this indicates whether the calculated (i.e. not stored) fields
29+
* should be included or not
30+
*/
31+
public static final String INCLUDE_CALCULATED_FIELDS = "include_calculated_fields";
32+
2733
private ToXContentParams() {
2834
}
2935
}

0 commit comments

Comments
 (0)