including CAIT data, models and seeds
This commit is contained in:
5
lib/base.model.js
Normal file
5
lib/base.model.js
Normal file
@@ -0,0 +1,5 @@
|
||||
export class Model {
|
||||
|
||||
static associate(){}
|
||||
|
||||
}
|
||||
1
lib/d3/bar.d3.js
Normal file
1
lib/d3/bar.d3.js
Normal file
@@ -0,0 +1 @@
|
||||
bar.d3.js
|
||||
1
lib/d3/base.d3.js
Normal file
1
lib/d3/base.d3.js
Normal file
@@ -0,0 +1 @@
|
||||
base.d3.js
|
||||
1
lib/d3/composite.d3.js
Normal file
1
lib/d3/composite.d3.js
Normal file
@@ -0,0 +1 @@
|
||||
composite.d3.js
|
||||
0
lib/d3/grid.d3.js
Normal file
0
lib/d3/grid.d3.js
Normal file
1
lib/d3/line.d3.js
Normal file
1
lib/d3/line.d3.js
Normal file
@@ -0,0 +1 @@
|
||||
line.d3.js
|
||||
1
lib/d3/pie.d3.js
Normal file
1
lib/d3/pie.d3.js
Normal file
@@ -0,0 +1 @@
|
||||
pie.d3.js
|
||||
1
lib/dashboard/base.component.js
Normal file
1
lib/dashboard/base.component.js
Normal file
@@ -0,0 +1 @@
|
||||
base.widget.js
|
||||
1
lib/dashboard/campaign_orders.component.js
Normal file
1
lib/dashboard/campaign_orders.component.js
Normal file
@@ -0,0 +1 @@
|
||||
campaign_orders.widget.js
|
||||
1
lib/dashboard/energy_time_series.component.js
Normal file
1
lib/dashboard/energy_time_series.component.js
Normal file
@@ -0,0 +1 @@
|
||||
energy_time_series.widget.js
|
||||
51
lib/dashboard/power_time_series.component.js
Normal file
51
lib/dashboard/power_time_series.component.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import React from 'react';
|
||||
import Relay from 'react-relay';
|
||||
|
||||
class PowerTimeSeries extends React.Component {
|
||||
|
||||
get timeframes(){
|
||||
return [
|
||||
{display: "Today", value: 'today'},
|
||||
{display: "1 week", value: 'week'},
|
||||
{display: "1 month", value: 'month'},
|
||||
{display: "6 months", value: 'half_year'},
|
||||
{display: "1 year", value: 'year'}
|
||||
];
|
||||
}
|
||||
|
||||
render() {
|
||||
var power_time_series = this;
|
||||
return (
|
||||
<div>
|
||||
<h1>Power Time Series</h1>
|
||||
<ul>
|
||||
{power_time_series.props.viewer.widgets.edges.map(edge =>
|
||||
<li key={edge.node.id}>{edge.node.name} (ID: {edge.node.id})</li>
|
||||
)}
|
||||
</ul>
|
||||
<div class="spk-power-time-series-timeframes">
|
||||
{power_time_series.timeframes.map(timeframe =>
|
||||
<div data-value="{timeframe.value}" class="spk-power-time-series-timeframe">{timeframe.display}</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default Relay.createContainer(PowerTimeSeries, {
|
||||
fragments: {
|
||||
viewer: () => Relay.QL`
|
||||
fragment on User {
|
||||
widgets(first: 10) {
|
||||
edges {
|
||||
node {
|
||||
id,
|
||||
name,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
`,
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
production_radiation_time_series.widget.js
|
||||
6
lib/dashboard/readme.md
Normal file
6
lib/dashboard/readme.md
Normal file
@@ -0,0 +1,6 @@
|
||||
## Dashboard Widgets
|
||||
|
||||
After initialized:
|
||||
1. They get/ check the data they need.
|
||||
2. Render default graph settings.
|
||||
3. Update graphs based on user input.
|
||||
27
lib/node.relay.js
Normal file
27
lib/node.relay.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* We get the node interface and field from the Relay library.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
},
|
||||
(obj) => {
|
||||
if (obj instanceof User) {
|
||||
return userType;
|
||||
} else if (obj instanceof Widget) {
|
||||
return widgetType;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
);
|
||||
104
lib/tasks/seed_data.js
Normal file
104
lib/tasks/seed_data.js
Normal file
@@ -0,0 +1,104 @@
|
||||
module.exports = function(done) {
|
||||
|
||||
var csv = require("fast-csv"),
|
||||
Database = require("./config/database"),
|
||||
Country = require("./models/country"),
|
||||
Datum = require("./models/datum"),
|
||||
countries = [],
|
||||
data = new Map();
|
||||
|
||||
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
|
||||
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user