fix problems with rendering data
This commit is contained in:
@@ -41,5 +41,8 @@ module.exports = {
|
||||
d3: "d3",
|
||||
"window.d3": "d3"
|
||||
})
|
||||
]
|
||||
],
|
||||
node: {
|
||||
fs: "empty"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,5 +51,8 @@ module.exports = {
|
||||
jQuery: "jquery",
|
||||
"window.jQuery": "jquery"
|
||||
})
|
||||
]
|
||||
],
|
||||
node: {
|
||||
fs: "empty"
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@ class HousesController {
|
||||
static index(req, res){
|
||||
var params = {};
|
||||
if (req.query.ids) query.id = ids;
|
||||
DB.House.findAll({where: params, attributes: ['id', 'name', 'timezone']}).then((houses)=>{
|
||||
DB.House.findAll({where: params}).then((houses)=>{
|
||||
res.json({data: houses.map((house)=>{ return house.dataValues; })});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ const NAME = 'PowerController';
|
||||
class PowerController{
|
||||
|
||||
static index(req, res){
|
||||
console.log(req.query);
|
||||
DB.PowerDatum.exposeForHouseAtDates(req.query.house_id, req.query.dates).then((power_data)=>{
|
||||
res.json({data: power_data});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import moment from 'moment';
|
||||
|
||||
class ApiHelper {
|
||||
|
||||
// assume all dates from api coming as UNIX timestamps.
|
||||
@@ -12,15 +10,15 @@ class ApiHelper {
|
||||
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 (min_max[0]) condition_n[field_name]['$gt'] = min_max[0];
|
||||
if (min_max[1]) condition_n[field_name]['$lt'] = min_max[1];
|
||||
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 (min_max[0]) condition['$gt'] = min_max[0];
|
||||
if (min_max[1]) condition['$lt'] = min_max[1];
|
||||
if (Object.keys(condition).length) params[field_name] = condition;
|
||||
}
|
||||
return params;
|
||||
|
||||
@@ -18,7 +18,7 @@ export class PowerDataSeed {
|
||||
rows = [];
|
||||
|
||||
csvStream.on("data", function(data){
|
||||
data.time = moment.utc(parseInt(data.time * 1000)).format();
|
||||
data.time = data.time;
|
||||
rows.push(data);
|
||||
if (rows.length % 100 === 0){
|
||||
DB.PowerDatum.bulkCreate(rows, {validate: true}).catch((error)=>{
|
||||
@@ -47,13 +47,13 @@ export class PowerDataSeed {
|
||||
|
||||
static generateCsv(opts, done){
|
||||
opts = extend({
|
||||
start_date: moment().subtract(2, "months").unix(),
|
||||
start_date: moment().subtract(120, "months").unix(),
|
||||
end_date: moment().unix(),
|
||||
interval: 900, // every 15 minutes (in s)
|
||||
average: 1400, // Wh
|
||||
path: DATA_PATH + "power_data.csv"
|
||||
}, opts || {});
|
||||
|
||||
console.log(opts.start_date, opts.end_date)
|
||||
var row_date = opts.start_date,
|
||||
csvStream = csv.format({headers: true}),
|
||||
writableStream = fs.createWriteStream(opts.path),
|
||||
|
||||
@@ -15,7 +15,7 @@ var EnergyDatum = DB.sequelize.define(NAME, {
|
||||
autoIncrement: true // Automatically gets converted to SERIAL for postgres
|
||||
},
|
||||
day: {
|
||||
type: DB.Sequelize.DATEONLY,
|
||||
type: DB.Sequelize.INTEGER,
|
||||
},
|
||||
production: DB.Sequelize.FLOAT,
|
||||
consumption: DB.Sequelize.FLOAT
|
||||
@@ -23,14 +23,7 @@ var EnergyDatum = DB.sequelize.define(NAME, {
|
||||
paranoid: true,
|
||||
underscored: true,
|
||||
tableName: "energy_data",
|
||||
instanceMethods: {
|
||||
exposeToApi: function(){
|
||||
var energy_datum = this,
|
||||
values = energy_datum.dataValues;
|
||||
values.energy_datum = energy_datum.day.getTime() / 1000;
|
||||
return values;
|
||||
}
|
||||
},
|
||||
instanceMethods: {},
|
||||
classMethods: {
|
||||
set: ()=>{
|
||||
EnergyDatum.associate();
|
||||
@@ -46,7 +39,7 @@ var EnergyDatum = DB.sequelize.define(NAME, {
|
||||
attributes: ['id', 'production', 'consumption', 'day']
|
||||
}).then((energy_data)=>{
|
||||
return energy_data.map((energy_datum)=>{
|
||||
return energy_datum.exposeToApi();
|
||||
return energy_datum.dataValues;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -14,7 +14,13 @@ var House = DB.sequelize.define(NAME, {
|
||||
autoIncrement: true // Automatically gets converted to SERIAL for postgres
|
||||
},
|
||||
timezone: DB.Sequelize.STRING,
|
||||
name: DB.Sequelize.STRING
|
||||
name: DB.Sequelize.STRING,
|
||||
data_until: {
|
||||
type: DB.Sequelize.INTEGER,
|
||||
},
|
||||
data_from: {
|
||||
type: DB.Sequelize.INTEGER,
|
||||
}
|
||||
}, {
|
||||
paranoid: true,
|
||||
underscored: true,
|
||||
@@ -29,26 +35,47 @@ var House = DB.sequelize.define(NAME, {
|
||||
}
|
||||
return multiplier;
|
||||
},
|
||||
timeToDateString: function(timestamp){
|
||||
unixToLocalDay: function(unix){
|
||||
var house = this;
|
||||
return moment.tz(timestamp, house.timezone).format("YYYY-MM-DD");
|
||||
return moment.tz(unix * 1000, house.timezone).startOf('day').unix();
|
||||
},
|
||||
aggregatePowerToEnergyData: function(){
|
||||
var house = this;
|
||||
return DB.EnergyDatum.destroy({where: {house_id: house.id}})
|
||||
.then(()=>{
|
||||
return house.getPowerData();
|
||||
return DB.PowerDatum.count({where: {house_id: house.id}})
|
||||
})
|
||||
.then((power_data)=>{
|
||||
var energy_data = new Map();
|
||||
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};
|
||||
energy_datum.production += power_datum.production / 1000; // convert Wh to kWh
|
||||
energy_datum.consumption += power_datum.consumption / 1000; // convert Wh to kWh
|
||||
energy_data.set(day, energy_datum);
|
||||
.then((count)=>{
|
||||
var limit = 0,
|
||||
energy_data = new Map(),
|
||||
promises = [];
|
||||
while (limit < count){
|
||||
let complete = DB.PowerDatum.findAll({where: {house_id: house.id}, limit: 1000, offset: limit, order: 'id ASC'})
|
||||
.then((power_data)=>{
|
||||
power_data.forEach((power_datum)=>{
|
||||
var day = house.unixToLocalDay(power_datum.time),
|
||||
energy_datum = energy_data.get(day) || {production: 0, consumption: 0, day: day, house_id: house.id};
|
||||
energy_datum.production += power_datum.production / 1000; // convert Wh to kWh
|
||||
energy_datum.consumption += power_datum.consumption / 1000; // convert Wh to kWh
|
||||
energy_data.set(day, energy_datum);
|
||||
});
|
||||
});
|
||||
promises.push(complete);
|
||||
limit += 1000;
|
||||
}
|
||||
return Promise.all(promises).then(()=>{
|
||||
return DB.EnergyDatum.bulkCreate(Array.from(energy_data.values()), {validate: true});
|
||||
});
|
||||
return DB.EnergyDatum.bulkCreate(Array.from(energy_data.values()), {validate: true});
|
||||
})
|
||||
.then(()=>{
|
||||
return house.getPowerData({order: 'time DESC', limit: 1});
|
||||
})
|
||||
.then((max_data)=>{
|
||||
house.data_until = max_data[0].time;
|
||||
return house.getPowerData({order: 'time ASC', limit: 1});
|
||||
}).then((min_data)=>{
|
||||
house.data_from = min_data[0].time;
|
||||
return house.save();
|
||||
});
|
||||
}
|
||||
},
|
||||
@@ -58,16 +85,6 @@ var House = DB.sequelize.define(NAME, {
|
||||
},
|
||||
associate: ()=>{
|
||||
House.hasMany(DB.PowerDatum, {as: 'PowerData'});
|
||||
},
|
||||
getPowerDataByTime: (dates)=>{
|
||||
var params = {
|
||||
where: {time: {}},
|
||||
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();
|
||||
|
||||
return House.getPowerData(params);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -15,7 +15,7 @@ var PowerDatum = DB.sequelize.define(NAME, {
|
||||
autoIncrement: true // Automatically gets converted to SERIAL for postgres
|
||||
},
|
||||
time: {
|
||||
type: DB.Sequelize.DATE,
|
||||
type: DB.Sequelize.INTEGER,
|
||||
},
|
||||
consumption: DB.Sequelize.FLOAT,
|
||||
production: DB.Sequelize.FLOAT
|
||||
|
||||
Reference in New Issue
Block a user