Skip to content

[ML-Dataframe] Data frame transforms config HLRC objects #39691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.dataframe.transforms;

import org.elasticsearch.client.dataframe.transforms.pivot.PivotConfig;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;

public class DataFrameTransformConfig implements ToXContentObject {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, it would be nice for this class to provide a fluent builder API.


public static final ParseField ID = new ParseField("id");
public static final ParseField SOURCE = new ParseField("source");
public static final ParseField DEST = new ParseField("dest");
public static final ParseField QUERY = new ParseField("query");
// types of transforms
public static final ParseField PIVOT_TRANSFORM = new ParseField("pivot");

private final String id;
private final String source;
private final String dest;
private final QueryConfig queryConfig;
private final PivotConfig pivotConfig;

private static final ConstructingObjectParser<DataFrameTransformConfig, String> PARSER =
new ConstructingObjectParser<>("data_frame_transform", true,
(args) -> {
String id = (String) args[0];
String source = (String) args[1];
String dest = (String) args[2];
QueryConfig queryConfig = (QueryConfig) args[3];
PivotConfig pivotConfig = (PivotConfig) args[4];
return new DataFrameTransformConfig(id, source, dest, queryConfig, pivotConfig);
});

static {
PARSER.declareString(constructorArg(), ID);
PARSER.declareString(constructorArg(), SOURCE);
PARSER.declareString(constructorArg(), DEST);
PARSER.declareObject(optionalConstructorArg(), (p, c) -> QueryConfig.fromXContent(p), QUERY);
PARSER.declareObject(optionalConstructorArg(), (p, c) -> PivotConfig.fromXContent(p), PIVOT_TRANSFORM);
}

public static DataFrameTransformConfig fromXContent(final XContentParser parser) {
return PARSER.apply(parser, null);
}


public DataFrameTransformConfig(final String id,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note I've taken the headers out of this class

final String source,
final String dest,
final QueryConfig queryConfig,
final PivotConfig pivotConfig) {
this.id = Objects.requireNonNull(id);
this.source = Objects.requireNonNull(source);
this.dest = Objects.requireNonNull(dest);
this.queryConfig = queryConfig;
this.pivotConfig = pivotConfig;
}

public String getId() {
return id;
}

public String getSource() {
return source;
}

public String getDestination() {
return dest;
}

public PivotConfig getPivotConfig() {
return pivotConfig;
}

public QueryConfig getQueryConfig() {
return queryConfig;
}

public boolean isValid() {
if (queryConfig != null && queryConfig.isValid() == false) {
return false;
}

if (pivotConfig == null || pivotConfig.isValid() == false) {
return false;
}

return true;
}

@Override
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
builder.startObject();
builder.field(ID.getPreferredName(), id);
builder.field(SOURCE.getPreferredName(), source);
builder.field(DEST.getPreferredName(), dest);
if (queryConfig != null) {
builder.field(QUERY.getPreferredName(), queryConfig);
}
if (pivotConfig != null) {
builder.field(PIVOT_TRANSFORM.getPreferredName(), pivotConfig);
}
builder.endObject();
return builder;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (other == null || getClass() != other.getClass()) {
return false;
}

final DataFrameTransformConfig that = (DataFrameTransformConfig) other;

return Objects.equals(this.id, that.id)
&& Objects.equals(this.source, that.source)
&& Objects.equals(this.dest, that.dest)
&& Objects.equals(this.queryConfig, that.queryConfig)
&& Objects.equals(this.pivotConfig, that.pivotConfig);
}

@Override
public int hashCode() {
return Objects.hash(id, source, dest, queryConfig, pivotConfig);
}

@Override
public String toString() {
return Strings.toString(this, true, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.dataframe.transforms;

import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;

import java.io.IOException;
import java.util.Objects;

public class QueryConfig implements ToXContentObject {

private final QueryBuilder query;

public static QueryConfig fromXContent(XContentParser parser) throws IOException {
QueryBuilder query = AbstractQueryBuilder.parseInnerQueryBuilder(parser);
return new QueryConfig(query);
}

public QueryConfig(QueryBuilder query) {
this.query = query;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
query.toXContent(builder, params);
return builder;
}

public QueryBuilder getQuery() {
return query;
}

@Override
public int hashCode() {
return Objects.hash(query);
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (other == null || getClass() != other.getClass()) {
return false;
}

final QueryConfig that = (QueryConfig) other;

return Objects.equals(this.query, that.query);
}

public boolean isValid() {
return this.query != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.dataframe.transforms.pivot;

import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories;

import java.io.IOException;
import java.util.Collection;
import java.util.Objects;

public class AggregationConfig implements ToXContentObject {
private final AggregatorFactories.Builder aggregations;

public static AggregationConfig fromXContent(XContentParser parser) throws IOException {
if (parser.currentToken() == null) {
parser.nextToken();
}
AggregatorFactories.Builder aggregations = AggregatorFactories.parseAggregators(parser);
return new AggregationConfig(aggregations);
}

public AggregationConfig(AggregatorFactories.Builder aggregations) {
this.aggregations = aggregations;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
aggregations.toXContent(builder, params);
return builder;
}

public Collection<AggregationBuilder> getAggregatorFactories() {
return aggregations.getAggregatorFactories();
}

@Override
public int hashCode() {
return Objects.hash(aggregations);
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (other == null || getClass() != other.getClass()) {
return false;
}

final AggregationConfig that = (AggregationConfig) other;

return Objects.equals(this.aggregations, that.aggregations);
}

public boolean isValid() {
return this.aggregations != null;
}
}
Loading