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.datamodel.currentmonitor.HubPowerMinute;
|
||||||
import com.lanternsoftware.util.DateUtils;
|
import com.lanternsoftware.util.DateUtils;
|
||||||
import com.lanternsoftware.util.DebugTimer;
|
import com.lanternsoftware.util.DebugTimer;
|
||||||
|
import com.lanternsoftware.util.concurrency.ConcurrencyUtils;
|
||||||
import com.lanternsoftware.util.external.LanternFiles;
|
import com.lanternsoftware.util.external.LanternFiles;
|
||||||
import com.lanternsoftware.util.NullUtils;
|
import com.lanternsoftware.util.NullUtils;
|
||||||
import com.lanternsoftware.util.dao.DaoQuery;
|
import com.lanternsoftware.util.dao.DaoQuery;
|
||||||
import com.lanternsoftware.util.dao.DaoSort;
|
import com.lanternsoftware.util.dao.DaoSort;
|
||||||
import com.lanternsoftware.util.dao.mongo.MongoConfig;
|
import com.lanternsoftware.util.dao.mongo.MongoConfig;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.TimeZone;
|
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 class BackupMinutes {
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
CurrentMonitorDao dao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.CONFIG_PATH + "mongo.cfg"));
|
CurrentMonitorDao sourceDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.CONFIG_PATH + "mongo.cfg"));
|
||||||
CurrentMonitorDao backupDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_DEST_PATH + "mongo.cfg"));
|
CurrentMonitorDao destDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_DEST_PATH + "mongo.cfg"));
|
||||||
Date now = new Date();
|
ExecutorService ex = Executors.newFixedThreadPool(8);
|
||||||
for (Account a : dao.getProxy().queryAll(Account.class)) {
|
List<Future<?>> tasks = new ArrayList<>();
|
||||||
|
for (Account a : sourceDao.getProxy().queryAll(Account.class)) {
|
||||||
if (a.getId() == 0)
|
if (a.getId() == 0)
|
||||||
continue;
|
continue;
|
||||||
DebugTimer t = new DebugTimer("Account " + a.getId());
|
DebugTimer t = new DebugTimer("Account " + a.getId());
|
||||||
|
@ -27,28 +36,35 @@ public class BackupMinutes {
|
||||||
a.setTimezone("America/Chicago");
|
a.setTimezone("America/Chicago");
|
||||||
}
|
}
|
||||||
TimeZone tz = TimeZone.getTimeZone(a.getTimezone());
|
TimeZone tz = TimeZone.getTimeZone(a.getTimezone());
|
||||||
HubPowerMinute minute = dao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sort("minute"));
|
HubPowerMinute firstMinute = sourceDao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sort("minute"));
|
||||||
if (minute == null)
|
if (firstMinute == null)
|
||||||
continue;
|
continue;
|
||||||
HubPowerMinute lastBackup = backupDao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sortDesc("minute"));
|
HubPowerMinute lastMinute = sourceDao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sortDesc("minute"));
|
||||||
Date start = lastBackup == null ? DateUtils.getMidnightBefore(minute.getMinuteAsDate(), tz) : lastBackup.getMinuteAsDate();
|
HubPowerMinute lastBackup = destDao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sortDesc("minute"));
|
||||||
// Date start = DateUtils.date(10,16,2021,tz);
|
Date start = lastBackup == null ? DateUtils.getMidnightBefore(firstMinute.getMinuteAsDate(), tz) : lastBackup.getMinuteAsDate();
|
||||||
|
Date lastMin = lastMinute.getMinuteAsDate();
|
||||||
Date end = DateUtils.addDays(start, 1, tz);
|
Date end = DateUtils.addDays(start, 1, tz);
|
||||||
while (start.before(now)) {
|
while (start.before(lastMin)) {
|
||||||
DebugTimer t2 = new DebugTimer("Account Id: " + a.getId() + " Query Day " + DateUtils.format("MM/dd/yyyy", tz, start));
|
final Date curStart = 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)));
|
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();
|
t2.stop();
|
||||||
if (!minutes.isEmpty()) {
|
if (!minutes.isEmpty()) {
|
||||||
DebugTimer t3 = new DebugTimer("Save Day");
|
DebugTimer t3 = new DebugTimer("Save Day");
|
||||||
backupDao.getProxy().save(minutes);
|
destDao.getProxy().save(minutes);
|
||||||
t3.stop();
|
t3.stop();
|
||||||
}
|
}
|
||||||
|
}));
|
||||||
start = end;
|
start = end;
|
||||||
end = DateUtils.addDays(end, 1, tz);
|
end = DateUtils.addDays(end, 1, tz);
|
||||||
}
|
}
|
||||||
t.stop();
|
t.stop();
|
||||||
}
|
}
|
||||||
dao.shutdown();
|
ConcurrencyUtils.getAll(tasks);
|
||||||
backupDao.shutdown();
|
ex.shutdown();
|
||||||
|
sourceDao.shutdown();
|
||||||
|
destDao.shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -203,7 +203,7 @@ public class BreakerConfig implements IIdentical<BreakerConfig> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public BillingCurrency getCurrency() {
|
public BillingCurrency getCurrency() {
|
||||||
return CollectionUtils.getFirst(CollectionUtils.transformToSet(billingRates, BillingRate::getCurrency));
|
return CollectionUtils.getFirst(CollectionUtils.transformToSet(CollectionUtils.aggregate(billingPlans, BillingPlan::getRates), BillingRate::getCurrency));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.api-client</groupId>
|
<groupId>com.google.api-client</groupId>
|
||||||
<artifactId>google-api-client-bom</artifactId>
|
<artifactId>google-api-client-bom</artifactId>
|
||||||
<version>1.32.1</version>
|
<version>1.33.2</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<property name="log.pattern" value="%date %-5level %logger{0} - %message%n"/>
|
<property name="log.pattern" value="%date %-5level %logger{0} - %message%n"/>
|
||||||
|
|
||||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
<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">
|
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||||
<fileNamePattern>/opt/tomcat/log/log.%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
|
<fileNamePattern>/opt/tomcat/log/log.%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
|
||||||
<maxFileSize>20MB</maxFileSize>
|
<maxFileSize>20MB</maxFileSize>
|
||||||
|
|
|
@ -64,6 +64,11 @@ public class MongoRulesDataAccess implements RulesDataAccess {
|
||||||
proxy.save(_device);
|
proxy.save(_device);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeFcmDevice(String _id) {
|
||||||
|
proxy.delete(FcmDevice.class, new DaoQuery("_id", _id));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<FcmDevice> getFcmDevicesForAccount(int _accountId) {
|
public List<FcmDevice> getFcmDevicesForAccount(int _accountId) {
|
||||||
return proxy.query(FcmDevice.class, new DaoQuery("account_id", _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);
|
Event getMostRecentEvent(int _accountId, EventType _type, String _sourceId);
|
||||||
List<Event> getEvents(int _accountId, EventType _type, String _sourceId, Date _from, Date _to);
|
List<Event> getEvents(int _accountId, EventType _type, String _sourceId, Date _from, Date _to);
|
||||||
void putFcmDevice(FcmDevice _device);
|
void putFcmDevice(FcmDevice _device);
|
||||||
|
void removeFcmDevice(String _id);
|
||||||
List<FcmDevice> getFcmDevicesForAccount(int _accountId);
|
List<FcmDevice> getFcmDevicesForAccount(int _accountId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,9 @@ public abstract class AbstractAlertAction implements ActionImpl {
|
||||||
try {
|
try {
|
||||||
messaging.send(msg);
|
messaging.send(msg);
|
||||||
} catch (Exception _e) {
|
} 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);
|
logger.error("Failed to send message to account {}, device {}", _rule.getAccountId(), device.getName(), _e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -379,7 +379,10 @@ public class MongoProxy extends AbstractDaoProxy {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int count(String _tableName, DaoQuery _query) {
|
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) {
|
public void ensureIndex(Class<?> _class, DaoSort _indexOrder) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user