Added configuration page
Former-commit-id: 8ab1873678b8e4a56332da785cd6f943648f6afd
This commit is contained in:
parent
b88f260ebc
commit
de1706d67a
7 changed files with 176 additions and 106 deletions
26
src/se/koc/hal/PCConfigureHttpPage.java
Normal file
26
src/se/koc/hal/PCConfigureHttpPage.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package se.koc.hal;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import zutil.io.file.FileUtil;
|
||||
import zutil.net.http.HttpHeaderParser;
|
||||
import zutil.net.http.HttpPage;
|
||||
import zutil.net.http.HttpPrintStream;
|
||||
import zutil.parser.Templator;
|
||||
|
||||
public class PCConfigureHttpPage implements HttpPage {
|
||||
|
||||
@Override
|
||||
public void respond(HttpPrintStream out, HttpHeaderParser client_info,
|
||||
Map<String, Object> session, Map<String, String> cookie,
|
||||
Map<String, String> request) throws IOException {
|
||||
|
||||
Templator tmpl = new Templator(FileUtil.find("web-resource/configure.html"));
|
||||
|
||||
out.print(tmpl.compile());
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -15,23 +15,32 @@ import zutil.net.http.HttpPage;
|
|||
import zutil.net.http.HttpPrintStream;
|
||||
import zutil.parser.Templator;
|
||||
|
||||
public class PowerChallengeHttpPage implements HttpPage {
|
||||
public class PCOverviewHttpPage implements HttpPage {
|
||||
|
||||
@Override
|
||||
public void respond(HttpPrintStream out, HttpHeaderParser client_info, Map<String, Object> session, Map<String, String> cookie, Map<String, String> request) throws IOException {
|
||||
|
||||
try {
|
||||
ArrayList<PowerData> minDataList = PowerChallenge.db.exec(
|
||||
"SELECT * FROM sensor_data_aggr "
|
||||
+ "WHERE sensor_id == 1 AND timestamp_end-timestamp_start == " + (DataAggregatorDaemon.FIVE_MINUTES_IN_MS-1),
|
||||
"SELECT user.username as username, sensor_data_aggr.timestamp_start as timestamp, sensor_data_aggr.data as data "
|
||||
+ "FROM sensor_data_aggr, user, sensor "
|
||||
+ "WHERE sensor.id = sensor_data_aggr.sensor_id "
|
||||
+ "AND user.id = sensor.user_id "
|
||||
+ "AND timestamp_end-timestamp_start == " + (DataAggregatorDaemon.FIVE_MINUTES_IN_MS-1),
|
||||
new SQLPowerDataBuilder());
|
||||
ArrayList<PowerData> hourDataList = PowerChallenge.db.exec(
|
||||
"SELECT * FROM sensor_data_aggr "
|
||||
+ "WHERE sensor_id == 1 AND timestamp_end-timestamp_start == " + (DataAggregatorDaemon.HOUR_IN_MS-1),
|
||||
"SELECT user.username as username, sensor_data_aggr.timestamp_start as timestamp, sensor_data_aggr.data as data "
|
||||
+ "FROM sensor_data_aggr, user, sensor "
|
||||
+ "WHERE sensor.id = sensor_data_aggr.sensor_id "
|
||||
+ "AND user.id = sensor.user_id "
|
||||
+ "AND timestamp_end-timestamp_start == " + (DataAggregatorDaemon.HOUR_IN_MS-1),
|
||||
new SQLPowerDataBuilder());
|
||||
ArrayList<PowerData> dayDataList = PowerChallenge.db.exec(
|
||||
"SELECT * FROM sensor_data_aggr "
|
||||
+ "WHERE sensor_id == 1 AND timestamp_end-timestamp_start == " + (DataAggregatorDaemon.DAY_IN_MS-1),
|
||||
"SELECT user.username as username, sensor_data_aggr.timestamp_start as timestamp, sensor_data_aggr.data as data "
|
||||
+ "FROM sensor_data_aggr, user, sensor "
|
||||
+ "WHERE sensor.id = sensor_data_aggr.sensor_id "
|
||||
+ "AND user.id = sensor.user_id "
|
||||
+ "AND timestamp_end-timestamp_start == " + (DataAggregatorDaemon.DAY_IN_MS-1),
|
||||
new SQLPowerDataBuilder());
|
||||
|
||||
|
||||
|
|
@ -39,7 +48,7 @@ public class PowerChallengeHttpPage implements HttpPage {
|
|||
tmpl.set("minData", minDataList);
|
||||
tmpl.set("hourData", hourDataList);
|
||||
tmpl.set("dayData", dayDataList);
|
||||
tmpl.set("username", "Ziver");
|
||||
tmpl.set("username", new String[]{"Ziver", "Daniel"});
|
||||
|
||||
out.print(tmpl.compile());
|
||||
|
||||
|
|
@ -51,9 +60,11 @@ public class PowerChallengeHttpPage implements HttpPage {
|
|||
public static class PowerData{
|
||||
long timestamp;
|
||||
int data;
|
||||
public PowerData(long time, int data) {
|
||||
String username;
|
||||
public PowerData(long time, int data, String uname) {
|
||||
this.timestamp = time;
|
||||
this.data = data;
|
||||
this.username = uname;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -62,7 +73,7 @@ public class PowerChallengeHttpPage implements HttpPage {
|
|||
public ArrayList<PowerData> handleQueryResult(Statement stmt, ResultSet result) throws SQLException {
|
||||
ArrayList<PowerData> list = new ArrayList<>();
|
||||
while(result.next()){
|
||||
list.add(new PowerData(result.getLong("timestamp_start"), result.getInt("data")));
|
||||
list.add(new PowerData(result.getLong("timestamp"), result.getInt("data"), result.getString("username")));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
|
@ -41,7 +41,8 @@ public class PowerChallenge {
|
|||
|
||||
HttpServer http = new HttpServer(8080);
|
||||
http.setDefaultPage(new HttpFilePage(FileUtil.find("web-resource/")));
|
||||
http.setPage("/", new PowerChallengeHttpPage());
|
||||
http.setPage("/", new PCOverviewHttpPage());
|
||||
http.setPage("/configure", new PCConfigureHttpPage());
|
||||
http.start();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ package se.koc.hal.deamon;
|
|||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
|
@ -33,41 +35,61 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
|||
run();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
public void run(){
|
||||
try {
|
||||
List<Integer> sensorIdList = PowerChallenge.db.exec("SELECT id FROM sensor", new SQLResultHandler<List<Integer>>(){
|
||||
@Override
|
||||
public List<Integer> handleQueryResult(Statement stmt, ResultSet result) throws SQLException {
|
||||
ArrayList<Integer> list = new ArrayList<>();
|
||||
while(result.next()){
|
||||
list.add(result.getInt("id"));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
});
|
||||
for(int id : sensorIdList){
|
||||
logger.fine("Aggregating sensor_id: " + id);
|
||||
aggregateSensor(id);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void aggregateSensor(int sensorId) {
|
||||
DBConnection db = PowerChallenge.db;
|
||||
try {
|
||||
Long maxDBTimestamp = db.exec("SELECT MAX(timestamp_end) FROM sensor_data_aggr", new SimpleSQLHandler<Long>());
|
||||
Long maxDBTimestamp = db.exec("SELECT MAX(timestamp_end) FROM sensor_data_aggr WHERE sensor_id == "+sensorId, new SimpleSQLHandler<Long>());
|
||||
if(maxDBTimestamp == null)
|
||||
maxDBTimestamp = 0l;
|
||||
// 5 minute aggregation
|
||||
long minPeriodTimestamp = getTimestampMinutePeriodStart(5, System.currentTimeMillis());
|
||||
logger.fine("Calculating 5 min periods... (from:"+ maxDBTimestamp +", to:"+ minPeriodTimestamp +")");
|
||||
db.exec("SELECT * FROM sensor_data_raw "
|
||||
+ "WHERE timestamp > " + maxDBTimestamp + " AND timestamp < " + minPeriodTimestamp
|
||||
+ "WHERE sensor_id == "+sensorId+" AND timestamp > " + maxDBTimestamp + " AND timestamp < " + minPeriodTimestamp
|
||||
+ " ORDER BY timestamp ASC",
|
||||
new FiveMinuteAggregator());
|
||||
|
||||
// hour aggregation
|
||||
maxDBTimestamp = db.exec("SELECT MAX(timestamp_end) FROM sensor_data_aggr WHERE timestamp_end-timestamp_start == " + (HOUR_IN_MS-1), new SimpleSQLHandler<Long>());
|
||||
maxDBTimestamp = db.exec("SELECT MAX(timestamp_end) FROM sensor_data_aggr WHERE sensor_id == "+sensorId+" AND timestamp_end-timestamp_start == " + (HOUR_IN_MS-1), new SimpleSQLHandler<Long>());
|
||||
if(maxDBTimestamp == null)
|
||||
maxDBTimestamp = 0l;
|
||||
long hourPeriodTimestamp = getTimestampMinutePeriodStart(60, System.currentTimeMillis()-HOUR_AGGREGATION_OFFSET);
|
||||
logger.fine("Calculating hour periods... (from:"+ maxDBTimestamp +", to:"+ hourPeriodTimestamp +")");
|
||||
db.exec("SELECT * FROM sensor_data_aggr "
|
||||
+ "WHERE " + maxDBTimestamp + " < timestamp_start AND timestamp_start < " + hourPeriodTimestamp + " AND timestamp_end-timestamp_start == " + (FIVE_MINUTES_IN_MS-1)
|
||||
+ "WHERE sensor_id == "+sensorId+" AND " + maxDBTimestamp + " < timestamp_start AND timestamp_start < " + hourPeriodTimestamp + " AND timestamp_end-timestamp_start == " + (FIVE_MINUTES_IN_MS-1)
|
||||
+" ORDER BY timestamp_start ASC",
|
||||
new HourAggregator());
|
||||
|
||||
// day aggregation
|
||||
maxDBTimestamp = db.exec("SELECT MAX(timestamp_end) FROM sensor_data_aggr WHERE timestamp_end-timestamp_start == " + (DAY_IN_MS-1), new SimpleSQLHandler<Long>());
|
||||
maxDBTimestamp = db.exec("SELECT MAX(timestamp_end) FROM sensor_data_aggr WHERE sensor_id == "+sensorId+" AND timestamp_end-timestamp_start == " + (DAY_IN_MS-1), new SimpleSQLHandler<Long>());
|
||||
if(maxDBTimestamp == null)
|
||||
maxDBTimestamp = 0l;
|
||||
long dayPeriodTimestamp = getTimestampHourPeriodStart(24, System.currentTimeMillis()-DAY_AGGREGATION_OFFSET);
|
||||
logger.fine("Calculating day periods... (from:"+ maxDBTimestamp +", to:"+ dayPeriodTimestamp +")");
|
||||
db.exec("SELECT * FROM sensor_data_aggr "
|
||||
+ "WHERE " + maxDBTimestamp + " < timestamp_start AND timestamp_start < " + dayPeriodTimestamp + " AND timestamp_end-timestamp_start == " + (HOUR_IN_MS-1)
|
||||
+ "WHERE sensor_id == "+sensorId+" AND " + maxDBTimestamp + " < timestamp_start AND timestamp_start < " + dayPeriodTimestamp + " AND timestamp_end-timestamp_start == " + (HOUR_IN_MS-1)
|
||||
+" ORDER BY timestamp_start ASC",
|
||||
new DayAggregator());
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue