2017-12-01 14:13:06 +00:00
|
|
|
#include "databasemanager.h"
|
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
#include "../util/dateutil.h"
|
2017-12-01 14:13:06 +00:00
|
|
|
#include "../util/fileutil.h"
|
2017-12-04 13:55:03 +00:00
|
|
|
#include "databasesql.h"
|
2017-12-01 14:13:06 +00:00
|
|
|
#include "sqlite/sqlitedb.h"
|
2017-12-06 13:41:26 +00:00
|
|
|
#include "sqlite/sqliteengine.h"
|
2017-12-01 14:13:06 +00:00
|
|
|
#include "sqlite/sqlitestmt.h"
|
|
|
|
|
|
|
|
DatabaseManager::DatabaseManager(const QString &filePath,
|
|
|
|
QObject *parent) :
|
|
|
|
QObject(parent),
|
2017-12-06 09:45:31 +00:00
|
|
|
m_lastTrafHour(0),
|
|
|
|
m_lastTrafDay(0),
|
|
|
|
m_lastTrafMonth(0),
|
2017-12-01 14:13:06 +00:00
|
|
|
m_filePath(filePath),
|
|
|
|
m_sqliteDb(new SqliteDb())
|
|
|
|
{
|
|
|
|
SqliteEngine::initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
DatabaseManager::~DatabaseManager()
|
|
|
|
{
|
2017-12-06 13:41:26 +00:00
|
|
|
qDeleteAll(m_sqliteStmts);
|
2017-12-01 14:13:06 +00:00
|
|
|
|
|
|
|
delete m_sqliteDb;
|
|
|
|
|
|
|
|
SqliteEngine::shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DatabaseManager::initialize()
|
|
|
|
{
|
|
|
|
const bool fileExists = FileUtil::fileExists(m_filePath);
|
|
|
|
|
|
|
|
if (!m_sqliteDb->open(m_filePath))
|
|
|
|
return false;
|
|
|
|
|
2017-12-04 13:55:03 +00:00
|
|
|
m_sqliteDb->execute(DatabaseSql::sqlPragmas);
|
2017-12-01 14:13:06 +00:00
|
|
|
|
|
|
|
return fileExists || createTables();
|
|
|
|
}
|
|
|
|
|
2017-12-08 02:38:02 +00:00
|
|
|
void DatabaseManager::logProcNew(const QString &appPath, bool &isNew)
|
2017-12-01 14:13:06 +00:00
|
|
|
{
|
2017-12-06 13:40:58 +00:00
|
|
|
qint64 appId = getAppId(appPath);
|
|
|
|
if (appId == 0) {
|
|
|
|
appId = createAppId(appPath);
|
|
|
|
isNew = true;
|
|
|
|
}
|
2017-12-01 14:13:06 +00:00
|
|
|
|
2017-12-08 02:38:02 +00:00
|
|
|
m_appPaths.prepend(appPath);
|
|
|
|
m_appIds.prepend(appId);
|
2017-12-01 14:13:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 02:38:02 +00:00
|
|
|
void DatabaseManager::logStatTraf(quint16 procCount, const quint8 *procBits,
|
|
|
|
const quint32 *trafBytes)
|
2017-12-01 14:13:06 +00:00
|
|
|
{
|
2017-12-08 15:00:48 +00:00
|
|
|
Q_ASSERT(procCount == m_appIds.size());
|
|
|
|
|
2017-12-01 14:13:06 +00:00
|
|
|
QVector<quint16> delProcIndexes;
|
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
const qint64 unixTime = DateUtil::getUnixTime();
|
2017-12-04 13:55:03 +00:00
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
const qint32 trafHour = DateUtil::getUnixHour(unixTime);
|
2017-12-06 09:45:31 +00:00
|
|
|
const bool isNewHour = (trafHour != m_lastTrafHour);
|
2017-12-04 13:55:03 +00:00
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
const qint32 trafDay = isNewHour ? DateUtil::getUnixDay(unixTime)
|
2017-12-06 09:45:31 +00:00
|
|
|
: m_lastTrafDay;
|
|
|
|
const bool isNewDay = (trafDay != m_lastTrafDay);
|
2017-12-04 13:55:03 +00:00
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
const qint32 trafMonth = isNewDay ? DateUtil::getUnixMonth(unixTime)
|
2017-12-06 09:45:31 +00:00
|
|
|
: m_lastTrafMonth;
|
2017-12-04 13:55:03 +00:00
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
m_lastTrafHour = trafHour;
|
|
|
|
m_lastTrafDay = trafDay;
|
|
|
|
m_lastTrafMonth = trafMonth;
|
|
|
|
|
|
|
|
// Insert Statemets
|
|
|
|
QStmtList insertTrafAppStmts = QStmtList()
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafficAppHour, trafHour)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafficAppDay, trafDay)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafficAppMonth, trafMonth)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafficAppTotal, -1);
|
|
|
|
|
|
|
|
QStmtList insertTrafStmts = QStmtList()
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafficHour, trafHour)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafficDay, trafDay)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafficMonth, trafMonth);
|
|
|
|
|
|
|
|
// Update Statemets
|
|
|
|
QStmtList updateTrafAppStmts = QStmtList()
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafficAppHour, trafHour)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafficAppDay, trafDay)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafficAppMonth, trafMonth)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafficAppTotal, -1);
|
|
|
|
|
|
|
|
QStmtList updateTrafStmts = QStmtList()
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafficHour, trafHour)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafficDay, trafDay)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafficMonth, trafMonth);
|
2017-12-01 14:13:06 +00:00
|
|
|
|
|
|
|
m_sqliteDb->beginTransaction();
|
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
for (quint16 i = 0; i < procCount; ++i) {
|
|
|
|
const bool active = procBits[i / 8] & (1 << (i & 7));
|
2017-12-01 14:13:06 +00:00
|
|
|
if (!active) {
|
2017-12-09 09:38:32 +00:00
|
|
|
delProcIndexes.append(i);
|
2017-12-01 14:13:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
const quint32 *procTrafBytes = &trafBytes[i * 2];
|
2017-12-01 14:13:06 +00:00
|
|
|
const quint32 inBytes = procTrafBytes[0];
|
|
|
|
const quint32 outBytes = procTrafBytes[1];
|
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
if (inBytes || outBytes) {
|
|
|
|
const qint64 appId = m_appIds.at(i);
|
2017-12-01 14:13:06 +00:00
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
// Update or insert app bytes
|
|
|
|
updateTrafficList(insertTrafAppStmts, updateTrafAppStmts,
|
|
|
|
inBytes, outBytes, appId);
|
2017-12-01 14:13:06 +00:00
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
// Update or insert total bytes
|
|
|
|
updateTrafficList(insertTrafStmts, updateTrafStmts,
|
|
|
|
inBytes, outBytes);
|
2017-12-01 14:13:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_sqliteDb->commitTransaction();
|
|
|
|
|
|
|
|
// Delete inactive processes
|
|
|
|
{
|
|
|
|
int i = delProcIndexes.size();
|
|
|
|
while (--i >= 0) {
|
|
|
|
const quint16 procIndex = delProcIndexes.at(i);
|
2017-12-08 02:38:02 +00:00
|
|
|
|
|
|
|
m_appPaths.removeAt(procIndex);
|
2017-12-01 14:13:06 +00:00
|
|
|
m_appIds.removeAt(procIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 02:38:02 +00:00
|
|
|
void DatabaseManager::logClear()
|
|
|
|
{
|
|
|
|
m_appPaths.clear();
|
|
|
|
m_appIds.clear();
|
|
|
|
}
|
|
|
|
|
2017-12-01 14:13:06 +00:00
|
|
|
bool DatabaseManager::createTables()
|
|
|
|
{
|
2017-12-05 04:04:14 +00:00
|
|
|
m_sqliteDb->beginTransaction();
|
|
|
|
|
|
|
|
const bool res = m_sqliteDb->execute(DatabaseSql::sqlCreateTables);
|
|
|
|
|
|
|
|
m_sqliteDb->commitTransaction();
|
|
|
|
|
|
|
|
return res;
|
2017-12-01 14:13:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
qint64 DatabaseManager::getAppId(const QString &appPath)
|
2017-12-01 14:13:06 +00:00
|
|
|
{
|
|
|
|
qint64 appId = 0;
|
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
SqliteStmt *stmt = getSqliteStmt(DatabaseSql::sqlSelectAppId);
|
2017-12-01 14:13:06 +00:00
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
stmt->bindText(1, appPath);
|
|
|
|
if (stmt->step() == SqliteStmt::StepRow) {
|
|
|
|
appId = stmt->columnInt64();
|
2017-12-01 14:13:06 +00:00
|
|
|
}
|
2017-12-06 13:40:58 +00:00
|
|
|
stmt->reset();
|
2017-12-01 14:13:06 +00:00
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
return appId;
|
|
|
|
}
|
2017-12-01 14:13:06 +00:00
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
qint64 DatabaseManager::createAppId(const QString &appPath)
|
|
|
|
{
|
|
|
|
qint64 appId = 0;
|
2017-12-05 03:24:48 +00:00
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
SqliteStmt *stmt = getSqliteStmt(DatabaseSql::sqlInsertAppId);
|
|
|
|
const qint64 unixTime = DateUtil::getUnixTime();
|
|
|
|
|
|
|
|
stmt->bindText(1, appPath);
|
|
|
|
stmt->bindInt64(2, unixTime);
|
2017-12-08 07:18:37 +00:00
|
|
|
stmt->bindInt(3, DateUtil::getUnixHour(unixTime));
|
2017-12-06 13:40:58 +00:00
|
|
|
|
|
|
|
if (stmt->step() == SqliteStmt::StepDone) {
|
|
|
|
appId = m_sqliteDb->lastInsertRowid();
|
2017-12-01 14:13:06 +00:00
|
|
|
}
|
2017-12-06 13:40:58 +00:00
|
|
|
stmt->reset();
|
2017-12-01 14:13:06 +00:00
|
|
|
|
|
|
|
return appId;
|
|
|
|
}
|
|
|
|
|
2017-12-08 02:38:02 +00:00
|
|
|
void DatabaseManager::getAppList(QStringList &list, QVector<qint64> &appIds)
|
2017-12-05 05:06:15 +00:00
|
|
|
{
|
|
|
|
SqliteStmt *stmt = getSqliteStmt(DatabaseSql::sqlSelectAppPaths);
|
2017-12-05 12:50:10 +00:00
|
|
|
|
2017-12-05 05:06:15 +00:00
|
|
|
while (stmt->step() == SqliteStmt::StepRow) {
|
2017-12-08 02:38:02 +00:00
|
|
|
appIds.append(stmt->columnInt64(0));
|
|
|
|
list.append(stmt->columnText(1));
|
2017-12-05 05:06:15 +00:00
|
|
|
}
|
|
|
|
stmt->reset();
|
|
|
|
}
|
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
void DatabaseManager::updateTrafficList(const QStmtList &insertStmtList,
|
|
|
|
const QStmtList &updateStmtList,
|
|
|
|
quint32 inBytes, quint32 outBytes,
|
|
|
|
qint64 appId)
|
2017-12-04 13:55:03 +00:00
|
|
|
{
|
2017-12-09 09:38:32 +00:00
|
|
|
int i = 0;
|
|
|
|
foreach (SqliteStmt *stmtUpdate, updateStmtList) {
|
|
|
|
if (!updateTraffic(stmtUpdate, inBytes, outBytes, appId)) {
|
|
|
|
qDebug() << "!Insert -> Update>";
|
2017-12-04 13:55:03 +00:00
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
SqliteStmt *stmtInsert = insertStmtList.at(i);
|
|
|
|
updateTraffic(stmtInsert, inBytes, outBytes, appId);
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
2017-12-04 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
bool DatabaseManager::updateTraffic(SqliteStmt *stmt, quint32 inBytes,
|
2017-12-06 09:45:31 +00:00
|
|
|
quint32 outBytes, qint64 appId)
|
2017-12-04 13:55:03 +00:00
|
|
|
{
|
|
|
|
stmt->bindInt64(2, inBytes);
|
|
|
|
stmt->bindInt64(3, outBytes);
|
|
|
|
|
|
|
|
if (appId != 0) {
|
|
|
|
stmt->bindInt64(4, appId);
|
|
|
|
}
|
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
const SqliteStmt::StepResult res = stmt->step();
|
|
|
|
|
2017-12-04 13:55:03 +00:00
|
|
|
stmt->reset();
|
2017-12-09 09:38:32 +00:00
|
|
|
|
|
|
|
return res == SqliteStmt::StepDone
|
|
|
|
&& m_sqliteDb->changes() != 0;
|
2017-12-04 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 07:18:37 +00:00
|
|
|
qint32 DatabaseManager::getTrafficTime(const char *sql, qint64 appId)
|
2017-12-04 13:55:03 +00:00
|
|
|
{
|
2017-12-06 13:40:58 +00:00
|
|
|
qint32 trafTime = 0;
|
|
|
|
|
|
|
|
SqliteStmt *stmt = getSqliteStmt(sql);
|
|
|
|
|
|
|
|
if (appId != 0) {
|
|
|
|
stmt->bindInt64(1, appId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stmt->step() == SqliteStmt::StepRow) {
|
|
|
|
trafTime = stmt->columnInt();
|
|
|
|
}
|
|
|
|
stmt->reset();
|
2017-12-04 13:55:03 +00:00
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
return trafTime;
|
2017-12-04 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 07:18:37 +00:00
|
|
|
void DatabaseManager::getTraffic(const char *sql, qint32 trafTime,
|
|
|
|
qint64 &inBytes, qint64 &outBytes,
|
|
|
|
qint64 appId)
|
2017-12-06 14:43:33 +00:00
|
|
|
{
|
|
|
|
SqliteStmt *stmt = getSqliteStmt(sql);
|
|
|
|
|
2017-12-08 07:18:37 +00:00
|
|
|
stmt->bindInt(1, trafTime);
|
|
|
|
|
2017-12-06 14:43:33 +00:00
|
|
|
if (appId != 0) {
|
2017-12-08 07:18:37 +00:00
|
|
|
stmt->bindInt64(2, appId);
|
2017-12-06 14:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stmt->step() == SqliteStmt::StepRow) {
|
|
|
|
inBytes = stmt->columnInt64(0);
|
|
|
|
outBytes = stmt->columnInt64(1);
|
2017-12-09 09:54:56 +00:00
|
|
|
} else {
|
|
|
|
inBytes = outBytes = 0;
|
2017-12-06 14:43:33 +00:00
|
|
|
}
|
2017-12-09 09:54:56 +00:00
|
|
|
|
2017-12-06 14:43:33 +00:00
|
|
|
stmt->reset();
|
|
|
|
}
|
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
SqliteStmt *DatabaseManager::getTrafficStmt(const char *sql, qint32 trafTime)
|
|
|
|
{
|
|
|
|
SqliteStmt *stmt = getSqliteStmt(sql);
|
|
|
|
|
|
|
|
stmt->bindInt(1, trafTime);
|
|
|
|
|
|
|
|
return stmt;
|
|
|
|
}
|
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
SqliteStmt *DatabaseManager::getSqliteStmt(const char *sql)
|
2017-12-04 13:55:03 +00:00
|
|
|
{
|
2017-12-06 13:40:58 +00:00
|
|
|
SqliteStmt *stmt = m_sqliteStmts.value(sql);
|
2017-12-04 13:55:03 +00:00
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
if (stmt == nullptr) {
|
|
|
|
stmt = new SqliteStmt();
|
|
|
|
stmt->prepare(m_sqliteDb->db(), sql, SqliteStmt::PreparePersistent);
|
|
|
|
|
|
|
|
m_sqliteStmts.insert(sql, stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stmt;
|
2017-12-04 13:55:03 +00:00
|
|
|
}
|