diff --git a/src/ui/control/control.h b/src/ui/control/control.h index 5239016d..b52817a7 100644 --- a/src/ui/control/control.h +++ b/src/ui/control/control.h @@ -44,11 +44,17 @@ enum Command : qint8 { Rpc_QuotaManager_alert, Rpc_StatManager_deleteStatApp, - Rpc_StatManager_clear, - Rpc_StatManager_cleared, + Rpc_StatManager_deleteConn, + Rpc_StatManager_deleteConnAll, + Rpc_StatManager_resetAppTrafTotals, + Rpc_StatManager_clearTraffic, + Rpc_StatManager_trafficCleared, Rpc_StatManager_appStatRemoved, Rpc_StatManager_appCreated, Rpc_StatManager_trafficAdded, + Rpc_StatManager_connBlockAdded, + Rpc_StatManager_connRemoved, + Rpc_StatManager_appTrafTotalsResetted, Rpc_TaskManager_runTask, Rpc_TaskManager_abortTask, diff --git a/src/ui/model/traflistmodel.cpp b/src/ui/model/traflistmodel.cpp index db279c2c..7a5b7fad 100644 --- a/src/ui/model/traflistmodel.cpp +++ b/src/ui/model/traflistmodel.cpp @@ -10,6 +10,8 @@ TrafListModel::TrafListModel(StatManager *statManager, QObject *parent) : TableItemModel(parent), m_statManager(statManager) { + connect(m_statManager, &StatManager::trafficCleared, this, &TrafListModel::resetTraf); + connect(m_statManager, &StatManager::appTrafTotalsResetted, this, &TrafListModel::resetTraf); } void TrafListModel::setUnit(TrafListModel::TrafUnit unit) @@ -84,16 +86,12 @@ QVariant TrafListModel::data(const QModelIndex &index, int role) const void TrafListModel::clear() { - statManager()->clear(); - - resetTraf(); + statManager()->clearTraffic(); } void TrafListModel::resetAppTotals() { statManager()->resetAppTrafTotals(); - - resetTraf(); } void TrafListModel::resetTraf() diff --git a/src/ui/rpc/rpcmanager.cpp b/src/ui/rpc/rpcmanager.cpp index dada0461..4539b7f5 100644 --- a/src/ui/rpc/rpcmanager.cpp +++ b/src/ui/rpc/rpcmanager.cpp @@ -126,8 +126,8 @@ void RpcManager::setupQuotaManagerSignals() void RpcManager::setupStatManagerSignals() { - connect(statManager(), &StatManager::cleared, this, - [&] { invokeOnClients(Control::Rpc_StatManager_cleared); }); + connect(statManager(), &StatManager::trafficCleared, this, + [&] { invokeOnClients(Control::Rpc_StatManager_trafficCleared); }); connect(statManager(), &StatManager::appStatRemoved, this, [&](qint64 appId) { invokeOnClients(Control::Rpc_StatManager_appStatRemoved, { appId }); }); @@ -140,6 +140,12 @@ void RpcManager::setupStatManagerSignals() invokeOnClients( Control::Rpc_StatManager_trafficAdded, { unixTime, inBytes, outBytes }); }); + connect(statManager(), &StatManager::connBlockAdded, this, + [&] { invokeOnClients(Control::Rpc_StatManager_connBlockAdded); }); + connect(statManager(), &StatManager::connRemoved, this, + [&] { invokeOnClients(Control::Rpc_StatManager_connRemoved); }); + connect(statManager(), &StatManager::appTrafTotalsResetted, this, + [&] { invokeOnClients(Control::Rpc_StatManager_appTrafTotalsResetted); }); } void RpcManager::setupTaskManagerSignals() @@ -280,14 +286,20 @@ bool RpcManager::processCommandRpc( return processQuotaManagerRpc(cmd, args); case Control::Rpc_StatManager_deleteStatApp: - case Control::Rpc_StatManager_clear: + case Control::Rpc_StatManager_deleteConn: + case Control::Rpc_StatManager_deleteConnAll: + case Control::Rpc_StatManager_resetAppTrafTotals: + case Control::Rpc_StatManager_clearTraffic: if (!checkClientValidated(w)) return false; Q_FALLTHROUGH(); - case Control::Rpc_StatManager_cleared: + case Control::Rpc_StatManager_trafficCleared: case Control::Rpc_StatManager_appStatRemoved: case Control::Rpc_StatManager_appCreated: case Control::Rpc_StatManager_trafficAdded: + case Control::Rpc_StatManager_connBlockAdded: + case Control::Rpc_StatManager_connRemoved: + case Control::Rpc_StatManager_appTrafTotalsResetted: return processStatManagerRpc(w, cmd, args); case Control::Rpc_TaskManager_runTask: @@ -444,11 +456,21 @@ bool RpcManager::processStatManagerRpc( case Control::Rpc_StatManager_deleteStatApp: sendResult(w, statManager()->deleteStatApp(args.value(0).toLongLong())); return true; - case Control::Rpc_StatManager_clear: - sendResult(w, statManager()->clear()); + case Control::Rpc_StatManager_deleteConn: + sendResult( + w, statManager()->deleteConn(args.value(0).toLongLong(), args.value(1).toBool())); return true; - case Control::Rpc_StatManager_cleared: - emit statManager()->cleared(); + case Control::Rpc_StatManager_deleteConnAll: + sendResult(w, statManager()->deleteConnAll()); + return true; + case Control::Rpc_StatManager_resetAppTrafTotals: + sendResult(w, statManager()->resetAppTrafTotals()); + return true; + case Control::Rpc_StatManager_clearTraffic: + sendResult(w, statManager()->clearTraffic()); + return true; + case Control::Rpc_StatManager_trafficCleared: + emit statManager()->trafficCleared(); return true; case Control::Rpc_StatManager_appStatRemoved: emit statManager()->appStatRemoved(args.value(0).toLongLong()); @@ -460,6 +482,15 @@ bool RpcManager::processStatManagerRpc( emit statManager()->trafficAdded( args.value(0).toLongLong(), args.value(1).toUInt(), args.value(2).toUInt()); return true; + case Control::Rpc_StatManager_connBlockAdded: + emit statManager()->connBlockAdded(); + return true; + case Control::Rpc_StatManager_connRemoved: + emit statManager()->connRemoved(); + return true; + case Control::Rpc_StatManager_appTrafTotalsResetted: + emit statManager()->appTrafTotalsResetted(); + return true; default: return false; } diff --git a/src/ui/rpc/statmanagerrpc.cpp b/src/ui/rpc/statmanagerrpc.cpp index f4359a2d..aa86d117 100644 --- a/src/ui/rpc/statmanagerrpc.cpp +++ b/src/ui/rpc/statmanagerrpc.cpp @@ -20,7 +20,22 @@ bool StatManagerRpc::deleteStatApp(qint64 appId) return rpcManager()->doOnServer(Control::Rpc_StatManager_deleteStatApp, { appId }); } -bool StatManagerRpc::clear() +bool StatManagerRpc::deleteConn(qint64 rowIdTo, bool blocked) { - return rpcManager()->doOnServer(Control::Rpc_StatManager_clear); + return rpcManager()->doOnServer(Control::Rpc_StatManager_deleteConn, { rowIdTo, blocked }); +} + +bool StatManagerRpc::deleteConnAll() +{ + return rpcManager()->doOnServer(Control::Rpc_StatManager_deleteConnAll); +} + +bool StatManagerRpc::resetAppTrafTotals() +{ + return rpcManager()->doOnServer(Control::Rpc_StatManager_resetAppTrafTotals); +} + +bool StatManagerRpc::clearTraffic() +{ + return rpcManager()->doOnServer(Control::Rpc_StatManager_clearTraffic); } diff --git a/src/ui/rpc/statmanagerrpc.h b/src/ui/rpc/statmanagerrpc.h index 432776ec..b5379da3 100644 --- a/src/ui/rpc/statmanagerrpc.h +++ b/src/ui/rpc/statmanagerrpc.h @@ -21,8 +21,13 @@ public: bool deleteStatApp(qint64 appId) override; + bool deleteConn(qint64 rowIdTo, bool blocked) override; + bool deleteConnAll() override; + + bool resetAppTrafTotals() override; + public slots: - bool clear() override; + bool clearTraffic() override; private: FortManager *m_fortManager = nullptr; diff --git a/src/ui/stat/statmanager.cpp b/src/ui/stat/statmanager.cpp index 49629dab..196d6551 100644 --- a/src/ui/stat/statmanager.cpp +++ b/src/ui/stat/statmanager.cpp @@ -89,18 +89,14 @@ bool StatManager::initialize() return false; } - setupAfterClear(); - - connect(this, &StatManager::cleared, this, &StatManager::setupAfterClear); + setupConnBlockId(); return true; } -void StatManager::setupAfterClear() +void StatManager::setupTrafDate() { m_trafHour = m_trafDay = m_trafMonth = 0; - - setupConnBlockId(); } void StatManager::setupConnBlockId() @@ -209,18 +205,19 @@ bool StatManager::updateTrafDay(qint64 unixTime) return isNewDay; } -bool StatManager::clear() +bool StatManager::clearTraffic() { sqliteDb()->beginTransaction(); - sqliteDb()->execute(StatSql::sqlClear); + sqliteDb()->execute(StatSql::sqlClearTraffic); sqliteDb()->execute(StatSql::sqlVacuum); sqliteDb()->commitTransaction(); clearAppIdCache(); + setupTrafDate(); quotaManager()->clear(); - emit cleared(); + emit trafficCleared(); return true; } @@ -431,7 +428,13 @@ bool StatManager::resetAppTrafTotals() stmt->bindInt(1, DateUtil::getUnixHour(unixTime)); - return sqliteDb()->done(stmt); + const bool ok = sqliteDb()->done(stmt); + + if (ok) { + emit appTrafTotalsResetted(); + } + + return ok; } bool StatManager::hasAppTraf(qint64 appId) diff --git a/src/ui/stat/statmanager.h b/src/ui/stat/statmanager.h index f5215553..653aa62d 100644 --- a/src/ui/stat/statmanager.h +++ b/src/ui/stat/statmanager.h @@ -64,7 +64,7 @@ public: const char *sql, qint32 trafTime, qint64 &inBytes, qint64 &outBytes, qint64 appId = 0); signals: - void cleared(); + void trafficCleared(); void appStatRemoved(qint64 appId); void appCreated(qint64 appId, const QString &appPath); @@ -73,13 +73,15 @@ signals: void connBlockAdded(); void connRemoved(); + void appTrafTotalsResetted(); + public slots: - virtual bool clear(); + virtual bool clearTraffic(); private: using QStmtList = QList; - void setupAfterClear(); + void setupTrafDate(); void setupConnBlockId(); void setupByConf(); diff --git a/src/ui/stat/statsql.cpp b/src/ui/stat/statsql.cpp index 02440284..679ed493 100644 --- a/src/ui/stat/statsql.cpp +++ b/src/ui/stat/statsql.cpp @@ -192,17 +192,15 @@ const char *const StatSql::sqlSelectDeletedAllConnAppList = " LEFT JOIN traffic_app ta ON ta.app_id = t.app_id" " WHERE ta.app_id IS NULL;"; -const char *const StatSql::sqlClear = "DELETE FROM app;" - "DELETE FROM traffic_app;" - "DELETE FROM traffic_app_hour;" - "DELETE FROM traffic_app_day;" - "DELETE FROM traffic_app_month;" - "DELETE FROM traffic_hour;" - "DELETE FROM traffic_day;" - "DELETE FROM traffic_month;" - "DELETE FROM conn;" - "DELETE FROM conn_block;" - "DELETE FROM conn_traffic;" - "DELETE FROM conn_flow;"; +const char *const StatSql::sqlClearTraffic = + "DELETE FROM traffic_app;" + "DELETE FROM traffic_app_hour;" + "DELETE FROM traffic_app_day;" + "DELETE FROM traffic_app_month;" + "DELETE FROM traffic_hour;" + "DELETE FROM traffic_day;" + "DELETE FROM traffic_month;" + "DELETE FROM app WHERE (" + " SELECT 1 FROM conn c WHERE c.app_id = app.app_id LIMIT 1) IS NULL;"; const char *const StatSql::sqlVacuum = "VACUUM;"; diff --git a/src/ui/stat/statsql.h b/src/ui/stat/statsql.h index 828ad7ee..457f48d4 100644 --- a/src/ui/stat/statsql.h +++ b/src/ui/stat/statsql.h @@ -80,7 +80,7 @@ public: static const char *const sqlDeleteAllConnBlock; static const char *const sqlSelectDeletedAllConnAppList; - static const char *const sqlClear; + static const char *const sqlClearTraffic; static const char *const sqlVacuum; };