Skip to content

Commit d1b22db

Browse files
committed
some config objs
1 parent 1c42178 commit d1b22db

17 files changed

+1494
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.dataframe.transforms;
21+
22+
import org.elasticsearch.client.dataframe.transforms.pivot.PivotConfig;
23+
import org.elasticsearch.common.ParseField;
24+
import org.elasticsearch.common.Strings;
25+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
26+
import org.elasticsearch.common.xcontent.ToXContentObject;
27+
import org.elasticsearch.common.xcontent.XContentBuilder;
28+
import org.elasticsearch.common.xcontent.XContentParser;
29+
import org.elasticsearch.index.query.MatchAllQueryBuilder;
30+
31+
import java.io.IOException;
32+
import java.util.Objects;
33+
34+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
35+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
36+
37+
public class DataFrameTransformConfig implements ToXContentObject {
38+
39+
public static final ParseField ID = new ParseField("id");
40+
public static final ParseField SOURCE = new ParseField("source");
41+
public static final ParseField DEST = new ParseField("dest");
42+
public static final ParseField QUERY = new ParseField("query");
43+
// types of transforms
44+
public static final ParseField PIVOT_TRANSFORM = new ParseField("pivot");
45+
46+
private final String id;
47+
private final String source;
48+
private final String dest;
49+
private final QueryConfig queryConfig;
50+
private final PivotConfig pivotConfig;
51+
52+
private static final ConstructingObjectParser<DataFrameTransformConfig, String> PARSER =
53+
new ConstructingObjectParser<>("data_frame_transform", true,
54+
(args) -> {
55+
String id = (String) args[0];
56+
String source = (String) args[1];
57+
String dest = (String) args[2];
58+
// default handling: if the user does not specify a query, we default to match_all
59+
QueryConfig queryConfig = (args[3] == null) ? new QueryConfig(new MatchAllQueryBuilder()) : (QueryConfig) args[3];
60+
PivotConfig pivotConfig = (PivotConfig) args[4];
61+
return new DataFrameTransformConfig(id, source, dest, queryConfig, pivotConfig);
62+
});
63+
64+
static {
65+
PARSER.declareString(optionalConstructorArg(), ID);
66+
PARSER.declareString(constructorArg(), SOURCE);
67+
PARSER.declareString(constructorArg(), DEST);
68+
PARSER.declareObject(optionalConstructorArg(), (p, c) -> QueryConfig.fromXContent(p), QUERY);
69+
PARSER.declareObject(optionalConstructorArg(), (p, c) -> PivotConfig.fromXContent(p), PIVOT_TRANSFORM);
70+
}
71+
72+
public static DataFrameTransformConfig fromXContent(final XContentParser parser) {
73+
return PARSER.apply(parser, null);
74+
}
75+
76+
77+
public DataFrameTransformConfig(final String id,
78+
final String source,
79+
final String dest,
80+
final QueryConfig queryConfig,
81+
final PivotConfig pivotConfig) {
82+
this.id = Objects.requireNonNull(id);
83+
this.source = Objects.requireNonNull(source);
84+
this.dest = Objects.requireNonNull(dest);
85+
this.queryConfig = queryConfig;
86+
this.pivotConfig = pivotConfig;
87+
}
88+
89+
public String getId() {
90+
return id;
91+
}
92+
93+
// TODO should this function be removed?
94+
public String getCron() {
95+
return "*";
96+
}
97+
98+
public String getSource() {
99+
return source;
100+
}
101+
102+
public String getDestination() {
103+
return dest;
104+
}
105+
106+
public PivotConfig getPivotConfig() {
107+
return pivotConfig;
108+
}
109+
110+
public QueryConfig getQueryConfig() {
111+
return queryConfig;
112+
}
113+
114+
public boolean isValid() {
115+
if (queryConfig != null && queryConfig.isValid() == false) {
116+
return false;
117+
}
118+
119+
if (pivotConfig == null || pivotConfig.isValid() == false) {
120+
return false;
121+
}
122+
123+
return true;
124+
}
125+
126+
@Override
127+
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
128+
builder.startObject();
129+
builder.field(ID.getPreferredName(), id);
130+
builder.field(SOURCE.getPreferredName(), source);
131+
builder.field(DEST.getPreferredName(), dest);
132+
if (queryConfig != null) {
133+
builder.field(QUERY.getPreferredName(), queryConfig);
134+
}
135+
if (pivotConfig != null) {
136+
builder.field(PIVOT_TRANSFORM.getPreferredName(), pivotConfig);
137+
}
138+
builder.endObject();
139+
return builder;
140+
}
141+
142+
@Override
143+
public boolean equals(Object other) {
144+
if (this == other) {
145+
return true;
146+
}
147+
148+
if (other == null || getClass() != other.getClass()) {
149+
return false;
150+
}
151+
152+
final DataFrameTransformConfig that = (DataFrameTransformConfig) other;
153+
154+
return Objects.equals(this.id, that.id)
155+
&& Objects.equals(this.source, that.source)
156+
&& Objects.equals(this.dest, that.dest)
157+
&& Objects.equals(this.queryConfig, that.queryConfig)
158+
&& Objects.equals(this.pivotConfig, that.pivotConfig);
159+
}
160+
161+
@Override
162+
public int hashCode() {
163+
return Objects.hash(id, source, dest, queryConfig, pivotConfig);
164+
}
165+
166+
@Override
167+
public String toString() {
168+
return Strings.toString(this, true, true);
169+
}
170+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.dataframe.transforms;
21+
22+
import org.elasticsearch.common.xcontent.ToXContentObject;
23+
import org.elasticsearch.common.xcontent.XContentBuilder;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
import org.elasticsearch.index.query.AbstractQueryBuilder;
26+
import org.elasticsearch.index.query.QueryBuilder;
27+
28+
import java.io.IOException;
29+
import java.util.Objects;
30+
31+
public class QueryConfig implements ToXContentObject {
32+
33+
private final QueryBuilder query;
34+
35+
public static QueryConfig fromXContent(XContentParser parser) throws IOException {
36+
QueryBuilder query = AbstractQueryBuilder.parseInnerQueryBuilder(parser);
37+
return new QueryConfig(query);
38+
}
39+
40+
public QueryConfig(QueryBuilder query) {
41+
this.query = query;
42+
}
43+
44+
@Override
45+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
46+
query.toXContent(builder, params);
47+
return builder;
48+
}
49+
50+
public QueryBuilder getQuery() {
51+
return query;
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hash(query);
57+
}
58+
59+
@Override
60+
public boolean equals(Object other) {
61+
if (this == other) {
62+
return true;
63+
}
64+
65+
if (other == null || getClass() != other.getClass()) {
66+
return false;
67+
}
68+
69+
final QueryConfig that = (QueryConfig) other;
70+
71+
return Objects.equals(this.query, that.query);
72+
}
73+
74+
public boolean isValid() {
75+
return this.query != null;
76+
}
77+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.dataframe.transforms.pivot;
21+
22+
import org.elasticsearch.common.xcontent.ToXContentObject;
23+
import org.elasticsearch.common.xcontent.XContentBuilder;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
import org.elasticsearch.search.aggregations.AggregationBuilder;
26+
import org.elasticsearch.search.aggregations.AggregatorFactories;
27+
28+
import java.io.IOException;
29+
import java.util.Collection;
30+
import java.util.Objects;
31+
32+
public class AggregationConfig implements ToXContentObject {
33+
private final AggregatorFactories.Builder aggregations;
34+
35+
public static AggregationConfig fromXContent(XContentParser parser) throws IOException {
36+
if (parser.currentToken() == null) {
37+
parser.nextToken();
38+
}
39+
AggregatorFactories.Builder aggregations = AggregatorFactories.parseAggregators(parser);
40+
return new AggregationConfig(aggregations);
41+
}
42+
43+
public AggregationConfig(AggregatorFactories.Builder aggregations) {
44+
this.aggregations = aggregations;
45+
}
46+
47+
@Override
48+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
49+
aggregations.toXContent(builder, params);
50+
return builder;
51+
}
52+
53+
public Collection<AggregationBuilder> getAggregatorFactories() {
54+
return aggregations.getAggregatorFactories();
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return Objects.hash(aggregations);
60+
}
61+
62+
@Override
63+
public boolean equals(Object other) {
64+
if (this == other) {
65+
return true;
66+
}
67+
68+
if (other == null || getClass() != other.getClass()) {
69+
return false;
70+
}
71+
72+
final AggregationConfig that = (AggregationConfig) other;
73+
74+
return Objects.equals(this.aggregations, that.aggregations);
75+
}
76+
77+
public boolean isValid() {
78+
return this.aggregations != null;
79+
}
80+
}

0 commit comments

Comments
 (0)