5
5
*/
6
6
package org .elasticsearch .xpack .rollup ;
7
7
8
+ import org .elasticsearch .common .rounding .DateTimeUnit ;
8
9
import org .elasticsearch .common .unit .TimeValue ;
9
10
import org .elasticsearch .search .aggregations .AggregationBuilder ;
10
11
import org .elasticsearch .search .aggregations .bucket .histogram .DateHistogramAggregationBuilder ;
18
19
import org .joda .time .DateTimeZone ;
19
20
20
21
import java .util .ArrayList ;
21
- import java .util .Collections ;
22
22
import java .util .Comparator ;
23
- import java .util .HashMap ;
24
23
import java .util .HashSet ;
25
24
import java .util .List ;
26
25
import java .util .Map ;
33
32
*/
34
33
public class RollupJobIdentifierUtils {
35
34
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 ();
60
36
61
37
/**
62
38
* 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,
176
152
177
153
// The request must be gte the config. The CALENDAR_ORDERING map values are integers representing
178
154
// 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 ();
181
159
182
160
// All calendar units are multiples naturally, so we just care about gte
183
161
return requestOrder >= configOrder ;
@@ -190,7 +168,7 @@ static boolean validateFixedInterval(DateHistogramInterval requestInterval,
190
168
return false ;
191
169
}
192
170
193
- // Both are fixed, good to conver to millis now
171
+ // Both are fixed, good to convert to millis now
194
172
long configIntervalMillis = TimeValue .parseTimeValue (configInterval .toString (),
195
173
"date_histo.config.interval" ).getMillis ();
196
174
long requestIntervalMillis = TimeValue .parseTimeValue (requestInterval .toString (),
@@ -326,8 +304,8 @@ private static Comparator<RollupJobCaps> getComparator() {
326
304
return 0 ;
327
305
}
328
306
329
- TimeValue thisTime = null ;
330
- TimeValue thatTime = null ;
307
+ long thisTime = Long . MAX_VALUE ;
308
+ long thatTime = Long . MAX_VALUE ;
331
309
332
310
// histogram intervals are averaged and compared, with the idea that
333
311
// a larger average == better, because it will generate fewer documents
@@ -344,7 +322,7 @@ private static Comparator<RollupJobCaps> getComparator() {
344
322
for (RollupJobCaps .RollupFieldCaps fieldCaps : o1 .getFieldCaps ().values ()) {
345
323
for (Map <String , Object > agg : fieldCaps .getAggs ()) {
346
324
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 ));
348
326
} else if (agg .get (RollupField .AGG ).equals (HistogramAggregationBuilder .NAME )) {
349
327
thisHistoWeights += (long ) agg .get (RollupField .INTERVAL );
350
328
counter += 1 ;
@@ -360,7 +338,7 @@ private static Comparator<RollupJobCaps> getComparator() {
360
338
for (RollupJobCaps .RollupFieldCaps fieldCaps : o2 .getFieldCaps ().values ()) {
361
339
for (Map <String , Object > agg : fieldCaps .getAggs ()) {
362
340
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 ));
364
342
} else if (agg .get (RollupField .AGG ).equals (HistogramAggregationBuilder .NAME )) {
365
343
thatHistoWeights += (long ) agg .get (RollupField .INTERVAL );
366
344
counter += 1 ;
@@ -371,13 +349,9 @@ private static Comparator<RollupJobCaps> getComparator() {
371
349
}
372
350
thatHistoWeights = counter == 0 ? 0 : thatHistoWeights / counter ;
373
351
374
- // DateHistos are mandatory so these should always be present no matter what
375
- assert thisTime != null ;
376
- assert thatTime != null ;
377
-
378
352
// Compare on date interval first
379
353
// The "smaller" job is the one with the larger interval
380
- int timeCompare = thisTime . compareTo ( thatTime );
354
+ int timeCompare = Long . compare ( thisTime , thatTime );
381
355
if (timeCompare != 0 ) {
382
356
return -timeCompare ;
383
357
}
@@ -409,4 +383,14 @@ private static Comparator<RollupJobCaps> getComparator() {
409
383
// coverage
410
384
};
411
385
}
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
+ }
412
396
}
0 commit comments