diff --git a/plugins/mock/translations/de_DE.ts b/plugins/mock/translations/de_DE.ts index 3e4427f5..f3791890 100644 --- a/plugins/mock/translations/de_DE.ts +++ b/plugins/mock/translations/de_DE.ts @@ -12,228 +12,267 @@ MockDevice + guh The name of the vendor (2062d64d-3232-433c-88bc-0d33c0ba2ba6) guh + Mock Device The name of the DeviceClass (753f0d32-0468-4d08-82ed-1964aab03298) Mock Gerät + http port The name of the paramType (d4f06047-125e-4479-9810-b54c189917f5) of Mock Device HTTP Port + Mock Action 3 (async) The name of the ActionType fbae06d3-7666-483e-a39e-ec50fe89054e of deviceClass Mock Device Mock Aktion 3 (async) + Mock Action 4 (broken) The name of the ActionType df3cf33d-26d5-4577-9132-9823bd33fad0 of deviceClass Mock Device Mock Aktion 4 (kaputt) + Mock Action 5 (async, broken) The name of the ActionType bfe89a1d-3497-4121-8318-e77c37537219 of deviceClass Mock Device Mock Aktion 5 (async, kaputt) + Mock Device (Auto created) The name of the DeviceClass (ab4257b3-7548-47ee-9bd4-7dc3004fd197) Mock Gerät (Auto erstellt) + Mock Device (Push Button) The name of the DeviceClass (9e03144c-e436-4eea-82d9-ccb33ef778db) Mock Gerät (Drückknopf) + Wait 3 second before you continue, the push button will be pressed automatically. The pairing info of deviceClass Mock Device (Push Button) Warte 3 Sekunden bevor du fortfährst, the Knopf wird automatisch gerückt. + configParamInt The name of the paramType (e1f72121-a426-45e2-b475-8262b5cdf103) of Mock Devices configParamInt + configParamBool The name of the paramType (c75723b6-ea4f-4982-9751-6c5e39c88145) of Mock Devices configParamBool + async The name of the paramType (f2977061-4dd0-4ef5-85aa-3b7134743be3) of Mock Device async + broken The name of the paramType (ae8f8901-f2c1-42a5-8111-6d2fc8e4c1e4) of Mock Device kaputt + resultCount The name of the paramType (d222adb4-2f9c-4c3f-8655-76400d0fb6ce) of Mock Device Resultat Anzahl + Dummy int state changed The name of the autocreated EventType (80baec19-54de-4948-ac46-31eabfaceb83) Int Zustand verändert + Dummy int state The name of the ParamType of StateType (80baec19-54de-4948-ac46-31eabfaceb83) of DeviceClass Mock Device Dummy Int Zustand + Dummy bool state changed The name of the autocreated EventType (9dd6a97c-dfd1-43dc-acbd-367932742310) Bool Zustand verändert + Dummy bool state The name of the ParamType of StateType (9dd6a97c-dfd1-43dc-acbd-367932742310) of DeviceClass Mock Device Dummy Bool Zustand + Mock Action 1 (with params) The name of the ActionType dea0f4e1-65e3-4981-8eaa-2701c53a9185 of deviceClass Mock Device Mock Aktion 1 (Mit Parametern) + mockActionParam1 The name of the paramType (a2d3a256-a551-4712-a65b-ecd5a436a1cb) of Mock Device mockActionParam1 + mockActionParam2 The name of the paramType (304a4899-18be-4e3b-94f4-d03be52f3233) of Mock Device mockActionParam2 + Mock Action 2 (without params) The name of the ActionType defd3ed6-1a0d-400b-8879-a0202cf39935 of deviceClass Mock Device Mock Aktion (ohne Parameter + mockParamInt The name of the paramType (0550e16d-60b9-4ba5-83f4-4d3cee656121) of Mock Device mockParamInt + Mock Event 1 The name of the EventType 45bf3752-0fc6-46b9-89fd-ffd878b5b22b of deviceClass Mock Device (Auto created) Mock Event 1 + Mock Event 2 The name of the EventType 863d5920-b1cf-4eb9-88bd-8f7b8583b1cf of deviceClass Mock Device (Auto created) Mock Event 2 + color changed The name of the autocreated EventType (20dc7c22-c50e-42db-837c-2bbced939f8e) Farbe geändert + color The name of the ParamType of StateType (20dc7c22-c50e-42db-837c-2bbced939f8e) of DeviceClass Mock Device (Push Button) Farbe + Set color The name of the autocreated ActionType (20dc7c22-c50e-42db-837c-2bbced939f8e) Setze Farbe + percentage changed The name of the autocreated EventType (72981c04-267a-4ba0-a59e-9921d2f3af9c) Prozent gändert + percentage The name of the ParamType of StateType (72981c04-267a-4ba0-a59e-9921d2f3af9c) of DeviceClass Mock Device (Push Button) Prozent + Set percentage The name of the autocreated ActionType (72981c04-267a-4ba0-a59e-9921d2f3af9c) Setze Prozentwert + allowed values changed The name of the autocreated EventType (05f63f9c-f61e-4dcf-ad55-3f13fde2765b) Erlaubter Wert geändert + allowed values The name of the ParamType of StateType (05f63f9c-f61e-4dcf-ad55-3f13fde2765b) of DeviceClass Mock Device (Push Button) Erlaubte Werte + Set allowed values The name of the autocreated ActionType (05f63f9c-f61e-4dcf-ad55-3f13fde2765b) Setze erlaubten Wert + double value changed The name of the autocreated EventType (53cd7c55-49b7-441b-b970-9048f20f0e2c) Double Wert geändert + double value The name of the ParamType of StateType (53cd7c55-49b7-441b-b970-9048f20f0e2c) of DeviceClass Mock Device (Push Button) Double Wert + Set double value The name of the autocreated ActionType (53cd7c55-49b7-441b-b970-9048f20f0e2c) Setze double Wert + + Set bool value @@ -243,18 +282,22 @@ The name of the autocreated ActionType (d24ede5f-4064-4898-bb84-cfb533b1fbc0)Setze boll Wert + Timeout action The name of the ActionType 54646e7c-bc54-4895-81a2-590d72d120f9 of deviceClass Mock Device (Push Button) Timeout Aktion + Mock Device (Display Pin) The name of the DeviceClass (296f1fd4-e893-46b2-8a42-50d1bceb8730) Mock Gerät (Pin anzeigen) + + bool value changed @@ -264,6 +307,8 @@ The name of the autocreated EventType (d24ede5f-4064-4898-bb84-cfb533b1fbc0)Bool Wert geändert + + bool value @@ -273,96 +318,112 @@ The name of the ParamType of StateType (d24ede5f-4064-4898-bb84-cfb533b1fbc0) of Bool Wert + Text line The name of the paramType (e6acf0c7-4b8e-4296-ac62-855d20deb816) of Mock Device (InputTypes) Textzeile + Text area The name of the paramType (716f0994-bc01-42b0-b64d-59236f7320d2) of Mock Device (InputTypes) Textfeld + Password text The name of the paramType (e5c0d14b-c9f1-4aca-a56e-85bfa6977150) of Mock Device (InputTypes) Passwort Text + Search text The name of the paramType (22add8c9-ee4f-43ad-8931-58e999313ac3) of Mock Device (InputTypes) Suchtext + Mail address The name of the paramType (a8494faf-3a0f-4cf3-84b7-4b39148a838d) of Mock Device (InputTypes) Mail Adresse + IPv4 address The name of the paramType (9e5f86a0-4bb3-4892-bff8-3fc4032af6e2) of Mock Device (InputTypes) IPv4 Adresse + IPv6 address The name of the paramType (43bf3832-dd48-4090-a836-656e8b60216e) of Mock Device (InputTypes) IPv6 Adresse + URL The name of the paramType (fa67229f-fcef-496f-b671-59a4b48f3ab5) of Mock Device (InputTypes) URL + Mac address The name of the paramType (e93db587-7919-48f3-8c88-1651de63c765) of Mock Device (InputTypes) Mac Adresse + Please enter the secret which normaly will be displayed on the device. For the mockdevice the pin is 243681. The pairing info of deviceClass Mock Device (Display Pin) Bitte geben sie den Pincode ein der normalerweise auf dem Gerät angezeit werden würde. In diesem fall lautet der Pincode 243681. + Mock Devices The name of the plugin Mock Devices (727a4a9a-c187-446f-aadf-f1b2220607d1) Mock Gerät + pin The name of the paramType (da820e07-22dc-4173-9c07-2f49a4e265f9) of Mock Device (Display Pin) Pin + Mock Device (Parent) The name of the DeviceClass (a71fbde9-9a38-4bf8-beab-c8aade2608ba) Mock Gerät (Elternteil) + Mock Device (Child) The name of the DeviceClass (40893c9f-bc47-40c1-8bf7-b390c7c1b4fc) Mock Gerät (Kind) + parent uuid The name of the paramType (104b5288-404e-42d3-bf38-e40682e75681) of Mock Device (Child) Elternteil Uuid + Mock Device (InputTypes) The name of the DeviceClass (515ffdf1-55e5-498d-9abc-4e2fe768f3a9) diff --git a/plugins/mock/translations/en_US.ts b/plugins/mock/translations/en_US.ts index e1f165a1..8dc6bcf3 100644 --- a/plugins/mock/translations/en_US.ts +++ b/plugins/mock/translations/en_US.ts @@ -12,228 +12,267 @@ MockDevice + guh The name of the vendor (2062d64d-3232-433c-88bc-0d33c0ba2ba6) + Mock Device The name of the DeviceClass (753f0d32-0468-4d08-82ed-1964aab03298) + http port The name of the paramType (d4f06047-125e-4479-9810-b54c189917f5) of Mock Device + Mock Action 3 (async) The name of the ActionType fbae06d3-7666-483e-a39e-ec50fe89054e of deviceClass Mock Device + Mock Action 4 (broken) The name of the ActionType df3cf33d-26d5-4577-9132-9823bd33fad0 of deviceClass Mock Device + Mock Action 5 (async, broken) The name of the ActionType bfe89a1d-3497-4121-8318-e77c37537219 of deviceClass Mock Device + Mock Device (Auto created) The name of the DeviceClass (ab4257b3-7548-47ee-9bd4-7dc3004fd197) + Mock Device (Push Button) The name of the DeviceClass (9e03144c-e436-4eea-82d9-ccb33ef778db) + Wait 3 second before you continue, the push button will be pressed automatically. The pairing info of deviceClass Mock Device (Push Button) + configParamInt The name of the paramType (e1f72121-a426-45e2-b475-8262b5cdf103) of Mock Devices + configParamBool The name of the paramType (c75723b6-ea4f-4982-9751-6c5e39c88145) of Mock Devices + async The name of the paramType (f2977061-4dd0-4ef5-85aa-3b7134743be3) of Mock Device + broken The name of the paramType (ae8f8901-f2c1-42a5-8111-6d2fc8e4c1e4) of Mock Device + resultCount The name of the paramType (d222adb4-2f9c-4c3f-8655-76400d0fb6ce) of Mock Device + Dummy int state changed The name of the autocreated EventType (80baec19-54de-4948-ac46-31eabfaceb83) + Dummy int state The name of the ParamType of StateType (80baec19-54de-4948-ac46-31eabfaceb83) of DeviceClass Mock Device + Dummy bool state changed The name of the autocreated EventType (9dd6a97c-dfd1-43dc-acbd-367932742310) + Dummy bool state The name of the ParamType of StateType (9dd6a97c-dfd1-43dc-acbd-367932742310) of DeviceClass Mock Device + Mock Action 1 (with params) The name of the ActionType dea0f4e1-65e3-4981-8eaa-2701c53a9185 of deviceClass Mock Device + mockActionParam1 The name of the paramType (a2d3a256-a551-4712-a65b-ecd5a436a1cb) of Mock Device + mockActionParam2 The name of the paramType (304a4899-18be-4e3b-94f4-d03be52f3233) of Mock Device + Mock Action 2 (without params) The name of the ActionType defd3ed6-1a0d-400b-8879-a0202cf39935 of deviceClass Mock Device + mockParamInt The name of the paramType (0550e16d-60b9-4ba5-83f4-4d3cee656121) of Mock Device + Mock Event 1 The name of the EventType 45bf3752-0fc6-46b9-89fd-ffd878b5b22b of deviceClass Mock Device (Auto created) + Mock Event 2 The name of the EventType 863d5920-b1cf-4eb9-88bd-8f7b8583b1cf of deviceClass Mock Device (Auto created) + color changed The name of the autocreated EventType (20dc7c22-c50e-42db-837c-2bbced939f8e) + color The name of the ParamType of StateType (20dc7c22-c50e-42db-837c-2bbced939f8e) of DeviceClass Mock Device (Push Button) + Set color The name of the autocreated ActionType (20dc7c22-c50e-42db-837c-2bbced939f8e) + percentage changed The name of the autocreated EventType (72981c04-267a-4ba0-a59e-9921d2f3af9c) + percentage The name of the ParamType of StateType (72981c04-267a-4ba0-a59e-9921d2f3af9c) of DeviceClass Mock Device (Push Button) + Set percentage The name of the autocreated ActionType (72981c04-267a-4ba0-a59e-9921d2f3af9c) + allowed values changed The name of the autocreated EventType (05f63f9c-f61e-4dcf-ad55-3f13fde2765b) + allowed values The name of the ParamType of StateType (05f63f9c-f61e-4dcf-ad55-3f13fde2765b) of DeviceClass Mock Device (Push Button) + Set allowed values The name of the autocreated ActionType (05f63f9c-f61e-4dcf-ad55-3f13fde2765b) + double value changed The name of the autocreated EventType (53cd7c55-49b7-441b-b970-9048f20f0e2c) + double value The name of the ParamType of StateType (53cd7c55-49b7-441b-b970-9048f20f0e2c) of DeviceClass Mock Device (Push Button) + Set double value The name of the autocreated ActionType (53cd7c55-49b7-441b-b970-9048f20f0e2c) + + Set bool value @@ -243,18 +282,22 @@ The name of the autocreated ActionType (d24ede5f-4064-4898-bb84-cfb533b1fbc0) + Timeout action The name of the ActionType 54646e7c-bc54-4895-81a2-590d72d120f9 of deviceClass Mock Device (Push Button) + Mock Device (Display Pin) The name of the DeviceClass (296f1fd4-e893-46b2-8a42-50d1bceb8730) + + bool value changed @@ -264,6 +307,8 @@ The name of the autocreated EventType (d24ede5f-4064-4898-bb84-cfb533b1fbc0) + + bool value @@ -273,96 +318,112 @@ The name of the ParamType of StateType (d24ede5f-4064-4898-bb84-cfb533b1fbc0) of + Text line The name of the paramType (e6acf0c7-4b8e-4296-ac62-855d20deb816) of Mock Device (InputTypes) + Text area The name of the paramType (716f0994-bc01-42b0-b64d-59236f7320d2) of Mock Device (InputTypes) + Password text The name of the paramType (e5c0d14b-c9f1-4aca-a56e-85bfa6977150) of Mock Device (InputTypes) + Search text The name of the paramType (22add8c9-ee4f-43ad-8931-58e999313ac3) of Mock Device (InputTypes) + Mail address The name of the paramType (a8494faf-3a0f-4cf3-84b7-4b39148a838d) of Mock Device (InputTypes) + IPv4 address The name of the paramType (9e5f86a0-4bb3-4892-bff8-3fc4032af6e2) of Mock Device (InputTypes) + IPv6 address The name of the paramType (43bf3832-dd48-4090-a836-656e8b60216e) of Mock Device (InputTypes) + URL The name of the paramType (fa67229f-fcef-496f-b671-59a4b48f3ab5) of Mock Device (InputTypes) + Mac address The name of the paramType (e93db587-7919-48f3-8c88-1651de63c765) of Mock Device (InputTypes) + Please enter the secret which normaly will be displayed on the device. For the mockdevice the pin is 243681. The pairing info of deviceClass Mock Device (Display Pin) + Mock Devices The name of the plugin Mock Devices (727a4a9a-c187-446f-aadf-f1b2220607d1) + pin The name of the paramType (da820e07-22dc-4173-9c07-2f49a4e265f9) of Mock Device (Display Pin) + Mock Device (Parent) The name of the DeviceClass (a71fbde9-9a38-4bf8-beab-c8aade2608ba) + Mock Device (Child) The name of the DeviceClass (40893c9f-bc47-40c1-8bf7-b390c7c1b4fc) + parent uuid The name of the paramType (104b5288-404e-42d3-bf38-e40682e75681) of Mock Device (Child) + Mock Device (InputTypes) The name of the DeviceClass (515ffdf1-55e5-498d-9abc-4e2fe768f3a9) diff --git a/server/logging/logengine.cpp b/server/logging/logengine.cpp index ac86b922..209f0207 100644 --- a/server/logging/logengine.cpp +++ b/server/logging/logengine.cpp @@ -135,7 +135,8 @@ LogEngine::LogEngine(QObject *parent): { m_db = QSqlDatabase::addDatabase("QSQLITE", "logs"); m_db.setDatabaseName(GuhSettings::logPath()); - m_dbMaxSize = 20000; + m_dbMaxSize = 50000; + m_overflow = 100; if (QCoreApplication::instance()->organizationName() == "guh-test") { m_dbMaxSize = 20; @@ -160,6 +161,12 @@ LogEngine::LogEngine(QObject *parent): qCWarning(dcLogEngine()) << "Error fixing log database. Giving up. Logs can't be stored."; } } + + connect(&m_housekeepingTimer, &QTimer::timeout, this, &LogEngine::checkDBSize); + m_housekeepingTimer.setInterval(1); // Trigger on next idle event loop run + m_housekeepingTimer.setSingleShot(true); + + checkDBSize(); } /*! Destructs the \l{LogEngine}. */ @@ -211,6 +218,13 @@ QList LogEngine::logEntries(const LogFilter &filter) const return results; } +void LogEngine::setMaxLogEntries(int maxLogEntries, int overflow) +{ + m_dbMaxSize = maxLogEntries; + m_overflow = overflow; + checkDBSize(); +} + /*! Removes all entries from the database. This method will be used for the tests. */ void LogEngine::clearDatabase() { @@ -337,7 +351,6 @@ void LogEngine::removeRuleLogs(const RuleId &ruleId) void LogEngine::appendLogEntry(const LogEntry &entry) { - checkDBSize(); QString queryString = QString("INSERT INTO entries (timestamp, loggingEventType, loggingLevel, sourceType, typeId, deviceId, value, active, errorCode) values ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9');") .arg(entry.timestamp().toTime_t()) .arg(entry.eventType()) @@ -356,31 +369,41 @@ void LogEngine::appendLogEntry(const LogEntry &entry) } emit logEntryAdded(entry); + + if (++m_entryCount > m_dbMaxSize + m_overflow) { + m_housekeepingTimer.start(); + } } void LogEngine::checkDBSize() { - QString queryString = "SELECT ROWID FROM entries;"; - QSqlQuery query = m_db.exec(queryString); - int numRows = 0; - if (m_db.driver()->hasFeature(QSqlDriver::QuerySize)) { - numRows = query.size(); - } else { - // this can be very slow - query.last(); - numRows = query.at() + 1; + QDateTime startTime = QDateTime::currentDateTime(); + QString queryString = "SELECT COUNT(*) FROM entries;"; + QSqlQuery result = m_db.exec(queryString); + if (m_db.lastError().type() != QSqlError::NoError) { + qWarning(dcLogEngine()) << "Failed to query entry count in db:" << m_db.lastError().databaseText(); + return; } + if (!result.first()) { + qWarning(dcLogEngine()) << "Failed retrieving entry count."; + return; + } + m_entryCount = result.value(0).toInt(); - if (numRows >= m_dbMaxSize) { + if (m_entryCount >= m_dbMaxSize) { // keep only the latest m_dbMaxSize entries - qCDebug(dcLogEngine) << "Deleting oldest entries and keep only the latest" << m_dbMaxSize << "entries."; + if (!m_trimWarningPrinted) { + qCDebug(dcLogEngine) << "Deleting oldest entries" << (m_entryCount - m_dbMaxSize) << "and keep only the latest" << m_dbMaxSize << "entries."; + m_trimWarningPrinted = true; + } QString queryDeleteString = QString("DELETE FROM entries WHERE ROWID IN (SELECT ROWID FROM entries ORDER BY timestamp DESC LIMIT -1 OFFSET %1);").arg(QString::number(m_dbMaxSize)); if (m_db.exec(queryDeleteString).lastError().type() != QSqlError::NoError) { qCWarning(dcLogEngine) << "Error deleting oldest log entries to keep size. Driver error:" << m_db.lastError().driverText() << "Database error:" << m_db.lastError().databaseText(); - } else { - emit logDatabaseUpdated(); } + m_entryCount = m_dbMaxSize; + emit logDatabaseUpdated(); } + qCDebug(dcLogEngine()) << "Ran housekeeping on log database in" << startTime.msecsTo(QDateTime::currentDateTime()) << "ms."; } void LogEngine::rotate(const QString &dbName) diff --git a/server/logging/logengine.h b/server/logging/logengine.h index a9184f0b..fa8f40ed 100644 --- a/server/logging/logengine.h +++ b/server/logging/logengine.h @@ -30,6 +30,7 @@ #include #include +#include namespace guhserver { @@ -42,27 +43,9 @@ public: QList logEntries(const LogFilter &filter = LogFilter()) const; + void setMaxLogEntries(int maxLogEntries, int overflow); void clearDatabase(); -signals: - void logEntryAdded(const LogEntry &logEntry); - void logDatabaseUpdated(); - -private: - QSqlDatabase m_db; - int m_dbMaxSize; - - bool initDB(); - void appendLogEntry(const LogEntry &entry); - void checkDBSize(); - - void rotate(const QString &dbName); - -private: - // Only GuhCore and ruleEngine are allowed to log events. - friend class GuhCore; - friend class RuleEngine; - void logSystemEvent(const QDateTime &dateTime, bool active, Logging::LoggingLevel level = Logging::LoggingLevelInfo); void logEvent(const Event &event); void logAction(const Action &action, Logging::LoggingLevel level = Logging::LoggingLevelInfo, int errorCode = 0); @@ -74,6 +57,25 @@ private: void removeDeviceLogs(const DeviceId &deviceId); void removeRuleLogs(const RuleId &ruleId); +signals: + void logEntryAdded(const LogEntry &logEntry); + void logDatabaseUpdated(); + +private: + bool initDB(); + void appendLogEntry(const LogEntry &entry); + void rotate(const QString &dbName); + +private slots: + void checkDBSize(); + +private: + QSqlDatabase m_db; + int m_dbMaxSize; + int m_overflow; + bool m_trimWarningPrinted = false; + int m_entryCount = 0; + QTimer m_housekeepingTimer; }; } diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index c577c819..23e92a51 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -15,6 +15,7 @@ SUBDIRS = versioning \ restrules \ websocketserver \ logging \ + loggingdirect \ restlogging \ #coap \ # temporary removed until fixed configurations \ diff --git a/tests/auto/loggingdirect/loggingdirect.pro b/tests/auto/loggingdirect/loggingdirect.pro new file mode 100644 index 00000000..bf60e3ec --- /dev/null +++ b/tests/auto/loggingdirect/loggingdirect.pro @@ -0,0 +1,5 @@ +include(../../../guh.pri) +include(../autotests.pri) + +TARGET = testloggingdirect +SOURCES += testloggingdirect.cpp diff --git a/tests/auto/loggingdirect/testlogging b/tests/auto/loggingdirect/testlogging new file mode 100755 index 00000000..dc570291 Binary files /dev/null and b/tests/auto/loggingdirect/testlogging differ diff --git a/tests/auto/loggingdirect/testloggingdirect.cpp b/tests/auto/loggingdirect/testloggingdirect.cpp new file mode 100644 index 00000000..77f04b67 --- /dev/null +++ b/tests/auto/loggingdirect/testloggingdirect.cpp @@ -0,0 +1,97 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Michael Zanetti * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh 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 guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include + +#include "logging/logengine.h" + +using namespace guhserver; + +class TestLoggingDirect: public QObject +{ + Q_OBJECT +public: + TestLoggingDirect(QObject* parent = nullptr); + +private slots: + void benchmarkDB_data(); + void benchmarkDB(); + +private: + LogEngine engine; +}; + +TestLoggingDirect::TestLoggingDirect(QObject *parent): QObject(parent) +{ + // Setting timeout to 20 mins + qputenv("QTEST_FUNCTION_TIMEOUT", "1200000"); +} + +void TestLoggingDirect::benchmarkDB_data() { + QTest::addColumn("prefill"); + QTest::addColumn("maxSize"); + + QTest::newRow("empty, no trim") << 0 << 20000; + QTest::newRow("empty, trim") << 1 << 1; + QTest::newRow("10000, no trim") << 10000 << 20000; + QTest::newRow("10000, trim") << 10000 << 10000; + QTest::newRow("20000, no trim") << 20000 << 30000; +// QTest::newRow("20000, trim") << 20000 << 20000; +// QTest::newRow("30000, no trim") << 30000 << 40000; +// QTest::newRow("30000, trim") << 30000 << 30000; +// QTest::newRow("40000, no trim") << 40000 << 50000; +// QTest::newRow("40000, trim") << 40000 << 40000; +// QTest::newRow("50000, no trim") << 50000 << 60000; +// QTest::newRow("50000, trim") << 50000 << 50000; +// QTest::newRow("60000, no trim") << 60000 << 70000; +// QTest::newRow("60000, trim") << 60000 << 60000; + +} + +void TestLoggingDirect::benchmarkDB() +{ + QFETCH(int, prefill); + QFETCH(int, maxSize); + + // setting max log entries to "prefill" to trim it down to what this test needs. + int overflow = 10; + engine.setMaxLogEntries(prefill, overflow); + engine.setMaxLogEntries(maxSize, overflow); + + for (int i = engine.logEntries().count(); i < prefill; i++) { + engine.logSystemEvent(QDateTime::currentDateTime(), true); + } + + qDebug() << "Starting benchmark with" << engine.logEntries().count() << "entries in the db"; + QBENCHMARK { + engine.logSystemEvent(QDateTime::currentDateTime(), true); + } + QDateTime now = QDateTime::currentDateTime(); + while (engine.logEntries().count() > maxSize + overflow) { + qApp->processEvents(); + if (now.addSecs(5) < QDateTime::currentDateTime()) { + QVERIFY2(false, QString("Housekeeping didn't work. Have %1 entries but expected to have max %2").arg(engine.logEntries().count()).arg(QString::number(maxSize)).toLocal8Bit()); + } + } + qDebug() << "Ended benchmark with" << engine.logEntries().count() << "entries in the db"; +} + +#include "testloggingdirect.moc" +QTEST_MAIN(TestLoggingDirect)