|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.dataframe.transforms.pivot; |
| 7 | + |
| 8 | +import org.elasticsearch.common.ParseField; |
| 9 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 10 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 11 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 12 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 13 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 14 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 15 | +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.time.ZoneId; |
| 19 | +import java.time.ZoneOffset; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | + |
| 23 | +public class DateHistogramGroupSource extends SingleGroupSource<DateHistogramGroupSource> { |
| 24 | + |
| 25 | + private static final String NAME = "data_frame_date_histogram_group"; |
| 26 | + private static final ParseField TIME_ZONE = new ParseField("time_zone"); |
| 27 | + private static final ParseField FORMAT = new ParseField("format"); |
| 28 | + |
| 29 | + private static final ConstructingObjectParser<DateHistogramGroupSource, Void> STRICT_PARSER = createParser(false); |
| 30 | + private static final ConstructingObjectParser<DateHistogramGroupSource, Void> LENIENT_PARSER = createParser(true); |
| 31 | + private long interval = 0; |
| 32 | + private DateHistogramInterval dateHistogramInterval; |
| 33 | + private String format; |
| 34 | + private ZoneId timeZone; |
| 35 | + |
| 36 | + public DateHistogramGroupSource(String field) { |
| 37 | + super(field); |
| 38 | + } |
| 39 | + |
| 40 | + public DateHistogramGroupSource(StreamInput in) throws IOException { |
| 41 | + super(in); |
| 42 | + this.interval = in.readLong(); |
| 43 | + this.dateHistogramInterval = in.readOptionalWriteable(DateHistogramInterval::new); |
| 44 | + this.timeZone = in.readOptionalZoneId(); |
| 45 | + this.format = in.readOptionalString(); |
| 46 | + } |
| 47 | + |
| 48 | + private static ConstructingObjectParser<DateHistogramGroupSource, Void> createParser(boolean lenient) { |
| 49 | + ConstructingObjectParser<DateHistogramGroupSource, Void> parser = new ConstructingObjectParser<>(NAME, lenient, (args) -> { |
| 50 | + String field = (String) args[0]; |
| 51 | + return new DateHistogramGroupSource(field); |
| 52 | + }); |
| 53 | + |
| 54 | + SingleGroupSource.declareValuesSourceFields(parser, null); |
| 55 | + |
| 56 | + parser.declareField((histogram, interval) -> { |
| 57 | + if (interval instanceof Long) { |
| 58 | + histogram.setInterval((long) interval); |
| 59 | + } else { |
| 60 | + histogram.setDateHistogramInterval((DateHistogramInterval) interval); |
| 61 | + } |
| 62 | + }, p -> { |
| 63 | + if (p.currentToken() == XContentParser.Token.VALUE_NUMBER) { |
| 64 | + return p.longValue(); |
| 65 | + } else { |
| 66 | + return new DateHistogramInterval(p.text()); |
| 67 | + } |
| 68 | + }, HistogramGroupSource.INTERVAL, ObjectParser.ValueType.LONG); |
| 69 | + |
| 70 | + parser.declareField(DateHistogramGroupSource::setTimeZone, p -> { |
| 71 | + if (p.currentToken() == XContentParser.Token.VALUE_STRING) { |
| 72 | + return ZoneId.of(p.text()); |
| 73 | + } else { |
| 74 | + return ZoneOffset.ofHours(p.intValue()); |
| 75 | + } |
| 76 | + }, TIME_ZONE, ObjectParser.ValueType.LONG); |
| 77 | + |
| 78 | + parser.declareString(DateHistogramGroupSource::setFormat, FORMAT); |
| 79 | + return parser; |
| 80 | + } |
| 81 | + |
| 82 | + public static DateHistogramGroupSource fromXContent(final XContentParser parser, boolean lenient) throws IOException { |
| 83 | + return lenient ? LENIENT_PARSER.apply(parser, null) : STRICT_PARSER.apply(parser, null); |
| 84 | + } |
| 85 | + |
| 86 | + public long getInterval() { |
| 87 | + return interval; |
| 88 | + } |
| 89 | + |
| 90 | + public void setInterval(long interval) { |
| 91 | + if (interval < 1) { |
| 92 | + throw new IllegalArgumentException("[interval] must be greater than or equal to 1."); |
| 93 | + } |
| 94 | + this.interval = interval; |
| 95 | + } |
| 96 | + |
| 97 | + public DateHistogramInterval getDateHistogramInterval() { |
| 98 | + return dateHistogramInterval; |
| 99 | + } |
| 100 | + |
| 101 | + public void setDateHistogramInterval(DateHistogramInterval dateHistogramInterval) { |
| 102 | + if (dateHistogramInterval == null) { |
| 103 | + throw new IllegalArgumentException("[dateHistogramInterval] must not be null"); |
| 104 | + } |
| 105 | + this.dateHistogramInterval = dateHistogramInterval; |
| 106 | + } |
| 107 | + |
| 108 | + public String getFormat() { |
| 109 | + return format; |
| 110 | + } |
| 111 | + |
| 112 | + public void setFormat(String format) { |
| 113 | + this.format = format; |
| 114 | + } |
| 115 | + |
| 116 | + public ZoneId getTimeZone() { |
| 117 | + return timeZone; |
| 118 | + } |
| 119 | + |
| 120 | + public void setTimeZone(ZoneId timeZone) { |
| 121 | + this.timeZone = timeZone; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void writeTo(StreamOutput out) throws IOException { |
| 126 | + out.writeOptionalString(field); |
| 127 | + out.writeLong(interval); |
| 128 | + out.writeOptionalWriteable(dateHistogramInterval); |
| 129 | + out.writeOptionalZoneId(timeZone); |
| 130 | + out.writeOptionalString(format); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 135 | + builder.startObject(); |
| 136 | + if (field != null) { |
| 137 | + builder.field(FIELD.getPreferredName(), field); |
| 138 | + } |
| 139 | + if (dateHistogramInterval == null) { |
| 140 | + builder.field(HistogramGroupSource.INTERVAL.getPreferredName(), interval); |
| 141 | + } else { |
| 142 | + builder.field(HistogramGroupSource.INTERVAL.getPreferredName(), dateHistogramInterval.toString()); |
| 143 | + } |
| 144 | + if (timeZone != null) { |
| 145 | + builder.field(TIME_ZONE.getPreferredName(), timeZone.toString()); |
| 146 | + } |
| 147 | + if (format != null) { |
| 148 | + builder.field(FORMAT.getPreferredName(), format); |
| 149 | + } |
| 150 | + builder.endObject(); |
| 151 | + return builder; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public boolean equals(Object other) { |
| 156 | + if (this == other) { |
| 157 | + return true; |
| 158 | + } |
| 159 | + |
| 160 | + if (other == null || getClass() != other.getClass()) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + final DateHistogramGroupSource that = (DateHistogramGroupSource) other; |
| 165 | + |
| 166 | + return Objects.equals(this.field, that.field) && |
| 167 | + Objects.equals(interval, that.interval) && |
| 168 | + Objects.equals(dateHistogramInterval, that.dateHistogramInterval) && |
| 169 | + Objects.equals(timeZone, that.timeZone) && |
| 170 | + Objects.equals(format, that.format); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public int hashCode() { |
| 175 | + return Objects.hash(field, interval, dateHistogramInterval, timeZone, format); |
| 176 | + } |
| 177 | +} |
0 commit comments