diff --git a/client/app.js b/client/app.js index c6f0749..c938854 100644 --- a/client/app.js +++ b/client/app.js @@ -5,7 +5,6 @@ import React from 'react'; import ReactDOM from 'react-dom'; import Layout from './dashboard/layout/layout'; - ReactDOM.render( React.createElement(Layout), document.getElementById('root') diff --git a/client/config/db.js b/client/config/db.js new file mode 100644 index 0000000..c974c4b --- /dev/null +++ b/client/config/db.js @@ -0,0 +1,9 @@ +import Loki from 'lokijs'; + +var db = new Loki('spike'); + +db.addCollection('PowerData'); +db.addCollection('EnergyData'); +db.addCollection('Houses'); + +export default diff --git a/client/lib/model.js b/client/lib/model.js new file mode 100644 index 0000000..0a61b40 --- /dev/null +++ b/client/lib/model.js @@ -0,0 +1,9 @@ +import Loki from 'lokijs'; + + +class Model { + + + + +} diff --git a/client/models/house.js b/client/models/house.js index 8bc04c6..ef105a7 100644 --- a/client/models/house.js +++ b/client/models/house.js @@ -1,4 +1,5 @@ import extend from 'extend'; +import Loki from 'loki'; import PowerDatum from './power_datum'; import EnergyDatum from './energy_datum'; @@ -18,6 +19,8 @@ class House { house.power_data = []; house.energy_data_store = new Map(); house.power_data_store = new Map(); + + house.initDatabase(); } get react_key(){ @@ -29,32 +32,14 @@ class House { start_date: undefined, end_date: undefined }, opts || {}); - var house = this, - date_range = Array.from(house.power_data_store.keys()), - min_date = Math.min(date_range), - max_date = Math.max(date_range), - query_ranges, cache; + var house = this; - if (date_range.length === 0){ - return house.getPowerData({dates: [[opts.start_date, opts.end_date]]}); - } - - query_ranges = MathUtil.minusRange([opts.start_date, opts.end_date], [min_date, max_date]); - if (!query_ranges) return Promise.resolve(house.power_data); - - cache = ArrayUtil.selectMap(date_range, (datum_time)=>{ - return ArrayUtil.all(query_ranges, (query_range)=>{ - !MathUtil.inRange(datum_time, query_range); - }); - }, (datum_time)=>{ - return house.power_data_store.get(datum_time); + // check mins and maxes. + house.initCollection(PowerDatum.NAME).then(()=>{ + house.power_datum_collection. }); - if (query_ranges.length > 0){ - return house.getPowerData({dates: query_ranges}).then((new_power_data)=>{ - return new_power_data.concat(cache); - }); - } else return Promise.resolve(cache); + return Promise.resolve(cache); } getPowerData(params){ @@ -62,8 +47,10 @@ class House { params.house_id = house.data.id; return PowerDataApi.index(params).then((power_data)=>{ return power_data.map((power_datum_data)=>{ + // save new power data into db + // update mins and maxes. var power_datum = PowerDatum.updateOrInitialize(power_datum_data, house); - house.power_data_store.set(power_datum.data.time, power_datum); + house.power_data_collection.set(power_datum.data.time, power_datum); house.power_data.push(power_datum); return power_datum; }); @@ -126,26 +113,63 @@ class House { return house || new House(data, data) } - static ensureHouses(ids){ - var required_ids, cached_houses = []; - if (ids){ - required_ids = ArrayUtil.diff(ids, House.store.keys()); - cached_houses = ArrayUtil.diff(ids, required_ids).map((id)=>{ return House.store.get(id); }); - } - if (required_ids && required_ids.length == 0) return Promise.resolve([]); - - return House.getHouses(required_ids).then((new_houses)=>{ - // if these need to be ordered, then concatenation needs to be merged in order. - return new_houses.concat(cached_houses); + static accessDb(){ + return new Promise((fnResolve, fnReject){ + if (!this.db) { + this.db = new Loki('houses', adapter: ''); + this.db.loadDatabase({}, ()=>{ + fnResolve(this.db); + }); + } else { fnResolve(this.db); } }); } - static getHouses(ids){ - return HousesApi.index({id: ids}).then((houses_data)=>{ - return houses_data.map((house_data)=>{ - return new House(house_data); + static closeDb(){ + if (this.db){ + this.db.save(); + this.db.close(); + this.db = undefined; + } + } + + static collection(){ + return House.accessDb() + .then((db)=>{ + var house_collection = db.getCollection('houses') + if (!house_collection){ + house_collection = db.addCollection('houses', {indices: ['id']}); + } + return house_collection; + }); + } + + static ensureHouses(ids){ + House.collection() + .then((house_collection)=>{ + houses = house_collection.find({id: {$in: ids}}); + if (houses.length !== ids.length){ + required_ids = ArrayUtil.diff(ids, houses.map((house)=>{ return house.id; })); + return House.getHouses(required_ids) + .then((required_houses){ + return houses.concat(required_houses); + }); + } else { return houses; } + }).then((house_data)=>{ + return houses_data.map((house_data)=>{ return new House(house_data); }) + }); + } + + static getHouses(ids){ + return HousesApi.index({id: ids}) + .then((houses_data)=>{ + return House.collection() + .then((house_collection)=>{ + houses_data.forEach((house_data)=>{ + house_collection.insert(house_data); + }); + return houses_data; + }); }); - }); } } diff --git a/package.json b/package.json index fc3fb2d..1922ea9 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ "moment-timezone":"0.5.0", "yargs": "3.32.0", "extend": "3.0.0", - "through2": "2.0.1" + "through2": "2.0.1", + "lokijs": "1.3.11" }, "devDependencies": { "gulp": "^3.9.0",