This moves all the things and rules logic away from NymeaCore into their respective modules where it belongs. One major change is the removal of the removePolicy functionality. This was somewhat broken as it was only working for rules but not for all the other modules like scripts, experiences etc. After an attempt to create something that works with all modules it really seemed that this does not make a lot of sence after all, given that updating rules would in most cases leave something very broken behind and removing them was the only sane thing to do. On the other hand, experience plugins may not work well with such a policy eithre as they may require to do their own special thing. So in the end the removePolicy was dropped altogether. Apps should instead figure out themselves what removal of a thing may imply and inform the user about that beforehand.
189 lines
5.7 KiB
C++
189 lines
5.7 KiB
C++
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
*
|
|
* Copyright 2013 - 2020, nymea GmbH
|
|
* Contact: contact@nymea.io
|
|
*
|
|
* This file is part of nymea.
|
|
* This project including source code and documentation is protected by
|
|
* copyright law, and remains the property of nymea GmbH. All rights, including
|
|
* reproduction, publication, editing and translation, are reserved. The use of
|
|
* this project is subject to the terms of a license agreement to be concluded
|
|
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
|
|
* under https://nymea.io/license
|
|
*
|
|
* GNU General Public License Usage
|
|
* Alternatively, this project may be redistributed and/or modified under the
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
* Foundation, GNU version 3. This project is distributed in the hope that it
|
|
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
* Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* this project. If not, see <https://www.gnu.org/licenses/>.
|
|
*
|
|
* For any further details and any questions please contact us under
|
|
* contact@nymea.io or see our FAQ/Licensing Information on
|
|
* https://nymea.io/license/faq
|
|
*
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
#ifndef LOGENGINE_H
|
|
#define LOGENGINE_H
|
|
|
|
#include "logentry.h"
|
|
#include "logfilter.h"
|
|
#include "types/event.h"
|
|
#include "types/action.h"
|
|
#include "types/browseritemaction.h"
|
|
#include "types/browseraction.h"
|
|
#include "ruleengine/rule.h"
|
|
#include "integrations/thingmanager.h"
|
|
|
|
#include <QObject>
|
|
#include <QSqlDatabase>
|
|
#include <QSqlQuery>
|
|
#include <QSqlError>
|
|
#include <QSqlRecord>
|
|
#include <QTimer>
|
|
#include <QFutureWatcher>
|
|
|
|
namespace nymeaserver {
|
|
|
|
class DatabaseJob;
|
|
class LogEntriesFetchJob;
|
|
class ThingsFetchJob;
|
|
|
|
class LogEngine: public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
LogEngine(const QString &driver, const QString &dbName, const QString &hostname = QString("127.0.0.1"), const QString &username = QString(), const QString &password = QString(), int maxDBSize = 50000, QObject *parent = nullptr);
|
|
~LogEngine();
|
|
|
|
LogEntriesFetchJob *fetchLogEntries(const LogFilter &filter = LogFilter());
|
|
ThingsFetchJob *fetchThings();
|
|
|
|
bool jobsRunning() const;
|
|
|
|
void setMaxLogEntries(int maxLogEntries, int trimSize);
|
|
void clearDatabase();
|
|
|
|
void removeThingLogs(const ThingId &thingId);
|
|
void removeRuleLogs(const RuleId &ruleId);
|
|
|
|
public slots:
|
|
void logSystemEvent(const QDateTime &dateTime, bool active, Logging::LoggingLevel level = Logging::LoggingLevelInfo);
|
|
void logEvent(const Event &event);
|
|
void logStateChange(Thing *thing, const StateTypeId &stateTypeId, const QVariant &value);
|
|
void logAction(const Action &action, Thing::ThingError status);
|
|
void logBrowserAction(const BrowserAction &browserAction, Logging::LoggingLevel level = Logging::LoggingLevelInfo, int errorCode = 0);
|
|
void logBrowserItemAction(const BrowserItemAction &browserItemAction, Logging::LoggingLevel level = Logging::LoggingLevelInfo, int errorCode = 0);
|
|
void logRuleTriggered(const Rule &rule);
|
|
void logRuleActiveChanged(const Rule &rule);
|
|
void logRuleEnabledChanged(const Rule &rule, const bool &enabled);
|
|
void logRuleActionsExecuted(const Rule &rule);
|
|
void logRuleExitActionsExecuted(const Rule &rule);
|
|
|
|
signals:
|
|
void logEntryAdded(const LogEntry &logEntry);
|
|
void logDatabaseUpdated();
|
|
|
|
void jobsRunningChanged();
|
|
|
|
private:
|
|
bool initDB(const QString &username, const QString &password);
|
|
void appendLogEntry(const LogEntry &entry);
|
|
void rotate(const QString &dbName);
|
|
|
|
bool migrateDatabaseVersion3to4();
|
|
void migrateEntries3to4();
|
|
void finalizeMigration3To4();
|
|
|
|
private slots:
|
|
void checkDBSize();
|
|
void trim();
|
|
|
|
void enqueJob(DatabaseJob *job, bool priority = false);
|
|
void processQueue();
|
|
void handleJobFinished();
|
|
|
|
private:
|
|
QSqlDatabase m_db;
|
|
QString m_username;
|
|
QString m_password;
|
|
int m_dbMaxSize;
|
|
int m_trimSize;
|
|
int m_entryCount = 0;
|
|
bool m_initialized = false;
|
|
bool m_dbMalformed = false;
|
|
|
|
// When maxQueueLength is exceeded, jobs will be flagged and discarded if this source logs more events
|
|
int m_maxQueueLength;
|
|
QHash<QString, QList<DatabaseJob*>> m_flaggedJobs;
|
|
|
|
QList<DatabaseJob*> m_jobQueue;
|
|
DatabaseJob *m_currentJob = nullptr;
|
|
QFutureWatcher<DatabaseJob*> m_jobWatcher;
|
|
};
|
|
|
|
class DatabaseJob: public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
DatabaseJob(const QSqlDatabase &db, const QString &queryString, const QVariantList &bindValues = QVariantList()):
|
|
m_db(db),
|
|
m_queryString(queryString),
|
|
m_bindValues(bindValues)
|
|
{
|
|
}
|
|
|
|
QString executedQuery() const { return m_executedQuery; }
|
|
QSqlError error() const { return m_error; }
|
|
QList<QSqlRecord> results() const { return m_results; }
|
|
|
|
signals:
|
|
void finished();
|
|
|
|
private:
|
|
QSqlDatabase m_db;
|
|
QString m_queryString;
|
|
QVariantList m_bindValues;
|
|
|
|
QString m_executedQuery;
|
|
QSqlError m_error;
|
|
QList<QSqlRecord> m_results;
|
|
|
|
friend class LogEngine;
|
|
};
|
|
|
|
class LogEntriesFetchJob: public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
LogEntriesFetchJob(QObject *parent): QObject(parent) {}
|
|
QList<LogEntry> results() { return m_results; }
|
|
signals:
|
|
void finished();
|
|
private:
|
|
QList<LogEntry> m_results;
|
|
friend class LogEngine;
|
|
};
|
|
|
|
class ThingsFetchJob: public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
ThingsFetchJob(QObject *parent): QObject(parent) {}
|
|
QList<ThingId> results() { return m_results; }
|
|
signals:
|
|
void finished();
|
|
private:
|
|
QList<ThingId> m_results;
|
|
friend class LogEngine;
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|