Skip to content

Commit

Permalink
[Rollup] Move getMetadata() methods out of rollup config objects
Browse files Browse the repository at this point in the history
  • Loading branch information
tlrx committed Aug 20, 2018
1 parent faa42de commit 3c66713
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ public Map<String, Object> toAggCap() {
return map;
}

public Map<String, Object> getMetadata() {
return Collections.singletonMap(RollupField.formatMetaField(RollupField.INTERVAL), interval.toString());
}

public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse,
ActionRequestValidationException validationException) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
Expand Down Expand Up @@ -115,8 +116,8 @@ public Map<String, Object> toAggCap() {
return map;
}

public Map<String, Object> getMetadata() {
return Collections.singletonMap(RollupField.formatMetaField(RollupField.INTERVAL), interval);
public Set<String> getAllFields() {
return Arrays.stream(fields).collect(Collectors.toSet());
}

public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.xpack.core.rollup.RollupField;
import org.elasticsearch.xpack.core.rollup.job.DateHistogramGroupConfig;
import org.elasticsearch.xpack.core.rollup.job.GroupConfig;
import org.elasticsearch.xpack.core.rollup.job.HistogramGroupConfig;
import org.elasticsearch.xpack.core.rollup.job.IndexerState;
import org.elasticsearch.xpack.core.rollup.job.RollupJob;
import org.elasticsearch.xpack.core.rollup.job.RollupJobConfig;
Expand Down Expand Up @@ -392,15 +393,12 @@ private SearchRequest buildSearchRequest() {
private CompositeAggregationBuilder createCompositeBuilder(RollupJobConfig config) {
final GroupConfig groupConfig = config.getGroupConfig();
List<CompositeValuesSourceBuilder<?>> builders = new ArrayList<>();
Map<String, Object> metadata = new HashMap<>();

// Add all the agg builders to our request in order: date_histo -> histo -> terms
if (groupConfig != null) {
builders.addAll(groupConfig.getDateHistogram().toBuilders());
metadata.putAll(groupConfig.getDateHistogram().getMetadata());
if (groupConfig.getHistogram() != null) {
builders.addAll(groupConfig.getHistogram().toBuilders());
metadata.putAll(groupConfig.getHistogram().getMetadata());
}
if (groupConfig.getTerms() != null) {
builders.addAll(groupConfig.getTerms().toBuilders());
Expand All @@ -409,6 +407,8 @@ private CompositeAggregationBuilder createCompositeBuilder(RollupJobConfig confi

CompositeAggregationBuilder composite = new CompositeAggregationBuilder(AGGREGATION_NAME, builders);
config.getMetricsConfig().forEach(m -> m.toBuilders().forEach(composite::subAggregation));

final Map<String, Object> metadata = createMetadata(groupConfig);
if (metadata.isEmpty() == false) {
composite.setMetaData(metadata);
}
Expand Down Expand Up @@ -441,5 +441,20 @@ private QueryBuilder createBoundaryQuery(Map<String, Object> position) {
.format("epoch_millis");
return query;
}

static Map<String, Object> createMetadata(final GroupConfig groupConfig) {
final Map<String, Object> metadata = new HashMap<>();
if (groupConfig != null) {
// Add all the metadata in order: date_histo -> histo
final DateHistogramGroupConfig dateHistogram = groupConfig.getDateHistogram();
metadata.put(RollupField.formatMetaField(RollupField.INTERVAL), dateHistogram.getInterval().toString());

final HistogramGroupConfig histogram = groupConfig.getHistogram();
if (histogram != null) {
metadata.put(RollupField.formatMetaField(RollupField.INTERVAL), histogram.getInterval());
}
}
return metadata;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.rollup.job;

import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.rollup.ConfigTestHelpers;
import org.elasticsearch.xpack.core.rollup.job.DateHistogramGroupConfig;
import org.elasticsearch.xpack.core.rollup.job.GroupConfig;
import org.elasticsearch.xpack.core.rollup.job.HistogramGroupConfig;

import java.util.Map;

import static org.hamcrest.Matchers.equalTo;

public class RollupIndexerTests extends ESTestCase {

public void testCreateMetadataNoGroupConfig() {
final Map<String, Object> metadata = RollupIndexer.createMetadata(null);
assertNotNull(metadata);
assertTrue(metadata.isEmpty());
}

public void testCreateMetadataWithDateHistogramGroupConfigOnly() {
final DateHistogramGroupConfig dateHistogram = ConfigTestHelpers.randomDateHistogramGroupConfig(random());
final GroupConfig groupConfig = new GroupConfig(dateHistogram);

final Map<String, Object> metadata = RollupIndexer.createMetadata(groupConfig);
assertEquals(1, metadata.size());
assertTrue(metadata.containsKey("_rollup.interval"));
Object value = metadata.get("_rollup.interval");
assertThat(value, equalTo(dateHistogram.getInterval().toString()));
}

public void testCreateMetadata() {
final DateHistogramGroupConfig dateHistogram = ConfigTestHelpers.randomDateHistogramGroupConfig(random());
final HistogramGroupConfig histogram = ConfigTestHelpers.randomHistogramGroupConfig(random());
final GroupConfig groupConfig = new GroupConfig(dateHistogram, histogram, null);

final Map<String, Object> metadata = RollupIndexer.createMetadata(groupConfig);
assertEquals(1, metadata.size());
assertTrue(metadata.containsKey("_rollup.interval"));
Object value = metadata.get("_rollup.interval");
assertThat(value, equalTo(histogram.getInterval()));
}
}

0 comments on commit 3c66713

Please sign in to comment.