6#include <QElapsedTimer>
18using namespace swift::misc::network;
19using namespace swift::misc::simulation;
20using namespace swift::misc::simulation::data;
37 if (sApp && sApp->hasWebDataServices())
47 return m_messageHistory;
52 Q_ASSERT_X(this->
hasStarted(), Q_FUNC_INFO,
"Worker not started yet");
53 m_updateTimer.startTimer(ms);
56 void CBackgroundDataUpdater::doWork()
58 if (!this->doWorkCheck())
70 this->addHistory(
CLogMessage(
this).info(u
"Background consolidation cycle %1") << cycle);
77 this->addHistory(
CLogMessage(
this).info(u
"Triggered info reads from DB"));
78 this->triggerInfoReads();
81 this->addHistory(
CLogMessage(
this).info(u
"Synchronize DB entities"));
89 this->addHistory(
CLogMessage(
this).info(u
"Synchronize %1") << this->modelCaches(
true).getDescription());
90 this->syncModelOrModelSetCacheWithDbData(
true);
93 this->addHistory(
CLogMessage(
this).info(u
"Synchronize %1") << this->modelCaches(
false).getDescription());
94 this->syncModelOrModelSetCacheWithDbData(
false);
105 void CBackgroundDataUpdater::triggerInfoReads()
107 if (!this->doWorkCheck())
115 void CBackgroundDataUpdater::syncModelOrModelSetCacheWithDbData(
bool modelSetFlag,
116 const CAircraftModelList &dbModelsConsidered)
118 if (!this->doWorkCheck())
122 IMultiSimulatorModelCaches &modelCaches = this->modelCaches(modelSetFlag);
123 const QDateTime latestDbModelsTs =
126 if (!latestDbModelsTs.
isValid())
132 const QDateTime dbModelsLatestSync = m_syncedModelsLatestChange.value(modelCaches.getDescription());
133 if (dbModelsLatestSync.
isValid() && latestDbModelsTs <= dbModelsLatestSync)
138 const QString description = modelCaches.getDescription();
139 m_syncedModelsLatestChange[description] = latestDbModelsTs;
140 const CSimulatorInfo simulators = modelCaches.simulatorsWithInitializedCache();
146 const CAircraftModelList dbModels =
154 for (
const CSimulatorInfo &singleSimulator : simulatorsSet)
156 if (!this->doWorkCheck())
160 CAircraftModelList simulatorModels = modelCaches.getSynchronizedCachedModels(singleSimulator);
166 const CAircraftModelList dbModelsForSimulator = dbModels.
matchesSimulator(singleSimulator);
167 if (dbModelsForSimulator.
isEmpty())
176 const CStatusMessage m = modelCaches.setCachedModels(simulatorModels, singleSimulator);
177 const qint64 msElapsed = time.
elapsed();
178 this->addHistory(CLogMessage(
this).info(u
"Consolidated %1 models (%2) for '%3' in %4ms")
179 << c << description << singleSimulator.convertToQString() << msElapsed);
185 this->addHistory(CLogMessage(
this).info(u
"Synchronized, no changes for '%1'")
186 << singleSimulator.convertToQString());
189 if (simulatorsSet.size() > 1)
192 if (!this->doWorkCheck())
196 CEventLoop eventLoop(
this);
197 eventLoop.exec(1000);
202 void CBackgroundDataUpdater::syncDbEntity(CEntityFlags::Entity entity)
204 if (!this->doWorkCheck())
222 if (latestDbTs <= latestCacheTs)
225 CLogMessage(
this).info(
226 u
"Background updater (%1), no auto synchronization with DB, entity '%2', DB ts: %3 cache ts: %4")
228 << latestCacheTsStr);
232 this->addHistory(CLogMessage(
this).info(u
"Background updater (%1) triggering read of '%2' since '%3'")
237 bool CBackgroundDataUpdater::doWorkCheck()
const
250 void CBackgroundDataUpdater::onModelsPublished(
const CAircraftModelList &modelsPublished,
bool directWrite)
252 if (!this->doWorkCheck())
260 if (!m_updatePublishedModels)
274 this->syncModelOrModelSetCacheWithDbData(
true, modelsPublished);
275 this->syncModelOrModelSetCacheWithDbData(
false, modelsPublished);
279 IMultiSimulatorModelCaches &CBackgroundDataUpdater::modelCaches(
bool modelSetFlag)
285 return m_modelCaches;
288 void CBackgroundDataUpdater::addHistory(
const CStatusMessage &msg)
290 QWriteLocker l(&m_lockMsg);
291 m_messageHistory.push_frontMaxElements(msg, 100);
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
bool hasWebDataServices() const
Web data services available?
bool isShuttingDown() const
Is application shutting down?
CWebDataServices * getWebDataServices() const
Get the web data services.
QDateTime getLatestDbEntityTimestamp(swift::misc::network::CEntityFlags::Entity entity) const
Corresponding DB timestamp if applicable.
swift::misc::simulation::CAircraftModelList getModels() const
Models.
QDateTime getCacheTimestamp(swift::misc::network::CEntityFlags::Entity entity) const
Corresponding cache timestamp if applicable.
swift::misc::network::CEntityFlags::Entity triggerLoadingDirectlyFromDb(swift::misc::network::CEntityFlags::Entity whatToRead, const QDateTime &newerThan=QDateTime())
Trigger reload from DB, loads the DB data and bypasses the caches checks and info objects.
void triggerReadOfSharedInfoObjects()
Trigger read of shared info objects.
void triggerReadOfDbInfoObjects()
Trigger read of DB info objects.
void consolidating(bool running)
Consolidation.
CBackgroundDataUpdater(QObject *owner)
Constructor.
swift::misc::CStatusMessageList getMessageHistory() const
The message history.
static const QStringList & getLogCategories()
Log categories.
void startUpdating(std::chrono::milliseconds ms)
Start the updating timer.
static int consolidateModelsWithDbData(swift::misc::simulation::CAircraftModelList &models, bool force)
Consolidate models with DB data.
void publishedModelsSimplified(const swift::misc::simulation::CAircraftModelList &modelsPublished, bool directWrite)
Published models, simplified version of publishedModels.
bool isEnabled() const
Enabled (running)?
const QObject * owner() const
Owner of the worker.
CContinuousWorker(QObject *owner, const QString &name)
Constructor.
static const QString & modelCache()
Model cache.
static const QString & worker()
Background task.
static const QString & modelSetCache()
Model set cache.
Class for emitting a log message.
static void preformatted(const CStatusMessage &statusMessage)
Sends a verbatim, preformatted message to the log.
bool isEmpty() const
Synonym for empty.
Status messages, e.g. from Core -> GUI.
static QString currentThreadInfo()
Info about current thread, for debug messages.
void timeout()
Timer timed out.
bool hasStarted() const
True if the worker has started.
QDateTime latestTimestamp() const
Latest timestamp.
@ AirlineIcaoEntity
ICAO codes for airlines.
@ AircraftIcaoEntity
ICAO codes for aircraft.
@ DistributorEntity
distributors
static QString entitiesToString(CEntityFlags::Entity entities, const QString &separator=", ")
Entities to string list.
CAircraftModelList matchesSimulator(const CSimulatorInfo &simulator) const
Find for given simulator.
QSet< CSimulatorInfo > asSingleSimulatorSet() const
As a set of single simulator info objects.
bool isNoSimulator() const
No simulator?
Classes interacting with the swift database (aka "datastore").
Backend services of the swift project, like dealing with the network or the simulators.
Free functions in swift::misc.
bool isValid() const const
QString toString(QStringView format) const const
qint64 elapsed() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)