2016-01-27 20:15:09 -06:00
|
|
|
import extend from "extend";
|
|
|
|
|
import moment from "moment";
|
|
|
|
|
import csv from "fast-csv";
|
|
|
|
|
import fs from 'fs';
|
2016-02-07 15:32:02 -06:00
|
|
|
import MathUtils from "./../../../shared/utils/math"
|
2016-01-27 20:15:09 -06:00
|
|
|
import DB from './../../config/database';
|
2016-01-26 19:42:30 -06:00
|
|
|
|
2016-02-07 15:32:02 -06:00
|
|
|
const DATA_PATH = __dirname + '/../../../shared/data/'
|
|
|
|
|
|
2016-01-27 20:15:09 -06:00
|
|
|
export class PowerDataSeed {
|
2016-01-26 19:42:30 -06:00
|
|
|
|
2016-01-27 20:15:09 -06:00
|
|
|
static saveCsv(opts, done){
|
|
|
|
|
opts = extend({
|
2016-02-07 15:32:02 -06:00
|
|
|
path: DATA_PATH + "power_data.csv"
|
2016-01-27 20:15:09 -06:00
|
|
|
}, opts || {});
|
|
|
|
|
var stream = fs.createReadStream(opts.path),
|
2016-02-07 15:32:02 -06:00
|
|
|
csvStream = csv.fromStream(stream, {headers: ['house_id', 'time', 'consumption', 'production']}),
|
2016-01-27 20:15:09 -06:00
|
|
|
rows = [];
|
2016-01-26 19:42:30 -06:00
|
|
|
|
2016-01-27 20:15:09 -06:00
|
|
|
csvStream.on("data", function(data){
|
2016-02-07 15:32:02 -06:00
|
|
|
data.time = moment.utc(parseInt(data.time * 1000)).format();
|
2016-01-31 11:35:22 -06:00
|
|
|
rows.push(data);
|
|
|
|
|
if (rows.length % 100 === 0){
|
2016-02-10 18:57:15 -06:00
|
|
|
DB.PowerDatum.bulkCreate(rows, {validate: true}).catch((error)=>{
|
|
|
|
|
console.error(JSON.stringify(error));
|
|
|
|
|
console.error(JSON.stringify(rows));
|
2016-01-31 11:35:22 -06:00
|
|
|
});
|
2016-02-10 18:57:15 -06:00
|
|
|
rows = [];
|
2016-01-31 11:35:22 -06:00
|
|
|
}
|
2016-01-27 20:15:09 -06:00
|
|
|
});
|
|
|
|
|
csvStream.on("end", function(){
|
2016-02-07 15:32:02 -06:00
|
|
|
console.log("all rows parsed")
|
2016-01-31 11:35:22 -06:00
|
|
|
DB.PowerDatum.bulkCreate(rows, {validate: true}).then(()=>{
|
2016-02-07 15:32:02 -06:00
|
|
|
return DB.House.findAll().then((houses)=>{
|
2016-02-10 18:57:15 -06:00
|
|
|
var promises = [];
|
2016-02-07 15:32:02 -06:00
|
|
|
for (var house of houses){
|
2016-02-10 18:57:15 -06:00
|
|
|
var p = house.aggregatePowerToEnergyData();
|
|
|
|
|
promises.push(p);
|
2016-02-07 15:32:02 -06:00
|
|
|
}
|
2016-02-10 18:57:15 -06:00
|
|
|
return Promise.all(promises);
|
2016-02-07 15:32:02 -06:00
|
|
|
});
|
|
|
|
|
}).then(()=>{
|
2016-01-31 11:35:22 -06:00
|
|
|
console.log("DONE!")
|
|
|
|
|
});
|
2016-01-27 20:15:09 -06:00
|
|
|
});
|
2016-01-26 19:42:30 -06:00
|
|
|
}
|
|
|
|
|
|
2016-01-27 20:15:09 -06:00
|
|
|
static generateCsv(opts, done){
|
|
|
|
|
opts = extend({
|
2016-01-31 11:35:22 -06:00
|
|
|
start_date: moment().subtract(2, "months").unix(),
|
2016-01-27 20:15:09 -06:00
|
|
|
end_date: moment().unix(),
|
2016-02-07 15:32:02 -06:00
|
|
|
interval: 900, // every 15 minutes (in s)
|
2016-01-27 20:15:09 -06:00
|
|
|
average: 1400, // Wh
|
2016-02-07 15:32:02 -06:00
|
|
|
path: DATA_PATH + "power_data.csv"
|
2016-01-27 20:15:09 -06:00
|
|
|
}, opts || {});
|
2016-01-26 19:42:30 -06:00
|
|
|
|
2016-01-27 20:15:09 -06:00
|
|
|
var row_date = opts.start_date,
|
|
|
|
|
csvStream = csv.format({headers: true}),
|
2016-01-31 11:35:22 -06:00
|
|
|
writableStream = fs.createWriteStream(opts.path),
|
|
|
|
|
house_ids = opts.house_ids.split(",")
|
2016-01-26 19:42:30 -06:00
|
|
|
|
2016-02-07 15:32:02 -06:00
|
|
|
DB.House.findAll({where: {id: house_ids}}).then((houses)=>{
|
2016-01-26 19:42:30 -06:00
|
|
|
|
2016-02-07 15:32:02 -06:00
|
|
|
csvStream.pipe(writableStream);
|
|
|
|
|
writableStream.on("finish", ()=>{
|
2016-01-31 11:35:22 -06:00
|
|
|
console.log("DONE!")
|
|
|
|
|
done();
|
|
|
|
|
});
|
2016-02-07 15:32:02 -06:00
|
|
|
|
|
|
|
|
while (row_date <= opts.end_date){
|
|
|
|
|
for (var house of houses){
|
|
|
|
|
var consumption = MathUtils.normal(opts.average),
|
|
|
|
|
production = MathUtils.normal(opts.average) * house.productionMultiplier(row_date * 1000);
|
|
|
|
|
csvStream.write([house.id, row_date, consumption, production]);
|
|
|
|
|
}
|
|
|
|
|
row_date += opts.interval;
|
|
|
|
|
}
|
|
|
|
|
csvStream.end();
|
2016-01-26 19:42:30 -06:00
|
|
|
});
|
2016-01-27 20:15:09 -06:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-26 19:42:30 -06:00
|
|
|
|
2016-02-07 15:32:02 -06:00
|
|
|
export class HouseSeed {
|
2016-01-31 17:18:27 -06:00
|
|
|
static saveCsv(opts, done){
|
|
|
|
|
opts = extend({
|
2016-02-07 15:32:02 -06:00
|
|
|
path: DATA_PATH + "houses.csv"
|
2016-01-31 17:18:27 -06:00
|
|
|
}, opts || {});
|
|
|
|
|
var stream = fs.createReadStream(opts.path),
|
2016-02-07 15:32:02 -06:00
|
|
|
csvStream = csv.fromStream(stream, {headers: ['id', 'name', 'timezone']}),
|
2016-01-31 17:18:27 -06:00
|
|
|
rows = [];
|
|
|
|
|
|
|
|
|
|
csvStream.on("data", function(data){
|
|
|
|
|
rows.push(data);
|
|
|
|
|
});
|
|
|
|
|
csvStream.on("end", function(){
|
|
|
|
|
console.log(rows);
|
2016-02-07 15:32:02 -06:00
|
|
|
DB.House.bulkCreate(rows, {validate: true}).then(()=>{
|
2016-01-31 17:18:27 -06:00
|
|
|
console.log("DONE!")
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|