55 */
66package org .elasticsearch .xpack .rollup ;
77
8+ import org .elasticsearch .common .rounding .DateTimeUnit ;
89import org .elasticsearch .common .unit .TimeValue ;
910import org .elasticsearch .search .aggregations .AggregationBuilder ;
1011import org .elasticsearch .search .aggregations .bucket .histogram .DateHistogramAggregationBuilder ;
1819import org .joda .time .DateTimeZone ;
1920
2021import java .util .ArrayList ;
21- import java .util .Collections ;
2222import java .util .Comparator ;
23- import java .util .HashMap ;
2423import java .util .HashSet ;
2524import java .util .List ;
2625import java .util .Map ;
3332 */
3433public class RollupJobIdentifierUtils {
3534
36- private static final Comparator <RollupJobCaps > COMPARATOR = RollupJobIdentifierUtils .getComparator ();
37-
38- public static final Map <String , Integer > CALENDAR_ORDERING ;
39-
40- static {
41- Map <String , Integer > dateFieldUnits = new HashMap <>(16 );
42- dateFieldUnits .put ("year" , 8 );
43- dateFieldUnits .put ("1y" , 8 );
44- dateFieldUnits .put ("quarter" , 7 );
45- dateFieldUnits .put ("1q" , 7 );
46- dateFieldUnits .put ("month" , 6 );
47- dateFieldUnits .put ("1M" , 6 );
48- dateFieldUnits .put ("week" , 5 );
49- dateFieldUnits .put ("1w" , 5 );
50- dateFieldUnits .put ("day" , 4 );
51- dateFieldUnits .put ("1d" , 4 );
52- dateFieldUnits .put ("hour" , 3 );
53- dateFieldUnits .put ("1h" , 3 );
54- dateFieldUnits .put ("minute" , 2 );
55- dateFieldUnits .put ("1m" , 2 );
56- dateFieldUnits .put ("second" , 1 );
57- dateFieldUnits .put ("1s" , 1 );
58- CALENDAR_ORDERING = Collections .unmodifiableMap (dateFieldUnits );
59- }
35+ static final Comparator <RollupJobCaps > COMPARATOR = RollupJobIdentifierUtils .getComparator ();
6036
6137 /**
6238 * Given the aggregation tree and a list of available job capabilities, this method will return a set
@@ -176,8 +152,10 @@ static boolean validateCalendarInterval(DateHistogramInterval requestInterval,
176152
177153 // The request must be gte the config. The CALENDAR_ORDERING map values are integers representing
178154 // relative orders between the calendar units
179- int requestOrder = CALENDAR_ORDERING .getOrDefault (requestInterval .toString (), Integer .MAX_VALUE );
180- int configOrder = CALENDAR_ORDERING .getOrDefault (configInterval .toString (), Integer .MAX_VALUE );
155+ DateTimeUnit requestUnit = DateHistogramAggregationBuilder .DATE_FIELD_UNITS .get (requestInterval .toString ());
156+ long requestOrder = requestUnit .field (DateTimeZone .UTC ).getDurationField ().getUnitMillis ();
157+ DateTimeUnit configUnit = DateHistogramAggregationBuilder .DATE_FIELD_UNITS .get (configInterval .toString ());
158+ long configOrder = configUnit .field (DateTimeZone .UTC ).getDurationField ().getUnitMillis ();
181159
182160 // All calendar units are multiples naturally, so we just care about gte
183161 return requestOrder >= configOrder ;
@@ -190,7 +168,7 @@ static boolean validateFixedInterval(DateHistogramInterval requestInterval,
190168 return false ;
191169 }
192170
193- // Both are fixed, good to conver to millis now
171+ // Both are fixed, good to convert to millis now
194172 long configIntervalMillis = TimeValue .parseTimeValue (configInterval .toString (),
195173 "date_histo.config.interval" ).getMillis ();
196174 long requestIntervalMillis = TimeValue .parseTimeValue (requestInterval .toString (),
@@ -326,8 +304,8 @@ private static Comparator<RollupJobCaps> getComparator() {
326304 return 0 ;
327305 }
328306
329- TimeValue thisTime = null ;
330- TimeValue thatTime = null ;
307+ long thisTime = Long . MAX_VALUE ;
308+ long thatTime = Long . MAX_VALUE ;
331309
332310 // histogram intervals are averaged and compared, with the idea that
333311 // a larger average == better, because it will generate fewer documents
@@ -344,7 +322,7 @@ private static Comparator<RollupJobCaps> getComparator() {
344322 for (RollupJobCaps .RollupFieldCaps fieldCaps : o1 .getFieldCaps ().values ()) {
345323 for (Map <String , Object > agg : fieldCaps .getAggs ()) {
346324 if (agg .get (RollupField .AGG ).equals (DateHistogramAggregationBuilder .NAME )) {
347- thisTime = TimeValue . parseTimeValue ((String ) agg .get (RollupField .INTERVAL ), RollupField . INTERVAL );
325+ thisTime = getMillisFixedOrCalendar ((String ) agg .get (RollupField .INTERVAL ));
348326 } else if (agg .get (RollupField .AGG ).equals (HistogramAggregationBuilder .NAME )) {
349327 thisHistoWeights += (long ) agg .get (RollupField .INTERVAL );
350328 counter += 1 ;
@@ -360,7 +338,7 @@ private static Comparator<RollupJobCaps> getComparator() {
360338 for (RollupJobCaps .RollupFieldCaps fieldCaps : o2 .getFieldCaps ().values ()) {
361339 for (Map <String , Object > agg : fieldCaps .getAggs ()) {
362340 if (agg .get (RollupField .AGG ).equals (DateHistogramAggregationBuilder .NAME )) {
363- thatTime = TimeValue . parseTimeValue ((String ) agg .get (RollupField .INTERVAL ), RollupField . INTERVAL );
341+ thatTime = getMillisFixedOrCalendar ((String ) agg .get (RollupField .INTERVAL ));
364342 } else if (agg .get (RollupField .AGG ).equals (HistogramAggregationBuilder .NAME )) {
365343 thatHistoWeights += (long ) agg .get (RollupField .INTERVAL );
366344 counter += 1 ;
@@ -371,13 +349,9 @@ private static Comparator<RollupJobCaps> getComparator() {
371349 }
372350 thatHistoWeights = counter == 0 ? 0 : thatHistoWeights / counter ;
373351
374- // DateHistos are mandatory so these should always be present no matter what
375- assert thisTime != null ;
376- assert thatTime != null ;
377-
378352 // Compare on date interval first
379353 // The "smaller" job is the one with the larger interval
380- int timeCompare = thisTime . compareTo ( thatTime );
354+ int timeCompare = Long . compare ( thisTime , thatTime );
381355 if (timeCompare != 0 ) {
382356 return -timeCompare ;
383357 }
@@ -409,4 +383,14 @@ private static Comparator<RollupJobCaps> getComparator() {
409383 // coverage
410384 };
411385 }
386+
387+ static long getMillisFixedOrCalendar (String value ) {
388+ DateHistogramInterval interval = new DateHistogramInterval (value );
389+ if (isCalendarInterval (interval )) {
390+ DateTimeUnit intervalUnit = DateHistogramAggregationBuilder .DATE_FIELD_UNITS .get (interval .toString ());
391+ return intervalUnit .field (DateTimeZone .UTC ).getDurationField ().getUnitMillis ();
392+ } else {
393+ return TimeValue .parseTimeValue (value , "date_histo.comparator.interval" ).getMillis ();
394+ }
395+ }
412396}
0 commit comments