UI: DB: Add app's total traffic.

This commit is contained in:
Nodir Temirkhodjaev 2017-12-05 08:24:48 +05:00
parent 5cea967304
commit 12312739f0
5 changed files with 30 additions and 5 deletions

View File

@ -97,10 +97,14 @@ void Test::debugStatTraf(SqliteDb *sqliteDb)
debugStatTrafStep(sqliteDb, "traffic_month",
"SELECT 0, unix_time, in_bytes, out_bytes"
" FROM traffic_month;");
debugStatTrafStep(sqliteDb, "traffic_app_total",
"SELECT id, unix_time, in_bytes, out_bytes"
" FROM app;", 1);
}
void Test::debugStatTrafStep(SqliteDb *sqliteDb, const char *name,
const char *sql)
const char *sql, int timeMult)
{
SqliteStmt stmt;
@ -108,7 +112,7 @@ void Test::debugStatTrafStep(SqliteDb *sqliteDb, const char *name,
qDebug() << '>' << name << '<';
while (stmt.step() == SqliteStmt::StepRow) {
const qint64 unixTime = stmt.columnInt64(1) * 3600;
const qint64 unixTime = stmt.columnInt64(1) * timeMult;
qDebug() << '>'
<< stmt.columnInt64(0)

View File

@ -17,7 +17,7 @@ private:
void debugStatTraf(SqliteDb *sqliteDb);
void debugStatTrafStep(SqliteDb *sqliteDb, const char *name,
const char *sql);
const char *sql, int timeMult = 3600);
};
#endif // TEST_H

View File

@ -113,6 +113,8 @@ void DatabaseManager::handleStatTraf(quint16 procCount, const quint8 *procBits,
SqliteStmt *stmtUpdateDay = getSqliteStmt(DatabaseSql::sqlUpdateTrafficDay);
SqliteStmt *stmtUpdateMonth = getSqliteStmt(DatabaseSql::sqlUpdateTrafficMonth);
SqliteStmt *stmtUpdateAppTotal = getSqliteStmt(DatabaseSql::sqlUpdateTrafficAppTotal);
stmtUpdateAppHour->bindInt(1, unixHour);
stmtUpdateAppDay->bindInt(1, unixDay);
stmtUpdateAppMonth->bindInt(1, unixMonth);
@ -162,6 +164,10 @@ void DatabaseManager::handleStatTraf(quint16 procCount, const quint8 *procBits,
updateTraffic(stmtUpdateHour, inBytes, outBytes);
updateTraffic(stmtUpdateDay, inBytes, outBytes);
updateTraffic(stmtUpdateMonth, inBytes, outBytes);
// Update total bytes
stmtUpdateAppTotal->bindInt64(1, appId);
updateTraffic(stmtUpdateAppTotal, inBytes, outBytes);
}
m_sqliteDb->commitTransaction();
@ -202,6 +208,8 @@ qint64 DatabaseManager::getAppId(const QString &appPath)
SqliteStmt *stmt = getSqliteStmt(DatabaseSql::sqlInsertAppId);
stmt->bindText(1, appPath);
stmt->bindInt64(2, QDateTime::currentSecsSinceEpoch());
if (stmt->step() == SqliteStmt::StepDone) {
appId = m_sqliteDb->lastInsertRowid();
}

View File

@ -9,7 +9,10 @@ const char * const DatabaseSql::sqlPragmas =
const char * const DatabaseSql::sqlCreateTables =
"CREATE TABLE app("
" id INTEGER PRIMARY KEY,"
" path TEXT UNIQUE NOT NULL"
" path TEXT UNIQUE NOT NULL,"
" unix_time INTEGER NOT NULL,"
" in_bytes INTEGER NOT NULL,"
" out_bytes INTEGER NOT NULL"
");"
"CREATE TABLE traffic_app_hour("
@ -60,7 +63,8 @@ const char * const DatabaseSql::sqlSelectAppId =
;
const char * const DatabaseSql::sqlInsertAppId =
"INSERT INTO app(path) VALUES(?1);"
"INSERT INTO app(path, unix_time, in_bytes, out_bytes)"
" VALUES(?1, ?2, 0, 0);"
;
const char * const DatabaseSql::sqlInsertTrafficAppHour =
@ -134,3 +138,10 @@ const char * const DatabaseSql::sqlUpdateTrafficMonth =
" out_bytes = out_bytes + ?3"
" WHERE unix_time = ?1;"
;
const char * const DatabaseSql::sqlUpdateTrafficAppTotal =
"UPDATE app"
" SET in_bytes = in_bytes + ?2,"
" out_bytes = out_bytes + ?3"
" WHERE id = ?1;"
;

View File

@ -25,6 +25,8 @@ public:
static const char * const sqlUpdateTrafficHour;
static const char * const sqlUpdateTrafficDay;
static const char * const sqlUpdateTrafficMonth;
static const char * const sqlUpdateTrafficAppTotal;
};
#endif // DATABASESQL_H