Add result_type field to TimingStats and DatafeedTimingStats documents (#44812) (#44841)

This commit is contained in:
Przemysław Witek 2019-07-25 10:11:55 +02:00 committed by GitHub
parent e0d4544ef6
commit 53f409e5ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 7 deletions

View File

@ -15,6 +15,7 @@ import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.ml.job.results.Result;
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
import java.io.IOException; import java.io.IOException;
@ -38,7 +39,7 @@ public class DatafeedTimingStats implements ToXContentObject, Writeable {
private static ConstructingObjectParser<DatafeedTimingStats, Void> createParser() { private static ConstructingObjectParser<DatafeedTimingStats, Void> createParser() {
ConstructingObjectParser<DatafeedTimingStats, Void> parser = ConstructingObjectParser<DatafeedTimingStats, Void> parser =
new ConstructingObjectParser<>( new ConstructingObjectParser<>(
"datafeed_timing_stats", TYPE.getPreferredName(),
true, true,
args -> { args -> {
String jobId = (String) args[0]; String jobId = (String) args[0];
@ -128,6 +129,9 @@ public class DatafeedTimingStats implements ToXContentObject, Writeable {
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject(); builder.startObject();
if (params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false)) {
builder.field(Result.RESULT_TYPE.getPreferredName(), TYPE.getPreferredName());
}
builder.field(JOB_ID.getPreferredName(), jobId); builder.field(JOB_ID.getPreferredName(), jobId);
builder.field(SEARCH_COUNT.getPreferredName(), searchCount); builder.field(SEARCH_COUNT.getPreferredName(), searchCount);
builder.field(BUCKET_COUNT.getPreferredName(), bucketCount); builder.field(BUCKET_COUNT.getPreferredName(), bucketCount);

View File

@ -15,6 +15,7 @@ import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.config.Job;
import org.elasticsearch.xpack.core.ml.job.results.Result;
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
import java.io.IOException; import java.io.IOException;
@ -195,6 +196,9 @@ public class TimingStats implements ToXContentObject, Writeable {
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(); builder.startObject();
if (params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false)) {
builder.field(Result.RESULT_TYPE.getPreferredName(), TYPE.getPreferredName());
}
builder.field(Job.ID.getPreferredName(), jobId); builder.field(Job.ID.getPreferredName(), jobId);
builder.field(BUCKET_COUNT.getPreferredName(), bucketCount); builder.field(BUCKET_COUNT.getPreferredName(), bucketCount);
if (params.paramAsBoolean(ToXContentParams.INCLUDE_CALCULATED_FIELDS, false)) { if (params.paramAsBoolean(ToXContentParams.INCLUDE_CALCULATED_FIELDS, false)) {

View File

@ -38,6 +38,7 @@ import org.elasticsearch.xpack.core.ml.job.results.Forecast;
import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats; import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats;
import org.elasticsearch.xpack.core.ml.job.results.Influencer; import org.elasticsearch.xpack.core.ml.job.results.Influencer;
import org.elasticsearch.xpack.core.ml.job.results.ModelPlot; import org.elasticsearch.xpack.core.ml.job.results.ModelPlot;
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
@ -130,7 +131,11 @@ public class JobResultsPersister {
* @return this * @return this
*/ */
public Builder persistTimingStats(TimingStats timingStats) { public Builder persistTimingStats(TimingStats timingStats) {
indexResult(TimingStats.documentId(timingStats.getJobId()), timingStats, TimingStats.TYPE.getPreferredName()); indexResult(
TimingStats.documentId(timingStats.getJobId()),
timingStats,
new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")),
TimingStats.TYPE.getPreferredName());
return this; return this;
} }
@ -185,7 +190,11 @@ public class JobResultsPersister {
} }
private void indexResult(String id, ToXContent resultDoc, String resultType) { private void indexResult(String id, ToXContent resultDoc, String resultType) {
try (XContentBuilder content = toXContentBuilder(resultDoc)) { indexResult(id, resultDoc, ToXContent.EMPTY_PARAMS, resultType);
}
private void indexResult(String id, ToXContent resultDoc, ToXContent.Params params, String resultType) {
try (XContentBuilder content = toXContentBuilder(resultDoc, params)) {
bulkRequest.add(new IndexRequest(indexName).id(id).source(content)); bulkRequest.add(new IndexRequest(indexName).id(id).source(content));
} catch (IOException e) { } catch (IOException e) {
logger.error(new ParameterizedMessage("[{}] Error serialising {}", jobId, resultType), e); logger.error(new ParameterizedMessage("[{}] Error serialising {}", jobId, resultType), e);
@ -335,14 +344,18 @@ public class JobResultsPersister {
public IndexResponse persistDatafeedTimingStats(DatafeedTimingStats timingStats, WriteRequest.RefreshPolicy refreshPolicy) { public IndexResponse persistDatafeedTimingStats(DatafeedTimingStats timingStats, WriteRequest.RefreshPolicy refreshPolicy) {
String jobId = timingStats.getJobId(); String jobId = timingStats.getJobId();
logger.trace("[{}] Persisting datafeed timing stats", jobId); logger.trace("[{}] Persisting datafeed timing stats", jobId);
Persistable persistable = new Persistable(jobId, timingStats, DatafeedTimingStats.documentId(timingStats.getJobId())); Persistable persistable = new Persistable(
jobId,
timingStats,
new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")),
DatafeedTimingStats.documentId(timingStats.getJobId()));
persistable.setRefreshPolicy(refreshPolicy); persistable.setRefreshPolicy(refreshPolicy);
return persistable.persist(AnomalyDetectorsIndex.resultsWriteAlias(jobId)).actionGet(); return persistable.persist(AnomalyDetectorsIndex.resultsWriteAlias(jobId)).actionGet();
} }
private XContentBuilder toXContentBuilder(ToXContent obj) throws IOException { private static XContentBuilder toXContentBuilder(ToXContent obj, ToXContent.Params params) throws IOException {
XContentBuilder builder = jsonBuilder(); XContentBuilder builder = jsonBuilder();
obj.toXContent(builder, ToXContent.EMPTY_PARAMS); obj.toXContent(builder, params);
return builder; return builder;
} }
@ -350,12 +363,18 @@ public class JobResultsPersister {
private final String jobId; private final String jobId;
private final ToXContent object; private final ToXContent object;
private final ToXContent.Params params;
private final String id; private final String id;
private WriteRequest.RefreshPolicy refreshPolicy; private WriteRequest.RefreshPolicy refreshPolicy;
Persistable(String jobId, ToXContent object, String id) { Persistable(String jobId, ToXContent object, String id) {
this(jobId, object, ToXContent.EMPTY_PARAMS, id);
}
Persistable(String jobId, ToXContent object, ToXContent.Params params, String id) {
this.jobId = jobId; this.jobId = jobId;
this.object = object; this.object = object;
this.params = params;
this.id = id; this.id = id;
this.refreshPolicy = WriteRequest.RefreshPolicy.NONE; this.refreshPolicy = WriteRequest.RefreshPolicy.NONE;
} }
@ -373,7 +392,7 @@ public class JobResultsPersister {
void persist(String indexName, ActionListener<IndexResponse> listener) { void persist(String indexName, ActionListener<IndexResponse> listener) {
logCall(indexName); logCall(indexName);
try (XContentBuilder content = toXContentBuilder(object)) { try (XContentBuilder content = toXContentBuilder(object, params)) {
IndexRequest indexRequest = new IndexRequest(indexName).id(id).source(content).setRefreshPolicy(refreshPolicy); IndexRequest indexRequest = new IndexRequest(indexName).id(id).source(content).setRefreshPolicy(refreshPolicy);
executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, indexRequest, listener, client::index); executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, indexRequest, listener, client::index);
} catch (IOException e) { } catch (IOException e) {

View File

@ -219,6 +219,7 @@ public class JobResultsPersisterTests extends ESTestCase {
assertThat(indexRequest.index(), equalTo(".ml-anomalies-.write-foo")); assertThat(indexRequest.index(), equalTo(".ml-anomalies-.write-foo"));
assertThat(indexRequest.id(), equalTo("foo_timing_stats")); assertThat(indexRequest.id(), equalTo("foo_timing_stats"));
Map<String, Object> expectedSourceAsMap = new HashMap<>(); Map<String, Object> expectedSourceAsMap = new HashMap<>();
expectedSourceAsMap.put("result_type", "timing_stats");
expectedSourceAsMap.put("job_id", "foo"); expectedSourceAsMap.put("job_id", "foo");
expectedSourceAsMap.put("bucket_count", 7); expectedSourceAsMap.put("bucket_count", 7);
expectedSourceAsMap.put("minimum_bucket_processing_time_ms", 1.0); expectedSourceAsMap.put("minimum_bucket_processing_time_ms", 1.0);
@ -255,6 +256,7 @@ public class JobResultsPersisterTests extends ESTestCase {
assertThat(indexRequest.id(), equalTo("foo_datafeed_timing_stats")); assertThat(indexRequest.id(), equalTo("foo_datafeed_timing_stats"));
assertThat(indexRequest.getRefreshPolicy(), equalTo(WriteRequest.RefreshPolicy.IMMEDIATE)); assertThat(indexRequest.getRefreshPolicy(), equalTo(WriteRequest.RefreshPolicy.IMMEDIATE));
Map<String, Object> expectedSourceAsMap = new HashMap<>(); Map<String, Object> expectedSourceAsMap = new HashMap<>();
expectedSourceAsMap.put("result_type", "datafeed_timing_stats");
expectedSourceAsMap.put("job_id", "foo"); expectedSourceAsMap.put("job_id", "foo");
expectedSourceAsMap.put("search_count", 6); expectedSourceAsMap.put("search_count", 6);
expectedSourceAsMap.put("bucket_count", 66); expectedSourceAsMap.put("bucket_count", 66);