Ref T292, Ref T285 background updater improvements

* less cycles
* message history
* more detailed status messages
This commit is contained in:
Klaus Basan
2018-07-26 21:43:51 +02:00
parent 5a51d06e33
commit 1fd7ad6891
3 changed files with 65 additions and 28 deletions

View File

@@ -41,10 +41,16 @@ namespace BlackCore
m_updateTimer.setInterval(60 * 1000); m_updateTimer.setInterval(60 * 1000);
if (sApp && sApp->hasWebDataServices()) if (sApp && sApp->hasWebDataServices())
{ {
connect(sApp->getWebDataServices()->getDatabaseWriter(), &CDatabaseWriter::publishedModelsSimplified, this, &CBackgroundDataUpdater::onModelsPublished); connect(sApp->getWebDataServices()->getDatabaseWriter(), &CDatabaseWriter::publishedModelsSimplified, this, &CBackgroundDataUpdater::onModelsPublished, Qt::QueuedConnection);
} }
} }
CStatusMessageList CBackgroundDataUpdater::getMessageHistory() const
{
QReadLocker l(&m_lockMsg);
return m_messageHistory;
}
void CBackgroundDataUpdater::doWork() void CBackgroundDataUpdater::doWork()
{ {
if (!this->doWorkCheck()) { return; } if (!this->doWorkCheck()) { return; }
@@ -57,26 +63,30 @@ namespace BlackCore
case 0: case 0:
// normally redundant, will be read in other places as well // normally redundant, will be read in other places as well
// new metadata for next comparison // new metadata for next comparison
this->addHistory(CLogMessage(this).info("Triggered info reads from DB"));
this->triggerInfoReads(); this->triggerInfoReads();
break; break;
case 1: case 1:
this->addHistory(CLogMessage(this).info("Synchronize DB entities"));
this->syncDbEntity(CEntityFlags::AirlineIcaoEntity);
this->syncDbEntity(CEntityFlags::LiveryEntity);
this->syncDbEntity(CEntityFlags::ModelEntity); this->syncDbEntity(CEntityFlags::ModelEntity);
this->syncDbEntity(CEntityFlags::DistributorEntity); this->syncDbEntity(CEntityFlags::DistributorEntity);
this->syncDbEntity(CEntityFlags::AircraftIcaoEntity);
break; break;
case 2: case 2:
this->addHistory(CLogMessage(this).info("Synchronize %1") << m_modelCaches.getDescription());
this->syncModelOrModelSetCacheWithDbData(m_modelCaches); this->syncModelOrModelSetCacheWithDbData(m_modelCaches);
break; break;
case 3: case 3:
this->addHistory(CLogMessage(this).info("Synchronize %1") << m_modelSetCaches.getDescription());
this->syncModelOrModelSetCacheWithDbData(m_modelSetCaches); this->syncModelOrModelSetCacheWithDbData(m_modelSetCaches);
break; break;
case 4:
this->syncDbEntity(CEntityFlags::AircraftIcaoEntity);
this->syncDbEntity(CEntityFlags::AirlineIcaoEntity);
break;
default: default:
break; break;
} }
++m_cycle %= 5; ++m_cycle %= 4;
m_inWork = false; m_inWork = false;
emit this->consolidating(false); emit this->consolidating(false);
} }
@@ -96,43 +106,51 @@ namespace BlackCore
dbModelsConsidered.latestTimestamp(); dbModelsConsidered.latestTimestamp();
if (!latestDbModelsTs.isValid()) { return; } if (!latestDbModelsTs.isValid()) { return; }
// newer DB models as cache
const QDateTime dbModelsLatestSync = m_syncedModelsLatestChange.value(cache.getDescription()); const QDateTime dbModelsLatestSync = m_syncedModelsLatestChange.value(cache.getDescription());
if (dbModelsLatestSync.isValid() && latestDbModelsTs <= dbModelsLatestSync) { return; } if (dbModelsLatestSync.isValid() && latestDbModelsTs <= dbModelsLatestSync) { return; }
m_syncedModelsLatestChange[cache.getDescription()] = latestDbModelsTs; m_syncedModelsLatestChange[cache.getDescription()] = latestDbModelsTs;
const CSimulatorInfo sims = cache.simulatorsWithInitializedCache(); // sims ever used const CSimulatorInfo simulators = cache.simulatorsWithInitializedCache(); // simulators ever used
if (sims.isNoSimulator()) { return; } if (simulators.isNoSimulator()) { return; }
const CAircraftModelList dbModels = dbModelsConsidered.isEmpty() ? const CAircraftModelList dbModels = dbModelsConsidered.isEmpty() ?
sApp->getWebDataServices()->getModels() : sApp->getWebDataServices()->getModels() :
dbModelsConsidered; dbModelsConsidered;
if (dbModels.isEmpty()) { return; } if (dbModels.isEmpty()) { return; }
const QSet<CSimulatorInfo> simSet = sims.asSingleSimulatorSet(); const QSet<CSimulatorInfo> simulatorsSet = simulators.asSingleSimulatorSet();
for (const CSimulatorInfo &singleInfo : simSet) for (const CSimulatorInfo &singleSimulator : simulatorsSet)
{ {
if (!this->doWorkCheck()) { return; } if (!this->doWorkCheck()) { return; }
CAircraftModelList simModels = cache.getSynchronizedCachedModels(singleInfo); CAircraftModelList simulatorModels = cache.getSynchronizedCachedModels(singleSimulator);
if (simModels.isEmpty()) { continue; } if (simulatorModels.isEmpty()) { continue; }
const CAircraftModelList dbModelsForSim = dbModels.matchesSimulator(singleInfo); const CAircraftModelList dbModelsForSimulator = dbModels.matchesSimulator(singleSimulator);
if (dbModelsForSim.isEmpty()) { continue; } if (dbModelsForSimulator.isEmpty()) { continue; }
// time consuming part // time consuming part
const int c = CDatabaseUtils::consolidateModelsWithDbData(dbModelsForSim, simModels, true); const int c = CDatabaseUtils::consolidateModelsWithDbData(dbModelsForSimulator, simulatorModels, true);
if (c > 0) if (c > 0)
{ {
CLogMessage(this).info("Consolidated %1 models for '%2'") << c << singleInfo.convertToQString(); this->addHistory(CLogMessage(this).info("Consolidated %1 models for '%2'") << c << singleSimulator.convertToQString());
const CStatusMessage m = cache.setCachedModels(simModels, singleInfo); const CStatusMessage m = cache.setCachedModels(simulatorModels, singleSimulator);
CLogMessage::preformatted(m); CLogMessage::preformatted(m);
this->addHistory(m);
} }
else else
{ {
CLogMessage(this).info("Synchronized, no changes for '%1'") << singleInfo.convertToQString(); this->addHistory(CLogMessage(this).info("Synchronized, no changes for '%1'") << singleSimulator.convertToQString());
}
if (simulatorsSet.size() > 1)
{
// just give the system some time to relax, consolidation is time consuming
if (!this->doWorkCheck()) { return; }
CEventLoop::processEventsFor(1000);
} }
if (simSet.size() > 1) { CEventLoop::processEventsFor(5000); } // just give the system some time to relax, consolidation is time consuming
} }
} }
void CBackgroundDataUpdater::syncDbEntity(CEntityFlags::Entity entity) const void CBackgroundDataUpdater::syncDbEntity(CEntityFlags::Entity entity)
{ {
if (!this->doWorkCheck()) { return; } if (!this->doWorkCheck()) { return; }
const QDateTime latestCacheTs = sApp->getWebDataServices()->getCacheTimestamp(entity); const QDateTime latestCacheTs = sApp->getWebDataServices()->getCacheTimestamp(entity);
@@ -141,18 +159,18 @@ namespace BlackCore
if (!latestDbTs.isValid()) { return; } if (!latestDbTs.isValid()) { return; }
if (latestDbTs <= latestCacheTs) if (latestDbTs <= latestCacheTs)
{ {
CLogMessage(this).info("No auto sync with DB, entity '%1', DB ts: %2 cache ts: %3") << CEntityFlags::flagToString(entity) << latestDbTs.toString(Qt::ISODate) << latestCacheTs.toString(Qt::ISODate); this->addHistory(CLogMessage(this).info("No auto sync with DB, entity '%1', DB ts: %2 cache ts: %3") << CEntityFlags::flagToString(entity) << latestDbTs.toString(Qt::ISODate) << latestCacheTs.toString(Qt::ISODate));
return; return;
} }
CLogMessage(this).info("Triggering read of '%1' since '%2'") << CEntityFlags::flagToString(entity) << latestCacheTs.toString(Qt::ISODate); this->addHistory(CLogMessage(this).info("Triggering read of '%1' since '%2'") << CEntityFlags::flagToString(entity) << latestCacheTs.toString(Qt::ISODate));
sApp->getWebDataServices()->triggerLoadingDirectlyFromDb(CEntityFlags::ModelEntity, latestCacheTs); sApp->getWebDataServices()->triggerLoadingDirectlyFromDb(CEntityFlags::ModelEntity, latestCacheTs);
} }
bool CBackgroundDataUpdater::doWorkCheck() const bool CBackgroundDataUpdater::doWorkCheck() const
{ {
if (!sApp || sApp->isShuttingDown() || !sApp->hasWebDataServices()) { return false; } if (!sApp || sApp->isShuttingDown() || !sApp->hasWebDataServices()) { return false; }
if (!isEnabled()) { return false; } if (!this->isEnabled()) { return false; }
return true; return true;
} }
@@ -168,5 +186,11 @@ namespace BlackCore
this->syncModelOrModelSetCacheWithDbData(m_modelSetCaches, modelsPublished); this->syncModelOrModelSetCacheWithDbData(m_modelSetCaches, modelsPublished);
emit this->consolidating(false); emit this->consolidating(false);
} }
void CBackgroundDataUpdater::addHistory(const CStatusMessage &msg)
{
QWriteLocker l(&m_lockMsg);
m_messageHistory.push_frontMaxElements(msg, 100); // latest
}
} // ns } // ns
} // ns } // ns

View File

@@ -16,8 +16,10 @@
#include "blackcore/blackcoreexport.h" #include "blackcore/blackcoreexport.h"
#include "blackmisc/simulation/data/modelcaches.h" #include "blackmisc/simulation/data/modelcaches.h"
#include "blackmisc/network/entityflags.h" #include "blackmisc/network/entityflags.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/worker.h" #include "blackmisc/worker.h"
#include <QMap> #include <QMap>
#include <QReadWriteLock>
namespace BlackCore namespace BlackCore
{ {
@@ -35,6 +37,10 @@ namespace BlackCore
//! Constructor //! Constructor
CBackgroundDataUpdater(QObject *owner); CBackgroundDataUpdater(QObject *owner);
//! The message history
//! \threadsafe
BlackMisc::CStatusMessageList getMessageHistory() const;
signals: signals:
//! Consolidation //! Consolidation
void consolidating(bool running); void consolidating(bool running);
@@ -43,10 +49,12 @@ namespace BlackCore
int m_cycle = 0; //!< cycle int m_cycle = 0; //!< cycle
bool m_inWork = false; //!< indicates a running update bool m_inWork = false; //!< indicates a running update
bool m_updatePublishedModels = true; //!< update when models have been updated bool m_updatePublishedModels = true; //!< update when models have been updated
mutable QReadWriteLock m_lockMsg; //!< lock snapshot
BlackMisc::Simulation::Data::CModelCaches m_modelCaches { false, this }; //!< caches BlackMisc::Simulation::Data::CModelCaches m_modelCaches { false, this }; //!< caches
BlackMisc::Simulation::Data::CModelSetCaches m_modelSetCaches { false, this }; //!< caches BlackMisc::Simulation::Data::CModelSetCaches m_modelSetCaches { false, this }; //!< caches
QMap<QString, QDateTime> m_syncedModelsLatestChange; //! timestamp per cache QMap<QString, QDateTime> m_syncedModelsLatestChange; //! timestamp per cache when last synced
BlackMisc::CStatusMessageList m_messageHistory;
//! Do the update checks //! Do the update checks
void doWork(); void doWork();
@@ -55,17 +63,22 @@ namespace BlackCore
void triggerInfoReads(); void triggerInfoReads();
//! Sync the model cache, normally model set or simulator models cache //! Sync the model cache, normally model set or simulator models cache
//! \remark if no DB models are passed all DB models are used
void syncModelOrModelSetCacheWithDbData(BlackMisc::Simulation::Data::IMultiSimulatorModelCaches &cache, void syncModelOrModelSetCacheWithDbData(BlackMisc::Simulation::Data::IMultiSimulatorModelCaches &cache,
const BlackMisc::Simulation::CAircraftModelList &dbModelsConsidered = {}); const BlackMisc::Simulation::CAircraftModelList &dbModelsConsidered = {});
//! Sync DB entity //! Sync DB entity
void syncDbEntity(BlackMisc::Network::CEntityFlags::Entity entity) const; void syncDbEntity(BlackMisc::Network::CEntityFlags::Entity entity);
//! Still enabled etc. //! Still enabled etc.
bool doWorkCheck() const; bool doWorkCheck() const;
//! Models have been published //! Models have been published
void onModelsPublished(const BlackMisc::Simulation::CAircraftModelList &modelsPublished, bool directWrite); void onModelsPublished(const BlackMisc::Simulation::CAircraftModelList &modelsPublished, bool directWrite);
//! Add history message
//! \threadsafe
void addHistory(const BlackMisc::CStatusMessage &msg);
}; };
} // ns } // ns
} // ns } // ns

View File

@@ -96,10 +96,10 @@ namespace BlackGui
static bool isValid(const int &valueInSeconds) { return valueInSeconds == -1 || (valueInSeconds >= minSecs() && valueInSeconds <= maxSecs()); } static bool isValid(const int &valueInSeconds) { return valueInSeconds == -1 || (valueInSeconds >= minSecs() && valueInSeconds <= maxSecs()); }
//! \copydoc BlackCore::TSettingTrait::defaultValue //! \copydoc BlackCore::TSettingTrait::defaultValue
static const int &defaultValue() { static const int i = 240; return i; } static const int &defaultValue() { static const int i = 60; return i; }
//! Minimum //! Minimum
static int minSecs() { return 60; } static int minSecs() { return 30; }
//! Maximum //! Maximum
static int maxSecs() { return 3600; } static int maxSecs() { return 3600; }