Housekeeping on energy logs (removal of removed things)
parent
a34dd7642b
commit
602347dba1
|
|
@ -42,15 +42,8 @@ EnergyLogger::EnergyLogger(QObject *parent) : EnergyLogs(parent)
|
|||
addConfig(SampleRate1Year, SampleRate1Month, 20); // 20 years
|
||||
|
||||
// Load thingIds from logs so we have the complete list available for sampling, even if a thing might not produce any logs for a while.
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare("SELECT DISTINCT thingId FROM thingPower;");
|
||||
query.exec();
|
||||
if (query.lastError().isValid()) {
|
||||
qCWarning(dcEnergyExperience()) << "Failed to load existing things from logs:" << query.lastError();
|
||||
} else {
|
||||
while (query.next()) {
|
||||
m_thingsPowerLiveLogs[query.value("thingId").toUuid()] = ThingPowerLogEntries();
|
||||
}
|
||||
foreach (const ThingId &thingId, loggedThings()) {
|
||||
m_thingsPowerLiveLogs[thingId] = ThingPowerLogEntries();
|
||||
}
|
||||
|
||||
// Start the scheduling
|
||||
|
|
@ -207,6 +200,34 @@ PowerBalanceLogEntry EnergyLogger::latestLogEntry(SampleRate sampleRate)
|
|||
return queryResultToBalanceLogEntry(query.record());
|
||||
}
|
||||
|
||||
void EnergyLogger::removeThingLogs(const ThingId &thingId)
|
||||
{
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare("DELETE FROM thingPower WHERE thingId = ?;");
|
||||
query.addBindValue(thingId);
|
||||
query.exec();
|
||||
if (query.lastError().isValid()) {
|
||||
qCWarning(dcEnergyExperience()) << "Error removing thing energy logs for thing id" << thingId << query.lastError() << query.executedQuery();
|
||||
}
|
||||
}
|
||||
|
||||
QList<ThingId> EnergyLogger::loggedThings() const
|
||||
{
|
||||
QList<ThingId> ret;
|
||||
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare("SELECT DISTINCT thingId FROM thingPower;");
|
||||
query.exec();
|
||||
if (query.lastError().isValid()) {
|
||||
qCWarning(dcEnergyExperience()) << "Failed to load existing things from logs:" << query.lastError();
|
||||
} else {
|
||||
while (query.next()) {
|
||||
ret.append(query.value("thingId").toUuid());
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void EnergyLogger::sample()
|
||||
{
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
|
|
|
|||
|
|
@ -26,6 +26,9 @@ public:
|
|||
|
||||
PowerBalanceLogEntry latestLogEntry(SampleRate sampleRate);
|
||||
|
||||
void removeThingLogs(const ThingId &thingId);
|
||||
QList<ThingId> loggedThings() const;
|
||||
|
||||
private slots:
|
||||
void sample();
|
||||
|
||||
|
|
|
|||
|
|
@ -36,6 +36,14 @@ EnergyManagerImpl::EnergyManagerImpl(ThingManager *thingManager, QObject *parent
|
|||
}
|
||||
connect(thingManager, &ThingManager::thingAdded, this, &EnergyManagerImpl::watchThing);
|
||||
connect(thingManager, &ThingManager::thingRemoved, this, &EnergyManagerImpl::unwatchThing);
|
||||
|
||||
// Housekeeping on the logger
|
||||
foreach (const ThingId &thingId, m_logger->loggedThings()) {
|
||||
if (!m_thingManager->findConfiguredThing(thingId)) {
|
||||
qCDebug(dcEnergyExperience()) << "Clearing thing logs for unknown thing id" << thingId << "from energy logs.";
|
||||
m_logger->removeThingLogs(thingId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Thing *EnergyManagerImpl::rootMeter() const
|
||||
|
|
@ -151,6 +159,8 @@ void EnergyManagerImpl::unwatchThing(const ThingId &thingId)
|
|||
m_rootMeter = nullptr;
|
||||
emit rootMeterChanged();
|
||||
}
|
||||
|
||||
m_logger->removeThingLogs(thingId);
|
||||
}
|
||||
|
||||
void EnergyManagerImpl::updatePowerBalance()
|
||||
|
|
|
|||
Loading…
Reference in New Issue