Skip to content

Commit 53f409e

Browse files
authored
Add result_type field to TimingStats and DatafeedTimingStats documents (#44812) (#44841)
1 parent e0d4544 commit 53f409e

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

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

Lines changed: 5 additions & 1 deletion
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.job.results.Result;
1819
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
1920

2021
import java.io.IOException;
@@ -38,7 +39,7 @@ public class DatafeedTimingStats implements ToXContentObject, Writeable {
3839
private static ConstructingObjectParser<DatafeedTimingStats, Void> createParser() {
3940
ConstructingObjectParser<DatafeedTimingStats, Void> parser =
4041
new ConstructingObjectParser<>(
41-
"datafeed_timing_stats",
42+
TYPE.getPreferredName(),
4243
true,
4344
args -> {
4445
String jobId = (String) args[0];
@@ -128,6 +129,9 @@ public void writeTo(StreamOutput out) throws IOException {
128129
@Override
129130
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
130131
builder.startObject();
132+
if (params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false)) {
133+
builder.field(Result.RESULT_TYPE.getPreferredName(), TYPE.getPreferredName());
134+
}
131135
builder.field(JOB_ID.getPreferredName(), jobId);
132136
builder.field(SEARCH_COUNT.getPreferredName(), searchCount);
133137
builder.field(BUCKET_COUNT.getPreferredName(), bucketCount);

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

Lines changed: 4 additions & 0 deletions
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.job.results.Result;
1819
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
1920

2021
import java.io.IOException;
@@ -195,6 +196,9 @@ public void writeTo(StreamOutput out) throws IOException {
195196
@Override
196197
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
197198
builder.startObject();
199+
if (params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false)) {
200+
builder.field(Result.RESULT_TYPE.getPreferredName(), TYPE.getPreferredName());
201+
}
198202
builder.field(Job.ID.getPreferredName(), jobId);
199203
builder.field(BUCKET_COUNT.getPreferredName(), bucketCount);
200204
if (params.paramAsBoolean(ToXContentParams.INCLUDE_CALCULATED_FIELDS, false)) {

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats;
3939
import org.elasticsearch.xpack.core.ml.job.results.Influencer;
4040
import org.elasticsearch.xpack.core.ml.job.results.ModelPlot;
41+
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
4142

4243
import java.io.IOException;
4344
import java.util.Collections;
@@ -130,7 +131,11 @@ private void persistBucketInfluencersStandalone(String jobId, List<BucketInfluen
130131
* @return this
131132
*/
132133
public Builder persistTimingStats(TimingStats timingStats) {
133-
indexResult(TimingStats.documentId(timingStats.getJobId()), timingStats, TimingStats.TYPE.getPreferredName());
134+
indexResult(
135+
TimingStats.documentId(timingStats.getJobId()),
136+
timingStats,
137+
new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")),
138+
TimingStats.TYPE.getPreferredName());
134139
return this;
135140
}
136141

@@ -185,7 +190,11 @@ public Builder persistForecastRequestStats(ForecastRequestStats forecastRequestS
185190
}
186191

187192
private void indexResult(String id, ToXContent resultDoc, String resultType) {
188-
try (XContentBuilder content = toXContentBuilder(resultDoc)) {
193+
indexResult(id, resultDoc, ToXContent.EMPTY_PARAMS, resultType);
194+
}
195+
196+
private void indexResult(String id, ToXContent resultDoc, ToXContent.Params params, String resultType) {
197+
try (XContentBuilder content = toXContentBuilder(resultDoc, params)) {
189198
bulkRequest.add(new IndexRequest(indexName).id(id).source(content));
190199
} catch (IOException e) {
191200
logger.error(new ParameterizedMessage("[{}] Error serialising {}", jobId, resultType), e);
@@ -335,27 +344,37 @@ public void commitStateWrites(String jobId) {
335344
public IndexResponse persistDatafeedTimingStats(DatafeedTimingStats timingStats, WriteRequest.RefreshPolicy refreshPolicy) {
336345
String jobId = timingStats.getJobId();
337346
logger.trace("[{}] Persisting datafeed timing stats", jobId);
338-
Persistable persistable = new Persistable(jobId, timingStats, DatafeedTimingStats.documentId(timingStats.getJobId()));
347+
Persistable persistable = new Persistable(
348+
jobId,
349+
timingStats,
350+
new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")),
351+
DatafeedTimingStats.documentId(timingStats.getJobId()));
339352
persistable.setRefreshPolicy(refreshPolicy);
340353
return persistable.persist(AnomalyDetectorsIndex.resultsWriteAlias(jobId)).actionGet();
341354
}
342355

343-
private XContentBuilder toXContentBuilder(ToXContent obj) throws IOException {
356+
private static XContentBuilder toXContentBuilder(ToXContent obj, ToXContent.Params params) throws IOException {
344357
XContentBuilder builder = jsonBuilder();
345-
obj.toXContent(builder, ToXContent.EMPTY_PARAMS);
358+
obj.toXContent(builder, params);
346359
return builder;
347360
}
348361

349362
private class Persistable {
350363

351364
private final String jobId;
352365
private final ToXContent object;
366+
private final ToXContent.Params params;
353367
private final String id;
354368
private WriteRequest.RefreshPolicy refreshPolicy;
355369

356370
Persistable(String jobId, ToXContent object, String id) {
371+
this(jobId, object, ToXContent.EMPTY_PARAMS, id);
372+
}
373+
374+
Persistable(String jobId, ToXContent object, ToXContent.Params params, String id) {
357375
this.jobId = jobId;
358376
this.object = object;
377+
this.params = params;
359378
this.id = id;
360379
this.refreshPolicy = WriteRequest.RefreshPolicy.NONE;
361380
}
@@ -373,7 +392,7 @@ ActionFuture<IndexResponse> persist(String indexName) {
373392
void persist(String indexName, ActionListener<IndexResponse> listener) {
374393
logCall(indexName);
375394

376-
try (XContentBuilder content = toXContentBuilder(object)) {
395+
try (XContentBuilder content = toXContentBuilder(object, params)) {
377396
IndexRequest indexRequest = new IndexRequest(indexName).id(id).source(content).setRefreshPolicy(refreshPolicy);
378397
executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, indexRequest, listener, client::index);
379398
} catch (IOException e) {

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsPersisterTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public void testPersistTimingStats() {
219219
assertThat(indexRequest.index(), equalTo(".ml-anomalies-.write-foo"));
220220
assertThat(indexRequest.id(), equalTo("foo_timing_stats"));
221221
Map<String, Object> expectedSourceAsMap = new HashMap<>();
222+
expectedSourceAsMap.put("result_type", "timing_stats");
222223
expectedSourceAsMap.put("job_id", "foo");
223224
expectedSourceAsMap.put("bucket_count", 7);
224225
expectedSourceAsMap.put("minimum_bucket_processing_time_ms", 1.0);
@@ -255,6 +256,7 @@ public void testPersistDatafeedTimingStats() {
255256
assertThat(indexRequest.id(), equalTo("foo_datafeed_timing_stats"));
256257
assertThat(indexRequest.getRefreshPolicy(), equalTo(WriteRequest.RefreshPolicy.IMMEDIATE));
257258
Map<String, Object> expectedSourceAsMap = new HashMap<>();
259+
expectedSourceAsMap.put("result_type", "datafeed_timing_stats");
258260
expectedSourceAsMap.put("job_id", "foo");
259261
expectedSourceAsMap.put("search_count", 6);
260262
expectedSourceAsMap.put("bucket_count", 66);

0 commit comments

Comments
 (0)