@@ -740,6 +740,39 @@ private static void addTimestampField(
740740 .endObject ();
741741 }
742742
743+ // public for testing
744+ public record AggregateMetricDoubleFieldSupportedMetrics (String defaultMetric , List <String > supportedMetrics ) {}
745+
746+ // public for testing
747+ public static AggregateMetricDoubleFieldSupportedMetrics getSupportedMetrics (
748+ final TimeSeriesParams .MetricType metricType ,
749+ final Map <String , ?> fieldProperties
750+ ) {
751+ boolean sourceIsAggregate = fieldProperties .get ("type" ).equals (AggregateMetricDoubleFieldMapper .CONTENT_TYPE );
752+ List <String > supportedAggs = List .of (metricType .supportedAggs ());
753+
754+ if (sourceIsAggregate ) {
755+ @ SuppressWarnings ("unchecked" )
756+ List <String > currentAggs = (List <String >) fieldProperties .get (AggregateMetricDoubleFieldMapper .Names .METRICS );
757+ supportedAggs = supportedAggs .stream ().filter (currentAggs ::contains ).toList ();
758+ }
759+
760+ assert supportedAggs .size () > 0 ;
761+
762+ String defaultMetric = "max" ;
763+ if (supportedAggs .contains (defaultMetric ) == false ) {
764+ defaultMetric = supportedAggs .get (0 );
765+ }
766+ if (sourceIsAggregate ) {
767+ defaultMetric = Objects .requireNonNullElse (
768+ (String ) fieldProperties .get (AggregateMetricDoubleFieldMapper .Names .DEFAULT_METRIC ),
769+ defaultMetric
770+ );
771+ }
772+
773+ return new AggregateMetricDoubleFieldSupportedMetrics (defaultMetric , supportedAggs );
774+ }
775+
743776 private static void addMetricFieldMapping (final XContentBuilder builder , final String field , final Map <String , ?> fieldProperties )
744777 throws IOException {
745778 final TimeSeriesParams .MetricType metricType = TimeSeriesParams .MetricType .fromString (
@@ -753,25 +786,13 @@ private static void addMetricFieldMapping(final XContentBuilder builder, final S
753786 builder .field (fieldProperty , fieldProperties .get (fieldProperty ));
754787 }
755788 } else {
756- List <String > supportedAggs = List .of (metricType .supportedAggs ());
757- // We choose max as the default metric
758- String defaultMetric = supportedAggs .contains ("max" ) ? "max" : supportedAggs .get (0 );
759-
760- if (fieldProperties .get ("type" ).equals (AggregateMetricDoubleFieldMapper .CONTENT_TYPE )) {
761- @ SuppressWarnings ("unchecked" )
762- List <String > currentAggs = (List <String >) fieldProperties .get (AggregateMetricDoubleFieldMapper .Names .METRICS );
763- supportedAggs = supportedAggs .stream ().filter (currentAggs ::contains ).toList ();
764- defaultMetric = Objects .requireNonNullElse (
765- (String ) fieldProperties .get (AggregateMetricDoubleFieldMapper .Names .DEFAULT_METRIC ),
766- defaultMetric
767- );
768- }
789+ var supported = getSupportedMetrics (metricType , fieldProperties );
769790
770- String [] supportedAggsArray = supportedAggs .toArray (String []::new );
791+ String [] supportedMetricsArray = supported . supportedMetrics .toArray (String []::new );
771792
772793 builder .field ("type" , AggregateMetricDoubleFieldMapper .CONTENT_TYPE )
773- .array (AggregateMetricDoubleFieldMapper .Names .METRICS , supportedAggsArray )
774- .field (AggregateMetricDoubleFieldMapper .Names .DEFAULT_METRIC , defaultMetric )
794+ .array (AggregateMetricDoubleFieldMapper .Names .METRICS , supportedMetricsArray )
795+ .field (AggregateMetricDoubleFieldMapper .Names .DEFAULT_METRIC , supported . defaultMetric )
775796 .field (TIME_SERIES_METRIC_PARAM , metricType );
776797 }
777798 builder .endObject ();
0 commit comments