polish energy/power data view interactions

This commit is contained in:
Eric Hulburd
2016-02-22 13:45:43 -06:00
parent c1fddd944e
commit df3152443a
16 changed files with 338 additions and 156 deletions

View File

@@ -10,7 +10,6 @@ class EnergyDatum {
constructor(data, house){
var energy_datum = this;
energy_datum.house = house;
data.day = moment.tz(data.day, house.data.timezone);
energy_datum.data = data;
}
@@ -18,16 +17,16 @@ class EnergyDatum {
return `energy-datum-${this.data.id}`;
}
// returns a datestamp that has the client timezone, but actually is house local time.
get day_to_date(){
var energy_datum = this,
moment_tz = moment.tz(energy_datum.data.day, energy_datum.house.data.timezone);
// will have to do some additional math here to account for local offset.
return moment(moment_tz.toArray()).toDate();
house = energy_datum.house;
return house.toDate(energy_datum.data.day);
}
get day_to_s(){
var energy_datum = this;
return energy_datum.data.day.format('YYYY-MM-DD');
return moment.tz(energy_datum.data.day * 1000, energy_datum.house.data.timezone).format('YYYY-MM-DD');
}
get consumption_to_s(){

View File

@@ -12,6 +12,8 @@ import MathUtil from './../../shared/utils/math';
import DateRange from './../../shared/utils/date_range';
import Databasable from './../lib/databasable';
const NAME = 'House';
class House {
// must be initiated with a dataset already in Loki database (not directly JSON).
@@ -19,69 +21,123 @@ class House {
var house = this;
house.data = data;
Object.assign(house, Databasable);
house.power_date_range = [house.default_power_start, house.default_power_end];
var n_years = house.data_until_moment.year() - house.data_from_moment.year() + 1;
house.years = [];
for (var year=house.data_from_moment.year(); year<=house.data_until_moment.year(); year+=1){
house.years.push(year);
}
house.current_month = house.data_until_moment.format('MMM');
house.current_year = house.data_until_moment.year();
house.setCurrentMonthMoment();
}
get data_from_moment(){
var house = this;
return moment.tz(house.data.data_from * 1000, house.data.timezone);
}
get data_until_moment(){
var house = this;
return moment.tz(house.data.data_until * 1000, house.data.timezone);
}
get end_of_current_data_moment(){
var house = this,
end_of_month = house.current_month_moment.clone().endOf('month');
return end_of_month > house.data_until_moment ? house.data_until_moment : end_of_month;
}
get scoped_id(){
return `house-${this.data.id}`;
}
get default_power_start(){
var house = this;
// 3600 * 24 seconds * 4 = 4 days.
return house.data.data_until - 3600 * 24 * 4;
availableMonths(){
var house = this,
all_months = moment.monthsShort(),
year = house.current_year.toString();
if ((year) === house.data_from_moment.format('YYYY')){
return all_months.slice(house.data_from_moment.month(), 12);
} else if (year === house.data_until_moment.format('YYYY')){
return all_months.slice(0, house.data_until_moment.month() + 1);
} else {
return all_months;
}
}
get default_power_end(){
setYear(year){
var house = this;
return house.data.data_until;
house.current_year = year;
return house.setCurrentMonthMoment();
}
setMonth(month){
var house = this;
house.current_month = month;
return house.setCurrentMonthMoment();
}
setCurrentMonthMoment(){
var house = this,
month_i = moment.monthsShort().indexOf(house.current_month),
new_month_moment = moment.tz({year: house.current_year, month: month_i, day: 1}, house.data.timezone).startOf('month');
if (!house.current_month_moment || new_month_moment.unix() !== house.current_month_moment.unix()){
house.current_month_moment = new_month_moment;
house.power_date_range = [house.end_of_current_data_moment.clone().subtract(4, 'days').unix(), house.end_of_current_data_moment.unix()];
house.energy_date_range = [house.end_of_current_data_moment.clone().startOf('year').unix(), house.end_of_current_data_moment.clone().endOf('year').unix()]
return true;
}
return false;
}
offset_diff(unix){
var house = this,
tz = moment.tz.zone(house.data.timezone);
return (new Date().getTimezoneOffset() - tz.offset(unix * 1000)) * 60;
}
toDate(unix){
var house = this;
return moment.tz(unix * 1000, house.data.timezone).toDate();
return new Date((unix + house.offset_diff(unix)) * 1000);
}
formatDate(unix, format){
var house = this;
return moment.tz(unix * 1000, house.data.timezone).format(format)
}
save(){
var house = this;
return House.collection(House.NAME)
return House.collection(House.NAME, House.NAME)
.then((house_collection)=>{
house_collection.update(house.data);
return House.db.save();
});
}
setPowerData(opts){
setPowerData(){
var house = this;
opts = Object.assign({
dates: house.power_date_range
}, opts || {});
return house.collection(PowerDatum.NAME, PowerDatum.COLLECTION_OPTIONS)
return house.collection(house.scoped_id, PowerDatum.NAME, PowerDatum.COLLECTION_OPTIONS)
.then((power_collection)=>{
return house.ensurePowerData(opts)
return house.ensurePowerData()
.then(()=>{
var params = house.rangeToLokiParams('time', opts.dates);
var params = house.rangeToLokiParams('time', house.power_date_range);
house.power_data = power_collection.find(params)
.sort((pd1, pd2)=>{
if (pd1.time === pd2.time) return 0;
if (pd1.time > pd2.time) return 1;
if (pd1.time < pd2.time) return -1;
})
.map((data)=>{ return new PowerDatum(data, house); })
.map((data)=>{ return new PowerDatum(data, house); });
});
});
}
ensurePowerData(opts){
opts = extend({
start_date: undefined,
end_date: undefined
}, opts || {});
ensurePowerData(){
var house = this,
existing_ranges = house.data.power_datum_ranges || [],
query_ranges;
query_ranges = DateRange.addRange(opts.dates, existing_ranges);
query_ranges = DateRange.addRange(house.power_date_range, house.data.power_datum_ranges || []);
if (query_ranges.gaps_filled.length > 0){
var params = {dates: query_ranges.gaps_filled};
return house.getPowerData(params)
@@ -95,60 +151,42 @@ class House {
getPowerData(params){
var house = this;
params.house_id = house.data.id;
return PowerDataApi.index(params)
.then((power_data)=>{
return house.collection(PowerDatum.NAME, PowerDatum.COLLECTION_OPTIONS)
.then((power_collection)=>{
power_collection.insert(power_data);
house.db.save();
});
})
}
clearData(){
var house = this;
return new Promise((fnResolve, fnReject)=>{
house.collection(PowerDatum.NAME)
.then((power_collection)=>{
power_collection.removeWhere({});
house.db.save(()=>{
House.collection(House.NAME)
.then((house_collection)=>{
house_collection.remove(house.data);
House.db.save(()=>{
fnResolve();
})
});
});
});
});
}
setEnergyData(opts){
var house = this;
return house.ensureEnergyData(opts)
.then(()=>{
return house.collection(EnergyDatum.NAME, EnergyDatum.COLLECTION_OPTIONS)
.then((energy_collection)=>{
var params = house.rangeToLokiParams('day', [opts.start_date, opts.end_date]);
house.energy_data = energy_collection.find(params).map((data)=>{ return new EnergyDatum(data, house); })
return house.collection(house.scoped_id, PowerDatum.NAME, PowerDatum.COLLECTION_OPTIONS)
.then((power_collection)=>{
return PowerDataApi.index(params)
.then((power_data)=>{
power_collection.insert(power_data);
house.db.save();
});
})
}
ensureEnergyData(opts){
opts = extend({
start_date: undefined,
end_date: undefined
}, opts || {});
setEnergyData(){
var house = this;
return house.collection(house.scoped_id, EnergyDatum.NAME, EnergyDatum.COLLECTION_OPTIONS)
.then((energy_collection)=>{
return house.ensureEnergyData()
.then(()=>{
var params = house.rangeToLokiParams('day', house.energy_date_range);
house.energy_data = energy_collection.find(params)
.sort((pd1, pd2)=>{
if (pd1.day === pd2.day) return 0;
if (pd1.day > pd2.day) return 1;
if (pd1.day < pd2.day) return -1;
})
.map((data)=>{ return new EnergyDatum(data, house); });
});
});
}
ensureEnergyData(){
var house = this,
query_ranges = DateRange.addRange([opts.start_date, opts.end_date], house.data.energy_datum_ranges);
query_ranges = DateRange.addRange(house.energy_date_range, house.data.energy_datum_ranges || []);
if (query_ranges.gaps_filled.length > 0){
house.getEnergyData({dates: query_ranges.gaps_filled})
return house.getEnergyData({dates: query_ranges.gaps_filled})
.then(()=>{
house.data.energy_datum_ranges = query_ranges.new_ranges;
return house.save();
house.save();
});
} else { return Promise.resolve(); }
}
@@ -156,18 +194,47 @@ class House {
getEnergyData(params){
var house = this;
params.house_id = house.data.id;
return EnergyDataApi.index(params)
.then((energy_data)=>{
return house.collection(EnergyDatum.NAME, EnergyDatum.COLLECTION_OPTIONS)
.then((energy_collection)=>{
return house.collection(house.scoped_id, EnergyDatum.NAME, EnergyDatum.COLLECTION_OPTIONS)
.then((energy_collection)=>{
return EnergyDataApi.index(params)
.then((energy_data)=>{
energy_collection.insert(energy_data);
return house.db.save();
house.db.save();
});
})
}
// removes all energy and power data from LokiJs (memory and persisted) database.
clearData(){
var house = this,
all = [
new Promise((fnResolve, fnReject)=>{
house.collection(house.scoped_id, PowerDatum.NAME)
.then((power_collection)=>{
power_collection.removeWhere({});
house.db.save(fnResolve);
});
}),
new Promise((fnResolve, fnReject)=>{
house.collection(house.scoped_id, EnergyDatum.NAME)
.then((energy_collection)=>{
energy_collection.removeWhere({});
house.db.save(fnResolve);
});
}),
new Promise((fnResolve, fnReject)=>{
House.collection(House.NAME, House.NAME)
.then((house_collection)=>{
house_collection.remove(house.data);
House.db.save(fnResolve);
});
})
]
return Promise.all(all)
}
static ensureHouses(ids){
return House.collection(House.NAME)
return House.collection(House.NAME, House.NAME)
.then((house_collection)=>{
var houses_data = ids ? house_collection.find({id: {$in: ids}}) : house_collection.find();
if (!ids && houses_data.length === 0 || ids && houses_data.length !== ids.length){
@@ -188,5 +255,7 @@ class House {
}
House.NAME = NAME;
Object.assign(House, Databasable);
export default House;

View File

@@ -20,14 +20,14 @@ class PowerDatum {
get time_to_date(){
var power_datum = this,
moment_tz = moment.tz(power_datum.data.time, power_datum.house.data.timezone);
// will have to do some additional math here to account for local offset.
return moment(moment_tz.toArray()).toDate();
house = power_datum.house;
return house.toDate(power_datum.data.time);
}
get time_to_s(){
var power_datum = this;
return power_datum.data.time.format('YYYY-MM-DD HH:mm');
var power_datum = this,
moment_tz = moment.tz(power_datum.data.time * 1000, power_datum.house.data.timezone);
return moment_tz.format('YYYY-MM-DD HH:mm');
}
get consumption_to_s(){
var power_datum = this;