seed houses

This commit is contained in:
Eric Hulburd
2016-01-27 20:15:09 -06:00
parent 220ddfed19
commit e4513282f9
17 changed files with 159 additions and 14634 deletions

View File

@@ -1,40 +1,39 @@
"use strict";
var fs = require("fs"),
models = [],
model_path = __dirname + "/../models",
files = fs.readdirSync(model_path),
Sequelize = require("sequelize"),
sequelize = new Sequelize("postgres://spikeuser:123456@localhost:5432/spike_proto", {
pool: {
max: 5,
min: 0,
idle: 10000
}
});
import fs from "fs";
import Sequelize from 'sequelize';
var sequelize = new Sequelize("postgres://spikeuser:123456@localhost:5432/spike_proto", {
pool: {
max: 5,
min: 0,
idle: 10000
}
});
const model_dir = __dirname + '/../models'
class Database {
static sync(){
// define each model
for (var filename of files){
var path = model_path + "/" + filename,
stats = fs.statSync(path)
if (stats.isFile()){
models.push(require(path));
}
}
fs.readdirSync(model_dir).forEach(function(file) {
model = require(model_dir + '/' + file);
Database[model.name] = model;
Database.models.push(model);
});
console.log("!!!! TEST !!!!")
console.log(Database.PowerDatum);
// add associations
for (var model of models){
for (var model of Database.models){
model.associate();
}
return sequelize.sync({force: true});
}
}
Database.Sequelize = Sequelize;
Database.sequelize = sequelize;
Database.Sequelize = Sequelize;
Database.models = [];
export Database;
export default Database;

View File

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

3
data/example/houses.csv Normal file
View File

@@ -0,0 +1,3 @@
1,Johnson
2,Beverley
3,Thompson
1 1 Johnson
2 2 Beverley
3 3 Thompson

22
gulpfile.babel.js Normal file
View File

@@ -0,0 +1,22 @@
import gulp from 'gulp';
import yargs from 'yargs';
import DB from './config/database';
import {PowerDataSeed, HouseSeed} from './lib/tasks/seed_data'
gulp.task('generate_power_csv', function(done){
DB.sync().then(()=>{
PowerDataSeed.generateCsv(yargs.argv, done);
});
});
gulp.task('save_power_csv', function(done){
DB.sync().then(()=>{
PowerDataSeed.saveCsv(yargs.argv, done);
});
});
gulp.task('save_house_csv', function(done){
DB.sync().then(()=>{
HouseSeed.saveCsv(yargs.argv, done);
});
});

View File

@@ -1,4 +0,0 @@
var gulp = require("gulp"),
seedDate = require("./lib/tasks/seed_data");
gulp.task('seedData', seedData);

View File

@@ -1,5 +0,0 @@
export class Model {
static associate(){}
}

View File

@@ -4,24 +4,23 @@
* The first method defines the way we resolve an ID to its object.
* The second defines the way we resolve an object to its GraphQL type.
*/
import {
nodeDefinitions,
} from 'graphql-relay';
var {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
var {type, id} = fromGlobalId(globalId);
if (type === 'PowerDatum') {
return getUser(id);
} else if (type === 'Widget') {
return getWidget(id);
} else {
return null;
}
return null;
},
(obj) => {
if (obj instanceof User) {
return userType;
} else if (obj instanceof Widget) {
return widgetType;
if (obj instanceof Array) {
return Array;
} else {
return null;
}
}
);
export {nodeInterface, nodeField};

View File

@@ -1,104 +1,71 @@
module.exports = function(done) {
import extend from "extend";
import moment from "moment";
import csv from "fast-csv";
import fs from 'fs';
import MathUtils from "./../utils/math"
import DB from './../../config/database';
var csv = require("fast-csv"),
Database = require("./config/database"),
Country = require("./models/country"),
Datum = require("./models/datum"),
countries = [],
data = new Map();
export class PowerDataSeed {
function seedEmissions(fn){
var i = -1;
csv
.fromPath(__dirname + "/data/CAIT/emissions.csv")
.on("data", (row)=>{
i += 1;
if (i == 0) return true;
var name = row[0],
year = parseInt(row[1]),
key = name + year,
energy = parseFloat(row[11]) || null,
industrial = parseFloat(row[12]) || null,
agriculture = parseFloat(row[13]) || null,
waste = parseFloat(row[14]) || null,
lucf = parseFloat(row[15]) || null,
total_emissions = Math.max((energy + industrial + agriculture + waste + lucf), parseFloat(row[3])) || null; // MtCO2eq
static saveCsv(opts, done){
opts = extend({
path: __dirname + "/../../data/example/power_data.csv"
}, opts || {});
var stream = fs.createReadStream(opts.path),
csvStream = csv.fromStream(stream, {headers: ['house_id', 'time', 'power']}),
rows = [];
if (countries.indexOf(name) < 0) countries.push(name);
data.set(key, data.get(key) || {name: name, year: year});
datum = data.get(key);
datum.energy_emissions = energy; // MtCO2eq
datum.industrial_emissions = industrial; // MtCO2eq
datum.agriculture_emissions = agriculture; // MtCO2eq
datum.waste_emissions = waste; // MtCO2eq
datum.lucf_emissions = lucf; // MtCO2eq
datum.total_emissions = total_emissions; // MtCO2eq
})
.on("end", fn);
}
function seedSocioEco(fn){
var i = -1;
csv
.fromPath(__dirname + "/data/CAIT/socioeconomic.csv")
.on("data", (row)=>{
i += 1;
if (i == 0) return true;
var name = row[0],
year = parseInt(row[1]),
key = (name + year),
population = parseInt(row[2]) || null,
gdp = parseFloat(row[4]) || null, // Million 2005 USD
energy = parseFloat(row[5]) || null; // ktoe
if (countries.indexOf(name) < 0) countries.push(name);
data.set(key, data.get(key) || {name: name, year: year});
datum = data.get(key);
datum.population = population;
datum.energy = energy;
datum.gdp = gdp;
})
.on("end", fn);
}
Database.sync().then(()=>{
seedEmissions(()=>{
seedSocioEco(()=>{
Country.sql.bulkCreate(countries.map((name)=>{ return {name: name}; }), {validate: true}).catch((errors)=>{
console.error(`=== Error ===\n${JSON.stringify(errors)}\n`);
}).then(()=>{
return Country.sql.findAll();
}).then((_countries)=>{
var country_name_to_id = new Map();
for (var country of _countries){
country_name_to_id.set(country.name, country.id);
}
for (var datum of data.values()){
datum.country_id = country_name_to_id.get(datum.name);
delete datum["name"];
}
var bulk_data = Array.from(data.values());
Datum.sql.bulkCreate(bulk_data, {validate: true}).catch((errors)=>{
console.error(`=== Error ===\n${JSON.stringify(errors)}\n`);
}).error((err)=>{
console.error(`=== Error ===\n${err}`);
}).then(()=>{
return Datum.sql.count();
}).then((count)=>{
console.log(`Count: ${count} ${bulk_data.length}`)
if (count === bulk_data.length){
done();
} else {
console.log("Error!");
done();
}
});
});
});
csvStream.on("data", function(data){
rows.push(rows);
});
});
csvStream.on("end", function(){
DB.PowerDatum.bulkCreate(rows, {validate: true}).then(done);
});
}
static generateCsv(opts, done){
opts = extend({
start_date: moment.subtract(1, "months").unix(),
end_date: moment().unix(),
interval: 15 * 60, // every 15 minutes (in s)
average: 1400, // Wh
path: __dirname + "/../../data/example/power_data.csv"
}, opts || {});
var row_date = opts.start_date,
csvStream = csv.format({headers: true}),
writableStream = fs.createWriteStream(opts.path);
csvStream.pipe(writableStream);
writableStream.on("finish", done);
while (row_date <= end_date){
csvStream.write(house_ids.map((house_id)=>{
return [house_id, row_date, MathUtils.normal(opts.average)]
}));
row_date += opts.interval;
}
csvStream.end();
}
}
export class HouseSeed {
static saveCsv(opts, done){
opts = extend({
path: __dirname + "/../../data/example/houses.csv"
}, opts || {});
var stream = fs.createReadStream(opts.path),
csvStream = csv.fromStream(stream, {headers: ['id', 'name']}),
rows = [];
csvStream.on("data", function(data){
console.log(JSON.stringify(data))
rows.push(data);
});
csvStream.on("end", function(){
console.log(rows);
DB.House.bulkCreate(rows, {validate: true}).then(done);
});
}
}
};

10
lib/utils/math.js Normal file
View File

@@ -0,0 +1,10 @@
export default class {
static normal(average){
return average + n6() * average;
}
static n6(){
return ((Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random()) - 3) / 3;
}
}

View File

@@ -1,54 +0,0 @@
"use strict";
var Database = require("./../config/database");
class Country {
static associate(){
var Datum = require("./datum");
this.sql.hasOne(Datum.sql, {as: "Data", foreignKey: "id", constraints: false});
}
};
Country.sql = Database.sequelize.define('Country', {
id: {
type: Database.Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true // Automatically gets converted to SERIAL for postgres
},
name: {
type: Database.Sequelize.STRING,
unique: true,
allowNull: false
}
}, {tableName: "countries"});
Country.graphql = new GraphQLObjectType({
name: "Country",
description: "A world country",
fields: ()=>{
id: {
type: new GraphQLNonNull(GraphQLInteger)
},
name: {
type: new GraphQLNonNull(GraphQLString)
},
data: {
type: new GraphQLList(Datum.graphql),
args: {
year: {
type: GraphQLList(GraphQLInteger)
}
},
resolve: (country, args)=>{
debugger
if (args.year){
country.getData({where: args});
} else {
country.getData();
}
}
}
});
module.exports = Country;

View File

@@ -1,78 +0,0 @@
"use strict";
var Database = require("./../config/database");
class Datum {
static associate(){
var Country = require("./country");
this.sql.belongsTo(Country.sql, {foreignKey: "country_id", targetKey: "id", constraints: false});
}
}
Datum.sql = Database.sequelize.define('Datum', {
id: {
type: Database.Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true // Automatically gets converted to SERIAL for postgres
},
country_id: {
type: Database.Sequelize.INTEGER,
unique: "country_year",
references: {
model: "countries",
key: "id",
allowNull: false
}
},
year: {
type: Database.Sequelize.INTEGER,
unique: "country_year",
allowNull: false
},
population: Database.Sequelize.BIGINT,
gdp: Database.Sequelize.FLOAT,
total_emissions: Database.Sequelize.FLOAT,
energy_emissions: Database.Sequelize.FLOAT,
industrial_emissions: Database.Sequelize.FLOAT,
agriculture_emissions: Database.Sequelize.FLOAT,
waste_emissions: Database.Sequelize.FLOAT,
lucf_emissions: Database.Sequelize.FLOAT,
energy: Database.Sequelize.FLOAT
}, {tableName: "data"});
Datum.graphql = new GraphQLObjectType({
name: "Datum",
description: "A world country",
fields: ()=>{
year: {
type: new GraphQLNonNull(GraphQLString)
},
population: {
type: GraphQLInteger
},
gdp: {
type: GraphQLFloat
},
total_emissions: {
type: GraphQLFloat
},
energy_emissions: {
type: GraphQLFloat
},
industrial_emissions: {
type: GraphQLFloat
},
agriculture_emissions: {
type: GraphQLFloat
},
waste_emissions: {
type: GraphQLFloat
},
lucf_emissions: {
type: GraphQLFloat
}
}
});
module.exports = Datum;

View File

@@ -10,28 +10,28 @@ import {
nodeDefinitions,
} from 'graphql-relay';
import Database from "./../config/database";
import PowerData from "./power_datum"
import DB from "./../config/database";
import {nodeInterface} from './../lib/node.relay';
/**
* Define your own types here
*/
var House = Database.sequelize.define('House', {
var House = DB.sequelize.define('House', {
id: {
type: Database.Sequelize.INTEGER,
type: DB.Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true // Automatically gets converted to SERIAL for postgres
},
name: Database.Sequelize.STRING
name: DB.Sequelize.STRING
}, {
tableName: "houses",
instanceMethods: {
},
classMethods: {
associate: function(){
House.hasMany(PowerDatum, {as: 'PowerData'})
associate: ()=>{
House.hasMany(DB.PowerDatum, {as: 'PowerData'});
}
}
});
@@ -45,5 +45,5 @@ House.graphql_type = new GraphQLObjectType({
}),
interfaces: [nodeInterface],
});
export House;
House.name = 'House';
module.exports = House;

View File

@@ -7,40 +7,38 @@ import {
import {
fromGlobalId,
globalIdField,
nodeDefinitions,
globalIdField
} from 'graphql-relay';
import Database from "./../config/database";
import PowerData from "./house"
import DB from "./../config/database";
import {nodeInterface} from './../lib/node.relay';
/**
* Define your own types here
*/
var PowerDatum = Database.sequelize.define('Datum', {
var PowerDatum = DB.sequelize.define('PowerDatum', {
id: {
type: Database.Sequelize.INTEGER,
type: DB.Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true // Automatically gets converted to SERIAL for postgres
},
time: Database.Sequelize.FLOAT,
power: Database.Sequelize.FLOAT
time: DB.Sequelize.FLOAT,
power: DB.Sequelize.FLOAT
}, {
tableName: "power_data",
instanceMethods: {
},
classMethods: {
associate: function(){
PowerDatum.belongsTo(House);
associate: ()=>{
PowerDatum.belongsTo(DB.House);
}
}
});
PowerDatum.graphql_type = new GraphQLObjectType({
name: 'Power Datum',
name: 'PowerDatum',
description: 'A person who uses our app',
fields: () => ({
id: globalIdField('PowerDatum'),
@@ -49,5 +47,6 @@ PowerDatum.graphql_type = new GraphQLObjectType({
}),
interfaces: [nodeInterface],
});
PowerDatum.name = 'PowerDatum';
export PowerDatum;
module.exports = PowerDatum;

View File

@@ -1,47 +0,0 @@
0 info it worked if it ends with ok
1 verbose cli [ '/home/eric/.nvm/v5.4.1/bin/node',
1 verbose cli '/home/eric/.nvm/v5.4.1/bin/npm',
1 verbose cli 'start' ]
2 info using npm@3.5.3
3 info using node@v5.4.1
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle spike_proto@0.0.0~prestart: spike_proto@0.0.0
6 silly lifecycle spike_proto@0.0.0~prestart: no script for prestart, continuing
7 info lifecycle spike_proto@0.0.0~start: spike_proto@0.0.0
8 verbose lifecycle spike_proto@0.0.0~start: unsafe-perm in lifecycle true
9 verbose lifecycle spike_proto@0.0.0~start: PATH: /home/eric/.nvm/v5.4.1/lib/node_modules/npm/bin/node-gyp-bin:/home/eric/Code/spike_proto/node_modules/.bin:/home/eric/.nvm/v5.4.1/bin:/home/eric/.rvm/gems/ruby-1.9.3-p484@oroeco_dev/bin:/home/eric/.rvm/gems/ruby-1.9.3-p484@global/bin:/home/eric/.rvm/rubies/ruby-1.9.3-p484/bin:/home/eric/.rvm/bin:/home/eric/bin:/usr/local/heroku/bin:/home/eric/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/lampp/bin
10 verbose lifecycle spike_proto@0.0.0~start: CWD: /home/eric/Code/spike_proto
11 silly lifecycle spike_proto@0.0.0~start: Args: [ '-c', 'babel-node ./app.js' ]
12 silly lifecycle spike_proto@0.0.0~start: Returned: code: 1 signal: null
13 info lifecycle spike_proto@0.0.0~start: Failed to exec start script
14 verbose stack Error: spike_proto@0.0.0 start: `babel-node ./app.js`
14 verbose stack Exit status 1
14 verbose stack at EventEmitter.<anonymous> (/home/eric/.nvm/v5.4.1/lib/node_modules/npm/lib/utils/lifecycle.js:232:16)
14 verbose stack at emitTwo (events.js:87:13)
14 verbose stack at EventEmitter.emit (events.js:172:7)
14 verbose stack at ChildProcess.<anonymous> (/home/eric/.nvm/v5.4.1/lib/node_modules/npm/lib/utils/spawn.js:24:14)
14 verbose stack at emitTwo (events.js:87:13)
14 verbose stack at ChildProcess.emit (events.js:172:7)
14 verbose stack at maybeClose (internal/child_process.js:821:16)
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
15 verbose pkgid spike_proto@0.0.0
16 verbose cwd /home/eric/Code/spike_proto
17 error Linux 3.19.0-43-generic
18 error argv "/home/eric/.nvm/v5.4.1/bin/node" "/home/eric/.nvm/v5.4.1/bin/npm" "start"
19 error node v5.4.1
20 error npm v3.5.3
21 error code ELIFECYCLE
22 error spike_proto@0.0.0 start: `babel-node ./app.js`
22 error Exit status 1
23 error Failed at the spike_proto@0.0.0 start script 'babel-node ./app.js'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the spike_proto package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error babel-node ./app.js
23 error You can get information on how to open an issue for this project with:
23 error npm bugs spike_proto
23 error Or if that isn't available, you can get their info via:
23 error npm owner ls spike_proto
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

View File

@@ -50,7 +50,11 @@
"css-loader": "^0.15.5",
"style-loader": "^0.12.3",
"connect-assets":"~4.7.0",
"node-sass": "3.4.2"
"node-sass": "3.4.2",
"moment": "2.11.1",
"yargs": "3.32.0",
"extend": "3.0.0"
},
"devDependencies": {
"gulp": "^3.9.0",