diff --git a/src/ui/db/databasemanager.cpp b/src/ui/db/databasemanager.cpp index dc329099..a7228f02 100644 --- a/src/ui/db/databasemanager.cpp +++ b/src/ui/db/databasemanager.cpp @@ -228,7 +228,7 @@ qint64 DatabaseManager::createAppId(const QString &appPath) stmt->bindText(1, appPath); stmt->bindInt64(2, unixTime); - stmt->bindInt64(3, DateUtil::getUnixHour(unixTime)); + stmt->bindInt(3, DateUtil::getUnixHour(unixTime)); if (stmt->step() == SqliteStmt::StepDone) { appId = m_sqliteDb->lastInsertRowid(); @@ -273,7 +273,7 @@ void DatabaseManager::updateTraffic(SqliteStmt *stmt, quint32 inBytes, stmt->reset(); } -qint32 DatabaseManager::getMinTrafTime(const char *sql, qint64 appId) +qint32 DatabaseManager::getTrafficTime(const char *sql, qint64 appId) { qint32 trafTime = 0; @@ -291,13 +291,16 @@ qint32 DatabaseManager::getMinTrafTime(const char *sql, qint64 appId) return trafTime; } -void DatabaseManager::getTraffic(const char *sql, qint64 &inBytes, - qint64 &outBytes, qint64 appId) +void DatabaseManager::getTraffic(const char *sql, qint32 trafTime, + qint64 &inBytes, qint64 &outBytes, + qint64 appId) { SqliteStmt *stmt = getSqliteStmt(sql); + stmt->bindInt(1, trafTime); + if (appId != 0) { - stmt->bindInt64(1, appId); + stmt->bindInt64(2, appId); } if (stmt->step() == SqliteStmt::StepRow) { diff --git a/src/ui/db/databasemanager.h b/src/ui/db/databasemanager.h index 7ef418ee..009d59ae 100644 --- a/src/ui/db/databasemanager.h +++ b/src/ui/db/databasemanager.h @@ -31,10 +31,11 @@ public: 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, - qint64 &outBytes, qint64 appId = 0); + void getTraffic(const char *sql, qint32 trafTime, + qint64 &inBytes, qint64 &outBytes, + qint64 appId = 0); signals: diff --git a/src/ui/db/databasesql.cpp b/src/ui/db/databasesql.cpp index 9b6fa05d..b5ff34c1 100644 --- a/src/ui/db/databasesql.cpp +++ b/src/ui/db/databasesql.cpp @@ -8,7 +8,7 @@ const char * const DatabaseSql::sqlPragmas = const char * const DatabaseSql::sqlCreateTables = "CREATE TABLE app(" - " id INTEGER PRIMARY KEY," + " app_id INTEGER PRIMARY KEY," " path TEXT UNIQUE NOT NULL," " creat_time INTEGER NOT NULL," " traf_time INTEGER NOT NULL," @@ -60,7 +60,7 @@ const char * const DatabaseSql::sqlCreateTables = ; 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 = @@ -69,7 +69,7 @@ const char * const DatabaseSql::sqlInsertAppId = ; 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 = @@ -148,7 +148,7 @@ const char * const DatabaseSql::sqlUpdateTrafficAppTotal = "UPDATE app" " SET in_bytes = in_bytes + ?2," " out_bytes = out_bytes + ?3" - " WHERE id = ?1;" + " WHERE app_id = ?1;" ; const char * const DatabaseSql::sqlSelectMinTrafAppHour = @@ -166,6 +166,10 @@ const char * const DatabaseSql::sqlSelectMinTrafAppMonth = " WHERE app_id = ?1;" ; +const char * const DatabaseSql::sqlSelectMinTrafAppTotal = + "SELECT traf_time FROM app WHERE app_id = ?1;" + ; + const char * const DatabaseSql::sqlSelectMinTrafHour = "SELECT min(traf_time) FROM traffic_hour;" ; @@ -178,48 +182,50 @@ const char * const DatabaseSql::sqlSelectMinTrafMonth = "SELECT min(traf_time) FROM traffic_app_month;" ; +const char * const DatabaseSql::sqlSelectMinTrafTotal = + "SELECT min(traf_time) FROM app;" + ; + const char * const DatabaseSql::sqlSelectTrafAppHour = - "SELECT traf_time, in_bytes, out_bytes" + "SELECT in_bytes, out_bytes" " 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 = - "SELECT traf_time, in_bytes, out_bytes" + "SELECT in_bytes, out_bytes" " 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 = - "SELECT traf_time, in_bytes, out_bytes" + "SELECT in_bytes, out_bytes" " 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 = - "SELECT traf_time, in_bytes, out_bytes" - " FROM app WHERE app_id = ?1;" + "SELECT in_bytes, out_bytes" + " FROM app" + " WHERE app_id = ?2 and 0 != ?1;" ; const char * const DatabaseSql::sqlSelectTrafHour = - "SELECT traf_time, in_bytes, out_bytes" - " FROM traffic_hour" - " WHERE traf_time between ?2 and ?3;" + "SELECT in_bytes, out_bytes" + " FROM traffic_hour WHERE traf_time = ?1;" ; const char * const DatabaseSql::sqlSelectTrafDay = - "SELECT traf_time, in_bytes, out_bytes" - " FROM traffic_day" - " WHERE traf_time between ?2 and ?3;" + "SELECT in_bytes, out_bytes" + " FROM traffic_day WHERE traf_time = ?1;" ; const char * const DatabaseSql::sqlSelectTrafMonth = - "SELECT traf_time, in_bytes, out_bytes" - " FROM traffic_month" - " WHERE traf_time between ?2 and ?3;" + "SELECT in_bytes, out_bytes" + " FROM traffic_month WHERE traf_time = ?1;" ; const char * const DatabaseSql::sqlSelectTrafTotal = - "SELECT traf_time, in_bytes, out_bytes" - " FROM app;" + "SELECT sum(in_bytes), sum(out_bytes)" + " FROM app WHERE 0 != ?1;" ; diff --git a/src/ui/db/databasesql.h b/src/ui/db/databasesql.h index e7196d2e..51f27c26 100644 --- a/src/ui/db/databasesql.h +++ b/src/ui/db/databasesql.h @@ -33,10 +33,12 @@ public: static const char * const sqlSelectMinTrafAppHour; static const char * const sqlSelectMinTrafAppDay; static const char * const sqlSelectMinTrafAppMonth; + static const char * const sqlSelectMinTrafAppTotal; static const char * const sqlSelectMinTrafHour; static const char * const sqlSelectMinTrafDay; static const char * const sqlSelectMinTrafMonth; + static const char * const sqlSelectMinTrafTotal; static const char * const sqlSelectTrafAppHour; static const char * const sqlSelectTrafAppDay; diff --git a/src/ui/log/model/appstatmodel.cpp b/src/ui/log/model/appstatmodel.cpp index f6612880..dd69caf5 100644 --- a/src/ui/log/model/appstatmodel.cpp +++ b/src/ui/log/model/appstatmodel.cpp @@ -20,7 +20,7 @@ TrafListModel *AppStatModel::trafListModel(int trafType, int row) const { m_trafListModel->setType(static_cast(trafType)); m_trafListModel->setAppId(row == -1 ? 0 : m_appIds.at(row)); - m_trafListModel->reset(m_appIds.size() - 1); + m_trafListModel->reset(); return m_trafListModel; } diff --git a/src/ui/log/model/traflistmodel.cpp b/src/ui/log/model/traflistmodel.cpp index 8ad359a0..6a889ca7 100644 --- a/src/ui/log/model/traflistmodel.cpp +++ b/src/ui/log/model/traflistmodel.cpp @@ -25,25 +25,23 @@ void TrafListModel::setAppId(qint64 appId) m_appId = appId; } -void TrafListModel::reset(int appCount) +void TrafListModel::reset() { const char *sqlMinTrafTime = getSqlMinTrafTime(m_type, m_appId); beginResetModel(); - if (sqlMinTrafTime) { - m_minTrafTime = m_databaseManager->getMinTrafTime( - sqlMinTrafTime, m_appId); + m_minTrafTime = m_databaseManager->getTrafficTime( + sqlMinTrafTime, m_appId); - m_maxTrafTime = getMaxTrafTime(m_type); + m_maxTrafTime = getMaxTrafTime(m_type); - m_trafCount = getTrafCount(m_type, m_minTrafTime, m_minTrafTime); - } else { - m_minTrafTime = m_maxTrafTime = 0; - - m_trafCount = m_appId ? 1 : appCount; + if (m_minTrafTime == 0) { + m_minTrafTime = m_maxTrafTime; } + m_trafCount = getTrafCount(m_type, m_minTrafTime, m_maxTrafTime); + endResetModel(); m_databaseManager->debugApps(); @@ -70,8 +68,8 @@ QVariant TrafListModel::data(const QModelIndex &index, int role) const const char *sqlSelectTraffic = getSqlSelectTraffic(m_type, m_appId); qint64 inBytes = 0, outBytes = 0; - m_databaseManager->getTraffic(sqlSelectTraffic, inBytes, - outBytes, m_appId); + m_databaseManager->getTraffic(sqlSelectTraffic, trafTime, + inBytes, outBytes, m_appId); switch (role) { case DownloadRole: return inBytes; @@ -103,10 +101,10 @@ QString TrafListModel::formatTrafTime(qint32 trafTime) const const qint64 unixTime = DateUtil::toUnixTime(trafTime); switch (m_type) { + case TrafTotal: Q_FALLTHROUGH(); case TrafHourly: return DateUtil::formatHour(unixTime); case TrafDaily: return DateUtil::formatDay(unixTime); case TrafMonthly: return DateUtil::formatMonth(unixTime); - case TrafTotal: return DateUtil::formatTime(unixTime); } return QString(); } @@ -117,7 +115,7 @@ qint32 TrafListModel::getTrafTime(int row) const case TrafHourly: return m_maxTrafTime - row; case TrafDaily: return m_maxTrafTime - row * 24; case TrafMonthly: return DateUtil::addUnixMonths(m_maxTrafTime, -row); - case TrafTotal: break; + case TrafTotal: return m_minTrafTime; } return 0; } @@ -125,18 +123,22 @@ qint32 TrafListModel::getTrafTime(int row) const qint32 TrafListModel::getTrafCount(TrafType type, qint32 minTrafTime, qint32 maxTrafTime) { + if (type == TrafTotal) + return 1; + const qint32 hours = maxTrafTime - minTrafTime + 1; if (type == TrafHourly) return hours; - const qint32 days = hours / 24; + const qint32 days = hours / 24 + 1; if (type == TrafDaily) return days; - const qint32 months = days / 30; + const qint32 months = days / 30 + 1; if (type == TrafMonthly) return months; + Q_UNREACHABLE(); return 0; } @@ -145,11 +147,13 @@ qint32 TrafListModel::getMaxTrafTime(TrafType type) const qint64 unixTime = DateUtil::getUnixTime(); switch (type) { + case TrafTotal: Q_FALLTHROUGH(); case TrafHourly: return DateUtil::getUnixHour(unixTime); case TrafDaily: return DateUtil::getUnixDay(unixTime); case TrafMonthly: return DateUtil::getUnixMonth(unixTime); - case TrafTotal: break; } + + Q_UNREACHABLE(); return 0; } @@ -162,8 +166,11 @@ const char *TrafListModel::getSqlMinTrafTime(TrafType type, qint64 appId) : DatabaseSql::sqlSelectMinTrafDay; case TrafMonthly: return appId ? DatabaseSql::sqlSelectMinTrafAppMonth : DatabaseSql::sqlSelectMinTrafMonth; - case TrafTotal: break; + case TrafTotal: return appId ? DatabaseSql::sqlSelectMinTrafAppTotal + : DatabaseSql::sqlSelectMinTrafTotal; } + + Q_UNREACHABLE(); return nullptr; } @@ -179,5 +186,7 @@ const char *TrafListModel::getSqlSelectTraffic(TrafType type, qint64 appId) case TrafTotal: return appId ? DatabaseSql::sqlSelectTrafAppTotal : DatabaseSql::sqlSelectTrafTotal; } + + Q_UNREACHABLE(); return nullptr; } diff --git a/src/ui/log/model/traflistmodel.h b/src/ui/log/model/traflistmodel.h index bbe43ce9..0e01cbd3 100644 --- a/src/ui/log/model/traflistmodel.h +++ b/src/ui/log/model/traflistmodel.h @@ -35,7 +35,7 @@ public: qint64 appId() const { return m_appId; } void setAppId(qint64 appId); - void reset(int appCount); + void reset(); int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;