dont know what happened

This commit is contained in:
Eric Hulburd
2016-02-22 14:36:07 -06:00
parent 0ddae601bd
commit b8d0a9434b
69 changed files with 3711 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
'use strict';
// through2 is a thin wrapper around node transform streams
import through from 'through2';
import gutil from 'gulp-util';
import rt from 'react-templates';
import path from 'path';
import extend from 'extend';
// Consts
const PLUGIN_NAME = 'gulp-react-templates';
var PluginError = gutil.PluginError;
function normalizeName(name) {
return name.replace(/-/g, '_');
}
export default function (opt) {
function replaceExtension(filePath) {
return filePath + '.js';
}
function transform(file, enc, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
var filePath = file.path,
str = file.contents.toString('utf8'),
data;
var options = extend({
filename: file.path,
sourceFiles: [file.relative],
generatedFile: replaceExtension(file.relative)
}, opt);
if (options.suffix && !options.name) {
options.name = normalizeName(path.basename(filePath, path.extname(filePath))) + options.suffix;
}
try {
data = rt.convertTemplateToReact(str, options);
} catch (err) {
return cb(new PluginError(PLUGIN_NAME, err));
}
file.contents = new Buffer(data);
file.path = replaceExtension(file.path);
cb(null, file);
}
return through.obj(transform);
};

View File

@@ -0,0 +1,103 @@
import extend from "extend";
import moment from "moment";
import csv from "fast-csv";
import fs from 'fs';
import MathUtils from "./../../../shared/utils/math"
import DB from './../../config/database';
const DATA_PATH = __dirname + '/../../../shared/data/'
export class PowerDataSeed {
static saveCsv(opts, done){
opts = extend({
path: DATA_PATH + "power_data.csv"
}, opts || {});
var stream = fs.createReadStream(opts.path),
csvStream = csv.fromStream(stream, {headers: ['house_id', 'time', 'consumption', 'production']}),
rows = [];
csvStream.on("data", function(data){
data.time = data.time;
rows.push(data);
if (rows.length % 100 === 0){
DB.PowerDatum.bulkCreate(rows, {validate: true}).catch((error)=>{
console.error(JSON.stringify(error));
console.error(JSON.stringify(rows));
});
rows = [];
}
});
csvStream.on("end", function(){
console.log("all rows parsed")
DB.PowerDatum.bulkCreate(rows, {validate: true}).then(()=>{
return DB.House.findAll().then((houses)=>{
var promises = [];
for (var house of houses){
var p = house.aggregatePowerToEnergyData();
promises.push(p);
}
return Promise.all(promises);
});
}).then(()=>{
console.log("DONE!")
});
});
}
static generateCsv(opts, done){
opts = extend({
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),
house_ids = opts.house_ids.split(",")
DB.House.findAll({where: {id: house_ids}}).then((houses)=>{
csvStream.pipe(writableStream);
writableStream.on("finish", ()=>{
console.log("DONE!")
done();
});
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();
});
}
}
export class HouseSeed {
static saveCsv(opts, done){
opts = extend({
path: DATA_PATH + "houses.csv"
}, opts || {});
var stream = fs.createReadStream(opts.path),
csvStream = csv.fromStream(stream, {headers: ['id', 'name', 'timezone']}),
rows = [];
csvStream.on("data", function(data){
rows.push(data);
});
csvStream.on("end", function(){
console.log(rows);
DB.House.bulkCreate(rows, {validate: true}).then(()=>{
console.log("DONE!")
done();
});
});
}
}