private void sampleDateTime() { List<String> text = new ArrayList<String>(); DateTime now = DateTime.now(); text.add("Now: " + now); text.add("Now + 30 minutes: " + now.plusMinutes(30)); text.add("Now + 5 hours: " + now.plusHours(5)); text.add("Now + 2 days: " + now.plusDays(2)); addSample("DateTime", text); }
DateTime dt = new DateTime(); DateTime twoHoursLater = dt.plusHours(2).plusMinutes(10).plusSeconds(5); Period period = new Period(dt, twoHoursLater); PeriodFormatter HHMMSSFormater = new PeriodFormatterBuilder() .printZeroAlways() .minimumPrintedDigits(2) .appendHours().appendSeparator("-") .appendMinutes().appendSeparator("-") .appendSeconds() .toFormatter(); // produce thread-safe formatter System.out.println(HHMMSSFormater.print(period));
private void sampleDateRange() { List<String> text = new ArrayList<String>(); DateTime start = DateTime.now(); DateTime end = start.plusMinutes(30).plusHours(2).plusDays(56); text.add("Range: " + DateUtils.formatDateRange(this, start, end, 0)); text.add("Range (with year): " + DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_SHOW_YEAR)); text.add("Range (abbreviated): " + DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_ABBREV_ALL)); text.add("Range (with time): " + DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_SHOW_TIME)); addSample("DateUtils.formatDateRange()", text); }
DataSegment createSegment(DateTime t) { return new DataSegment( "test", new Interval(t, t.plusHours(1)), "v1", null, null, null, null, 0, 0 ); }
private void sampleGetRelativeTimeSpanString() { List<String> text = new ArrayList<String>(); DateTime now = DateTime.now(); text.add("Short future: " + DateUtils.getRelativeTimeSpanString(this, now.plusMinutes(25))); text.add("Medium future: " + DateUtils.getRelativeTimeSpanString(this, now.plusHours(5))); text.add("Long future: " + DateUtils.getRelativeTimeSpanString(this, now.plusDays(3))); text.add("Short past: " + DateUtils.getRelativeTimeSpanString(this, now.minusMinutes(25))); text.add("Medium past: " + DateUtils.getRelativeTimeSpanString(this, now.minusHours(5))); text.add("Long past: " + DateUtils.getRelativeTimeSpanString(this, now.minusDays(3))); addSample("DateUtils.getRelativeTimeSpanString()", text); }
private void sampleGetRelativeDateTimeString() { List<String> text = new ArrayList<String>(); DateTime now = DateTime.now(); text.add("Short future: " + DateUtils.getRelativeDateTimeString(this, now.plusMinutes(25), null, 0)); text.add("Medium future: " + DateUtils.getRelativeDateTimeString(this, now.plusHours(5), null, 0)); text.add("Long future: " + DateUtils.getRelativeDateTimeString(this, now.plusDays(3), null, 0)); text.add("Short past: " + DateUtils.getRelativeDateTimeString(this, now.minusMinutes(25), null, 0)); text.add("Medium past: " + DateUtils.getRelativeDateTimeString(this, now.minusHours(5), null, 0)); text.add("Long past: " + DateUtils.getRelativeDateTimeString(this, now.minusDays(3), null, 0)); addSample("DateUtils.getRelativeDateTimeString()", text); }
private void sampleGetRelativeTimeSpanStringWithPreposition() { List<String> text = new ArrayList<String>(); DateTime now = DateTime.now(); text.add("Short future: " + DateUtils.getRelativeTimeSpanString(this, now.plusMinutes(25), true)); text.add("Medium future: " + DateUtils.getRelativeTimeSpanString(this, now.plusHours(5), true)); text.add("Long future: " + DateUtils.getRelativeTimeSpanString(this, now.plusDays(3), true)); text.add("Short past: " + DateUtils.getRelativeTimeSpanString(this, now.minusMinutes(25), true)); text.add("Medium past: " + DateUtils.getRelativeTimeSpanString(this, now.minusHours(5), true)); text.add("Long past: " + DateUtils.getRelativeTimeSpanString(this, now.minusDays(3), true)); addSample("DateUtils.getRelativeTimeSpanString() (with preposition)", text); }
private DataSegment createRandomSegment(Random random, DateTime referenceTime) { int timeShift = random.nextInt((int) TimeUnit.DAYS.toHours(DAYS_IN_MONTH * 12)); return new DataSegment( String.valueOf(random.nextInt(50)), new Interval(referenceTime.plusHours(timeShift), referenceTime.plusHours(timeShift + 1)), "version", Collections.emptyMap(), Collections.emptyList(), Collections.emptyList(), null, 0, 100 ); } }
@Before public void setUp() { coordinator = EasyMock.createMock(DruidCoordinator.class); mockPeon = EasyMock.createMock(LoadQueuePeon.class); emitter = EasyMock.createMock(ServiceEmitter.class); EmittingLogger.registerEmitter(emitter); databaseRuleManager = EasyMock.createMock(MetadataRuleManager.class); DateTime start = DateTimes.of("2012-01-01"); availableSegments = new ArrayList<>(); for (int i = 0; i < 24; i++) { availableSegments.add( new DataSegment( "test", new Interval(start, start.plusHours(1)), DateTimes.nowUtc().toString(), new HashMap<>(), new ArrayList<>(), new ArrayList<>(), NoneShardSpec.instance(), IndexIO.CURRENT_VERSION_ID, 1 ) ); start = start.plusHours(1); } ruleRunner = new DruidCoordinatorRuleRunner(new ReplicationThrottler(24, 1), coordinator); }
@Test(groups = "fast") public void testEventsForCancelledSubscriptionAfterTransfer() throws Exception { final DateTime subscriptionStartTime = clock.getUTCNow(); final DateTime subscriptionCancelTime = subscriptionStartTime.plusDays(1); final ImmutableList<ExistingEvent> existingEvents = ImmutableList.<ExistingEvent>of(createEvent(subscriptionStartTime, SubscriptionBaseTransitionType.CREATE), createEvent(subscriptionCancelTime, SubscriptionBaseTransitionType.CANCEL)); final SubscriptionBuilder subscriptionBuilder = new SubscriptionBuilder(); final DefaultSubscriptionBase subscription = new DefaultSubscriptionBase(subscriptionBuilder); final DateTime transferDate = subscriptionStartTime.plusHours(1); final List<SubscriptionBaseEvent> events = transferApi.toEvents(existingEvents, subscription, transferDate, catalog, internalCallContext); Assert.assertEquals(events.size(), 1); Assert.assertEquals(events.get(0).getType(), EventType.API_USER); Assert.assertEquals(events.get(0).getEffectiveDate(), transferDate); Assert.assertEquals(((ApiEventTransfer) events.get(0)).getApiEventType(), ApiEventType.TRANSFER); }
@Test public void randomSegmentsCostTest() { List<DataSegment> dataSegments = new ArrayList<>(1000); Random random = new Random(1); for (int i = 0; i < 1000; ++i) { dataSegments.add(createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, random.nextInt(20)), 100)); } DataSegment referenceSegment = createSegment("ANOTHER_DATA_SOURCE", shifted1HInterval(REFERENCE_TIME, 5), 100); SegmentsCostCache.Bucket.Builder prototype = SegmentsCostCache.Bucket.builder(new Interval( REFERENCE_TIME.minusHours(1), REFERENCE_TIME.plusHours(25) )); dataSegments.forEach(prototype::addSegment); SegmentsCostCache.Bucket bucket = prototype.build(); double cost = bucket.cost(referenceSegment); assertEquals(0.7065117101966677, cost, EPSILON); }
@Test public void testCompareDay() { Result<Object> res = new Result<Object>(time, null); Result<Object> same = new Result<Object>(time.plusHours(12), null); Result<Object> greater = new Result<Object>(time.plusHours(25), null); Result<Object> less = new Result<Object>(time.minusHours(1), null); Granularity day = Granularities.DAY; Assert.assertEquals(ResultGranularTimestampComparator.create(day, descending).compare(res, same), 0); Assert.assertEquals(ResultGranularTimestampComparator.create(day, descending).compare(res, greater), descending ? 1 : -1); Assert.assertEquals(ResultGranularTimestampComparator.create(day, descending).compare(res, less), descending ? -1 : 1); }
@Test public void calculationIntervalTest() { DataSegment segmentA = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 0), 100); DataSegment segmentB = createSegment( DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, (int) TimeUnit.DAYS.toHours(50)), 100 ); SegmentsCostCache.Bucket.Builder prototype = SegmentsCostCache.Bucket.builder( new Interval(REFERENCE_TIME.minusHours(5), REFERENCE_TIME.plusHours(5)) ); prototype.addSegment(segmentA); SegmentsCostCache.Bucket bucket = prototype.build(); assertTrue(bucket.inCalculationInterval(segmentA)); assertFalse(bucket.inCalculationInterval(segmentB)); }
@Test public void sameSegmentCostTest() { DataSegment segmentA = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 0), 100); DataSegment segmentB = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 0), 100); SegmentsCostCache.Bucket.Builder prototype = SegmentsCostCache.Bucket.builder(new Interval( REFERENCE_TIME.minusHours(5), REFERENCE_TIME.plusHours(5) )); prototype.addSegment(segmentA); SegmentsCostCache.Bucket bucket = prototype.build(); double segmentCost = bucket.cost(segmentB); assertEquals(8.26147353873985E-4, segmentCost, EPSILON); }
@Test public void twoSegmentsCostTest() { DataSegment segmentA = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 0), 100); DataSegment segmentB = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, -2), 100); SegmentsCostCache.Bucket.Builder prototype = SegmentsCostCache.Bucket.builder(new Interval( REFERENCE_TIME.minusHours(5), REFERENCE_TIME.plusHours(5) )); prototype.addSegment(segmentA); SegmentsCostCache.Bucket bucket = prototype.build(); double segmentCost = bucket.cost(segmentB); assertEquals(7.8735899489011E-4, segmentCost, EPSILON); }
@Test public void testCompareHour() { Result<Object> res = new Result<Object>(time, null); Result<Object> same = new Result<Object>(time.plusMinutes(55), null); Result<Object> greater = new Result<Object>(time.plusHours(1), null); Result<Object> less = new Result<Object>(time.minusHours(1), null); Granularity hour = Granularities.HOUR; Assert.assertEquals(ResultGranularTimestampComparator.create(hour, descending).compare(res, same), 0); Assert.assertEquals(ResultGranularTimestampComparator.create(hour, descending).compare(res, greater), descending ? 1 : -1); Assert.assertEquals(ResultGranularTimestampComparator.create(hour, descending).compare(res, less), descending ? -1 : 1); } }
@Test public void multipleSegmentsCostTest() { DataSegment segmentA = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, -2), 100); DataSegment segmentB = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 0), 100); DataSegment segmentC = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 2), 100); SegmentsCostCache.Bucket.Builder prototype = SegmentsCostCache.Bucket.builder(new Interval( REFERENCE_TIME.minusHours(5), REFERENCE_TIME.plusHours(5) )); prototype.addSegment(segmentA); prototype.addSegment(segmentC); SegmentsCostCache.Bucket bucket = prototype.build(); double segmentCost = bucket.cost(segmentB); assertEquals(0.001574717989780039, segmentCost, EPSILON); }
@Test public void testAddFieldToTimestamp() { assertFunction("date_add('millisecond', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMillis(3), session)); assertFunction("date_add('second', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusSeconds(3), session)); assertFunction("date_add('minute', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMinutes(3), session)); assertFunction("date_add('hour', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusHours(3), session)); assertFunction("date_add('hour', 23, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusHours(23), session)); assertFunction("date_add('hour', -4, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.minusHours(4), session)); assertFunction("date_add('hour', -23, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.minusHours(23), session)); assertFunction("date_add('day', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusDays(3), session)); assertFunction("date_add('week', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusWeeks(3), session)); assertFunction("date_add('month', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMonths(3), session)); assertFunction("date_add('quarter', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMonths(3 * 3), session)); assertFunction("date_add('year', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusYears(3), session)); assertFunction("date_add('millisecond', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMillis(3))); assertFunction("date_add('second', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusSeconds(3))); assertFunction("date_add('minute', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMinutes(3))); assertFunction("date_add('hour', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusHours(3))); assertFunction("date_add('day', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusDays(3))); assertFunction("date_add('week', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusWeeks(3))); assertFunction("date_add('month', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMonths(3))); assertFunction("date_add('quarter', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMonths(3 * 3))); assertFunction("date_add('year', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusYears(3))); }