UI: StatisticsPage: Fix traffic list.

This commit is contained in:
Nodir Temirkhodjaev 2017-12-08 12:18:37 +05:00
parent 370a279ca6
commit 45bdd1b72b
7 changed files with 72 additions and 51 deletions

View File

@ -228,7 +228,7 @@ qint64 DatabaseManager::createAppId(const QString &appPath)
stmt->bindText(1, appPath); stmt->bindText(1, appPath);
stmt->bindInt64(2, unixTime); stmt->bindInt64(2, unixTime);
stmt->bindInt64(3, DateUtil::getUnixHour(unixTime)); stmt->bindInt(3, DateUtil::getUnixHour(unixTime));
if (stmt->step() == SqliteStmt::StepDone) { if (stmt->step() == SqliteStmt::StepDone) {
appId = m_sqliteDb->lastInsertRowid(); appId = m_sqliteDb->lastInsertRowid();
@ -273,7 +273,7 @@ void DatabaseManager::updateTraffic(SqliteStmt *stmt, quint32 inBytes,
stmt->reset(); stmt->reset();
} }
qint32 DatabaseManager::getMinTrafTime(const char *sql, qint64 appId) qint32 DatabaseManager::getTrafficTime(const char *sql, qint64 appId)
{ {
qint32 trafTime = 0; qint32 trafTime = 0;
@ -291,13 +291,16 @@ qint32 DatabaseManager::getMinTrafTime(const char *sql, qint64 appId)
return trafTime; return trafTime;
} }
void DatabaseManager::getTraffic(const char *sql, qint64 &inBytes, void DatabaseManager::getTraffic(const char *sql, qint32 trafTime,
qint64 &outBytes, qint64 appId) qint64 &inBytes, qint64 &outBytes,
qint64 appId)
{ {
SqliteStmt *stmt = getSqliteStmt(sql); SqliteStmt *stmt = getSqliteStmt(sql);
stmt->bindInt(1, trafTime);
if (appId != 0) { if (appId != 0) {
stmt->bindInt64(1, appId); stmt->bindInt64(2, appId);
} }
if (stmt->step() == SqliteStmt::StepRow) { if (stmt->step() == SqliteStmt::StepRow) {

View File

@ -31,10 +31,11 @@ public:
qint64 getAppId(const QString &appPath); qint64 getAppId(const QString &appPath);
qint32 getMinTrafTime(const char *sql, qint64 appId = 0); qint32 getTrafficTime(const char *sql, qint64 appId = 0);
void getTraffic(const char *sql, qint64 &inBytes, void getTraffic(const char *sql, qint32 trafTime,
qint64 &outBytes, qint64 appId = 0); qint64 &inBytes, qint64 &outBytes,
qint64 appId = 0);
signals: signals:

View File

@ -8,7 +8,7 @@ const char * const DatabaseSql::sqlPragmas =
const char * const DatabaseSql::sqlCreateTables = const char * const DatabaseSql::sqlCreateTables =
"CREATE TABLE app(" "CREATE TABLE app("
" id INTEGER PRIMARY KEY," " app_id INTEGER PRIMARY KEY,"
" path TEXT UNIQUE NOT NULL," " path TEXT UNIQUE NOT NULL,"
" creat_time INTEGER NOT NULL," " creat_time INTEGER NOT NULL,"
" traf_time INTEGER NOT NULL," " traf_time INTEGER NOT NULL,"
@ -60,7 +60,7 @@ const char * const DatabaseSql::sqlCreateTables =
; ;
const char * const DatabaseSql::sqlSelectAppId = const char * const DatabaseSql::sqlSelectAppId =
"SELECT id FROM app WHERE path = ?1;" "SELECT app_id FROM app WHERE path = ?1;"
; ;
const char * const DatabaseSql::sqlInsertAppId = const char * const DatabaseSql::sqlInsertAppId =
@ -69,7 +69,7 @@ const char * const DatabaseSql::sqlInsertAppId =
; ;
const char * const DatabaseSql::sqlSelectAppPaths = const char * const DatabaseSql::sqlSelectAppPaths =
"SELECT id, path FROM app ORDER BY creat_time;" "SELECT app_id, path FROM app ORDER BY creat_time;"
; ;
const char * const DatabaseSql::sqlInsertTrafficAppHour = const char * const DatabaseSql::sqlInsertTrafficAppHour =
@ -148,7 +148,7 @@ const char * const DatabaseSql::sqlUpdateTrafficAppTotal =
"UPDATE app" "UPDATE app"
" SET in_bytes = in_bytes + ?2," " SET in_bytes = in_bytes + ?2,"
" out_bytes = out_bytes + ?3" " out_bytes = out_bytes + ?3"
" WHERE id = ?1;" " WHERE app_id = ?1;"
; ;
const char * const DatabaseSql::sqlSelectMinTrafAppHour = const char * const DatabaseSql::sqlSelectMinTrafAppHour =
@ -166,6 +166,10 @@ const char * const DatabaseSql::sqlSelectMinTrafAppMonth =
" WHERE app_id = ?1;" " WHERE app_id = ?1;"
; ;
const char * const DatabaseSql::sqlSelectMinTrafAppTotal =
"SELECT traf_time FROM app WHERE app_id = ?1;"
;
const char * const DatabaseSql::sqlSelectMinTrafHour = const char * const DatabaseSql::sqlSelectMinTrafHour =
"SELECT min(traf_time) FROM traffic_hour;" "SELECT min(traf_time) FROM traffic_hour;"
; ;
@ -178,48 +182,50 @@ const char * const DatabaseSql::sqlSelectMinTrafMonth =
"SELECT min(traf_time) FROM traffic_app_month;" "SELECT min(traf_time) FROM traffic_app_month;"
; ;
const char * const DatabaseSql::sqlSelectMinTrafTotal =
"SELECT min(traf_time) FROM app;"
;
const char * const DatabaseSql::sqlSelectTrafAppHour = const char * const DatabaseSql::sqlSelectTrafAppHour =
"SELECT traf_time, in_bytes, out_bytes" "SELECT in_bytes, out_bytes"
" FROM traffic_app_hour" " FROM traffic_app_hour"
" WHERE app_id = ?1 and traf_time between ?2 and ?3;" " WHERE app_id = ?2 and traf_time = ?1;"
; ;
const char * const DatabaseSql::sqlSelectTrafAppDay = const char * const DatabaseSql::sqlSelectTrafAppDay =
"SELECT traf_time, in_bytes, out_bytes" "SELECT in_bytes, out_bytes"
" FROM traffic_app_day" " FROM traffic_app_day"
" WHERE app_id = ?1 and traf_time between ?2 and ?3;" " WHERE app_id = ?2 and traf_time = ?1;"
; ;
const char * const DatabaseSql::sqlSelectTrafAppMonth = const char * const DatabaseSql::sqlSelectTrafAppMonth =
"SELECT traf_time, in_bytes, out_bytes" "SELECT in_bytes, out_bytes"
" FROM traffic_app_month" " FROM traffic_app_month"
" WHERE app_id = ?1 and traf_time between ?2 and ?3;" " WHERE app_id = ?2 and traf_time = ?1;"
; ;
const char * const DatabaseSql::sqlSelectTrafAppTotal = const char * const DatabaseSql::sqlSelectTrafAppTotal =
"SELECT traf_time, in_bytes, out_bytes" "SELECT in_bytes, out_bytes"
" FROM app WHERE app_id = ?1;" " FROM app"
" WHERE app_id = ?2 and 0 != ?1;"
; ;
const char * const DatabaseSql::sqlSelectTrafHour = const char * const DatabaseSql::sqlSelectTrafHour =
"SELECT traf_time, in_bytes, out_bytes" "SELECT in_bytes, out_bytes"
" FROM traffic_hour" " FROM traffic_hour WHERE traf_time = ?1;"
" WHERE traf_time between ?2 and ?3;"
; ;
const char * const DatabaseSql::sqlSelectTrafDay = const char * const DatabaseSql::sqlSelectTrafDay =
"SELECT traf_time, in_bytes, out_bytes" "SELECT in_bytes, out_bytes"
" FROM traffic_day" " FROM traffic_day WHERE traf_time = ?1;"
" WHERE traf_time between ?2 and ?3;"
; ;
const char * const DatabaseSql::sqlSelectTrafMonth = const char * const DatabaseSql::sqlSelectTrafMonth =
"SELECT traf_time, in_bytes, out_bytes" "SELECT in_bytes, out_bytes"
" FROM traffic_month" " FROM traffic_month WHERE traf_time = ?1;"
" WHERE traf_time between ?2 and ?3;"
; ;
const char * const DatabaseSql::sqlSelectTrafTotal = const char * const DatabaseSql::sqlSelectTrafTotal =
"SELECT traf_time, in_bytes, out_bytes" "SELECT sum(in_bytes), sum(out_bytes)"
" FROM app;" " FROM app WHERE 0 != ?1;"
; ;

View File

@ -33,10 +33,12 @@ public:
static const char * const sqlSelectMinTrafAppHour; static const char * const sqlSelectMinTrafAppHour;
static const char * const sqlSelectMinTrafAppDay; static const char * const sqlSelectMinTrafAppDay;
static const char * const sqlSelectMinTrafAppMonth; static const char * const sqlSelectMinTrafAppMonth;
static const char * const sqlSelectMinTrafAppTotal;
static const char * const sqlSelectMinTrafHour; static const char * const sqlSelectMinTrafHour;
static const char * const sqlSelectMinTrafDay; static const char * const sqlSelectMinTrafDay;
static const char * const sqlSelectMinTrafMonth; static const char * const sqlSelectMinTrafMonth;
static const char * const sqlSelectMinTrafTotal;
static const char * const sqlSelectTrafAppHour; static const char * const sqlSelectTrafAppHour;
static const char * const sqlSelectTrafAppDay; static const char * const sqlSelectTrafAppDay;

View File

@ -20,7 +20,7 @@ TrafListModel *AppStatModel::trafListModel(int trafType, int row) const
{ {
m_trafListModel->setType(static_cast<TrafListModel::TrafType>(trafType)); m_trafListModel->setType(static_cast<TrafListModel::TrafType>(trafType));
m_trafListModel->setAppId(row == -1 ? 0 : m_appIds.at(row)); m_trafListModel->setAppId(row == -1 ? 0 : m_appIds.at(row));
m_trafListModel->reset(m_appIds.size() - 1); m_trafListModel->reset();
return m_trafListModel; return m_trafListModel;
} }

View File

@ -25,25 +25,23 @@ void TrafListModel::setAppId(qint64 appId)
m_appId = appId; m_appId = appId;
} }
void TrafListModel::reset(int appCount) void TrafListModel::reset()
{ {
const char *sqlMinTrafTime = getSqlMinTrafTime(m_type, m_appId); const char *sqlMinTrafTime = getSqlMinTrafTime(m_type, m_appId);
beginResetModel(); beginResetModel();
if (sqlMinTrafTime) { m_minTrafTime = m_databaseManager->getTrafficTime(
m_minTrafTime = m_databaseManager->getMinTrafTime( sqlMinTrafTime, m_appId);
sqlMinTrafTime, m_appId);
m_maxTrafTime = getMaxTrafTime(m_type); m_maxTrafTime = getMaxTrafTime(m_type);
m_trafCount = getTrafCount(m_type, m_minTrafTime, m_minTrafTime); if (m_minTrafTime == 0) {
} else { m_minTrafTime = m_maxTrafTime;
m_minTrafTime = m_maxTrafTime = 0;
m_trafCount = m_appId ? 1 : appCount;
} }
m_trafCount = getTrafCount(m_type, m_minTrafTime, m_maxTrafTime);
endResetModel(); endResetModel();
m_databaseManager->debugApps(); m_databaseManager->debugApps();
@ -70,8 +68,8 @@ QVariant TrafListModel::data(const QModelIndex &index, int role) const
const char *sqlSelectTraffic = getSqlSelectTraffic(m_type, m_appId); const char *sqlSelectTraffic = getSqlSelectTraffic(m_type, m_appId);
qint64 inBytes = 0, outBytes = 0; qint64 inBytes = 0, outBytes = 0;
m_databaseManager->getTraffic(sqlSelectTraffic, inBytes, m_databaseManager->getTraffic(sqlSelectTraffic, trafTime,
outBytes, m_appId); inBytes, outBytes, m_appId);
switch (role) { switch (role) {
case DownloadRole: return inBytes; case DownloadRole: return inBytes;
@ -103,10 +101,10 @@ QString TrafListModel::formatTrafTime(qint32 trafTime) const
const qint64 unixTime = DateUtil::toUnixTime(trafTime); const qint64 unixTime = DateUtil::toUnixTime(trafTime);
switch (m_type) { switch (m_type) {
case TrafTotal: Q_FALLTHROUGH();
case TrafHourly: return DateUtil::formatHour(unixTime); case TrafHourly: return DateUtil::formatHour(unixTime);
case TrafDaily: return DateUtil::formatDay(unixTime); case TrafDaily: return DateUtil::formatDay(unixTime);
case TrafMonthly: return DateUtil::formatMonth(unixTime); case TrafMonthly: return DateUtil::formatMonth(unixTime);
case TrafTotal: return DateUtil::formatTime(unixTime);
} }
return QString(); return QString();
} }
@ -117,7 +115,7 @@ qint32 TrafListModel::getTrafTime(int row) const
case TrafHourly: return m_maxTrafTime - row; case TrafHourly: return m_maxTrafTime - row;
case TrafDaily: return m_maxTrafTime - row * 24; case TrafDaily: return m_maxTrafTime - row * 24;
case TrafMonthly: return DateUtil::addUnixMonths(m_maxTrafTime, -row); case TrafMonthly: return DateUtil::addUnixMonths(m_maxTrafTime, -row);
case TrafTotal: break; case TrafTotal: return m_minTrafTime;
} }
return 0; return 0;
} }
@ -125,18 +123,22 @@ qint32 TrafListModel::getTrafTime(int row) const
qint32 TrafListModel::getTrafCount(TrafType type, qint32 minTrafTime, qint32 TrafListModel::getTrafCount(TrafType type, qint32 minTrafTime,
qint32 maxTrafTime) qint32 maxTrafTime)
{ {
if (type == TrafTotal)
return 1;
const qint32 hours = maxTrafTime - minTrafTime + 1; const qint32 hours = maxTrafTime - minTrafTime + 1;
if (type == TrafHourly) if (type == TrafHourly)
return hours; return hours;
const qint32 days = hours / 24; const qint32 days = hours / 24 + 1;
if (type == TrafDaily) if (type == TrafDaily)
return days; return days;
const qint32 months = days / 30; const qint32 months = days / 30 + 1;
if (type == TrafMonthly) if (type == TrafMonthly)
return months; return months;
Q_UNREACHABLE();
return 0; return 0;
} }
@ -145,11 +147,13 @@ qint32 TrafListModel::getMaxTrafTime(TrafType type)
const qint64 unixTime = DateUtil::getUnixTime(); const qint64 unixTime = DateUtil::getUnixTime();
switch (type) { switch (type) {
case TrafTotal: Q_FALLTHROUGH();
case TrafHourly: return DateUtil::getUnixHour(unixTime); case TrafHourly: return DateUtil::getUnixHour(unixTime);
case TrafDaily: return DateUtil::getUnixDay(unixTime); case TrafDaily: return DateUtil::getUnixDay(unixTime);
case TrafMonthly: return DateUtil::getUnixMonth(unixTime); case TrafMonthly: return DateUtil::getUnixMonth(unixTime);
case TrafTotal: break;
} }
Q_UNREACHABLE();
return 0; return 0;
} }
@ -162,8 +166,11 @@ const char *TrafListModel::getSqlMinTrafTime(TrafType type, qint64 appId)
: DatabaseSql::sqlSelectMinTrafDay; : DatabaseSql::sqlSelectMinTrafDay;
case TrafMonthly: return appId ? DatabaseSql::sqlSelectMinTrafAppMonth case TrafMonthly: return appId ? DatabaseSql::sqlSelectMinTrafAppMonth
: DatabaseSql::sqlSelectMinTrafMonth; : DatabaseSql::sqlSelectMinTrafMonth;
case TrafTotal: break; case TrafTotal: return appId ? DatabaseSql::sqlSelectMinTrafAppTotal
: DatabaseSql::sqlSelectMinTrafTotal;
} }
Q_UNREACHABLE();
return nullptr; return nullptr;
} }
@ -179,5 +186,7 @@ const char *TrafListModel::getSqlSelectTraffic(TrafType type, qint64 appId)
case TrafTotal: return appId ? DatabaseSql::sqlSelectTrafAppTotal case TrafTotal: return appId ? DatabaseSql::sqlSelectTrafAppTotal
: DatabaseSql::sqlSelectTrafTotal; : DatabaseSql::sqlSelectTrafTotal;
} }
Q_UNREACHABLE();
return nullptr; return nullptr;
} }

View File

@ -35,7 +35,7 @@ public:
qint64 appId() const { return m_appId; } qint64 appId() const { return m_appId; }
void setAppId(qint64 appId); void setAppId(qint64 appId);
void reset(int appCount); void reset();
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;