From 3f6f4e43021f0465c5657021b25cf1239842bff6 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Thu, 18 Mar 2021 17:17:35 +0300 Subject: [PATCH] UI: StatManager: Refactor createConnBlock(). --- src/ui/stat/statmanager.cpp | 42 ++++++++++++++++++++++++------------- src/ui/stat/statmanager.h | 6 ++++-- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/ui/stat/statmanager.cpp b/src/ui/stat/statmanager.cpp index 761353eb..e05ca8b6 100644 --- a/src/ui/stat/statmanager.cpp +++ b/src/ui/stat/statmanager.cpp @@ -339,18 +339,7 @@ bool StatManager::logBlockedIp(const LogEntryBlockedIp &entry, qint64 unixTime) const qint64 appId = getOrCreateAppId(entry.path(), unixTime, true); ok = (appId != INVALID_APP_ID); if (ok) { - const qint64 connId = createConn(entry, unixTime, appId); - ok = (connId > 0); - if (ok) { - ok = createConnBlock(connId, entry.blockReason()); - if (ok) { - if (m_connBlockIdMax > 0) { - m_connBlockIdMax++; - } else { - m_connBlockIdMin = m_connBlockIdMax = 1; - } - } - } + ok = createConnBlock(entry, unixTime, appId); } m_sqliteDb->endTransaction(ok); @@ -554,7 +543,7 @@ bool StatManager::updateTraffic(SqliteStmt *stmt, quint32 inBytes, quint32 outBy return m_sqliteDb->done(stmt); } -qint64 StatManager::createConn(const LogEntryBlockedIp &entry, qint64 unixTime, qint64 appId) +qint64 StatManager::insertConn(const LogEntryBlockedIp &entry, qint64 unixTime, qint64 appId) { SqliteStmt *stmt = getSqliteStmt(StatSql::sqlInsertConn); @@ -575,14 +564,37 @@ qint64 StatManager::createConn(const LogEntryBlockedIp &entry, qint64 unixTime, return 0; } -bool StatManager::createConnBlock(qint64 connId, quint8 blockReason) +qint64 StatManager::insertConnBlock(qint64 connId, quint8 blockReason) { SqliteStmt *stmt = getSqliteStmt(StatSql::sqlInsertConnBlock); stmt->bindInt64(1, connId); stmt->bindInt(2, blockReason); - return m_sqliteDb->done(stmt); + if (m_sqliteDb->done(stmt)) { + return m_sqliteDb->lastInsertRowid(); + } + + return 0; +} + +bool StatManager::createConnBlock(const LogEntryBlockedIp &entry, qint64 unixTime, qint64 appId) +{ + const qint64 connId = insertConn(entry, unixTime, appId); + if (connId <= 0) + return false; + + const qint64 connBlockId = insertConnBlock(connId, entry.blockReason()); + if (connBlockId <= 0) + return false; + + if (m_connBlockIdMax > 0) { + m_connBlockIdMax++; + } else { + m_connBlockIdMin = m_connBlockIdMax = 1; + } + + return true; } void StatManager::deleteRangeConnBlock(qint64 rowIdFrom, qint64 rowIdTo) diff --git a/src/ui/stat/statmanager.h b/src/ui/stat/statmanager.h index e8e50876..9bd5f43a 100644 --- a/src/ui/stat/statmanager.h +++ b/src/ui/stat/statmanager.h @@ -94,8 +94,10 @@ private: bool updateTraffic(SqliteStmt *stmt, quint32 inBytes, quint32 outBytes, qint64 appId = 0); - qint64 createConn(const LogEntryBlockedIp &entry, qint64 unixTime, qint64 appId); - bool createConnBlock(qint64 connId, quint8 blockReason); + qint64 insertConn(const LogEntryBlockedIp &entry, qint64 unixTime, qint64 appId); + qint64 insertConnBlock(qint64 connId, quint8 blockReason); + + bool createConnBlock(const LogEntryBlockedIp &entry, qint64 unixTime, qint64 appId); void deleteRangeConnBlock(qint64 rowIdFrom, qint64 rowIdTo); void deleteAppStmtList(const QStmtList &stmtList, SqliteStmt *stmtAppList);