Files
old-spike/client/models/house.js

182 lines
5.0 KiB
JavaScript
Raw Normal View History

2016-02-10 16:11:56 -06:00
import extend from 'extend';
2016-02-18 12:29:16 -06:00
import Loki from 'loki';
2016-02-09 19:17:05 -06:00
2016-02-10 16:11:56 -06:00
import PowerDatum from './power_datum';
import EnergyDatum from './energy_datum';
import PowerDataApi from './../api/power_data';
import EnergyDataApi from './../api/energy_data';
import HousesApi from './../api/houses';
import ArrayUtil from './../../shared/utils/array'
import MathUtil from './../../shared/utils/math'
2016-02-09 19:17:05 -06:00
2016-02-10 16:11:56 -06:00
class House {
2016-02-19 10:49:07 -06:00
// must be initiated with a dataset already in Loki database (not directly JSON).
2016-02-10 16:11:56 -06:00
constructor(data){
var house = this;
2016-02-09 19:17:05 -06:00
house.data = data;
2016-02-19 10:49:07 -06:00
Object.assign(house, Databasable);
2016-02-10 16:11:56 -06:00
}
2016-02-19 10:49:07 -06:00
get scoped_id(){
2016-02-10 16:11:56 -06:00
return `house-${this.data.id}`;
2016-02-09 19:17:05 -06:00
}
2016-02-19 10:49:07 -06:00
save(){
var house = this;
return House.collection()
.then((house_collection)=>{
return house_collection.update(house.data);
})
}
2016-02-10 16:11:56 -06:00
ensurePowerData(opts){
opts = extend({
start_date: undefined,
end_date: undefined
}, opts || {});
2016-02-19 10:49:07 -06:00
var house = this,
query_ranges = DateUtil.dateOverlaps([opts.start_date, opts.end_date], house.data.query_dates]);
2016-02-09 19:17:05 -06:00
2016-02-19 10:49:07 -06:00
house.collection(PowerDatum.name, {})
.then((power_collection)=>{
})
2016-02-09 19:17:05 -06:00
}
getPowerData(params){
var house = this;
params.house_id = house.data.id;
2016-02-10 16:11:56 -06:00
return PowerDataApi.index(params).then((power_data)=>{
2016-02-09 19:17:05 -06:00
return power_data.map((power_datum_data)=>{
2016-02-18 12:29:16 -06:00
// save new power data into db
// update mins and maxes.
2016-02-10 16:11:56 -06:00
var power_datum = PowerDatum.updateOrInitialize(power_datum_data, house);
2016-02-18 12:29:16 -06:00
house.power_data_collection.set(power_datum.data.time, power_datum);
2016-02-10 16:11:56 -06:00
house.power_data.push(power_datum);
2016-02-09 19:17:05 -06:00
return power_datum;
});
});
}
2016-02-10 16:11:56 -06:00
ensureEnergyData(opts){
opts = extend({
start_date: undefined,
end_date: undefined
}, opts || {});
2016-02-09 19:17:05 -06:00
var house = this,
2016-02-10 16:11:56 -06:00
date_range = Array.from(house.energy_data_store.keys()),
2016-02-09 19:17:05 -06:00
min_date = Math.min(date_range),
max_date = Math.max(date_range),
query_ranges, cache;
2016-02-10 16:11:56 -06:00
if (date_range.length === 0) return house.getEnergyData({dates: [[opts.start_date, opts.end_date]]})
2016-02-09 19:17:05 -06:00
2016-02-10 18:57:15 -06:00
query_ranges = MathUtil.minusRange([opts.start_date, opts.end_date], [min_date, max_date]);
2016-02-13 16:49:32 -06:00
2016-02-10 19:07:37 -06:00
if (!query_ranges) return Promise.resolve(house.power_data);
2016-02-09 19:17:05 -06:00
cache = ArrayUtil.selectMap(date_range, (datum_day)=>{
return ArrayUtil.all(query_ranges, (query_range)=>{
2016-02-10 16:11:56 -06:00
return !MathUtil.inRange(datum_day, query_range);
});
2016-02-09 19:17:05 -06:00
}, (datum_day)=>{
2016-02-10 16:11:56 -06:00
return house.energy_data_store.get(datum_day);
2016-02-09 19:17:05 -06:00
});
if (query_ranges.length > 0){
2016-02-10 16:11:56 -06:00
return house.getEnergyData({dates: query_ranges}).then((new_energy_data)=>{
return new_energy_data_store.concat(cache);
2016-02-09 19:17:05 -06:00
});
} else return Promise.resolve(cache);
}
getEnergyData(params){
var house = this;
params.house_id = house.data.id;
2016-02-10 16:11:56 -06:00
return EnergyDataApi.index(params).then((energy_data)=>{
return energy_data.map((energy_datum_data)=>{
var energy_datum = EnergyDatum.updateOrInitialize(energy_datum_data, house);
2016-02-10 18:57:15 -06:00
house.energy_data_store.set(energy_datum.day, energy_datum);
2016-02-10 16:11:56 -06:00
house.energy_data.push(energy_datum);
2016-02-09 19:17:05 -06:00
return energy_datum;
});
});
}
update(data){
var house = this;
extend(house.data, data);
}
static updateOrInitialize(id, data){
var house = PowerDatum.store.get(id);
if (house) house.update(data);
return house || new House(data, data)
}
2016-02-18 12:29:16 -06:00
static accessDb(){
return new Promise((fnResolve, fnReject){
2016-02-19 10:49:07 -06:00
if (!House.db) {
House.db = new Loki('houses', adapter: '');
House.db.loadDatabase({}, ()=>{
fnResolve(House.db);
2016-02-18 12:29:16 -06:00
});
2016-02-19 10:49:07 -06:00
} else { fnResolve(House.db); }
2016-02-18 12:29:16 -06:00
});
}
static closeDb(){
2016-02-19 10:49:07 -06:00
if (House.db){
House.db.save();
House.db.close();
House.db = undefined;
2016-02-10 16:11:56 -06:00
}
2016-02-18 12:29:16 -06:00
}
2016-02-09 19:17:05 -06:00
2016-02-18 12:29:16 -06:00
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); })
});
2016-02-09 19:17:05 -06:00
}
static getHouses(ids){
2016-02-18 12:29:16 -06:00
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;
});
2016-02-09 19:17:05 -06:00
});
}
}
export default House;