|
@@ -6,11 +6,14 @@
|
|
|
|
|
|
package org.elasticsearch.xpack.core.transform.transforms;
|
|
|
|
|
|
+import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.action.ActionRequestValidationException;
|
|
|
+import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
import org.elasticsearch.common.io.stream.Writeable;
|
|
|
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
|
|
|
+import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
|
|
|
import org.elasticsearch.common.xcontent.ToXContentObject;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentParser;
|
|
@@ -28,33 +31,52 @@ public class SettingsConfig implements Writeable, ToXContentObject {
|
|
|
|
|
|
private static final int DEFAULT_MAX_PAGE_SEARCH_SIZE = -1;
|
|
|
private static final float DEFAULT_DOCS_PER_SECOND = -1F;
|
|
|
+ private static final int DEFAULT_DATES_AS_EPOCH_MILLIS = -1;
|
|
|
|
|
|
private static ConstructingObjectParser<SettingsConfig, Void> createParser(boolean lenient) {
|
|
|
ConstructingObjectParser<SettingsConfig, Void> parser = new ConstructingObjectParser<>(
|
|
|
"transform_config_settings",
|
|
|
lenient,
|
|
|
- args -> new SettingsConfig((Integer) args[0], (Float) args[1])
|
|
|
+ args -> new SettingsConfig((Integer) args[0], (Float) args[1], (Integer) args[2])
|
|
|
);
|
|
|
parser.declareIntOrNull(optionalConstructorArg(), DEFAULT_MAX_PAGE_SEARCH_SIZE, TransformField.MAX_PAGE_SEARCH_SIZE);
|
|
|
parser.declareFloatOrNull(optionalConstructorArg(), DEFAULT_DOCS_PER_SECOND, TransformField.DOCS_PER_SECOND);
|
|
|
+ // this boolean requires 4 possible values: true, false, not_specified, default, therefore using a custom parser
|
|
|
+ parser.declareField(
|
|
|
+ optionalConstructorArg(),
|
|
|
+ p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? DEFAULT_DATES_AS_EPOCH_MILLIS : p.booleanValue() ? 1 : 0,
|
|
|
+ TransformField.DATES_AS_EPOCH_MILLIS,
|
|
|
+ ValueType.BOOLEAN_OR_NULL
|
|
|
+ );
|
|
|
return parser;
|
|
|
}
|
|
|
|
|
|
private final Integer maxPageSearchSize;
|
|
|
private final Float docsPerSecond;
|
|
|
+ private final Integer datesAsEpochMillis;
|
|
|
|
|
|
public SettingsConfig() {
|
|
|
- this(null, null);
|
|
|
+ this(null, null, (Integer) null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public SettingsConfig(Integer maxPageSearchSize, Float docsPerSecond, Boolean datesAsEpochMillis) {
|
|
|
+ this(maxPageSearchSize, docsPerSecond, datesAsEpochMillis == null ? null : datesAsEpochMillis ? 1 : 0);
|
|
|
}
|
|
|
|
|
|
- public SettingsConfig(Integer maxPageSearchSize, Float docsPerSecond) {
|
|
|
+ public SettingsConfig(Integer maxPageSearchSize, Float docsPerSecond, Integer datesAsEpochMillis) {
|
|
|
this.maxPageSearchSize = maxPageSearchSize;
|
|
|
this.docsPerSecond = docsPerSecond;
|
|
|
+ this.datesAsEpochMillis = datesAsEpochMillis;
|
|
|
}
|
|
|
|
|
|
public SettingsConfig(final StreamInput in) throws IOException {
|
|
|
this.maxPageSearchSize = in.readOptionalInt();
|
|
|
this.docsPerSecond = in.readOptionalFloat();
|
|
|
+ if (in.getVersion().onOrAfter(Version.V_8_0_0)) { // todo: change to V_7_11
|
|
|
+ this.datesAsEpochMillis = in.readOptionalInt();
|
|
|
+ } else {
|
|
|
+ this.datesAsEpochMillis = DEFAULT_DATES_AS_EPOCH_MILLIS;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public Integer getMaxPageSearchSize() {
|
|
@@ -65,6 +87,14 @@ public class SettingsConfig implements Writeable, ToXContentObject {
|
|
|
return docsPerSecond;
|
|
|
}
|
|
|
|
|
|
+ public Boolean getDatesAsEpochMillis() {
|
|
|
+ return datesAsEpochMillis != null ? datesAsEpochMillis > 0 : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer getDatesAsEpochMillisForUpdate() {
|
|
|
+ return datesAsEpochMillis;
|
|
|
+ }
|
|
|
+
|
|
|
public ActionRequestValidationException validate(ActionRequestValidationException validationException) {
|
|
|
// TODO: make this dependent on search.max_buckets
|
|
|
if (maxPageSearchSize != null && (maxPageSearchSize < 10 || maxPageSearchSize > 10_000)) {
|
|
@@ -85,6 +115,9 @@ public class SettingsConfig implements Writeable, ToXContentObject {
|
|
|
public void writeTo(StreamOutput out) throws IOException {
|
|
|
out.writeOptionalInt(maxPageSearchSize);
|
|
|
out.writeOptionalFloat(docsPerSecond);
|
|
|
+ if (out.getVersion().onOrAfter(Version.V_8_0_0)) { // todo: change to V_7_11_0
|
|
|
+ out.writeOptionalInt(datesAsEpochMillis);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -97,6 +130,9 @@ public class SettingsConfig implements Writeable, ToXContentObject {
|
|
|
if (docsPerSecond != null && (docsPerSecond.equals(DEFAULT_DOCS_PER_SECOND) == false)) {
|
|
|
builder.field(TransformField.DOCS_PER_SECOND.getPreferredName(), docsPerSecond);
|
|
|
}
|
|
|
+ if (datesAsEpochMillis != null && (datesAsEpochMillis.equals(DEFAULT_DATES_AS_EPOCH_MILLIS) == false)) {
|
|
|
+ builder.field(TransformField.DATES_AS_EPOCH_MILLIS.getPreferredName(), datesAsEpochMillis > 0 ? true : false);
|
|
|
+ }
|
|
|
builder.endObject();
|
|
|
return builder;
|
|
|
}
|
|
@@ -111,12 +147,19 @@ public class SettingsConfig implements Writeable, ToXContentObject {
|
|
|
}
|
|
|
|
|
|
SettingsConfig that = (SettingsConfig) other;
|
|
|
- return Objects.equals(maxPageSearchSize, that.maxPageSearchSize) && Objects.equals(docsPerSecond, that.docsPerSecond);
|
|
|
+ return Objects.equals(maxPageSearchSize, that.maxPageSearchSize)
|
|
|
+ && Objects.equals(docsPerSecond, that.docsPerSecond)
|
|
|
+ && Objects.equals(datesAsEpochMillis, that.datesAsEpochMillis);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int hashCode() {
|
|
|
- return Objects.hash(maxPageSearchSize, docsPerSecond);
|
|
|
+ return Objects.hash(maxPageSearchSize, docsPerSecond, datesAsEpochMillis);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return Strings.toString(this, true, true);
|
|
|
}
|
|
|
|
|
|
public static SettingsConfig fromXContent(final XContentParser parser, boolean lenient) throws IOException {
|
|
@@ -126,13 +169,12 @@ public class SettingsConfig implements Writeable, ToXContentObject {
|
|
|
public static class Builder {
|
|
|
private Integer maxPageSearchSize;
|
|
|
private Float docsPerSecond;
|
|
|
+ private Integer datesAsEpochMillis;
|
|
|
|
|
|
/**
|
|
|
* Default builder
|
|
|
*/
|
|
|
- public Builder() {
|
|
|
-
|
|
|
- }
|
|
|
+ public Builder() {}
|
|
|
|
|
|
/**
|
|
|
* Builder starting from existing settings as base, for the purpose of partially updating settings.
|
|
@@ -142,6 +184,7 @@ public class SettingsConfig implements Writeable, ToXContentObject {
|
|
|
public Builder(SettingsConfig base) {
|
|
|
this.maxPageSearchSize = base.maxPageSearchSize;
|
|
|
this.docsPerSecond = base.docsPerSecond;
|
|
|
+ this.datesAsEpochMillis = base.datesAsEpochMillis;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -172,6 +215,22 @@ public class SettingsConfig implements Writeable, ToXContentObject {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Whether to write the output of a date aggregation as millis since epoch or as formatted string (ISO format).
|
|
|
+ *
|
|
|
+ * Transforms created before 7.11 write dates as epoch_millis. The new default is ISO string.
|
|
|
+ * You can use this setter to configure the old style writing as epoch millis.
|
|
|
+ *
|
|
|
+ * An explicit `null` resets to default.
|
|
|
+ *
|
|
|
+ * @param datesAsEpochMillis true if dates should be written as epoch_millis.
|
|
|
+ * @return the {@link Builder} with datesAsEpochMilli set.
|
|
|
+ */
|
|
|
+ public Builder setDatesAsEpochMillis(Boolean datesAsEpochMillis) {
|
|
|
+ this.datesAsEpochMillis = datesAsEpochMillis == null ? DEFAULT_DATES_AS_EPOCH_MILLIS : datesAsEpochMillis ? 1 : 0;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Update settings according to given settings config.
|
|
|
*
|
|
@@ -189,12 +248,17 @@ public class SettingsConfig implements Writeable, ToXContentObject {
|
|
|
? null
|
|
|
: update.getMaxPageSearchSize();
|
|
|
}
|
|
|
+ if (update.getDatesAsEpochMillisForUpdate() != null) {
|
|
|
+ this.datesAsEpochMillis = update.getDatesAsEpochMillisForUpdate().equals(DEFAULT_DATES_AS_EPOCH_MILLIS)
|
|
|
+ ? null
|
|
|
+ : update.getDatesAsEpochMillisForUpdate();
|
|
|
+ }
|
|
|
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
public SettingsConfig build() {
|
|
|
- return new SettingsConfig(maxPageSearchSize, docsPerSecond);
|
|
|
+ return new SettingsConfig(maxPageSearchSize, docsPerSecond, datesAsEpochMillis);
|
|
|
}
|
|
|
}
|
|
|
}
|