mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Add billing rates and track cost for all energy readings.
This commit is contained in:
@@ -17,6 +17,11 @@
|
||||
<artifactId>lantern-datamodel-currentmonitor</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lanternsoftware.rules</groupId>
|
||||
<artifactId>lantern-datamodel-rules</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lanternsoftware.util</groupId>
|
||||
<artifactId>lantern-util-dao-mongo</artifactId>
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.lanternsoftware.dataaccess.currentmonitor;
|
||||
|
||||
import com.lanternsoftware.datamodel.currentmonitor.Account;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerConfig;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerGroupEnergy;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerGroupSummary;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.Sequence;
|
||||
import com.lanternsoftware.datamodel.rules.Event;
|
||||
import com.lanternsoftware.datamodel.rules.FcmDevice;
|
||||
import com.lanternsoftware.datamodel.rules.Rule;
|
||||
import com.lanternsoftware.util.DebugTimer;
|
||||
import com.lanternsoftware.util.LanternFiles;
|
||||
import com.lanternsoftware.util.dao.mongo.MongoConfig;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Backup {
|
||||
public static void main(String[] args) {
|
||||
CurrentMonitorDao dao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.OPS_PATH + "mongo.cfg"));
|
||||
CurrentMonitorDao backupDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_PATH + "mongo.cfg"));
|
||||
|
||||
DebugTimer t1 = new DebugTimer("Query Accounts");
|
||||
List<Account> accounts = dao.getProxy().queryAll(Account.class);
|
||||
t1.stop();
|
||||
DebugTimer t2 = new DebugTimer("Save Accounts");
|
||||
backupDao.getProxy().save(accounts);
|
||||
t2.stop();
|
||||
|
||||
DebugTimer t3 = new DebugTimer("Query Configs");
|
||||
List<BreakerConfig> configs = dao.getProxy().queryAll(BreakerConfig.class);
|
||||
t3.stop();
|
||||
DebugTimer t4 = new DebugTimer("Save Configs");
|
||||
backupDao.getProxy().save(configs);
|
||||
t4.stop();
|
||||
|
||||
DebugTimer t5 = new DebugTimer("Query Energy");
|
||||
List<BreakerGroupEnergy> energy = dao.getProxy().queryAll(BreakerGroupEnergy.class);
|
||||
t5.stop();
|
||||
DebugTimer t6 = new DebugTimer("Save Energy");
|
||||
backupDao.getProxy().save(energy);
|
||||
t6.stop();
|
||||
|
||||
DebugTimer t7 = new DebugTimer("Query Summaries");
|
||||
List<BreakerGroupSummary> summary = dao.getProxy().queryAll(BreakerGroupSummary.class);
|
||||
t7.stop();
|
||||
DebugTimer t8 = new DebugTimer("Save Summaries");
|
||||
backupDao.getProxy().save(summary);
|
||||
t8.stop();
|
||||
|
||||
DebugTimer t9 = new DebugTimer("Query Events");
|
||||
List<Event> events = dao.getProxy().queryAll(Event.class);
|
||||
t9.stop();
|
||||
DebugTimer t10 = new DebugTimer("Save Events");
|
||||
backupDao.getProxy().save(events);
|
||||
t10.stop();
|
||||
|
||||
DebugTimer t11 = new DebugTimer("Query Devices");
|
||||
List<FcmDevice> devices = dao.getProxy().queryAll(FcmDevice.class);
|
||||
t11.stop();
|
||||
DebugTimer t12 = new DebugTimer("Save Devices");
|
||||
backupDao.getProxy().save(devices);
|
||||
t12.stop();
|
||||
|
||||
DebugTimer t13 = new DebugTimer("Query Rules");
|
||||
List<Rule> rules = dao.getProxy().queryAll(Rule.class);
|
||||
t13.stop();
|
||||
DebugTimer t14 = new DebugTimer("Save Rules");
|
||||
backupDao.getProxy().save(rules);
|
||||
t14.stop();
|
||||
|
||||
DebugTimer t15 = new DebugTimer("Query Sequences");
|
||||
List<Sequence> sequences = dao.getProxy().queryAll(Sequence.class);
|
||||
t15.stop();
|
||||
DebugTimer t16 = new DebugTimer("Save Sequences");
|
||||
backupDao.getProxy().save(sequences);
|
||||
t16.stop();
|
||||
|
||||
dao.shutdown();
|
||||
backupDao.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.lanternsoftware.dataaccess.currentmonitor;
|
||||
|
||||
import com.lanternsoftware.datamodel.currentmonitor.Account;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.HubPowerMinute;
|
||||
import com.lanternsoftware.util.DateUtils;
|
||||
import com.lanternsoftware.util.DebugTimer;
|
||||
import com.lanternsoftware.util.LanternFiles;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.dao.DaoQuery;
|
||||
import com.lanternsoftware.util.dao.DaoSort;
|
||||
import com.lanternsoftware.util.dao.mongo.MongoConfig;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class BackupMinutes {
|
||||
public static void main(String[] args) {
|
||||
CurrentMonitorDao dao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.OPS_PATH + "mongo.cfg"));
|
||||
CurrentMonitorDao backupDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_PATH + "mongo.cfg"));
|
||||
Date now = new Date();
|
||||
for (Account a : dao.getProxy().queryAll(Account.class)) {
|
||||
if (a.getId() == 100)
|
||||
continue;
|
||||
DebugTimer t = new DebugTimer("Account " + a.getId());
|
||||
if (NullUtils.isEmpty(a.getTimezone())) {
|
||||
a.setTimezone("America/Chicago");
|
||||
}
|
||||
TimeZone tz = TimeZone.getTimeZone(a.getTimezone());
|
||||
// Date start = DateUtils.addDays(DateUtils.getMidnightBeforeNow(tz), -2, tz);
|
||||
HubPowerMinute minute = dao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sort("minute"));
|
||||
if (minute == null)
|
||||
continue;
|
||||
Date start = DateUtils.getMidnightBefore(minute.getMinuteAsDate(), tz);
|
||||
Date end = DateUtils.addDays(start, 1, tz);
|
||||
while (end.before(now)) {
|
||||
DebugTimer t2 = new DebugTimer("Account Id: " + a.getId() + " Query Day " + DateUtils.format("MM/dd/yyyy", tz, start));
|
||||
List<HubPowerMinute> minutes = dao.getProxy().query(HubPowerMinute.class, new DaoQuery("account_id", a.getId()).andBetweenInclusiveExclusive("minute", (int) (start.getTime() / 60000), (int) (end.getTime() / 60000)));
|
||||
t2.stop();
|
||||
if (!minutes.isEmpty()) {
|
||||
DebugTimer t3 = new DebugTimer("Save Day");
|
||||
backupDao.getProxy().save(minutes);
|
||||
t3.stop();
|
||||
}
|
||||
start = end;
|
||||
end = DateUtils.addDays(end, 1, tz);
|
||||
}
|
||||
t.stop();
|
||||
}
|
||||
dao.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,8 @@ public interface CurrentMonitorDao {
|
||||
void putConfig(BreakerConfig _config);
|
||||
|
||||
void updateSummaries(BreakerGroup _rootGroup, Set<Date> _daysToSummarize, TimeZone _tz);
|
||||
void rebuildSummaries(int _accountId);
|
||||
void rebuildSummaries(int _accountId, Date _start, Date _end);
|
||||
|
||||
String addPasswordResetKey(String _email);
|
||||
String getEmailForResetKey(String _key);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.lanternsoftware.dataaccess.currentmonitor;
|
||||
|
||||
import com.lanternsoftware.datamodel.currentmonitor.Account;
|
||||
import com.lanternsoftware.util.dao.auth.AuthCode;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.Breaker;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerConfig;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerGroup;
|
||||
@@ -20,6 +19,7 @@ import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoQuery;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.dao.DaoSort;
|
||||
import com.lanternsoftware.util.dao.auth.AuthCode;
|
||||
import com.lanternsoftware.util.dao.mongo.MongoConfig;
|
||||
import com.lanternsoftware.util.dao.mongo.MongoProxy;
|
||||
import org.mindrot.jbcrypt.BCrypt;
|
||||
@@ -96,12 +96,14 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
|
||||
BreakerConfig config = getConfig(_minute.getAccountId());
|
||||
BreakerGroup group = CollectionUtils.getFirst(config.getBreakerGroups());
|
||||
Date day = DateUtils.getMidnightBefore(_minute.getMinuteAsDate(), tz);
|
||||
BreakerGroupEnergy summary = getBreakerGroupEnergy(_minute.getAccountId(), group.getId(), EnergyBlockViewMode.DAY, day);
|
||||
if (summary == null)
|
||||
summary = new BreakerGroupEnergy(group, minutes, EnergyBlockViewMode.DAY, day, tz);
|
||||
BreakerGroupEnergy energy = getBreakerGroupEnergy(_minute.getAccountId(), group.getId(), EnergyBlockViewMode.DAY, day);
|
||||
Date monthStart = DateUtils.getStartOfMonth(day, tz);
|
||||
BreakerGroupSummary month = proxy.queryOne(BreakerGroupSummary.class, new DaoQuery("_id", BreakerGroupEnergy.toId(_minute.getAccountId(), group.getId(), EnergyBlockViewMode.MONTH, monthStart)));
|
||||
if (energy == null)
|
||||
energy = new BreakerGroupEnergy(group, minutes, EnergyBlockViewMode.DAY, day, month, config.getBillingRates(), tz);
|
||||
else
|
||||
summary.addEnergy(group, minutes);
|
||||
putBreakerGroupEnergy(summary);
|
||||
energy.addEnergy(group, minutes, month, config.getBillingRates());
|
||||
putBreakerGroupEnergy(energy);
|
||||
updateSummaries(group, CollectionUtils.asHashSet(day), tz);
|
||||
timer.stop();
|
||||
}
|
||||
@@ -144,7 +146,7 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
|
||||
List<String> groupEnergyIds = new ArrayList<>();
|
||||
while (calMonthStart.before(end)) {
|
||||
groupEnergyIds.add(BreakerGroupEnergy.toId(_rootGroup.getAccountId(), _rootGroup.getId(), EnergyBlockViewMode.MONTH, calMonthStart.getTime()));
|
||||
calMonthStart.add(Calendar.DAY_OF_YEAR, 1);
|
||||
calMonthStart.add(Calendar.MONTH, 1);
|
||||
}
|
||||
List<BreakerGroupSummary> groupEnergies = CollectionUtils.aggregate(proxy.query(BreakerGroupSummary.class, DaoQuery.in("_id", groupEnergyIds)), BreakerGroupSummary::getAllGroups);
|
||||
Map<String, List<BreakerGroupSummary>> energies = CollectionUtils.transformToMultiMap(groupEnergies, BreakerGroupSummary::getGroupId);
|
||||
@@ -157,6 +159,42 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
|
||||
putBreakerGroupEnergy(summary);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rebuildSummaries(int _accountId) {
|
||||
HubPowerMinute firstMinute = proxy.queryOne(HubPowerMinute.class, new DaoQuery("account_id", _accountId), DaoSort.sort("minute"));
|
||||
if (firstMinute == null)
|
||||
return;
|
||||
rebuildSummaries(_accountId, firstMinute.getMinuteAsDate(), new Date());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rebuildSummaries(int _accountId, Date _start, Date _end) {
|
||||
BreakerConfig config = getConfig(_accountId);
|
||||
TimeZone tz = getTimeZoneForAccount(_accountId);
|
||||
Date start = DateUtils.getMidnightBefore(_start, tz);
|
||||
Date monthStart = DateUtils.getStartOfMonth(_start, tz);
|
||||
BreakerGroup root = CollectionUtils.getFirst(config.getBreakerGroups());
|
||||
proxy.delete(BreakerGroupSummary.class, new DaoQuery("_id", BreakerGroupEnergy.toId(_accountId, root.getId(), EnergyBlockViewMode.MONTH, monthStart)));
|
||||
while (start.before(_end)) {
|
||||
Date dayEnd = DateUtils.getMidnightAfter(start, tz);
|
||||
DebugTimer timer = new DebugTimer("Time to rebuild one day");
|
||||
DebugTimer t1 = new DebugTimer("Loading hub power for day, account: " + _accountId + " day: " + DateUtils.format("MM/dd/yyyy", tz, start));
|
||||
List<HubPowerMinute> minutes = proxy.query(HubPowerMinute.class, new DaoQuery("account_id", _accountId).andBetweenInclusiveExclusive("minute", (int) (start.getTime() / 60000), (int) (dayEnd.getTime() / 60000)));
|
||||
t1.stop();
|
||||
monthStart = DateUtils.getStartOfMonth(start, tz);
|
||||
BreakerGroupSummary month = null;
|
||||
if (monthStart.equals(start))
|
||||
proxy.delete(BreakerGroupSummary.class, new DaoQuery("_id", BreakerGroupEnergy.toId(_accountId, root.getId(), EnergyBlockViewMode.MONTH, monthStart)));
|
||||
else
|
||||
month = proxy.queryOne(BreakerGroupSummary.class, new DaoQuery("_id", BreakerGroupEnergy.toId(_accountId, root.getId(), EnergyBlockViewMode.MONTH, monthStart)));
|
||||
BreakerGroupEnergy energy = new BreakerGroupEnergy(root, minutes, EnergyBlockViewMode.DAY, start, month, config.getBillingRates(), tz);
|
||||
timer.stop();
|
||||
putBreakerGroupEnergy(energy);
|
||||
updateSummaries(root, CollectionUtils.asHashSet(start), tz);
|
||||
start = DateUtils.addDays(start, 1, tz);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putBreakerGroupEnergy(BreakerGroupEnergy _energy) {
|
||||
proxy.save(_energy);
|
||||
@@ -179,6 +217,7 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
|
||||
config.setBreakerGroups(CollectionUtils.aggregate(configs, BreakerConfig::getBreakerGroups));
|
||||
config.setPanels(CollectionUtils.aggregate(configs, BreakerConfig::getPanels));
|
||||
config.setMeters(CollectionUtils.aggregate(configs, BreakerConfig::getMeters));
|
||||
config.setBillingRates(CollectionUtils.aggregate(configs, BreakerConfig::getBillingRates));
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user