Skip to content

Commit dfbb47d

Browse files
author
Przemysław Witek
authored
Add a "verbose" option to the data frame analytics stats endpoint (#59589)
1 parent adc520b commit dfbb47d

File tree

16 files changed

+213
-20
lines changed

16 files changed

+213
-20
lines changed

docs/reference/ml/df-analytics/apis/get-dfanalytics-stats.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=from]
5858
(Optional, integer)
5959
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=size]
6060

61+
`verbose`::
62+
(Optional, boolean)
63+
include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=verbose]
64+
6165
[role="child_attributes"]
6266
[[ml-get-dfanalytics-stats-response-body]]
6367
==== {api-response-body-title}

docs/reference/ml/ml-shared.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,3 +1389,7 @@ tag::use-null[]
13891389
Defines whether a new series is used as the null series when there is no value
13901390
for the by or partition fields. The default value is `false`.
13911391
end::use-null[]
1392+
1393+
tag::verbose[]
1394+
Defines whether the stats response should be verbose. The default value is `false`.
1395+
end::verbose[]

server/src/main/java/org/elasticsearch/common/Strings.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,16 @@ public static String toString(ToXContent toXContent) {
761761
return toString(toXContent, false, false);
762762
}
763763

764+
/**
765+
* Return a {@link String} that is the json representation of the provided {@link ToXContent}.
766+
* Wraps the output into an anonymous object if needed.
767+
* Allows to configure the params.
768+
* The content is not pretty-printed nor human readable.
769+
*/
770+
public static String toString(ToXContent toXContent, ToXContent.Params params) {
771+
return toString(toXContent, params, false, false);
772+
}
773+
764774
/**
765775
* Returns a string representation of the builder (only applicable for text based xcontent).
766776
* @param xContentBuilder builder containing an object to converted to a string
@@ -776,12 +786,22 @@ public static String toString(XContentBuilder xContentBuilder) {
776786
*
777787
*/
778788
public static String toString(ToXContent toXContent, boolean pretty, boolean human) {
789+
return toString(toXContent, ToXContent.EMPTY_PARAMS, pretty, human);
790+
}
791+
792+
/**
793+
* Return a {@link String} that is the json representation of the provided {@link ToXContent}.
794+
* Wraps the output into an anonymous object if needed.
795+
* Allows to configure the params.
796+
* Allows to control whether the outputted json needs to be pretty printed and human readable.
797+
*/
798+
private static String toString(ToXContent toXContent, ToXContent.Params params, boolean pretty, boolean human) {
779799
try {
780800
XContentBuilder builder = createBuilder(pretty, human);
781801
if (toXContent.isFragment()) {
782802
builder.startObject();
783803
}
784-
toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS);
804+
toXContent.toXContent(builder, params);
785805
if (toXContent.isFragment()) {
786806
builder.endObject();
787807
}

server/src/test/java/org/elasticsearch/common/StringsTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.elasticsearch.common.xcontent.ToXContentObject;
2525
import org.elasticsearch.test.ESTestCase;
2626

27+
import java.util.Collections;
28+
2729
import static org.hamcrest.Matchers.containsString;
2830
import static org.hamcrest.Matchers.is;
2931

@@ -98,6 +100,16 @@ public void testToStringToXContent() {
98100
}
99101
}
100102

103+
public void testToStringToXContentWithOrWithoutParams() {
104+
ToXContent toXContent = (builder, params) -> builder.field("color_from_param", params.param("color", "red"));
105+
// Rely on the default value of "color" param when params are not passed
106+
assertThat(Strings.toString(toXContent), containsString("\"color_from_param\":\"red\""));
107+
// Pass "color" param explicitly
108+
assertThat(
109+
Strings.toString(toXContent, new ToXContent.MapParams(Collections.singletonMap("color", "blue"))),
110+
containsString("\"color_from_param\":\"blue\""));
111+
}
112+
101113
public void testSplitStringToSet() {
102114
assertEquals(Strings.tokenizeByCommaToSet(null), Sets.newHashSet());
103115
assertEquals(Strings.tokenizeByCommaToSet(""), Sets.newHashSet());

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.elasticsearch.xpack.core.ml.dataframe.stats.common.DataCounts;
3434
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
3535
import org.elasticsearch.xpack.core.ml.utils.PhaseProgress;
36+
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
3637

3738
import java.io.IOException;
3839
import java.util.Arrays;
@@ -141,7 +142,9 @@ public boolean equals(Object obj) {
141142
return false;
142143
}
143144
Request other = (Request) obj;
144-
return Objects.equals(id, other.id) && allowNoMatch == other.allowNoMatch && Objects.equals(pageParams, other.pageParams);
145+
return Objects.equals(id, other.id)
146+
&& allowNoMatch == other.allowNoMatch
147+
&& Objects.equals(pageParams, other.pageParams);
145148
}
146149
}
147150

@@ -154,6 +157,9 @@ public RequestBuilder(ElasticsearchClient client, GetDataFrameAnalyticsStatsActi
154157

155158
public static class Response extends BaseTasksResponse implements ToXContentObject {
156159

160+
/** Name of the response's REST param which is used to determine whether this response should be verbose. */
161+
public static final String VERBOSE = "verbose";
162+
157163
public static class Stats implements ToXContentObject, Writeable {
158164

159165
private final String id;
@@ -295,12 +301,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
295301
// TODO: Have callers wrap the content with an object as they choose rather than forcing it upon them
296302
builder.startObject();
297303
{
298-
toUnwrappedXContent(builder);
304+
toUnwrappedXContent(builder, params);
299305
}
300306
return builder.endObject();
301307
}
302308

303-
public XContentBuilder toUnwrappedXContent(XContentBuilder builder) throws IOException {
309+
private XContentBuilder toUnwrappedXContent(XContentBuilder builder, Params params) throws IOException {
304310
builder.field(DataFrameAnalyticsConfig.ID.getPreferredName(), id);
305311
builder.field("state", state.toString());
306312
if (failureReason != null) {
@@ -313,7 +319,12 @@ public XContentBuilder toUnwrappedXContent(XContentBuilder builder) throws IOExc
313319
builder.field("memory_usage", memoryUsage);
314320
if (analysisStats != null) {
315321
builder.startObject("analysis_stats");
316-
builder.field(analysisStats.getWriteableName(), analysisStats);
322+
builder.field(
323+
analysisStats.getWriteableName(),
324+
analysisStats,
325+
new MapParams(
326+
Collections.singletonMap(
327+
ToXContentParams.FOR_INTERNAL_STORAGE, Boolean.toString(params.paramAsBoolean(VERBOSE, false)))));
317328
builder.endObject();
318329
}
319330
if (node != null) {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/ClassificationStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
115115
builder.field(ITERATION.getPreferredName(), iteration);
116116
builder.field(HYPERPARAMETERS.getPreferredName(), hyperparameters);
117117
builder.field(TIMING_STATS.getPreferredName(), timingStats);
118-
builder.field(VALIDATION_LOSS.getPreferredName(), validationLoss);
118+
builder.field(VALIDATION_LOSS.getPreferredName(), validationLoss, params);
119119
builder.endObject();
120120
return builder;
121121
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/ValidationLoss.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.xcontent.XContentBuilder;
1515
import org.elasticsearch.common.xcontent.XContentParser;
1616
import org.elasticsearch.xpack.core.ml.dataframe.stats.common.FoldValues;
17+
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
1718

1819
import java.io.IOException;
1920
import java.util.List;
@@ -62,7 +63,9 @@ public void writeTo(StreamOutput out) throws IOException {
6263
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
6364
builder.startObject();
6465
builder.field(LOSS_TYPE.getPreferredName(), lossType);
65-
builder.field(FOLD_VALUES.getPreferredName(), foldValues);
66+
if (params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false)) {
67+
builder.field(FOLD_VALUES.getPreferredName(), foldValues);
68+
}
6669
builder.endObject();
6770
return builder;
6871
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/RegressionStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
115115
builder.field(ITERATION.getPreferredName(), iteration);
116116
builder.field(HYPERPARAMETERS.getPreferredName(), hyperparameters);
117117
builder.field(TIMING_STATS.getPreferredName(), timingStats);
118-
builder.field(VALIDATION_LOSS.getPreferredName(), validationLoss);
118+
builder.field(VALIDATION_LOSS.getPreferredName(), validationLoss, params);
119119
builder.endObject();
120120
return builder;
121121
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/ValidationLoss.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.xcontent.XContentBuilder;
1515
import org.elasticsearch.common.xcontent.XContentParser;
1616
import org.elasticsearch.xpack.core.ml.dataframe.stats.common.FoldValues;
17+
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
1718

1819
import java.io.IOException;
1920
import java.util.List;
@@ -62,7 +63,9 @@ public void writeTo(StreamOutput out) throws IOException {
6263
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
6364
builder.startObject();
6465
builder.field(LOSS_TYPE.getPreferredName(), lossType);
65-
builder.field(FOLD_VALUES.getPreferredName(), foldValues);
66+
if (params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false)) {
67+
builder.field(FOLD_VALUES.getPreferredName(), foldValues);
68+
}
6669
builder.endObject();
6770
return builder;
6871
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsStatsActionRequestTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.elasticsearch.test.ESTestCase;
1010

1111
import static org.hamcrest.Matchers.equalTo;
12+
import static org.hamcrest.Matchers.is;
1213
import static org.hamcrest.Matchers.notNullValue;
1314

1415
public class GetDataFrameAnalyticsStatsActionRequestTests extends ESTestCase {
@@ -31,4 +32,11 @@ public void testSetId_GivenString() {
3132

3233
assertThat(request.getId(), equalTo("foo"));
3334
}
35+
36+
public void testSetAllowNoMatch() {
37+
GetDataFrameAnalyticsStatsAction.Request request = new GetDataFrameAnalyticsStatsAction.Request();
38+
assertThat(request.isAllowNoMatch(), is(true));
39+
request.setAllowNoMatch(false);
40+
assertThat(request.isAllowNoMatch(), is(false));
41+
}
3442
}

0 commit comments

Comments
 (0)