UI: RPC: StatManager: Handle other virtuals.

This commit is contained in:
Nodir Temirkhodjaev 2021-05-13 15:13:28 +03:00
parent 1d77431b40
commit 8efd9449bf
9 changed files with 102 additions and 44 deletions

View File

@ -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,

View File

@ -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()

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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)

View File

@ -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<SqliteStmt *>;
void setupAfterClear();
void setupTrafDate();
void setupConnBlockId();
void setupByConf();

View File

@ -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;";

View File

@ -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;
};