mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Fix a bug that was making it impossible to display billing information in the android app.
This commit is contained in:
parent
8387216c44
commit
d30fc4b4ce
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -4,22 +4,31 @@ 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.concurrency.ConcurrencyUtils;
|
||||
import com.lanternsoftware.util.external.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.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class BackupMinutes {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
CurrentMonitorDao dao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.CONFIG_PATH + "mongo.cfg"));
|
||||
CurrentMonitorDao backupDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_DEST_PATH + "mongo.cfg"));
|
||||
Date now = new Date();
|
||||
for (Account a : dao.getProxy().queryAll(Account.class)) {
|
||||
CurrentMonitorDao sourceDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.CONFIG_PATH + "mongo.cfg"));
|
||||
CurrentMonitorDao destDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_DEST_PATH + "mongo.cfg"));
|
||||
ExecutorService ex = Executors.newFixedThreadPool(8);
|
||||
List<Future<?>> tasks = new ArrayList<>();
|
||||
for (Account a : sourceDao.getProxy().queryAll(Account.class)) {
|
||||
if (a.getId() == 0)
|
||||
continue;
|
||||
DebugTimer t = new DebugTimer("Account " + a.getId());
|
||||
|
@ -27,28 +36,35 @@ public class BackupMinutes {
|
|||
a.setTimezone("America/Chicago");
|
||||
}
|
||||
TimeZone tz = TimeZone.getTimeZone(a.getTimezone());
|
||||
HubPowerMinute minute = dao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sort("minute"));
|
||||
if (minute == null)
|
||||
HubPowerMinute firstMinute = sourceDao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sort("minute"));
|
||||
if (firstMinute == null)
|
||||
continue;
|
||||
HubPowerMinute lastBackup = backupDao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sortDesc("minute"));
|
||||
Date start = lastBackup == null ? DateUtils.getMidnightBefore(minute.getMinuteAsDate(), tz) : lastBackup.getMinuteAsDate();
|
||||
// Date start = DateUtils.date(10,16,2021,tz);
|
||||
HubPowerMinute lastMinute = sourceDao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sortDesc("minute"));
|
||||
HubPowerMinute lastBackup = destDao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sortDesc("minute"));
|
||||
Date start = lastBackup == null ? DateUtils.getMidnightBefore(firstMinute.getMinuteAsDate(), tz) : lastBackup.getMinuteAsDate();
|
||||
Date lastMin = lastMinute.getMinuteAsDate();
|
||||
Date end = DateUtils.addDays(start, 1, tz);
|
||||
while (start.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)));
|
||||
while (start.before(lastMin)) {
|
||||
final Date curStart = start;
|
||||
final Date curEnd = end;
|
||||
tasks.add(ex.submit(() -> {
|
||||
DebugTimer t2 = new DebugTimer("Account Id: " + a.getId() + " Query Day " + DateUtils.format("MM/dd/yyyy", tz, curStart));
|
||||
List<HubPowerMinute> minutes = sourceDao.getProxy().query(HubPowerMinute.class, new DaoQuery("account_id", a.getId()).andBetweenInclusiveExclusive("minute", (int) (curStart.getTime() / 60000), (int) (curEnd.getTime() / 60000)));
|
||||
t2.stop();
|
||||
if (!minutes.isEmpty()) {
|
||||
DebugTimer t3 = new DebugTimer("Save Day");
|
||||
backupDao.getProxy().save(minutes);
|
||||
destDao.getProxy().save(minutes);
|
||||
t3.stop();
|
||||
}
|
||||
}));
|
||||
start = end;
|
||||
end = DateUtils.addDays(end, 1, tz);
|
||||
}
|
||||
t.stop();
|
||||
}
|
||||
dao.shutdown();
|
||||
backupDao.shutdown();
|
||||
ConcurrencyUtils.getAll(tasks);
|
||||
ex.shutdown();
|
||||
sourceDao.shutdown();
|
||||
destDao.shutdown();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -203,7 +203,7 @@ public class BreakerConfig implements IIdentical<BreakerConfig> {
|
|||
}
|
||||
|
||||
public BillingCurrency getCurrency() {
|
||||
return CollectionUtils.getFirst(CollectionUtils.transformToSet(billingRates, BillingRate::getCurrency));
|
||||
return CollectionUtils.getFirst(CollectionUtils.transformToSet(CollectionUtils.aggregate(billingPlans, BillingPlan::getRates), BillingRate::getCurrency));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
<dependency>
|
||||
<groupId>com.google.api-client</groupId>
|
||||
<artifactId>google-api-client-bom</artifactId>
|
||||
<version>1.32.1</version>
|
||||
<version>1.33.2</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<property name="log.pattern" value="%date %-5level %logger{0} - %message%n"/>
|
||||
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>/opt/tomcat/logs/log.txt</file>
|
||||
<file>/opt/tomcat/log/log.txt</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<fileNamePattern>/opt/tomcat/log/log.%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
|
||||
<maxFileSize>20MB</maxFileSize>
|
||||
|
|
|
@ -64,6 +64,11 @@ public class MongoRulesDataAccess implements RulesDataAccess {
|
|||
proxy.save(_device);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFcmDevice(String _id) {
|
||||
proxy.delete(FcmDevice.class, new DaoQuery("_id", _id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FcmDevice> getFcmDevicesForAccount(int _accountId) {
|
||||
return proxy.query(FcmDevice.class, new DaoQuery("account_id", _accountId));
|
||||
|
|
|
@ -17,5 +17,6 @@ public interface RulesDataAccess {
|
|||
Event getMostRecentEvent(int _accountId, EventType _type, String _sourceId);
|
||||
List<Event> getEvents(int _accountId, EventType _type, String _sourceId, Date _from, Date _to);
|
||||
void putFcmDevice(FcmDevice _device);
|
||||
void removeFcmDevice(String _id);
|
||||
List<FcmDevice> getFcmDevicesForAccount(int _accountId);
|
||||
}
|
||||
|
|
|
@ -45,6 +45,9 @@ public abstract class AbstractAlertAction implements ActionImpl {
|
|||
try {
|
||||
messaging.send(msg);
|
||||
} catch (Exception _e) {
|
||||
if (_e.getMessage().contains("not found")) {
|
||||
RulesEngine.instance().dao().removeFcmDevice(device.getId());
|
||||
}
|
||||
logger.error("Failed to send message to account {}, device {}", _rule.getAccountId(), device.getName(), _e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -379,7 +379,10 @@ public class MongoProxy extends AbstractDaoProxy {
|
|||
|
||||
@Override
|
||||
public int count(String _tableName, DaoQuery _query) {
|
||||
return (int) db().getCollection(_tableName).count(prepareQuery(_query));
|
||||
if (CollectionUtils.isEmpty(_query))
|
||||
return (int) db().getCollection(_tableName).countDocuments();
|
||||
else
|
||||
return (int) db().getCollection(_tableName).countDocuments(prepareQuery(_query));
|
||||
}
|
||||
|
||||
public void ensureIndex(Class<?> _class, DaoSort _indexOrder) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user