Cpaitalized enum values + bug fix in the aggregation deamon

This commit is contained in:
Daniel Collin 2016-02-17 13:49:00 +01:00
parent 75d60d7f40
commit 8ca169a2ab
7 changed files with 209 additions and 207 deletions

View file

@ -24,15 +24,15 @@ public class SensorDataAggregatorDaemon implements HalDaemon {
private static final Logger logger = LogUtil.getLogger();
public enum AggregationPeriodLength{
second,
minute,
fiveMinutes,
fifteenMinutes,
hour,
day,
week,
month,
year
SECOND,
MINUTE,
FIVE_MINUTES,
FIFTEEN_MINUTES,
HOUR,
DAY,
WEEK,
MONTH,
YEAR
}
public void initiate(ScheduledExecutorService executor){
@ -61,16 +61,16 @@ public class SensorDataAggregatorDaemon implements HalDaemon {
logger.fine("The sensor is of type: " + sensor.getDeviceData().getClass().getName());
logger.fine("aggregating raw data up to a day old into five minute periods");
aggregateRawData(sensor, AggregationPeriodLength.fiveMinutes, TimeUtility.DAY_IN_MS, 5);
aggregateRawData(sensor, AggregationPeriodLength.FIVE_MINUTES, TimeUtility.DAY_IN_MS, 5);
logger.fine("aggregating raw data up to a week old into one hour periods");
aggregateRawData(sensor, AggregationPeriodLength.hour, TimeUtility.WEEK_IN_MS, 60);
aggregateRawData(sensor, AggregationPeriodLength.HOUR, TimeUtility.WEEK_IN_MS, 60);
logger.fine("aggregating raw data into one day periods");
aggregateRawData(sensor, AggregationPeriodLength.day, TimeUtility.INFINITY, 60*24);
aggregateRawData(sensor, AggregationPeriodLength.DAY, TimeUtility.INFINITY, 60*24);
logger.fine("aggregating raw data into one week periods");
aggregateRawData(sensor, AggregationPeriodLength.week, TimeUtility.INFINITY, 60*24*7);
aggregateRawData(sensor, AggregationPeriodLength.WEEK, TimeUtility.INFINITY, 60*24*7);
}
/**
@ -91,13 +91,13 @@ public class SensorDataAggregatorDaemon implements HalDaemon {
+ " AND timestamp_end-timestamp_start == ?");
stmt.setLong(1, sensorId);
switch(aggrPeriodLength){
case second: stmt.setLong(2, TimeUtility.SECOND_IN_MS-1); break;
case minute: stmt.setLong(2, TimeUtility.MINUTE_IN_MS-1); break;
case fiveMinutes: stmt.setLong(2, TimeUtility.FIVE_MINUTES_IN_MS-1); break;
case fifteenMinutes: stmt.setLong(2, TimeUtility.FIFTEEN_MINUTES_IN_MS-1); break;
case hour: stmt.setLong(2, TimeUtility.HOUR_IN_MS-1); break;
case day: stmt.setLong(2, TimeUtility.DAY_IN_MS-1); break;
case week: stmt.setLong(2, TimeUtility.WEEK_IN_MS-1); break;
case SECOND: stmt.setLong(2, TimeUtility.SECOND_IN_MS-1); break;
case MINUTE: stmt.setLong(2, TimeUtility.MINUTE_IN_MS-1); break;
case FIVE_MINUTES: stmt.setLong(2, TimeUtility.FIVE_MINUTES_IN_MS-1); break;
case FIFTEEN_MINUTES: stmt.setLong(2, TimeUtility.FIFTEEN_MINUTES_IN_MS-1); break;
case HOUR: stmt.setLong(2, TimeUtility.HOUR_IN_MS-1); break;
case DAY: stmt.setLong(2, TimeUtility.DAY_IN_MS-1); break;
case WEEK: stmt.setLong(2, TimeUtility.WEEK_IN_MS-1); break;
default: logger.warning("aggregation period length is not supported."); return;
}
Long maxTimestampFoundForSensor = DBConnection.exec(stmt, new SimpleSQLResult<Long>());
@ -112,12 +112,12 @@ public class SensorDataAggregatorDaemon implements HalDaemon {
+" WHERE sensor_id == ?"
+ " AND timestamp > ?"
+ " AND timestamp < ? "
+ " AND timestamp > ? "
+ " AND timestamp >= ? "
+" ORDER BY timestamp ASC");
stmt.setLong(1, sensorId);
stmt.setLong(2, maxTimestampFoundForSensor);
stmt.setLong(3, currentPeriodStartTimestamp);
stmt.setLong(4, System.currentTimeMillis()-ageLimitInMs);
stmt.setLong(4, TimeUtility.getTimestampPeriodStart_UTC(aggrPeriodLength, System.currentTimeMillis()-ageLimitInMs));
DBConnection.exec(stmt, new DataAggregator(sensorId, aggrPeriodLength, expectedSampleCount, aggrMethod));
} catch (SQLException e) {
logger.log(Level.SEVERE, null, e);

View file

@ -42,8 +42,8 @@ public class SensorDataCleanupDaemon implements HalDaemon {
public void cleanupSensor(Sensor sensor) {
if (sensor.getUser() != null) {
cleanupSensorData(sensor.getId(), AggregationPeriodLength.fiveMinutes, TimeUtility.DAY_IN_MS); //clear 5-minute data older than a day
cleanupSensorData(sensor.getId(), AggregationPeriodLength.hour, TimeUtility.WEEK_IN_MS); //clear 1-hour data older than a week
cleanupSensorData(sensor.getId(), AggregationPeriodLength.FIVE_MINUTES, TimeUtility.DAY_IN_MS); //clear 5-minute data older than a day
cleanupSensorData(sensor.getId(), AggregationPeriodLength.HOUR, TimeUtility.WEEK_IN_MS); //clear 1-hour data older than a week
//cleanupSensorData(sensor.getId(), AggregationPeriodLength.day, TimeUtility.INFINITY); //clear 1-day data older than infinity
//cleanupSensorData(sensor.getId(), AggregationPeriodLength.week, TimeUtility.INFINITY); //clear 1-week data older than infinity
}
@ -68,13 +68,13 @@ public class SensorDataCleanupDaemon implements HalDaemon {
+ "AND timestamp_end < ?");
stmt.setLong(1, sensorId);
switch(cleanupPeriodlength){
case second: stmt.setLong(2, TimeUtility.SECOND_IN_MS-1); break;
case minute: stmt.setLong(2, TimeUtility.MINUTE_IN_MS-1); break;
case fiveMinutes: stmt.setLong(2, TimeUtility.FIVE_MINUTES_IN_MS-1); break;
case fifteenMinutes: stmt.setLong(2, TimeUtility.FIFTEEN_MINUTES_IN_MS-1); break;
case hour: stmt.setLong(2, TimeUtility.HOUR_IN_MS-1); break;
case day: stmt.setLong(2, TimeUtility.DAY_IN_MS-1); break;
case week: stmt.setLong(2, TimeUtility.WEEK_IN_MS-1); break;
case SECOND: stmt.setLong(2, TimeUtility.SECOND_IN_MS-1); break;
case MINUTE: stmt.setLong(2, TimeUtility.MINUTE_IN_MS-1); break;
case FIVE_MINUTES: stmt.setLong(2, TimeUtility.FIVE_MINUTES_IN_MS-1); break;
case FIFTEEN_MINUTES: stmt.setLong(2, TimeUtility.FIFTEEN_MINUTES_IN_MS-1); break;
case HOUR: stmt.setLong(2, TimeUtility.HOUR_IN_MS-1); break;
case DAY: stmt.setLong(2, TimeUtility.DAY_IN_MS-1); break;
case WEEK: stmt.setLong(2, TimeUtility.WEEK_IN_MS-1); break;
default: logger.warning("cleanup period length is not supported."); return;
}
stmt.setLong(3, System.currentTimeMillis()-olderThan);

View file

@ -43,13 +43,13 @@ public class PCOverviewHttpPage extends HalHttpPage {
for(User user : users){
List<Sensor> userSensors = Sensor.getSensors(db, user);
for(Sensor sensor : userSensors){
minDataList.addAll(AggregateDataListSqlResult.getAggregateDataForPeriod(db, sensor, AggregationPeriodLength.fiveMinutes, TimeUtility.DAY_IN_MS));
minDataList.addAll(AggregateDataListSqlResult.getAggregateDataForPeriod(db, sensor, AggregationPeriodLength.FIVE_MINUTES, TimeUtility.DAY_IN_MS));
hourDataList.addAll(AggregateDataListSqlResult.getAggregateDataForPeriod(db, sensor, AggregationPeriodLength.hour, TimeUtility.WEEK_IN_MS));
hourDataList.addAll(AggregateDataListSqlResult.getAggregateDataForPeriod(db, sensor, AggregationPeriodLength.HOUR, TimeUtility.WEEK_IN_MS));
dayDataList.addAll(AggregateDataListSqlResult.getAggregateDataForPeriod(db, sensor, AggregationPeriodLength.day, TimeUtility.INFINITY));
dayDataList.addAll(AggregateDataListSqlResult.getAggregateDataForPeriod(db, sensor, AggregationPeriodLength.DAY, TimeUtility.INFINITY));
weekDataList.addAll(AggregateDataListSqlResult.getAggregateDataForPeriod(db, sensor, AggregationPeriodLength.week, TimeUtility.INFINITY));
weekDataList.addAll(AggregateDataListSqlResult.getAggregateDataForPeriod(db, sensor, AggregationPeriodLength.WEEK, TimeUtility.INFINITY));
}
}

View file

@ -51,7 +51,7 @@ public class SensorOverviewHttpPage extends HalHttpPage {
Templator tmpl = new Templator(FileUtil.find(DETAIL_TEMPLATE));
tmpl.set("sensor", sensor);
tmpl.set("history", history);
tmpl.set("aggregation", AggregateDataListSqlResult.getAggregateDataForPeriod(db, sensor, AggregationPeriodLength.hour, TimeUtility.WEEK_IN_MS));
tmpl.set("aggregation", AggregateDataListSqlResult.getAggregateDataForPeriod(db, sensor, AggregationPeriodLength.HOUR, TimeUtility.WEEK_IN_MS));
return tmpl;
}
else {

View file

@ -43,13 +43,13 @@ public class AggregateDataListSqlResult implements SQLResultHandler<ArrayList<Ag
stmt.setLong(1, sensor.getId());
stmt.setLong(2, sensor.getUser().getId());
switch(aggrPeriodLength){
case second: stmt.setLong(3, TimeUtility.SECOND_IN_MS-1); break;
case minute: stmt.setLong(3, TimeUtility.MINUTE_IN_MS-1); break;
case fiveMinutes: stmt.setLong(3, TimeUtility.FIVE_MINUTES_IN_MS-1); break;
case fifteenMinutes: stmt.setLong(3, TimeUtility.FIFTEEN_MINUTES_IN_MS-1); break;
case hour: stmt.setLong(3, TimeUtility.HOUR_IN_MS-1); break;
case day: stmt.setLong(3, TimeUtility.DAY_IN_MS-1); break;
case week: stmt.setLong(3, TimeUtility.WEEK_IN_MS-1); break;
case SECOND: stmt.setLong(3, TimeUtility.SECOND_IN_MS-1); break;
case MINUTE: stmt.setLong(3, TimeUtility.MINUTE_IN_MS-1); break;
case FIVE_MINUTES: stmt.setLong(3, TimeUtility.FIVE_MINUTES_IN_MS-1); break;
case FIFTEEN_MINUTES: stmt.setLong(3, TimeUtility.FIFTEEN_MINUTES_IN_MS-1); break;
case HOUR: stmt.setLong(3, TimeUtility.HOUR_IN_MS-1); break;
case DAY: stmt.setLong(3, TimeUtility.DAY_IN_MS-1); break;
case WEEK: stmt.setLong(3, TimeUtility.WEEK_IN_MS-1); break;
default: throw new IllegalArgumentException("selected aggrPeriodLength is not supported");
}
stmt.setLong(4, (System.currentTimeMillis() - ageLimitInMs) );

View file

@ -19,53 +19,53 @@ public class TimeUtility {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTimeInMillis(timestamp);
switch(aggrPeriodLength){
case year:
case YEAR:
cal.set(Calendar.DAY_OF_YEAR, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
break;
case month:
case MONTH:
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
break;
case week:
case WEEK:
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
break;
case day:
case DAY:
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
break;
case hour:
case HOUR:
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
break;
case fiveMinutes:
case FIVE_MINUTES:
cal.set(Calendar.MINUTE, (cal.get(Calendar.MINUTE)/5)*5);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
break;
case fifteenMinutes:
case FIFTEEN_MINUTES:
cal.set(Calendar.MINUTE, (cal.get(Calendar.MINUTE)/15)*15);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
break;
case minute:
case MINUTE:
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
break;
case second:
case SECOND:
cal.set(Calendar.MILLISECOND, 0);
break;
}
@ -77,31 +77,31 @@ public class TimeUtility {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTimeInMillis(start);
switch(aggrPeriodLength){
case year:
case YEAR:
cal.add(Calendar.YEAR, 1);
break;
case month:
case MONTH:
cal.add(Calendar.MONTH, 1);
break;
case week:
case WEEK:
cal.add(Calendar.WEEK_OF_YEAR, 1);
break;
case day:
case DAY:
cal.add(Calendar.DAY_OF_YEAR, 1);
break;
case hour:
case HOUR:
cal.add(Calendar.HOUR, 1);
break;
case fiveMinutes:
case FIVE_MINUTES:
cal.add(Calendar.MINUTE, 5);
break;
case fifteenMinutes:
case FIFTEEN_MINUTES:
cal.add(Calendar.MINUTE, 15);
break;
case minute:
case MINUTE:
cal.add(Calendar.MINUTE, 1);
break;
case second:
case SECOND:
cal.add(Calendar.SECOND, 1);
break;
}