WindowProcessor extends WindowBaseListener { public static Window process(String statement) throws ParseException { TokenStream tokens = createTokenStream(statement); if(tokens == null) { return null; } WindowProcessor treeBuilder = new WindowProcessor(); WindowParser parser = createParser(tokens, Optional.of(treeBuilder)); parser.window(); if(treeBuilder.throwable != null) { throw new ParseException(treeBuilder.throwable.getMessage(), treeBuilder.throwable); } return treeBuilder.getWindow(); } WindowProcessor(); Window getWindow(); @Override void exitIdentifier(WindowParser.IdentifierContext ctx); @Override void enterSpecifier(WindowParser.SpecifierContext ctx); @Override void exitSpecifier(WindowParser.SpecifierContext ctx); @Override void exitDay_specifier(WindowParser.Day_specifierContext ctx); @Override void enterExcluding_specifier(WindowParser.Excluding_specifierContext ctx); @Override void exitExcluding_specifier(WindowParser.Excluding_specifierContext ctx); @Override void enterIncluding_specifier(WindowParser.Including_specifierContext ctx); @Override void exitIncluding_specifier(WindowParser.Including_specifierContext ctx); @Override void exitFromToDuration(org.apache.metron.profiler.client.window.generated.WindowParser.FromToDurationContext ctx); @Override void exitFromDuration(org.apache.metron.profiler.client.window.generated.WindowParser.FromDurationContext ctx); @Override void exitSkipDistance(org.apache.metron.profiler.client.window.generated.WindowParser.SkipDistanceContext ctx); @Override void exitWindowWidth(org.apache.metron.profiler.client.window.generated.WindowParser.WindowWidthContext ctx); @Override void exitTimeInterval(org.apache.metron.profiler.client.window.generated.WindowParser.TimeIntervalContext ctx); @Override void exitTimeAmount(org.apache.metron.profiler.client.window.generated.WindowParser.TimeAmountContext ctx); @Override void exitTimeUnit(org.apache.metron.profiler.client.window.generated.WindowParser.TimeUnitContext ctx); static Window process(String statement); static String syntaxTree(String statement);  }
@Test public void testBaseCase() { for (String text : new String[] { "1 hour" ,"1 hour(s)" ,"1 hours" }) { Window w = WindowProcessor.process(text); Date now = new Date(); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(1, intervals.size()); assertEquals(now.getTime(), (long)intervals.get(0).getMaximum()); assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(1), (long)intervals.get(0).getMinimum()); } }
@Test public void testDenseWindow() { for (String text : new String[] { "from 2 hours ago to 30 minutes ago" ,"starting from 2 hours until 30 minutes" ,"starting from 2 hours ago until 30 minutes ago" ,"starting from 30 minutes ago until 2 hours ago" ,"from 30 minutes ago to 2 hours ago " }) { Window w = WindowProcessor.process(text); Date now = new Date(); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(1, intervals.size()); assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(2), intervals.get(0).getMinimum()); assertTimeEquals(now.getTime() - TimeUnit.MINUTES.toMillis(30), intervals.get(0).getMaximum()); } }
@Test public void testSparse() { for(String text : new String[] { "30 minute window every 1 hour from 2 hours ago to 30 minutes ago", "30 minute window every 1 hour starting from 2 hours ago to 30 minutes ago", "30 minute window every 1 hour starting from 2 hours ago until 30 minutes ago", "30 minute window for every 1 hour starting from 2 hours ago until 30 minutes ago", }) { Window w = WindowProcessor.process(text); Date now = new Date(); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(2, intervals.size()); assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(2), intervals.get(0).getMinimum()); assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(2) + TimeUnit.MINUTES.toMillis(30), intervals.get(0).getMaximum()); assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(1), intervals.get(1).getMinimum()); assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(1) + TimeUnit.MINUTES.toMillis(30), intervals.get(1).getMaximum()); } }
@Test public void testRepeatTilNow() { Window w = WindowProcessor.process("30 minute window every 1 hour from 3 hours ago"); Date now = new Date(); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(3, intervals.size()); assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(3), intervals.get(0).getMinimum()); assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(3) + TimeUnit.MINUTES.toMillis(30), intervals.get(0).getMaximum()); assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(2), intervals.get(1).getMinimum()); assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(2) + TimeUnit.MINUTES.toMillis(30), intervals.get(1).getMaximum()); assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(1), intervals.get(2).getMinimum()); assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(1) + TimeUnit.MINUTES.toMillis(30), intervals.get(2).getMaximum()); }
@Test public void testRepeatWithInclusions() { { Window w = WindowProcessor.process("30 minute window every 24 hours from 14 days ago including tuesdays"); Date now = new Date(); now.setHours(6); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(2, intervals.size()); } { Window w = WindowProcessor.process("30 minute window every 24 hours from 14 days ago including this day of the week"); Date now = new Date(); now.setHours(6); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(2, intervals.size()); } { Window w = WindowProcessor.process("30 minute window every 24 hours from 14 days ago"); Date now = new Date(); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(14, intervals.size()); } }
@Test public void testRepeatWithConflictingExclusionInclusion() { Window w = WindowProcessor.process("30 minute window every 24 hours from 7 days ago including saturdays excluding weekends"); Date now = new Date(); now.setHours(6); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(0, intervals.size()); }
@Test public void testRepeatWithWeekendExclusion() { Window w = WindowProcessor.process("30 minute window every 24 hours from 7 days ago excluding weekends"); Date now = new Date(); now.setHours(6); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(5, intervals.size()); }
@Test public void testRepeatWithInclusionExclusion() throws ParseException { Window w = WindowProcessor.process("30 minute window every 24 hours from 7 days ago including holidays:us excluding weekends"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); Date now = sdf.parse("2017/12/26 12:00"); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(1, intervals.size()); }
@Test public void testManyMoonsAgo() throws ParseException { { Window w = WindowProcessor.process("1 hour window every 24 hours starting from 56 days ago"); Date now = new Date(); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(56, intervals.size()); } { Window w = WindowProcessor.process("1 hour window every 24 hours starting from 56 days ago including this day of the week"); Date now = new Date(); now.setHours(6); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(8, intervals.size()); } }
@Test public void testRepeatWithWeekdayExclusion() throws ParseException { Window w = WindowProcessor.process("30 minute window every 24 hours from 7 days ago excluding weekdays"); Date now = new Date(); now.setHours(6); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(2, intervals.size()); }
@Test public void testRepeatWithHolidayExclusion() throws ParseException { { Window w = WindowProcessor.process("30 minute window every 24 hours from 14 days ago excluding holidays:us"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); Date now = sdf.parse("2017/12/26 12:00"); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(13, intervals.size()); } { Window w = WindowProcessor.process("30 minute window every 24 hours from 14 days ago excluding holidays:us:nyc"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); Date now = sdf.parse("2017/12/26 12:00"); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(13, intervals.size()); } { Window w = WindowProcessor.process("30 minute window every 24 hours from 14 days ago excluding holidays:us"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); Date now = sdf.parse("2017/08/26 12:00"); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(14, intervals.size()); } }
@Test public void testDateDaySpecifier() throws ParseException { for(String text : new String[] { "30 minute window every 24 hours from 14 days ago including date:20171225:yyyyMMdd", "30 minute window every 24 hours from 14 days ago including date:2017-12-25:yyyy-MM-dd", "30 minute window every 24 hours from 14 days ago including date:2017/12/25", }) { Window w = WindowProcessor.process(text); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); Date now = sdf.parse("2017/12/26 12:00"); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(1, intervals.size()); Date includedDate = new Date(intervals.get(0).getMinimum()); SimpleDateFormat equalityFormat = new SimpleDateFormat("yyyyMMdd"); assertEquals("20171225", equalityFormat.format(includedDate)); } { Window w = WindowProcessor.process("30 minute window every 24 hours from 14 days ago excluding date:2017/12/25"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); Date now = sdf.parse("2017/12/26 12:00"); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(13, intervals.size()); } { Window w = WindowProcessor.process("30 minute window every 24 hours from 14 days ago including date:2017/12/25, date:2017/12/24"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); Date now = sdf.parse("2017/12/26 12:00"); List<Range<Long>> intervals = w.toIntervals(now.getTime()); assertEquals(2, intervals.size()); { Date includedDate = new Date(intervals.get(0).getMinimum()); SimpleDateFormat equalityFormat = new SimpleDateFormat("yyyyMMdd"); assertEquals("20171224", equalityFormat.format(includedDate)); } { Date includedDate = new Date(intervals.get(1).getMinimum()); SimpleDateFormat equalityFormat = new SimpleDateFormat("yyyyMMdd"); assertEquals("20171225", equalityFormat.format(includedDate)); } } }
@Test public void testWithInvalidDaySpecifier() { assertThrows( org.apache.metron.stellar.dsl.ParseException.class, () -> WindowProcessor.process( "30 minute window every 24 hours from 14 days ago excluding hoolidays:us")); }
@Test public void testWithInvalidTimeUnit() { assertThrows( org.apache.metron.stellar.dsl.ParseException.class, () -> WindowProcessor.process("30 minute window every 24 months from 14 days ago")); }
@Test public void testWithInvalidWindowUnit() { assertThrows( org.apache.metron.stellar.dsl.ParseException.class, () -> WindowProcessor.process("30 minuete window every 24 hours from 14 days ago")); }
@Test public void testWithInvalidTimeNumber() { assertThrows( org.apache.metron.stellar.dsl.ParseException.class, () -> WindowProcessor.process("30p minute window every 24 hours from 14 days ago")); }
@Test public void testInvalidDaySpecifier() { assertThrows( org.apache.metron.stellar.dsl.ParseException.class, () -> WindowProcessor.process( "30 minute window every 14 hours from 14 days ago including date")); }