render power data in table format

This commit is contained in:
Eric Hulburd
2016-02-10 16:11:56 -06:00
parent b14c266de3
commit 9996dfc54e
26 changed files with 424 additions and 141 deletions

View File

@@ -45,7 +45,7 @@ var config = require('./config/webpack/development'),
contentBase: __dirname + '/../client/build/development',
publicPath: "/assets/",
proxy: {
'/data': `http://localhost:${APP_PORT}`,
'/data*': `http://localhost:${API_PORT}`,
},
stats: {colors: true}
}),

View File

@@ -11,10 +11,6 @@ module.exports = {
filename: '[name].js',
path: ROOT + 'client/build/development'
},
externals: {
jquery: "$",
d3: "d3"
},
module: {
loaders: [
{
@@ -26,6 +22,9 @@ module.exports = {
}, {
test: /\.js$/,
loader: 'babel'
}, {
test: /\.json$/,
loader: 'json-loader'
}
]
},
@@ -37,6 +36,10 @@ module.exports = {
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
}),
new webpack.ProvidePlugin({
d3: "d3",
"window.d3": "d3"
})
]
}

View File

@@ -5,10 +5,8 @@ const NAME = 'EnergyController';
class EnergyController{
static index(req, res){
DB.House.findOne({where: {name: req.housename}}).then((house)=>{
house.getEnergyDataByTime(req.params.start_time, req.params.end_time).then((energy_data)=>{
req.json(energy_data);
});
DB.EnergyDatum.exposeForHouseAtDates(req.query.house_id, req.query.dates).then((energy_data)=>{
req.json({data: energy_data});
});
}

View File

@@ -1,12 +1,14 @@
import DB from './../config/database.js';
const NAME = HousesController;
const NAME = 'HousesController';
class HousesController{
class HousesController {
static index(req, res){
DB.House.findAll({attributes: ['id', 'name']}).then((houses)=>{
res.json(houses);
var params = {};
if (req.query.ids) query.id = ids;
DB.House.findAll({where: params, attributes: ['id', 'name', 'timezone']}).then((houses)=>{
res.json({data: houses.map((house)=>{ return house.dataValues; })});
});
}

View File

@@ -5,10 +5,9 @@ const NAME = 'PowerController';
class PowerController{
static index(req, res){
DB.House.findOne({where: {name: req.housename}}).then((house)=>{
house.getPowerDataByTime(req.params.start_time, req.params.end_time).then((power_data)=>{
res.json(power_data);
});
console.log(req.query);
DB.PowerDatum.exposeForHouseAtDates(req.query.house_id, req.query.dates).then((power_data)=>{
res.json({data: power_data});
});
}

View File

@@ -0,0 +1,31 @@
import moment from 'moment';
class ApiHelper {
// assume all dates from api coming as UNIX timestamps.
static datesParamToSequelize(dates, field_name){
if (!dates) return {};
var params = {};
if (dates.length > 1){
params['$or'] = [];
dates.forEach((min_max)=>{
var condition_n = {};
condition_n[field_name] = {};
if (min_max[0]) condition_n[field_name]['$gt'] = moment.unix(min_max[0]).toDate();
if (min_max[1]) condition_n[field_name]['$lt'] = moment.unix(min_max[1]).toDate();
if (Object.keys(condition_n).length) params['$or'].push(condition_n);
});
} else {
var min_max = dates[0],
condition = {}
if (min_max[0]) params[field_name]['$gt'] = moment.unix(min_max[0]).toDate();
if (min_max[1]) params[field_name]['$lt'] = moment.unix(min_max[1]).toDate();
if (Object.keys(condition).length) params[field_name] = condition;
}
return params;
}
}
export default ApiHelper;

View File

@@ -1,4 +1,6 @@
import DB from "./../config/database";
import extend from 'extend';
import ApiHelper from './../helpers/api_helper';
const NAME = 'EnergyDatum';
@@ -20,7 +22,12 @@ var EnergyDatum = DB.sequelize.define(NAME, {
underscored: true,
tableName: "energy_data",
instanceMethods: {
exposeToApi: ()=>{
var energy_datum = this,
values = this.dataValues;
values.energy_datum = energy_datum.day.getTime() / 1000;
return values;
}
},
classMethods: {
set: ()=>{
@@ -28,6 +35,18 @@ var EnergyDatum = DB.sequelize.define(NAME, {
},
associate: ()=>{
EnergyDatum.belongsTo(DB.House);
},
exposeForHouseAtDates: (house_id, dates)=>{
var params = {house_id: house_id};
extend(params, ApiHelper.datesParamToSequelize(dates, 'day'));
return EnergyDatum.findAll({
where: params,
attributes: ['id', 'production', 'consumption', 'day']
}).then((energy_data)=>{
return energy_data.map((energy_datum)=>{
return energy_datum.exposeToApi();
});
});
}
}
});

View File

@@ -44,8 +44,6 @@ var House = DB.sequelize.define(NAME, {
power_data.forEach((power_datum)=>{
var day = house.timeToDateString(power_datum.time),
energy_datum = energy_data.get(day) || {production: 0, consumption: 0, day: day, house_id: house.id};
console.log(power_datum.time)
console.log(day)
energy_datum.production += power_datum.production;
energy_datum.consumption += power_datum.consumption;
energy_data.set(day, energy_datum);
@@ -62,10 +60,10 @@ var House = DB.sequelize.define(NAME, {
associate: ()=>{
House.hasMany(DB.PowerDatum, {as: 'PowerData'});
},
getPowerDataByTime: (start_date, end_date)=>{
getPowerDataByTime: (dates)=>{
var params = {
where: {time: {}},
attributes: ['time', 'consumption', 'production']
attributes: ['id', 'time', 'consumption', 'production']
};
if (start_date) params.where.time.$gt = moment.utc(start_date).toDate();
if (end_date) params.where.time.$lt = moment.utc(end_date).toDate();

View File

@@ -1,4 +1,6 @@
import DB from "./../config/database";
import extend from 'extend';
import ApiHelper from './../helpers/api_helper';
const NAME = 'PowerDatum';
@@ -20,9 +22,28 @@ var PowerDatum = DB.sequelize.define(NAME, {
underscored: true,
tableName: "power_data",
instanceMethods: {
exposeToApi: function(){
var power_datum = this,
data = power_datum.dataValues;
data.consumption = data.consumption * 4; // convert Wh / 15 minutes, to W
data.production = data.production * 4; // convert Wh / 15 minutes, to W
return data;
}
},
classMethods: {
exposeForHouseAtDates: (house_id, dates)=>{
var params = {house_id: house_id};
params = extend(params, ApiHelper.datesParamToSequelize(dates, 'time'));
console.log(params);
return PowerDatum.findAll({
where: params,
attributes: ['id', 'production', 'consumption', 'time']
}).then((power_data)=>{
return power_data.map((power_datum)=>{
return power_datum.exposeToApi();
});
});
},
set: ()=>{
PowerDatum.associate();
},

View File

@@ -4,8 +4,8 @@ export default function(app){
Controllers.sync();
app.use('/data/v1/savings/:housename', Controllers.PowerController.index);
app.use('/data/v1/production/:housename', Controllers.EnergyController.index);
app.use('/data/v1/houses/', Controllers.EnergyController.index);
app.use('/data/v1/power', Controllers.PowerController.index);
app.use('/data/v1/energy', Controllers.EnergyController.index);
app.use('/data/v1/houses', Controllers.HousesController.index);
};