2017-12-01 14:13:06 +00:00
|
|
|
#include "databasemanager.h"
|
|
|
|
|
2017-12-11 06:05:49 +00:00
|
|
|
#include "../conf/firewallconf.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"
|
2018-02-19 15:01:35 +00:00
|
|
|
#include "quotamanager.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"
|
|
|
|
|
2018-02-27 10:06:40 +00:00
|
|
|
#define INVALID_APP_INDEX qint16(-1)
|
|
|
|
#define INVALID_APP_ID qint64(-1)
|
2017-12-15 10:11:29 +00:00
|
|
|
|
2017-12-01 14:13:06 +00:00
|
|
|
DatabaseManager::DatabaseManager(const QString &filePath,
|
2018-02-19 15:01:35 +00:00
|
|
|
QuotaManager *quotaManager,
|
2017-12-01 14:13:06 +00:00
|
|
|
QObject *parent) :
|
|
|
|
QObject(parent),
|
2018-02-27 10:06:40 +00:00
|
|
|
m_appFreeIndex(INVALID_APP_INDEX),
|
2018-02-19 15:01:35 +00:00
|
|
|
m_lastTrafHour(0),
|
|
|
|
m_lastTrafDay(0),
|
|
|
|
m_lastTrafMonth(0),
|
2017-12-01 14:13:06 +00:00
|
|
|
m_filePath(filePath),
|
2018-02-19 15:01:35 +00:00
|
|
|
m_quotaManager(quotaManager),
|
2017-12-11 06:05:49 +00:00
|
|
|
m_conf(nullptr),
|
2017-12-01 14:13:06 +00:00
|
|
|
m_sqliteDb(new SqliteDb())
|
|
|
|
{
|
|
|
|
SqliteEngine::initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
DatabaseManager::~DatabaseManager()
|
|
|
|
{
|
2017-12-12 02:48:26 +00:00
|
|
|
clearStmts();
|
2017-12-01 14:13:06 +00:00
|
|
|
|
|
|
|
delete m_sqliteDb;
|
|
|
|
|
|
|
|
SqliteEngine::shutdown();
|
|
|
|
}
|
|
|
|
|
2017-12-11 06:05:49 +00:00
|
|
|
void DatabaseManager::setFirewallConf(const FirewallConf *conf)
|
|
|
|
{
|
|
|
|
m_conf = conf;
|
|
|
|
|
2018-02-20 12:21:10 +00:00
|
|
|
if (!m_conf || !m_conf->logStat()) {
|
2017-12-11 06:05:49 +00:00
|
|
|
logClear();
|
|
|
|
}
|
2018-02-19 15:01:35 +00:00
|
|
|
|
|
|
|
initializeQuota();
|
2017-12-11 06:05:49 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 14:13:06 +00:00
|
|
|
bool DatabaseManager::initialize()
|
|
|
|
{
|
|
|
|
const bool fileExists = FileUtil::fileExists(m_filePath);
|
|
|
|
|
2017-12-12 02:48:26 +00:00
|
|
|
m_lastTrafHour = m_lastTrafDay = m_lastTrafMonth = 0;
|
|
|
|
|
2017-12-01 14:13:06 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-02-19 15:01:35 +00:00
|
|
|
void DatabaseManager::initializeQuota()
|
|
|
|
{
|
2018-02-20 12:21:10 +00:00
|
|
|
if (!m_conf) return;
|
|
|
|
|
|
|
|
m_quotaManager->setQuotaDayBytes(qint64(m_conf->quotaDayMb()) * 1024 * 1024);
|
|
|
|
m_quotaManager->setQuotaMonthBytes(qint64(m_conf->quotaMonthMb()) * 1024 * 1024);
|
2018-02-19 15:01:35 +00:00
|
|
|
|
|
|
|
const qint64 unixTime = DateUtil::getUnixTime();
|
|
|
|
const qint32 trafDay = DateUtil::getUnixDay(unixTime);
|
|
|
|
const qint32 trafMonth = DateUtil::getUnixMonth(
|
2018-02-20 12:21:10 +00:00
|
|
|
unixTime, m_conf->monthStart());
|
2018-02-19 15:01:35 +00:00
|
|
|
|
|
|
|
qint64 inBytes, outBytes;
|
|
|
|
|
|
|
|
getTraffic(DatabaseSql::sqlSelectTrafDay, trafDay, inBytes, outBytes);
|
|
|
|
m_quotaManager->setTrafDayBytes(inBytes);
|
|
|
|
|
|
|
|
getTraffic(DatabaseSql::sqlSelectTrafMonth, trafMonth, inBytes, outBytes);
|
|
|
|
m_quotaManager->setTrafMonthBytes(inBytes);
|
|
|
|
}
|
|
|
|
|
2017-12-12 02:48:26 +00:00
|
|
|
void DatabaseManager::clear()
|
|
|
|
{
|
2017-12-15 10:11:29 +00:00
|
|
|
clearAppIds();
|
2017-12-12 02:48:26 +00:00
|
|
|
clearStmts();
|
|
|
|
|
|
|
|
m_sqliteDb->close();
|
|
|
|
|
|
|
|
FileUtil::removeFile(m_filePath);
|
|
|
|
|
|
|
|
initialize();
|
2018-02-19 15:01:35 +00:00
|
|
|
|
|
|
|
m_quotaManager->clear();
|
2017-12-12 02:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseManager::clearStmts()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_sqliteStmts);
|
|
|
|
m_sqliteStmts.clear();
|
|
|
|
}
|
|
|
|
|
2018-02-27 10:06:40 +00:00
|
|
|
void DatabaseManager::replaceAppPathAt(int index, const QString &appPath)
|
|
|
|
{
|
|
|
|
m_appPaths.replace(index, appPath);
|
|
|
|
}
|
|
|
|
|
2017-12-15 10:11:29 +00:00
|
|
|
void DatabaseManager::replaceAppIdAt(int index, qint64 appId)
|
|
|
|
{
|
|
|
|
m_appIds.replace(index, appId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseManager::clearAppId(qint64 appId)
|
|
|
|
{
|
|
|
|
const int index = m_appIds.indexOf(appId);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
replaceAppIdAt(index, INVALID_APP_ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseManager::clearAppIds()
|
|
|
|
{
|
|
|
|
int index = m_appIds.size();
|
|
|
|
|
|
|
|
while (--index >= 0) {
|
|
|
|
replaceAppIdAt(index, INVALID_APP_ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 10:11:45 +00:00
|
|
|
void DatabaseManager::logClear()
|
|
|
|
{
|
2018-02-27 10:06:40 +00:00
|
|
|
m_appFreeIndex = INVALID_APP_INDEX;
|
|
|
|
m_appFreeIndexes.clear();
|
2018-02-26 10:11:45 +00:00
|
|
|
m_appIndexes.clear();
|
|
|
|
m_appPaths.clear();
|
|
|
|
m_appIds.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseManager::logClearApp(quint32 pid, int index)
|
2017-12-01 14:13:06 +00:00
|
|
|
{
|
2018-02-26 10:11:45 +00:00
|
|
|
m_appIndexes.remove(pid);
|
2018-02-27 10:06:40 +00:00
|
|
|
|
|
|
|
if (index == m_appFreeIndexes.size() - 1) {
|
|
|
|
// Chop last index
|
|
|
|
m_appFreeIndexes.removeLast();
|
|
|
|
m_appPaths.removeLast();
|
|
|
|
m_appIds.removeLast();
|
|
|
|
} else {
|
|
|
|
// Reuse index later
|
|
|
|
m_appFreeIndexes[index] = m_appFreeIndex;
|
|
|
|
m_appFreeIndex = qint16(index);
|
|
|
|
|
|
|
|
replaceAppPathAt(index, QString());
|
|
|
|
replaceAppIdAt(index, INVALID_APP_ID);
|
|
|
|
}
|
2018-02-26 10:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseManager::logProcNew(quint32 pid, const QString &appPath)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!m_appIndexes.contains(pid));
|
|
|
|
|
|
|
|
// Get appId
|
2017-12-15 10:11:29 +00:00
|
|
|
m_sqliteDb->beginTransaction();
|
2017-12-11 06:05:49 +00:00
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
qint64 appId = getAppId(appPath);
|
2017-12-15 10:11:29 +00:00
|
|
|
if (appId == INVALID_APP_ID) {
|
2017-12-06 13:40:58 +00:00
|
|
|
appId = createAppId(appPath);
|
|
|
|
}
|
2017-12-01 14:13:06 +00:00
|
|
|
|
2017-12-15 10:11:29 +00:00
|
|
|
m_sqliteDb->commitTransaction();
|
|
|
|
|
2018-02-27 10:06:40 +00:00
|
|
|
// Add process
|
|
|
|
qint16 procIndex = m_appFreeIndex;
|
|
|
|
if (procIndex != INVALID_APP_INDEX) {
|
|
|
|
m_appFreeIndex = m_appFreeIndexes[procIndex];
|
|
|
|
m_appFreeIndexes[procIndex] = INVALID_APP_INDEX;
|
|
|
|
|
|
|
|
replaceAppPathAt(procIndex, appPath);
|
|
|
|
replaceAppIdAt(procIndex, appId);
|
|
|
|
} else {
|
|
|
|
procIndex = qint16(m_appFreeIndexes.size());
|
|
|
|
m_appFreeIndexes.append(INVALID_APP_INDEX);
|
|
|
|
|
|
|
|
m_appPaths.append(appPath);
|
|
|
|
m_appIds.append(appId);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_appIndexes.insert(pid, procIndex);
|
2017-12-01 14:13:06 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 10:11:45 +00:00
|
|
|
void DatabaseManager::logStatTraf(quint16 procCount, const quint32 *procTrafBytes)
|
2017-12-01 14:13:06 +00:00
|
|
|
{
|
2018-02-20 12:21:10 +00:00
|
|
|
if (!m_conf || !m_conf->logStat())
|
2017-12-11 06:05:49 +00:00
|
|
|
return;
|
|
|
|
|
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
|
|
|
|
2018-01-12 11:21:38 +00:00
|
|
|
const qint32 trafMonth = isNewDay
|
2018-02-20 12:21:10 +00:00
|
|
|
? DateUtil::getUnixMonth(unixTime, m_conf->monthStart())
|
2018-01-12 11:21:38 +00:00
|
|
|
: m_lastTrafMonth;
|
2018-02-19 15:01:35 +00:00
|
|
|
const bool isNewMonth = (trafMonth != m_lastTrafMonth);
|
2017-12-04 13:55:03 +00:00
|
|
|
|
2018-02-23 10:41:22 +00:00
|
|
|
// Initialize quotas traffic bytes
|
|
|
|
m_quotaManager->clear(isNewDay && m_lastTrafDay,
|
|
|
|
isNewMonth && m_lastTrafMonth);
|
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
m_lastTrafHour = trafHour;
|
|
|
|
m_lastTrafDay = trafDay;
|
|
|
|
m_lastTrafMonth = trafMonth;
|
|
|
|
|
2017-12-10 07:07:05 +00:00
|
|
|
m_sqliteDb->beginTransaction();
|
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
// Insert Statemets
|
2017-12-11 06:05:49 +00:00
|
|
|
const QStmtList insertTrafAppStmts = QStmtList()
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafAppHour, trafHour)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafAppDay, trafDay)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafAppMonth, trafMonth)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafAppTotal, -1);
|
2017-12-09 09:38:32 +00:00
|
|
|
|
2017-12-11 06:05:49 +00:00
|
|
|
const QStmtList insertTrafStmts = QStmtList()
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafHour, trafHour)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafDay, trafDay)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlInsertTrafMonth, trafMonth);
|
2017-12-09 09:38:32 +00:00
|
|
|
|
|
|
|
// Update Statemets
|
2017-12-11 06:05:49 +00:00
|
|
|
const QStmtList updateTrafAppStmts = QStmtList()
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafAppHour, trafHour)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafAppDay, trafDay)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafAppMonth, trafMonth)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafAppTotal, -1);
|
2017-12-09 09:38:32 +00:00
|
|
|
|
2017-12-11 06:05:49 +00:00
|
|
|
const QStmtList updateTrafStmts = QStmtList()
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafHour, trafHour)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafDay, trafDay)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlUpdateTrafMonth, trafMonth);
|
2017-12-01 14:13:06 +00:00
|
|
|
|
2018-02-26 10:11:45 +00:00
|
|
|
for (int i = 0; i < procCount; ++i) {
|
|
|
|
quint32 pid = *procTrafBytes++;
|
|
|
|
const bool inactive = (pid & 1) != 0;
|
|
|
|
const quint32 inBytes = *procTrafBytes++;
|
|
|
|
const quint32 outBytes = *procTrafBytes++;
|
|
|
|
|
|
|
|
if (inactive) {
|
|
|
|
pid ^= 1;
|
2017-12-01 14:13:06 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 10:06:40 +00:00
|
|
|
const int procIndex = m_appIndexes.value(pid, INVALID_APP_INDEX);
|
|
|
|
if (procIndex == INVALID_APP_INDEX) {
|
2018-02-26 10:11:45 +00:00
|
|
|
qFatal("DatabaseManager: UI & Driver's states mismatch.");
|
|
|
|
abort();
|
|
|
|
}
|
2017-12-01 14:13:06 +00:00
|
|
|
|
2017-12-09 09:38:32 +00:00
|
|
|
if (inBytes || outBytes) {
|
2018-02-26 10:11:45 +00:00
|
|
|
qint64 appId = m_appIds.at(procIndex);
|
2017-12-15 10:11:29 +00:00
|
|
|
|
|
|
|
// Was the app cleared?
|
|
|
|
if (appId == INVALID_APP_ID) {
|
2018-02-26 10:11:45 +00:00
|
|
|
appId = createAppId(m_appPaths.at(procIndex));
|
|
|
|
replaceAppIdAt(procIndex, appId);
|
2017-12-15 10:11:29 +00:00
|
|
|
}
|
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);
|
2018-02-19 15:01:35 +00:00
|
|
|
|
|
|
|
// Update quota traffic bytes
|
|
|
|
m_quotaManager->addTraf(inBytes);
|
2017-12-01 14:13:06 +00:00
|
|
|
}
|
2018-02-26 10:11:45 +00:00
|
|
|
|
|
|
|
if (inactive) {
|
|
|
|
logClearApp(pid, procIndex);
|
|
|
|
}
|
2017-12-01 14:13:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 06:05:49 +00:00
|
|
|
// Delete old data
|
|
|
|
if (isNewDay) {
|
2017-12-14 08:44:10 +00:00
|
|
|
QStmtList deleteTrafStmts;
|
|
|
|
|
|
|
|
// Traffic Hour
|
2018-02-20 12:21:10 +00:00
|
|
|
const int trafHourKeepDays = m_conf->trafHourKeepDays();
|
2017-12-14 08:44:10 +00:00
|
|
|
if (trafHourKeepDays >= 0) {
|
|
|
|
const qint32 oldTrafHour = trafHour - 24 * trafHourKeepDays;
|
|
|
|
|
|
|
|
deleteTrafStmts
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlDeleteTrafAppHour, oldTrafHour)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlDeleteTrafHour, oldTrafHour);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traffic Day
|
2018-02-20 12:21:10 +00:00
|
|
|
const int trafDayKeepDays = m_conf->trafDayKeepDays();
|
2017-12-14 08:44:10 +00:00
|
|
|
if (trafDayKeepDays >= 0) {
|
|
|
|
const qint32 oldTrafDay = trafHour - 24 * trafDayKeepDays;
|
|
|
|
|
|
|
|
deleteTrafStmts
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlDeleteTrafAppDay, oldTrafDay)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlDeleteTrafDay, oldTrafDay);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traffic Month
|
2018-02-20 12:21:10 +00:00
|
|
|
const int trafMonthKeepMonths = m_conf->trafMonthKeepMonths();
|
2017-12-14 08:44:10 +00:00
|
|
|
if (trafMonthKeepMonths >= 0) {
|
|
|
|
const qint32 oldTrafMonth = DateUtil::addUnixMonths(
|
|
|
|
trafHour, -trafMonthKeepMonths);
|
|
|
|
|
|
|
|
deleteTrafStmts
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlDeleteTrafAppMonth, oldTrafMonth)
|
|
|
|
<< getTrafficStmt(DatabaseSql::sqlDeleteTrafMonth, oldTrafMonth);
|
|
|
|
}
|
2017-12-11 06:05:49 +00:00
|
|
|
|
2017-12-14 04:39:57 +00:00
|
|
|
stepStmtList(deleteTrafStmts);
|
2017-12-11 06:05:49 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 14:13:06 +00:00
|
|
|
m_sqliteDb->commitTransaction();
|
|
|
|
|
2018-02-19 15:01:35 +00:00
|
|
|
// Check quotas
|
|
|
|
m_quotaManager->checkQuotaDay(trafDay);
|
|
|
|
m_quotaManager->checkQuotaMonth(trafMonth);
|
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-14 04:39:57 +00:00
|
|
|
void DatabaseManager::deleteApp(qint64 appId)
|
|
|
|
{
|
2017-12-15 10:11:29 +00:00
|
|
|
clearAppId(appId);
|
|
|
|
|
2017-12-14 04:39:57 +00:00
|
|
|
// Delete Statemets
|
|
|
|
const QStmtList deleteAppStmts = QStmtList()
|
|
|
|
<< getAppStmt(DatabaseSql::sqlDeleteAppTrafHour, appId)
|
|
|
|
<< getAppStmt(DatabaseSql::sqlDeleteAppTrafDay, appId)
|
|
|
|
<< getAppStmt(DatabaseSql::sqlDeleteAppTrafMonth, appId)
|
|
|
|
<< getAppStmt(DatabaseSql::sqlDeleteAppId, appId);
|
|
|
|
|
|
|
|
stepStmtList(deleteAppStmts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseManager::resetAppTotals()
|
|
|
|
{
|
|
|
|
m_sqliteDb->beginTransaction();
|
|
|
|
|
|
|
|
SqliteStmt *stmt = getSqliteStmt(DatabaseSql::sqlResetAppTrafTotals);
|
|
|
|
const qint64 unixTime = DateUtil::getUnixTime();
|
|
|
|
|
|
|
|
stmt->bindInt(1, DateUtil::getUnixHour(unixTime));
|
|
|
|
|
|
|
|
stmt->step();
|
|
|
|
stmt->reset();
|
|
|
|
|
|
|
|
m_sqliteDb->commitTransaction();
|
|
|
|
}
|
|
|
|
|
2018-02-26 10:11:45 +00:00
|
|
|
qint64 DatabaseManager::getAppId(const QString &appPath)
|
|
|
|
{
|
|
|
|
qint64 appId = INVALID_APP_ID;
|
|
|
|
|
|
|
|
SqliteStmt *stmt = getSqliteStmt(DatabaseSql::sqlSelectAppId);
|
|
|
|
|
|
|
|
stmt->bindText(1, appPath);
|
|
|
|
if (stmt->step() == SqliteStmt::StepRow) {
|
|
|
|
appId = stmt->columnInt64();
|
|
|
|
}
|
|
|
|
stmt->reset();
|
|
|
|
|
|
|
|
return appId;
|
|
|
|
}
|
|
|
|
|
2017-12-06 13:40:58 +00:00
|
|
|
qint64 DatabaseManager::createAppId(const QString &appPath)
|
|
|
|
{
|
2017-12-15 10:11:29 +00:00
|
|
|
qint64 appId = INVALID_APP_ID;
|
2017-12-10 07:07:05 +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-15 10:11:29 +00:00
|
|
|
|
|
|
|
emit appCreated(appId, appPath);
|
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)) {
|
|
|
|
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-14 04:39:57 +00:00
|
|
|
void DatabaseManager::stepStmtList(const QStmtList &stmtList)
|
2017-12-11 06:05:49 +00:00
|
|
|
{
|
2017-12-14 04:39:57 +00:00
|
|
|
foreach (SqliteStmt *stmtDelete, stmtList) {
|
2017-12-11 06:05:49 +00:00
|
|
|
stmtDelete->step();
|
|
|
|
stmtDelete->reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-14 04:39:57 +00:00
|
|
|
SqliteStmt *DatabaseManager::getAppStmt(const char *sql, qint64 appId)
|
|
|
|
{
|
|
|
|
SqliteStmt *stmt = getSqliteStmt(sql);
|
|
|
|
|
|
|
|
stmt->bindInt64(1, appId);
|
|
|
|
|
|
|
|
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
|
|
|
}
|