| 
7 | 7 | 
 
  | 
8 | 8 | package org.elasticsearch.xpack.downsample;  | 
9 | 9 | 
 
  | 
 | 10 | +import org.elasticsearch.action.downsample.DownsampleConfig;  | 
10 | 11 | import org.elasticsearch.cluster.metadata.IndexMetadata;  | 
11 | 12 | import org.elasticsearch.cluster.routing.allocation.DataTier;  | 
12 | 13 | import org.elasticsearch.common.settings.IndexScopedSettings;  | 
13 | 14 | import org.elasticsearch.common.settings.Settings;  | 
 | 15 | +import org.elasticsearch.common.xcontent.XContentHelper;  | 
14 | 16 | import org.elasticsearch.index.IndexSettings;  | 
15 | 17 | import org.elasticsearch.index.IndexVersion;  | 
16 | 18 | import org.elasticsearch.index.mapper.TimeSeriesParams;  | 
 | 19 | +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;  | 
17 | 20 | import org.elasticsearch.test.ESTestCase;  | 
 | 21 | +import org.elasticsearch.xcontent.json.JsonXContent;  | 
18 | 22 | import org.elasticsearch.xpack.core.ilm.LifecycleSettings;  | 
19 | 23 | 
 
  | 
 | 24 | +import java.io.IOException;  | 
20 | 25 | import java.util.List;  | 
21 | 26 | import java.util.Map;  | 
22 | 27 | import java.util.UUID;  | 
23 | 28 | 
 
  | 
 | 29 | +import static org.hamcrest.Matchers.equalTo;  | 
24 | 30 | import static org.hamcrest.Matchers.is;  | 
25 | 31 | 
 
  | 
26 | 32 | public class TransportDownsampleActionTests extends ESTestCase {  | 
 | 33 | +    private static final Map<String, Object> METRIC_COUNTER_FIELD = Map.of("type", "long", "time_series_metric", "counter");  | 
 | 34 | +    private static final Map<String, Object> METRIC_GAUGE_FIELD = Map.of("type", "double", "time_series_metric", "gauge");  | 
 | 35 | +    private static final Map<String, Object> DIMENSION_FIELD = Map.of("type", "keyword", "time_series_dimension", true);  | 
 | 36 | +    private static final Map<String, Object> FLOAT_FIELD = Map.of("type", "float", "scaling_factor", 1000.0);  | 
 | 37 | +    private static final Map<String, Object> DATE_FIELD = Map.of("type", "date");  | 
 | 38 | +    private static final Map<String, Object> FLATTENED_FIELD = Map.of("type", "flattened");  | 
 | 39 | +    private static final Map<String, Object> PASSTHROUGH_FIELD = Map.of("type", "passthrough", "time_series_dimension", true);  | 
 | 40 | +    private static final Map<String, Object> AGGREGATE_METRIC_DOUBLE_FIELD = Map.of(  | 
 | 41 | +        "type",  | 
 | 42 | +        "aggregate_metric_double",  | 
 | 43 | +        "time_series_metric",  | 
 | 44 | +        "gauge",  | 
 | 45 | +        "metrics",  | 
 | 46 | +        List.of("max", "min", "value_count", "sum"),  | 
 | 47 | +        "default_metric",  | 
 | 48 | +        "max"  | 
 | 49 | +    );  | 
 | 50 | + | 
27 | 51 |     public void testCopyIndexMetadata() {  | 
28 | 52 |         // GIVEN  | 
29 | 53 |         final List<String> tiers = List.of(DataTier.DATA_HOT, DataTier.DATA_WARM, DataTier.DATA_COLD, DataTier.DATA_CONTENT);  | 
@@ -132,4 +156,47 @@ public void testGetSupportedMetrics() {  | 
132 | 156 |         assertThat(supported.defaultMetric(), is("max"));  | 
133 | 157 |         assertThat(supported.supportedMetrics(), is(List.of(metricType.supportedAggs())));  | 
134 | 158 |     }  | 
 | 159 | + | 
 | 160 | +    @SuppressWarnings("unchecked")  | 
 | 161 | +    public void testDownsampledMapping() throws IOException {  | 
 | 162 | +        TimeseriesFieldTypeHelper helper = new TimeseriesFieldTypeHelper.Builder(null).build("@timestamp");  | 
 | 163 | +        String downsampledMappingStr = TransportDownsampleAction.createDownsampleIndexMapping(  | 
 | 164 | +            helper,  | 
 | 165 | +            new DownsampleConfig(new DateHistogramInterval("5m")),  | 
 | 166 | +            Map.of(  | 
 | 167 | +                "properties",  | 
 | 168 | +                Map.of(  | 
 | 169 | +                    "my-dimension",  | 
 | 170 | +                    DIMENSION_FIELD,  | 
 | 171 | +                    "my-passthrough",  | 
 | 172 | +                    PASSTHROUGH_FIELD,  | 
 | 173 | +                    "my-multi-field",  | 
 | 174 | +                    Map.of("type", "keyword", "fields", Map.of("my-gauge", METRIC_GAUGE_FIELD)),  | 
 | 175 | +                    "my-object",  | 
 | 176 | +                    Map.of("type", "text", "properties", Map.of("my-float", FLOAT_FIELD, "my-counter", METRIC_COUNTER_FIELD)),  | 
 | 177 | +                    "my-flattened",  | 
 | 178 | +                    FLATTENED_FIELD,  | 
 | 179 | +                    "@timestamp",  | 
 | 180 | +                    DATE_FIELD  | 
 | 181 | +                )  | 
 | 182 | +            )  | 
 | 183 | +        );  | 
 | 184 | +        Map<String, Object> downsampledMapping = XContentHelper.convertToMap(JsonXContent.jsonXContent, downsampledMappingStr, false);  | 
 | 185 | +        assertThat(downsampledMapping.containsKey("properties"), is(true));  | 
 | 186 | +        Map<String, Object> properties = (Map<String, Object>) downsampledMapping.get("properties");  | 
 | 187 | +        assertThat(properties.get("my-dimension"), equalTo(DIMENSION_FIELD));  | 
 | 188 | +        assertThat(properties.get("my-passthrough"), equalTo(PASSTHROUGH_FIELD));  | 
 | 189 | +        assertThat(properties.get("my-flattened"), equalTo(FLATTENED_FIELD));  | 
 | 190 | +        Map<String, Object> timestamp = (Map<String, Object>) properties.get("@timestamp");  | 
 | 191 | +        assertThat(timestamp.get("type"), equalTo("date"));  | 
 | 192 | +        assertThat(timestamp.get("meta"), equalTo(Map.of("fixed_interval", "5m", "time_zone", "UTC")));  | 
 | 193 | +        Map<String, Object> multiField = (Map<String, Object>) properties.get("my-multi-field");  | 
 | 194 | +        assertThat(multiField.get("type"), equalTo("keyword"));  | 
 | 195 | +        assertThat(((Map<String, Object>) multiField.get("fields")).get("my-gauge"), equalTo(AGGREGATE_METRIC_DOUBLE_FIELD));  | 
 | 196 | +        Map<String, Object> objectField = (Map<String, Object>) properties.get("my-object");  | 
 | 197 | +        assertThat(objectField.get("type"), equalTo("text"));  | 
 | 198 | +        Map<String, Object> subObjectsProperties = (Map<String, Object>) objectField.get("properties");  | 
 | 199 | +        assertThat(subObjectsProperties.get("my-float"), equalTo(FLOAT_FIELD));  | 
 | 200 | +        assertThat(subObjectsProperties.get("my-counter"), equalTo(METRIC_COUNTER_FIELD));  | 
 | 201 | +    }  | 
135 | 202 | }  | 
0 commit comments