TimePeriodHandler { public long[] transferToLongPeriod(String periodExpression) { TimePeriod timePeriod = TimePeriod.fromString(periodExpression); long currentTimeMillis = System.currentTimeMillis(); int seconds = (int) (currentTimeMillis / 1000) % 60 ; if(seconds < 30){ currentTimeMillis = currentTimeMillis - (seconds * 1000); } else { currentTimeMillis = currentTimeMillis - ((seconds-30) * 1000); } currentTimeMillis = currentTimeMillis / 1000 * 1000; if(timePeriod.equals(TimePeriod.MINUTE_30)){ long thrityMinutesAgoMillis = currentTimeMillis - 30*60*1000; return new long[]{thrityMinutesAgoMillis, currentTimeMillis}; } else if(timePeriod.equals(TimePeriod.HOUR_1)){ long oneHourAgoMillis = currentTimeMillis - 60*60*1000; return new long[]{oneHourAgoMillis, currentTimeMillis}; } else if(timePeriod.equals(TimePeriod.HOUR_2)){ long twoHourAgoMillis = currentTimeMillis - 120*60*1000; return new long[]{twoHourAgoMillis, currentTimeMillis}; } else if(timePeriod.equals(TimePeriod.HOUR_6)){ long sixHourAgoMillis = currentTimeMillis - 360*60*1000; return new long[]{sixHourAgoMillis, currentTimeMillis}; } else { throw new RuntimeException("No support time period: " + periodExpression); } } TimePeriodHandler(MetricsProperties metricsProperties); long[] transferToLongPeriod(String periodExpression); List<Long> getDefaultPlotPointInPeriod(long[] startAndEndTime); List<Long> getPlotPointInPeriod(long[] startAndEndTime, long periodOfPlotPoint); List<TimeValuePairBean> ingestPeriodData(List<TimeValuePairBean> timeValuePairs, List<Long> plotPointInPeriod); final static long DEFAULT_PLOT_PERIOD; }
@Test public void testTransfer30mToLongPeriod() { String periodExpression = "30m"; long[] transferToLongPeriod = this.timePeriodHandler.transferToLongPeriod(periodExpression); assertEquals(transferToLongPeriod.length, 2); assertEquals(transferToLongPeriod[1] - transferToLongPeriod[0], 30*60*1000); }
@Test public void testTransfer1hToLongPeriod() { String periodExpression = "1h"; long[] transferToLongPeriod = this.timePeriodHandler.transferToLongPeriod(periodExpression); assertEquals(transferToLongPeriod.length, 2); assertEquals(transferToLongPeriod[1] - transferToLongPeriod[0], 60*60*1000); }
@Test public void testTransfer2hToLongPeriod() { String periodExpression = "2h"; long[] transferToLongPeriod = this.timePeriodHandler.transferToLongPeriod(periodExpression); assertEquals(transferToLongPeriod.length, 2); assertEquals(transferToLongPeriod[1] - transferToLongPeriod[0], 120*60*1000); }
@Test public void testTransfer6hToLongPeriod() { String periodExpression = "6h"; long[] transferToLongPeriod = this.timePeriodHandler.transferToLongPeriod(periodExpression); assertEquals(transferToLongPeriod.length, 2); assertEquals(transferToLongPeriod[1] - transferToLongPeriod[0], 360*60*1000); }
@Test public void testTransferAnInvalidPeriod() { String periodExpression = "8h"; try{ this.timePeriodHandler.transferToLongPeriod(periodExpression); fail("8h is a invalid period expression"); } catch(RuntimeException e){ assertEquals(e.getMessage(), "Time period expression could not be matched: "+periodExpression); } }