use of org.smartdata.server.utils.TimeGranularity in project SSM by Intel-bigdata.
the class TestAccessCountTableManager method testGetTables.
@Test
public void testGetTables() throws SQLException {
DBAdapter adapter = mock(DBAdapter.class);
TableEvictor tableEvictor = new CountEvictor(20);
Map<TimeGranularity, AccessCountTableDeque> map = new HashMap<>();
AccessCountTableDeque dayDeque = new AccessCountTableDeque(tableEvictor);
AccessCountTable firstDay = new AccessCountTable(0L, Constants.ONE_DAY_IN_MILLIS);
dayDeque.add(firstDay);
map.put(TimeGranularity.DAY, dayDeque);
AccessCountTableDeque hourDeque = new AccessCountTableDeque(tableEvictor);
AccessCountTable firstHour = new AccessCountTable(23 * Constants.ONE_HOUR_IN_MILLIS, 24 * Constants.ONE_HOUR_IN_MILLIS);
AccessCountTable secondHour = new AccessCountTable(24 * Constants.ONE_HOUR_IN_MILLIS, 25 * Constants.ONE_HOUR_IN_MILLIS);
hourDeque.add(firstHour);
hourDeque.add(secondHour);
map.put(TimeGranularity.HOUR, hourDeque);
AccessCountTableDeque minuteDeque = new AccessCountTableDeque(tableEvictor);
Integer numMins = 25 * 60;
AccessCountTable firstMin = new AccessCountTable((numMins - 1) * Constants.ONE_MINUTE_IN_MILLIS, numMins * Constants.ONE_MINUTE_IN_MILLIS);
AccessCountTable secondMin = new AccessCountTable(numMins * Constants.ONE_MINUTE_IN_MILLIS, (numMins + 1) * Constants.ONE_MINUTE_IN_MILLIS);
minuteDeque.add(firstMin);
minuteDeque.add(secondMin);
map.put(TimeGranularity.MINUTE, minuteDeque);
AccessCountTableDeque secondDeque = new AccessCountTableDeque(tableEvictor);
Integer numSeconds = (25 * 60 + 1) * 60;
AccessCountTable firstFiveSeconds = new AccessCountTable((numSeconds - 5) * Constants.ONE_SECOND_IN_MILLIS, numSeconds * Constants.ONE_SECOND_IN_MILLIS);
AccessCountTable secondFiveSeconds = new AccessCountTable(numSeconds * Constants.ONE_SECOND_IN_MILLIS, (numSeconds + 5) * Constants.ONE_SECOND_IN_MILLIS);
secondDeque.add(firstFiveSeconds);
secondDeque.add(secondFiveSeconds);
map.put(TimeGranularity.SECOND, secondDeque);
List<AccessCountTable> firstResult = AccessCountTableManager.getTables(map, adapter, (numSeconds + 5) * Constants.ONE_SECOND_IN_MILLIS);
Assert.assertTrue(firstResult.size() == 4);
Assert.assertEquals(firstResult.get(0), firstDay);
Assert.assertEquals(firstResult.get(1), secondHour);
Assert.assertEquals(firstResult.get(2), secondMin);
Assert.assertEquals(firstResult.get(3), secondFiveSeconds);
List<AccessCountTable> secondResult = AccessCountTableManager.getTables(map, adapter, numSeconds * Constants.ONE_SECOND_IN_MILLIS);
Assert.assertTrue(secondResult.size() == 4);
AccessCountTable expectDay = new AccessCountTable(5 * Constants.ONE_SECOND_IN_MILLIS, Constants.ONE_DAY_IN_MILLIS);
Assert.assertEquals(expectDay, secondResult.get(0));
List<AccessCountTable> thirdResult = AccessCountTableManager.getTables(map, adapter, secondFiveSeconds.getEndTime() - 23 * Constants.ONE_HOUR_IN_MILLIS);
Assert.assertTrue(thirdResult.size() == 4);
Assert.assertEquals(thirdResult.get(0), firstHour);
List<AccessCountTable> fourthResult = AccessCountTableManager.getTables(map, adapter, secondFiveSeconds.getEndTime() - 24 * Constants.ONE_HOUR_IN_MILLIS);
Assert.assertTrue(fourthResult.size() == 3);
Assert.assertEquals(fourthResult.get(0), secondHour);
}
use of org.smartdata.server.utils.TimeGranularity in project SSM by Intel-bigdata.
the class TestAccessCountTableManager method testAccessCountTableManager.
@Test
public void testAccessCountTableManager() throws InterruptedException {
DBAdapter adapter = mock(DBAdapter.class);
AccessCountTableManager manager = new AccessCountTableManager(adapter);
Long firstDayEnd = 24 * 60 * 60 * 1000L;
AccessCountTable accessCountTable = new AccessCountTable(firstDayEnd - 5 * 1000, firstDayEnd, TimeGranularity.SECOND);
manager.addTable(accessCountTable);
Thread.sleep(5000);
Map<TimeGranularity, AccessCountTableDeque> map = manager.getTableDeques();
AccessCountTableDeque second = map.get(TimeGranularity.SECOND);
Assert.assertTrue(second.size() == 1);
Assert.assertEquals(second.peek(), accessCountTable);
AccessCountTableDeque minute = map.get(TimeGranularity.MINUTE);
AccessCountTable minuteTable = new AccessCountTable(firstDayEnd - 60 * 1000, firstDayEnd, TimeGranularity.MINUTE);
Assert.assertTrue(minute.size() == 1);
Assert.assertEquals(minute.peek(), minuteTable);
AccessCountTableDeque hour = map.get(TimeGranularity.HOUR);
AccessCountTable hourTable = new AccessCountTable(firstDayEnd - 60 * 60 * 1000, firstDayEnd, TimeGranularity.HOUR);
Assert.assertTrue(hour.size() == 1);
Assert.assertEquals(hour.peek(), hourTable);
AccessCountTableDeque day = map.get(TimeGranularity.DAY);
AccessCountTable dayTable = new AccessCountTable(firstDayEnd - 24 * 60 * 60 * 1000, firstDayEnd, TimeGranularity.DAY);
Assert.assertTrue(day.size() == 1);
Assert.assertEquals(day.peek(), dayTable);
}
use of org.smartdata.server.utils.TimeGranularity in project SSM by Intel-bigdata.
the class AccessCountTableManager method getTablesDuring.
// Todo: multi-thread issue
private static List<AccessCountTable> getTablesDuring(final Map<TimeGranularity, AccessCountTableDeque> tableDeques, DBAdapter adapter, final long length, final long endTime) throws SQLException {
long startTime = endTime - length;
TimeGranularity timeGranularity = TimeUtils.getGranularity(length);
AccessCountTableDeque tables = tableDeques.get(timeGranularity);
List<AccessCountTable> results = new ArrayList<>();
for (Iterator<AccessCountTable> iterator = tables.iterator(); iterator.hasNext(); ) {
// Here we assume that the tables are all sorted by time.
AccessCountTable table = iterator.next();
if (table.getEndTime() > startTime) {
if (table.getStartTime() >= startTime) {
results.add(table);
} else if (table.getStartTime() < startTime) {
// We got a table should be spilt here.
AccessCountTable splitTable = new AccessCountTable(startTime, table.getEndTime());
splitTable.setView(true);
adapter.createProportionView(splitTable, table);
results.add(splitTable);
}
startTime = table.getEndTime();
}
}
if (startTime != endTime && !timeGranularity.equals(TimeGranularity.SECOND)) {
results.addAll(getTablesDuring(tableDeques, adapter, endTime - startTime, endTime));
}
return results;
}