From 602347dba1283fa0e0c416bee59e1a3b245bd8f4 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 23 Nov 2021 14:46:22 +0100 Subject: [PATCH] Housekeeping on energy logs (removal of removed things) --- plugin/energylogger.cpp | 39 +++++++++++++++++++++++++++--------- plugin/energylogger.h | 3 +++ plugin/energymanagerimpl.cpp | 10 +++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/plugin/energylogger.cpp b/plugin/energylogger.cpp index a223b8a..f77c84f 100644 --- a/plugin/energylogger.cpp +++ b/plugin/energylogger.cpp @@ -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 EnergyLogger::loggedThings() const +{ + QList 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(); diff --git a/plugin/energylogger.h b/plugin/energylogger.h index fed393f..dcf7a3c 100644 --- a/plugin/energylogger.h +++ b/plugin/energylogger.h @@ -26,6 +26,9 @@ public: PowerBalanceLogEntry latestLogEntry(SampleRate sampleRate); + void removeThingLogs(const ThingId &thingId); + QList loggedThings() const; + private slots: void sample(); diff --git a/plugin/energymanagerimpl.cpp b/plugin/energymanagerimpl.cpp index 3444ea7..d361f20 100644 --- a/plugin/energymanagerimpl.cpp +++ b/plugin/energymanagerimpl.cpp @@ -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()